def test_native_P2WSH_SIGHASH_SINGLE_ANYONECANPAY(self): tx = TEST_CASES[2] deserialized = deserialize(tx['unsigned']) serialized = serialize(deserialized) self.assertEqual(serialized, tx['unsigned']) self.assertEqual(deserialized['locktime'], tx['locktime']) ins = self.get_pybtc_vins(tx) outs = self.get_pybtc_outs(tx) generated_tx = mktx(ins, outs) stripped_tx = strip_witness_data(generated_tx) self.assertEqual(stripped_tx, serialized) priv0 = self.append_compressed_flag_to_privkey(tx['ins'][1]['privkey']) partially_signed = segwit_sign(generated_tx, 0, priv0, int(0.16777215 * 10**8), hashcode=SIGHASH_SINGLE|SIGHASH_ANYONECANPAY, script=tx['ins'][0]['txinwitness'][1]) signed = segwit_sign(partially_signed, 1, priv0, int(0.16777215 * 10 ** 8), hashcode=SIGHASH_SINGLE|SIGHASH_ANYONECANPAY, script=tx['ins'][1]['txinwitness'][1], separator_index=tx['ins'][1]['separator']) self.assertEqual(signed, tx['signed']) print('[Native P2WSH] SIGHASH_SINGLE OK')
def test_native_P2WSH_SIGHASH_SINGLE_ANYONECANPAY(self): tx = TEST_CASES[2] deserialized = deserialize(tx['unsigned']) serialized = serialize(deserialized) self.assertEqual(serialized, tx['unsigned']) self.assertEqual(deserialized['locktime'], tx['locktime']) ins = self.get_pybtc_vins(tx) outs = self.get_pybtc_outs(tx) generated_tx = mktx(ins, outs) stripped_tx = strip_witness_data(generated_tx) self.assertEqual(stripped_tx, serialized) priv0 = self.append_compressed_flag_to_privkey(tx['ins'][1]['privkey']) partially_signed = segwit_sign(generated_tx, 0, priv0, int(0.16777215 * 10**8), hashcode=SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, script=tx['ins'][0]['txinwitness'][1]) signed = segwit_sign(partially_signed, 1, priv0, int(0.16777215 * 10**8), hashcode=SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, script=tx['ins'][1]['txinwitness'][1], separator_index=tx['ins'][1]['separator']) self.assertEqual(signed, tx['signed']) print('[Native P2WSH] SIGHASH_SINGLE OK')
def test_native_P2WSH_SIGHASH_SINGLE(self): tx = TEST_CASES[1] deserialized = deserialize(tx['unsigned']) serialized = serialize(deserialized) self.assertEqual(serialized, tx['unsigned']) self.assertEqual(deserialized['locktime'], tx['locktime']) ins = self.get_pybtc_vins(tx) outs = self.get_pybtc_outs(tx) generated_tx = mktx(ins, outs) stripped_tx = strip_witness_data(generated_tx) self.assertEqual(stripped_tx, serialized) partially_signed = p2pk_sign(stripped_tx, 0, self.append_compressed_flag_to_privkey( tx['ins'][0]['privkey']), hashcode=SIGHASH_ALL) priv0 = self.append_compressed_flag_to_privkey( tx['ins'][1]['privkeys'][0]) priv1 = self.append_compressed_flag_to_privkey( tx['ins'][1]['privkeys'][1]) pub0 = privtopub(priv0) pub1 = privtopub(priv1) REDEEM_SCRIPT_STRUCTURE = { 'keys': [pub0, pub1], 'schema': [{ 'reqs': 1, 'keys': [0], }, { 'reqs': 1, 'keys': [1], }] } witness_script = mk_OPCS_multisig_script(REDEEM_SCRIPT_STRUCTURE) sign1 = segwit_multisign(partially_signed, 1, witness_script, priv0, 49 * 10**8, hashcode=SIGHASH_SINGLE) sign2 = segwit_multisign(partially_signed, 1, witness_script, priv1, 49 * 10**8, hashcode=SIGHASH_SINGLE, separator_index=1) signed = apply_segwit_multisignatures(partially_signed, 1, witness_script, [sign2, sign1], dummy=False) self.assertEqual(signed, tx['signed']) print('[Native P2WSH] SIGHASH_SINGLE OK')
def test_native_P2WSH_SIGHASH_SINGLE(self): tx = TEST_CASES[1] deserialized = deserialize(tx['unsigned']) serialized = serialize(deserialized) self.assertEqual(serialized, tx['unsigned']) self.assertEqual(deserialized['locktime'], tx['locktime']) ins = self.get_pybtc_vins(tx) outs = self.get_pybtc_outs(tx) generated_tx = mktx(ins, outs) stripped_tx = strip_witness_data(generated_tx) self.assertEqual(stripped_tx, serialized) partially_signed = p2pk_sign(stripped_tx, 0, self.append_compressed_flag_to_privkey(tx['ins'][0]['privkey']), hashcode=SIGHASH_ALL) priv0 = self.append_compressed_flag_to_privkey(tx['ins'][1]['privkeys'][0]) priv1 = self.append_compressed_flag_to_privkey(tx['ins'][1]['privkeys'][1]) pub0 = privtopub(priv0) pub1 = privtopub(priv1) REDEEM_SCRIPT_STRUCTURE = { 'keys': [ pub0, pub1 ], 'schema': [ { 'reqs': 1, 'keys': [0], }, { 'reqs': 1, 'keys': [1], } ] } witness_script = mk_OPCS_multisig_script(REDEEM_SCRIPT_STRUCTURE) sign1 = segwit_multisign(partially_signed, 1, witness_script, priv0, 49 * 10**8, hashcode=SIGHASH_SINGLE) sign2 = segwit_multisign(partially_signed, 1, witness_script, priv1, 49 * 10 ** 8, hashcode=SIGHASH_SINGLE, separator_index=1) signed = apply_segwit_multisignatures(partially_signed, 1, witness_script, [sign2, sign1], dummy=False) self.assertEqual(signed, tx['signed']) print('[Native P2WSH] SIGHASH_SINGLE OK')
def test_native_P2WPKH_SIGHASH_ALL(self): tx = TEST_CASES[0] deserialized = deserialize(tx['unsigned']) serialized = serialize(deserialized) self.assertEqual(serialized, tx['unsigned']) self.assertEqual(deserialized['locktime'], tx['locktime']) ins = self.get_pybtc_vins(tx) outs = self.get_pybtc_outs(tx) generated_tx = mktx(ins, outs, locktime=tx['locktime']) stripped_tx = strip_witness_data(generated_tx) self.assertEqual(stripped_tx, serialized) partially_signed = p2pk_sign( stripped_tx, 0, self.append_compressed_flag_to_privkey(tx['ins'][0]['privkey'])) signed = segwit_sign( partially_signed, 1, self.append_compressed_flag_to_privkey(tx['ins'][1]['privkey']), tx['ins'][1]['amount'] * 10**8) self.assertEqual(signed, tx['signed']) print('[Native P2WPKH] SIGHASH_ALL OK')
def test_native_P2WPKH_SIGHASH_ALL(self): tx = TEST_CASES[0] deserialized = deserialize(tx['unsigned']) serialized = serialize(deserialized) self.assertEqual(serialized, tx['unsigned']) self.assertEqual(deserialized['locktime'], tx['locktime']) ins = self.get_pybtc_vins(tx) outs = self.get_pybtc_outs(tx) generated_tx = mktx(ins, outs, locktime=tx['locktime']) stripped_tx = strip_witness_data(generated_tx) self.assertEqual(stripped_tx, serialized) partially_signed = p2pk_sign(stripped_tx, 0, self.append_compressed_flag_to_privkey(tx['ins'][0]['privkey'])) signed = segwit_sign(partially_signed, 1, self.append_compressed_flag_to_privkey(tx['ins'][1]['privkey']), tx['ins'][1]['amount'] * 10**8) self.assertEqual(signed, tx['signed']) print('[Native P2WPKH] SIGHASH_ALL OK')
def test_P2SH_P2WSH_ALL_SIGHASH(self): tx = TEST_CASES[3] VIN_AMOUNT = int(9.87654321 * 10**8) deserialized = deserialize(tx['unsigned']) serialized = serialize(deserialized) self.assertEqual(serialized, tx['unsigned']) self.assertEqual(deserialized['locktime'], tx['locktime']) ins = self.get_pybtc_vins(tx) outs = self.get_pybtc_outs(tx) generated_tx = mktx(ins, outs) stripped_tx = strip_witness_data(generated_tx) self.assertEqual(stripped_tx, serialized) priv0 = self.append_compressed_flag_to_privkey( tx['ins'][0]['privkeys'][0]) priv1 = self.append_compressed_flag_to_privkey( tx['ins'][0]['privkeys'][1]) priv2 = self.append_compressed_flag_to_privkey( tx['ins'][0]['privkeys'][2]) priv3 = self.append_compressed_flag_to_privkey( tx['ins'][0]['privkeys'][3]) priv4 = self.append_compressed_flag_to_privkey( tx['ins'][0]['privkeys'][4]) priv5 = self.append_compressed_flag_to_privkey( tx['ins'][0]['privkeys'][5]) witness_script = mk_multisig_script(privtopub(priv0), privtopub(priv1), privtopub(priv2), privtopub(priv3), privtopub(priv4), privtopub(priv5), 6) self.assertEqual(witness_script, tx['ins'][0]['witness_script']) sign0 = segwit_multisign(generated_tx, 0, witness_script, priv0, VIN_AMOUNT, hashcode=SIGHASH_ALL) sign1 = segwit_multisign(generated_tx, 0, witness_script, priv1, VIN_AMOUNT, hashcode=SIGHASH_NONE) sign2 = segwit_multisign(generated_tx, 0, witness_script, priv2, VIN_AMOUNT, hashcode=SIGHASH_SINGLE) sign3 = segwit_multisign(generated_tx, 0, witness_script, priv3, VIN_AMOUNT, hashcode=SIGHASH_ALL | SIGHASH_ANYONECANPAY) sign4 = segwit_multisign(generated_tx, 0, witness_script, priv4, VIN_AMOUNT, hashcode=SIGHASH_NONE | SIGHASH_ANYONECANPAY) sign5 = segwit_multisign(generated_tx, 0, witness_script, priv5, VIN_AMOUNT, hashcode=SIGHASH_SINGLE | SIGHASH_ANYONECANPAY) signed = apply_segwit_multisignatures( stripped_tx, 0, witness_script, [sign0, sign1, sign2, sign3, sign4, sign5], nested=True) self.assertEqual(signed, tx['signed']) print( '[P2WSH 6-of-6 multisig NESTED in P2SH] SIGHASH_SINGLE\SIGHASH_ALL\SIGHASH_NONE & ANYONECANPAY' )
def test_P2SH_P2WSH_ALL_SIGHASH(self): tx = TEST_CASES[3] VIN_AMOUNT = int(9.87654321 * 10**8) deserialized = deserialize(tx['unsigned']) serialized = serialize(deserialized) self.assertEqual(serialized, tx['unsigned']) self.assertEqual(deserialized['locktime'], tx['locktime']) ins = self.get_pybtc_vins(tx) outs = self.get_pybtc_outs(tx) generated_tx = mktx(ins, outs) stripped_tx = strip_witness_data(generated_tx) self.assertEqual(stripped_tx, serialized) priv0 = self.append_compressed_flag_to_privkey(tx['ins'][0]['privkeys'][0]) priv1 = self.append_compressed_flag_to_privkey(tx['ins'][0]['privkeys'][1]) priv2 = self.append_compressed_flag_to_privkey(tx['ins'][0]['privkeys'][2]) priv3 = self.append_compressed_flag_to_privkey(tx['ins'][0]['privkeys'][3]) priv4 = self.append_compressed_flag_to_privkey(tx['ins'][0]['privkeys'][4]) priv5 = self.append_compressed_flag_to_privkey(tx['ins'][0]['privkeys'][5]) witness_script = mk_multisig_script(privtopub(priv0), privtopub(priv1), privtopub(priv2), privtopub(priv3), privtopub(priv4), privtopub(priv5), 6) self.assertEqual(witness_script, tx['ins'][0]['witness_script']) sign0 = segwit_multisign(generated_tx, 0, witness_script, priv0, VIN_AMOUNT, hashcode=SIGHASH_ALL) sign1 = segwit_multisign(generated_tx, 0, witness_script, priv1, VIN_AMOUNT, hashcode=SIGHASH_NONE) sign2 = segwit_multisign(generated_tx, 0, witness_script, priv2, VIN_AMOUNT, hashcode=SIGHASH_SINGLE) sign3 = segwit_multisign(generated_tx, 0, witness_script, priv3, VIN_AMOUNT, hashcode=SIGHASH_ALL|SIGHASH_ANYONECANPAY) sign4 = segwit_multisign(generated_tx, 0, witness_script, priv4, VIN_AMOUNT, hashcode=SIGHASH_NONE|SIGHASH_ANYONECANPAY) sign5 = segwit_multisign(generated_tx, 0, witness_script, priv5, VIN_AMOUNT, hashcode=SIGHASH_SINGLE|SIGHASH_ANYONECANPAY) signed = apply_segwit_multisignatures(stripped_tx, 0, witness_script, [sign0, sign1, sign2, sign3, sign4, sign5], nested=True) self.assertEqual(signed, tx['signed']) print('[P2WSH 6-of-6 multisig NESTED in P2SH] SIGHASH_SINGLE\SIGHASH_ALL\SIGHASH_NONE & ANYONECANPAY')