Search in sources :

Example 1 with TransactionOutput

use of co.rsk.bitcoinj.core.TransactionOutput in project rskj by rsksmart.

the class BridgeSupport method saveNewUTXOs.

/*
      Add the btcTx outputs that send btc to the federation(s) to the UTXO list
     */
private void saveNewUTXOs(BtcTransaction btcTx) throws IOException {
    // Outputs to the active federation
    List<TransactionOutput> outputsToTheActiveFederation = btcTx.getWalletOutputs(getActiveFederationWallet(false));
    for (TransactionOutput output : outputsToTheActiveFederation) {
        UTXO utxo = new UTXO(btcTx.getHash(), output.getIndex(), output.getValue(), 0, btcTx.isCoinBase(), output.getScriptPubKey());
        getActiveFederationBtcUTXOs().add(utxo);
    }
    // Outputs to the retiring federation (if any)
    Wallet retiringFederationWallet = getRetiringFederationWallet(false);
    if (retiringFederationWallet != null) {
        List<TransactionOutput> outputsToTheRetiringFederation = btcTx.getWalletOutputs(retiringFederationWallet);
        for (TransactionOutput output : outputsToTheRetiringFederation) {
            UTXO utxo = new UTXO(btcTx.getHash(), output.getIndex(), output.getValue(), 0, btcTx.isCoinBase(), output.getScriptPubKey());
            getRetiringFederationBtcUTXOs().add(utxo);
        }
    }
}
Also used : UTXO(co.rsk.bitcoinj.core.UTXO) TransactionOutput(co.rsk.bitcoinj.core.TransactionOutput) Wallet(co.rsk.bitcoinj.wallet.Wallet)

Example 2 with TransactionOutput

use of co.rsk.bitcoinj.core.TransactionOutput in project rskj by rsksmart.

the class RskAllowUnconfirmedCoinSelector method select.

@Override
public CoinSelection select(Coin target, List<TransactionOutput> candidates) {
    ArrayList<TransactionOutput> selected = new ArrayList<TransactionOutput>();
    // Sort the inputs by age*value so we get the highest "coindays" spent.
    // TODO: Consider changing the wallets internal format to track just outputs and keep them ordered.
    ArrayList<TransactionOutput> sortedOutputs = new ArrayList<TransactionOutput>(candidates);
    // TODO: Take in network parameters when instanatiated, and then test against the current network. Or just have a boolean parameter for "give me everything"
    if (!target.equals(NetworkParameters.MAX_MONEY)) {
        sortOutputs(sortedOutputs);
    }
    // Now iterate over the sorted outputs until we have got as close to the target as possible or a little
    // bit over (excessive value will be change).
    long total = 0;
    for (TransactionOutput output : sortedOutputs) {
        if (total >= target.value) {
            break;
        }
        selected.add(output);
        total += output.getValue().value;
    }
    // transaction.
    return new CoinSelection(Coin.valueOf(total), selected);
}
Also used : TransactionOutput(co.rsk.bitcoinj.core.TransactionOutput) ArrayList(java.util.ArrayList) CoinSelection(co.rsk.bitcoinj.wallet.CoinSelection)

Example 3 with TransactionOutput

use of co.rsk.bitcoinj.core.TransactionOutput in project rskj by rsksmart.

the class BridgeUtilsTest method createPegOutTx.

private BtcTransaction createPegOutTx(List<byte[]> signatures, int inputsToAdd, Federation federation, boolean isFastBridge) {
    // Setup
    Address address;
    byte[] program;
    if (federation == null) {
        federation = BridgeRegTestConstants.getInstance().getGenesisFederation();
    }
    if (isFastBridge) {
        // Create fast bridge redeem script
        Sha256Hash derivationArgumentsHash = Sha256Hash.of(new byte[] { 1 });
        Script fastBridgeRedeemScript;
        if (federation instanceof ErpFederation) {
            fastBridgeRedeemScript = FastBridgeErpRedeemScriptParser.createFastBridgeErpRedeemScript(federation.getRedeemScript(), derivationArgumentsHash);
        } else {
            fastBridgeRedeemScript = FastBridgeRedeemScriptParser.createMultiSigFastBridgeRedeemScript(federation.getRedeemScript(), derivationArgumentsHash);
        }
        Script fastBridgeP2SH = ScriptBuilder.createP2SHOutputScript(fastBridgeRedeemScript);
        address = Address.fromP2SHHash(networkParameters, fastBridgeP2SH.getPubKeyHash());
        program = fastBridgeRedeemScript.getProgram();
    } else {
        address = federation.getAddress();
        program = federation.getRedeemScript().getProgram();
    }
    // Build prev btc tx
    BtcTransaction prevTx = new BtcTransaction(networkParameters);
    TransactionOutput prevOut = new TransactionOutput(networkParameters, prevTx, Coin.FIFTY_COINS, address);
    prevTx.addOutput(prevOut);
    // Build btc tx to be signed
    BtcTransaction btcTx = new BtcTransaction(networkParameters);
    // Add inputs
    for (int i = 0; i < inputsToAdd; i++) {
        btcTx.addInput(prevOut);
    }
    Script scriptSig;
    if (signatures.isEmpty()) {
        scriptSig = PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation);
    } else {
        scriptSig = ScriptBuilder.createMultiSigInputScriptBytes(signatures, program);
    }
    // Sign inputs
    for (int i = 0; i < inputsToAdd; i++) {
        btcTx.getInput(i).setScriptSig(scriptSig);
    }
    TransactionOutput output = new TransactionOutput(networkParameters, btcTx, Coin.COIN, new BtcECKey().toAddress(networkParameters));
    btcTx.addOutput(output);
    TransactionOutput changeOutput = new TransactionOutput(networkParameters, btcTx, Coin.COIN, federation.getAddress());
    btcTx.addOutput(changeOutput);
    return btcTx;
}
Also used : Script(co.rsk.bitcoinj.script.Script) TransactionOutput(co.rsk.bitcoinj.core.TransactionOutput) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint)

Example 4 with TransactionOutput

use of co.rsk.bitcoinj.core.TransactionOutput in project rskj by rsksmart.

the class PeginInstructionsProvider method extractOpReturnData.

protected static byte[] extractOpReturnData(BtcTransaction btcTx) throws PeginInstructionsException {
    logger.trace("[extractOpReturnData] Getting OP_RETURN data for btc tx: {}", btcTx.getHash());
    byte[] data = new byte[] {};
    int opReturnForRskOccurrences = 0;
    for (int i = 0; i < btcTx.getOutputs().size(); i++) {
        TransactionOutput txOutput = btcTx.getOutput(i);
        if (hasOpReturnForRsk(txOutput)) {
            data = txOutput.getScriptPubKey().getChunks().get(1).data;
            opReturnForRskOccurrences++;
        }
    }
    if (opReturnForRskOccurrences == 0) {
        String message = String.format("No OP_RETURN output found for tx %s", btcTx.getHash());
        throw new NoOpReturnException(message);
    }
    if (opReturnForRskOccurrences > 1) {
        String message = String.format("Only one output with OP_RETURN for RSK is allowed. Found %d", opReturnForRskOccurrences);
        logger.debug("[extractOpReturnData] {}", message);
        throw new PeginInstructionsException(message);
    }
    return data;
}
Also used : TransactionOutput(co.rsk.bitcoinj.core.TransactionOutput)

Aggregations

TransactionOutput (co.rsk.bitcoinj.core.TransactionOutput)4 Address (co.rsk.bitcoinj.core.Address)1 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)1 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)1 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)1 TransactionOutPoint (co.rsk.bitcoinj.core.TransactionOutPoint)1 UTXO (co.rsk.bitcoinj.core.UTXO)1 Script (co.rsk.bitcoinj.script.Script)1 CoinSelection (co.rsk.bitcoinj.wallet.CoinSelection)1 Wallet (co.rsk.bitcoinj.wallet.Wallet)1 RskAddress (co.rsk.core.RskAddress)1 ArrayList (java.util.ArrayList)1