Skip to content

Commit

Permalink
Add TransactionWitness.redeemP2WSH()
Browse files Browse the repository at this point in the history
cherry pick bitcoinj@f92124d
  • Loading branch information
oscarguindzberg committed Oct 15, 2020
1 parent 1f6ee08 commit 3e2fac7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/src/main/java/org/bitcoinj/core/TransactionWitness.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.annotation.Nullable;

import org.bitcoinj.crypto.TransactionSignature;
import org.bitcoinj.script.Script;

public class TransactionWitness {
public static final TransactionWitness EMPTY = new TransactionWitness(0);
Expand All @@ -41,6 +42,20 @@ public static TransactionWitness redeemP2WPKH(@Nullable TransactionSignature sig
return witness;
}

/**
* Creates the stack pushes necessary to redeem a P2WSH output.
*/
public static TransactionWitness redeemP2WSH(Script witnessScript, TransactionSignature... signatures) {
TransactionWitness witness = new TransactionWitness(signatures.length + 2);
witness.setPush(0, new byte[]{});
int i;
for (i = 0; i < signatures.length; i++) {
witness.setPush(i + 1, signatures[i].encodeToBitcoin());
}
witness.setPush(i + 1, witnessScript.getProgram());
return witness;
}

private final List<byte[]> pushes;

public TransactionWitness(int pushCount) {
Expand Down
24 changes: 24 additions & 0 deletions core/src/test/java/org/bitcoinj/core/TransactionWitnessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
package org.bitcoinj.core;

import static org.junit.Assert.*;

import org.bitcoinj.crypto.TransactionSignature;
import org.bitcoinj.script.Script;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class TransactionWitnessTest {
Expand All @@ -33,4 +37,24 @@ public void testToString() throws Exception {
w3.setPush(3, Utils.HEX.decode("123ccc"));
assertEquals("123aaa 123bbb EMPTY 123ccc", w3.toString());
}


@Test
public void testRedeemP2WSH() throws SignatureDecodeException {
ECKey.ECDSASignature ecdsaSignature1 = TransactionSignature.decodeFromDER(Hex.decode("3045022100c3d84f7bf41c7eda3b23bbbccebde842a451c1a0aca39df706a3ff2fe78b1e0a02206e2e3c23559798b02302ad6fa5ddbbe87af5cc7d3b9f86b88588253770ab9f79"));
TransactionSignature signature1 = new TransactionSignature(ecdsaSignature1, Transaction.SigHash.ALL, false);
ECKey.ECDSASignature ecdsaSignature2 = TransactionSignature.decodeFromDER(Hex.decode("3045022100fcfe4a58f2878047ef7c5889fc52a3816ad2dd218807daa3c3eafd4841ffac4d022073454df7e212742f0fee20416b418a2c1340a33eebed5583d19a61088b112832"));
TransactionSignature signature2 = new TransactionSignature(ecdsaSignature2, Transaction.SigHash.ALL, false);

Script witnessScript = new Script(Hex.decode("522102bb65b325a986c5b15bd75e0d81cf149219597617a70995efedec6309b4600fa02103c54f073f5db9f68915019801435058c9232cb72c6528a2ca15af48eb74ca8b9a52ae"));

TransactionWitness witness = TransactionWitness.redeemP2WSH(witnessScript, signature1, signature2);
assertEquals(4, witness.getPushCount());
assertArrayEquals(new byte[]{}, witness.getPush(0));
assertArrayEquals(signature1.encodeToBitcoin(), witness.getPush(1));
assertArrayEquals(signature2.encodeToBitcoin(), witness.getPush(2));
assertArrayEquals(witnessScript.getProgram(), witness.getPush(3));


}
}

0 comments on commit 3e2fac7

Please sign in to comment.