Search in sources :

Example 6 with TransactionSignature

use of co.rsk.bitcoinj.crypto.TransactionSignature 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

TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)6 Script (co.rsk.bitcoinj.script.Script)4 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)3 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)2 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)2 RskAddress (co.rsk.core.RskAddress)2 RepositoryImpl (co.rsk.db.RepositoryImpl)2 SimpleBlockChain (co.rsk.peg.simples.SimpleBlockChain)2 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)2 org.ethereum.core (org.ethereum.core)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 BridgeConstants (co.rsk.config.BridgeConstants)1 BridgeRegTestConstants (co.rsk.config.BridgeRegTestConstants)1 Keccak256 (co.rsk.crypto.Keccak256)1