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);
}
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;
}
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));
}
}
Aggregations