Search in sources :

Example 16 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeUtilsTest method signWithNecessaryKeys.

private void signWithNecessaryKeys(Federation federation, List<BtcECKey> privateKeys, TransactionInput txIn, BtcTransaction tx, BridgeRegTestConstants bridgeConstants) {
    Script redeemScript = PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation(federation);
    Script inputScript = PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation);
    txIn.setScriptSig(inputScript);
    Sha256Hash sighash = tx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
    for (int i = 0; i < federation.getNumberOfSignaturesRequired(); i++) {
        inputScript = signWithOneKey(federation, privateKeys, inputScript, sighash, i, bridgeConstants);
    }
    txIn.setScriptSig(inputScript);
}
Also used : Script(co.rsk.bitcoinj.script.Script)

Example 17 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class PegTestUtils method createBaseInputScriptThatSpendsFromTheFederation.

public static Script createBaseInputScriptThatSpendsFromTheFederation(Federation federation) {
    Script scriptPubKey = federation.getP2SHScript();
    Script redeemScript = createBaseRedeemScriptThatSpendsFromTheFederation(federation);
    RedeemData redeemData = RedeemData.of(federation.getPublicKeys(), redeemScript);
    Script inputScript = scriptPubKey.createEmptyInputScript(redeemData.keys.get(0), redeemData.redeemScript);
    return inputScript;
}
Also used : Script(co.rsk.bitcoinj.script.Script) RedeemData(co.rsk.bitcoinj.wallet.RedeemData)

Example 18 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeSupport method processSigning.

private void processSigning(long executionBlockNumber, BtcECKey federatorPublicKey, List<byte[]> signatures, byte[] rskTxHash, BtcTransaction btcTx) throws IOException {
    // Build input hashes for signatures
    int numInputs = btcTx.getInputs().size();
    List<Sha256Hash> sighashes = new ArrayList<>();
    List<TransactionSignature> txSigs = new ArrayList<>();
    for (int i = 0; i < numInputs; i++) {
        TransactionInput txIn = btcTx.getInput(i);
        Script inputScript = txIn.getScriptSig();
        List<ScriptChunk> chunks = inputScript.getChunks();
        byte[] program = chunks.get(chunks.size() - 1).data;
        Script redeemScript = new Script(program);
        sighashes.add(btcTx.hashForSignature(i, redeemScript, BtcTransaction.SigHash.ALL, false));
    }
    // Verify given signatures are correct before proceeding
    for (int i = 0; i < numInputs; i++) {
        BtcECKey.ECDSASignature sig;
        try {
            sig = BtcECKey.ECDSASignature.decodeFromDER(signatures.get(i));
        } catch (RuntimeException e) {
            logger.warn("Malformed signature for input {} of tx {}: {}", i, new Keccak256(rskTxHash), Hex.toHexString(signatures.get(i)));
            return;
        }
        Sha256Hash sighash = sighashes.get(i);
        if (!federatorPublicKey.verify(sighash, sig)) {
            logger.warn("Signature {} {} is not valid for hash {} and public key {}", i, Hex.toHexString(sig.encodeToDER()), sighash, federatorPublicKey);
            return;
        }
        TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
        txSigs.add(txSig);
        if (!txSig.isCanonical()) {
            logger.warn("Signature {} {} is not canonical.", i, Hex.toHexString(signatures.get(i)));
            return;
        }
    }
    // All signatures are correct. Proceed to signing
    for (int i = 0; i < numInputs; i++) {
        Sha256Hash sighash = sighashes.get(i);
        TransactionInput input = btcTx.getInput(i);
        Script inputScript = input.getScriptSig();
        boolean alreadySignedByThisFederator = isInputSignedByThisFederator(federatorPublicKey, sighash, input);
        // Sign the input if it wasn't already
        if (!alreadySignedByThisFederator) {
            try {
                int sigIndex = inputScript.getSigInsertionIndex(sighash, federatorPublicKey);
                inputScript = ScriptBuilder.updateScriptWithSignature(inputScript, txSigs.get(i).encodeToBitcoin(), sigIndex, 1, 1);
                input.setScriptSig(inputScript);
                logger.debug("Tx input {} for tx {} signed.", i, new Keccak256(rskTxHash));
            } catch (IllegalStateException e) {
                Federation retiringFederation = getRetiringFederation();
                if (getActiveFederation().hasPublicKey(federatorPublicKey)) {
                    logger.debug("A member of the active federation is trying to sign a tx of the retiring one");
                    return;
                } else if (retiringFederation != null && retiringFederation.hasPublicKey(federatorPublicKey)) {
                    logger.debug("A member of the retiring federation is trying to sign a tx of the active one");
                    return;
                }
                throw e;
            }
        } else {
            logger.warn("Input {} of tx {} already signed by this federator.", i, new Keccak256(rskTxHash));
            break;
        }
    }
    // If tx fully signed
    if (hasEnoughSignatures(btcTx)) {
        logger.info("Tx fully signed {}. Hex: {}", btcTx, Hex.toHexString(btcTx.bitcoinSerialize()));
        provider.getRskTxsWaitingForSignatures().remove(new Keccak256(rskTxHash));
        eventLogger.logReleaseBtc(btcTx);
    } else {
        logger.debug("Tx not yet fully signed {}.", new Keccak256(rskTxHash));
    }
}
Also used : Script(co.rsk.bitcoinj.script.Script) TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) Keccak256(co.rsk.crypto.Keccak256) ScriptChunk(co.rsk.bitcoinj.script.ScriptChunk)

Aggregations

Script (co.rsk.bitcoinj.script.Script)18 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)8 Test (org.junit.Test)7 RskAddress (co.rsk.core.RskAddress)6 Keccak256 (co.rsk.crypto.Keccak256)6 TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)5 RepositoryImpl (co.rsk.db.RepositoryImpl)5 BridgeRegTestConstants (co.rsk.config.BridgeRegTestConstants)4 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)3 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)3 BridgeEventLoggerImpl (co.rsk.peg.utils.BridgeEventLoggerImpl)3 IOException (java.io.IOException)3 Repository (org.ethereum.core.Repository)3 RLPList (org.ethereum.util.RLPList)3 LogInfo (org.ethereum.vm.LogInfo)3 Wallet (co.rsk.bitcoinj.wallet.Wallet)2 BridgeConstants (co.rsk.config.BridgeConstants)2 BridgeStorageProvider (co.rsk.peg.BridgeStorageProvider)2