Skip to content

Commit

Permalink
Add logs when throwing errors. Use getScriptSigWithSignature instead …
Browse files Browse the repository at this point in the history
…of updateScriptWithSignature
  • Loading branch information
julia-zack committed Sep 10, 2024
1 parent 0164b3b commit 3ac5e26
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
22 changes: 15 additions & 7 deletions rskj-core/src/main/java/co/rsk/peg/bitcoin/BitcoinUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,29 @@ public static Optional<Script> extractRedeemScriptFromInput(TransactionInput txI

public static void removeSignaturesFromTransactionWithInputsWithP2shMultiSigInputScript(BtcTransaction transaction) {
if (transaction.hasWitness()) {
throw new IllegalArgumentException("Removing signatures from SegWit transactions is not allowed.");
String message = "Removing signatures from SegWit transactions is not allowed.";
logger.error(message);
throw new IllegalArgumentException(message);
}

if (transaction.getInputs().isEmpty()) {
throw new IllegalArgumentException("Cannot remove signatures from transaction inputs of a transaction without inputs.");
String message = "Cannot remove signatures from transaction inputs of a transaction without inputs.";
logger.error(message);
throw new IllegalArgumentException(message);
}

for (TransactionInput input : transaction.getInputs()) {
Optional<Script> inputRedeemScriptOpt = extractRedeemScriptFromInput(input);

if (!inputRedeemScriptOpt.isPresent()) {
throw new IllegalArgumentException("Cannot remove signatures from transaction inputs that does not have p2sh multisig input script.");
}

Script inputRedeemScript = inputRedeemScriptOpt.get();
Script inputRedeemScript = Optional.of(inputRedeemScriptOpt)
.get()
.orElseThrow(
() -> {
String message = "Cannot remove signatures from transaction inputs that do not have p2sh multisig input script.";
logger.error(message);
return new IllegalArgumentException(message);
}
);
Script p2shScript = ScriptBuilder.createP2SHOutputScript(inputRedeemScript);
Script emptyInputScript = p2shScript.createEmptyInputScript(null, inputRedeemScript);
input.setScriptSig(emptyInputScript);
Expand Down
15 changes: 8 additions & 7 deletions rskj-core/src/test/java/co/rsk/peg/bitcoin/BitcoinTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.crypto.HashUtil;

import static co.rsk.bitcoinj.script.ScriptBuilder.createP2SHOutputScript;

public class BitcoinTestUtils {

public static List<BtcECKey> getBtcEcKeysFromSeeds(String[] seeds, boolean sorted) {
Expand All @@ -38,7 +40,7 @@ public static Address createP2PKHAddress(NetworkParameters networkParameters, St

public static Address createP2SHMultisigAddress(NetworkParameters networkParameters, List<BtcECKey> keys) {
Script redeemScript = ScriptBuilder.createRedeemScript((keys.size() / 2) + 1, keys);
Script outputScript = ScriptBuilder.createP2SHOutputScript(redeemScript);
Script outputScript = createP2SHOutputScript(redeemScript);

return Address.fromP2SHScript(networkParameters, outputScript);
}
Expand Down Expand Up @@ -137,20 +139,19 @@ private static void signLegacyTransactionInputWithP2shMultiSigInputScript(BtcTra
TransactionInput input = transaction.getInput(inputIndex);

Optional<Script> inputRedeemScriptOpt = BitcoinUtils.extractRedeemScriptFromInput(input);
if (!inputRedeemScriptOpt.isPresent()) {
throw new IllegalArgumentException("Cannot sign inputs that are not from a multisig");
}
Script inputRedeemScript = Optional.of(inputRedeemScriptOpt)
.get()
.orElseThrow(() -> new IllegalArgumentException("Cannot sign inputs that are not from a multisig"));

Script inputRedeemScript = inputRedeemScriptOpt.get();
Sha256Hash sigHash = transaction.hashForSignature(inputIndex, inputRedeemScript, BtcTransaction.SigHash.ALL, false);
Script inputScriptSig = input.getScriptSig();
for (BtcECKey key : keys) {
BtcECKey.ECDSASignature sig = key.sign(sigHash);
TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
byte[] txSigEncoded = txSig.encodeToBitcoin();

inputScriptSig =
ScriptBuilder.updateScriptWithSignature(inputScriptSig, txSigEncoded, keys.indexOf(key), 1, 1);
Script outputScript = createP2SHOutputScript(inputRedeemScript);
inputScriptSig = outputScript.getScriptSigWithSignature(inputScriptSig, txSigEncoded, keys.indexOf(key));
input.setScriptSig(inputScriptSig);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ void removeSignaturesFromTransaction_whenTransactionInputsDoNotHaveP2shMultiSigI
// act & assert
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class,
() -> BitcoinUtils.removeSignaturesFromTransactionWithInputsWithP2shMultiSigInputScript(transaction));
Assertions.assertEquals("Cannot remove signatures from transaction inputs that does not have p2sh multisig input script.", exception.getMessage());
Assertions.assertEquals("Cannot remove signatures from transaction inputs that do not have p2sh multisig input script.", exception.getMessage());
}

@Test
Expand Down

0 comments on commit 3ac5e26

Please sign in to comment.