Search in sources :

Example 16 with ScriptChunk

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

the class BridgeSupport method isInputSignedByThisFederator.

/**
 * Check if the p2sh multisig scriptsig of the given input was already signed by federatorPublicKey.
 * @param federatorPublicKey The key that may have been used to sign
 * @param sighash the sighash that corresponds to the input
 * @param input The input
 * @return true if the input was already signed by the specified key, false otherwise.
 */
private boolean isInputSignedByThisFederator(BtcECKey federatorPublicKey, Sha256Hash sighash, TransactionInput input) {
    List<ScriptChunk> chunks = input.getScriptSig().getChunks();
    for (int j = 1; j < chunks.size() - 1; j++) {
        ScriptChunk chunk = chunks.get(j);
        if (chunk.data.length == 0) {
            continue;
        }
        TransactionSignature sig2 = TransactionSignature.decodeFromBitcoin(chunk.data, false, false);
        if (federatorPublicKey.verify(sighash, sig2)) {
            return true;
        }
    }
    return false;
}
Also used : TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) ScriptChunk(co.rsk.bitcoinj.script.ScriptChunk)

Example 17 with ScriptChunk

use of co.rsk.bitcoinj.script.ScriptChunk 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

ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)17 Script (co.rsk.bitcoinj.script.Script)14 TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)7 Keccak256 (co.rsk.crypto.Keccak256)5 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)5 Test (org.junit.Test)5 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)4 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)4 TransactionInput (co.rsk.bitcoinj.core.TransactionInput)4 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)3 TransactionOutPoint (co.rsk.bitcoinj.core.TransactionOutPoint)3 PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation)3 PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation)3 Block (org.ethereum.core.Block)3 Repository (org.ethereum.core.Repository)3 MutableRepository (org.ethereum.db.MutableRepository)3 RLPList (org.ethereum.util.RLPList)3 RedeemScriptParser (co.rsk.bitcoinj.script.RedeemScriptParser)2 MultiSigType (co.rsk.bitcoinj.script.RedeemScriptParser.MultiSigType)2 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)2