def do_test_tx(self, sighash, index_, flags): txhash, seq, script, witness_script = b'0' * 32, 0xffffffff, b'\x51', b'000000' out_script, spend_script, locktime = b'\x00\x00\x51', b'\x00\x51', 999999 txs_in = [TxIn(txhash, 0, script, seq), TxIn(txhash, 1, script+b'\x51', seq-1), TxIn(txhash, 2, script+b'\x51\x51', seq-2), TxIn(txhash, 3, script+b'\x51\x51\x51', seq-3)] txs_out = [TxOut(55, out_script), TxOut(54, out_script+b'\x51'), TxOut(53, out_script+b'\x51\x51')] pytx = Tx(2, txs_in, txs_out, lock_time=locktime) pytx.unspents = {0: TxOut(5000, spend_script), # FIXME: Make script unique 1: TxOut(5001, spend_script), 2: TxOut(5002, spend_script), 3: TxOut(5003, spend_script)} unspent = pytx.unspents[index_] pytx_hex = pytx.as_hex() if flags & USE_WITNESS: pytx_hash = pytx.signature_for_hash_type_segwit(unspent.script, index_, sighash) else: pytx_hash = pytx.signature_hash(spend_script, index_, sighash) pytx_hash = hex_from_bytes(to_bytes_32(pytx_hash)) tx = tx_init(2, locktime, 3, 3) tx_add_input(tx, tx_input_init(txhash, 0, seq, script, None)) tx_add_raw_input(tx, txhash, 1, seq-1, script+b'\x51', None) tx_add_raw_input(tx, txhash, 2, seq-2, script+b'\x51\x51', None) tx_add_raw_input(tx, txhash, 3, seq-3, script+b'\x51\x51\x51', None) tx_add_raw_output(tx, 55, out_script, 0) tx_add_raw_output(tx, 54, out_script+b'\x51', 0) tx_add_raw_output(tx, 53, out_script+b'\x51\x51', 0) tx_hex = tx_to_hex(tx, 0) amount = (index_ + 1) * 5000 tx_hash = tx_get_btc_signature_hash(tx, index_, unspent.script, unspent.coin_value, sighash, flags) tx_hash = hex_from_bytes(tx_hash) self.assertEqual(pytx_hex, tx_hex) self.assertEqual(pytx_hash, tx_hash)
def do_test_tx(self, sighash, index_, flags): txhash, seq, script, witness_script = b'0' * 32, 0xffffffff, b'\x51', b'000000' out_script, spend_script, locktime = b'\x00\x00\x51', b'\x00\x51', 999999 txs_in = [TxIn(txhash, 0, script, seq), TxIn(txhash, 1, script+b'\x51', seq-1), TxIn(txhash, 2, script+b'\x51\x51', seq-2), TxIn(txhash, 3, script+b'\x51\x51\x51', seq-3)] txs_out = [TxOut(55, out_script), TxOut(54, out_script+b'\x51'), TxOut(53, out_script+b'\x51\x51')] pytx = Tx(2, txs_in, txs_out, lock_time=locktime) pytx.unspents = {0: TxOut(5000, spend_script), # FIXME: Make script unique 1: TxOut(5001, spend_script), 2: TxOut(5002, spend_script), 3: TxOut(5003, spend_script)} unspent = pytx.unspents[index_] pytx_hex = pytx.as_hex() if flags & USE_WITNESS: pytx_hash = pytx.signature_for_hash_type_segwit(unspent.script, index_, sighash) else: pytx_hash = pytx.signature_hash(spend_script, index_, sighash) pytx_hash = hex_from_bytes(to_bytes_32(pytx_hash)) tx = tx_init(2, locktime, 3, 3) tx_add_input(tx, tx_input_init(txhash, 0, seq, script, None)) tx_add_raw_input(tx, txhash, 1, seq-1, script+b'\x51', None, 0) tx_add_raw_input(tx, txhash, 2, seq-2, script+b'\x51\x51', None, 0) tx_add_raw_input(tx, txhash, 3, seq-3, script+b'\x51\x51\x51', None, 0) tx_add_raw_output(tx, 55, out_script, 0) tx_add_raw_output(tx, 54, out_script+b'\x51', 0) tx_add_raw_output(tx, 53, out_script+b'\x51\x51', 0) tx_hex = tx_to_hex(tx, 0) amount = (index_ + 1) * 5000 tx_hash = tx_get_btc_signature_hash(tx, index_, unspent.script, unspent.coin_value, sighash, flags) tx_hash = hex_from_bytes(tx_hash) self.assertEqual(pytx_hex, tx_hex) self.assertEqual(pytx_hash, tx_hash)
def _test_sighash_single(self, netcode): k0 = Key(secret_exponent=PRIV_KEYS[0], is_compressed=True, netcode=netcode) k1 = Key(secret_exponent=PRIV_KEYS[1], is_compressed=True, netcode=netcode) k2 = Key(secret_exponent=PRIV_KEYS[2], is_compressed=True, netcode=netcode) k3 = Key(secret_exponent=PRIV_KEYS[3], is_compressed=True, netcode=netcode) k4 = Key(secret_exponent=PRIV_KEYS[4], is_compressed=True, netcode=netcode) k5 = Key(secret_exponent=PRIV_KEYS[5], is_compressed=True, netcode=netcode) # Fake a coinbase transaction coinbase_tx = Tx.coinbase_tx(k0.sec(), 500000000) coinbase_tx.txs_out.append( TxOut(1000000000, pycoin_compile('%s OP_CHECKSIG' % b2h(k1.sec())))) coinbase_tx.txs_out.append( TxOut(1000000000, pycoin_compile('%s OP_CHECKSIG' % b2h(k2.sec())))) self.assertEqual( '2acbe1006f7168bad538b477f7844e53de3a31ffddfcfc4c6625276dd714155a', b2h_rev(coinbase_tx.hash())) # Make the test transaction txs_in = [ TxIn(coinbase_tx.hash(), 0), TxIn(coinbase_tx.hash(), 1), TxIn(coinbase_tx.hash(), 2), ] txs_out = [ TxOut(900000000, standard_tx_out_script(k3.address())), TxOut(800000000, standard_tx_out_script(k4.address())), TxOut(800000000, standard_tx_out_script(k5.address())), ] tx = Tx(1, txs_in, txs_out) tx.set_unspents(coinbase_tx.txs_out) self.assertEqual( '791b98ef0a3ac87584fe273bc65abd89821569fd7c83538ac0625a8ca85ba587', b2h_rev(tx.hash())) sig_type = SIGHASH_SINGLE sig_hash = tx.signature_hash(coinbase_tx.txs_out[0].script, 0, sig_type) self.assertEqual( 'cc52d785a3b4133504d1af9e60cd71ca422609cb41df3a08bbb466b2a98a885e', b2h(to_bytes_32(sig_hash))) sig = sigmake(k0, sig_hash, sig_type) self.assertTrue(sigcheck(k0, sig_hash, sig[:-1])) tx.txs_in[0].script = pycoin_compile(b2h(sig)) self.assertTrue(tx.is_signature_ok(0)) sig_hash = tx.signature_hash(coinbase_tx.txs_out[1].script, 1, sig_type) self.assertEqual( '93bb883d70fccfba9b8aa2028567aca8357937c65af7f6f5ccc6993fd7735fb7', b2h(to_bytes_32(sig_hash))) sig = sigmake(k1, sig_hash, sig_type) self.assertTrue(sigcheck(k1, sig_hash, sig[:-1])) tx.txs_in[1].script = pycoin_compile(b2h(sig)) self.assertTrue(tx.is_signature_ok(1)) sig_hash = tx.signature_hash(coinbase_tx.txs_out[2].script, 2, sig_type) self.assertEqual( '53ef7f67c3541bffcf4e0d06c003c6014e2aa1fb38ff33240b3e1c1f3f8e2a35', b2h(to_bytes_32(sig_hash))) sig = sigmake(k2, sig_hash, sig_type) self.assertTrue(sigcheck(k2, sig_hash, sig[:-1])) tx.txs_in[2].script = pycoin_compile(b2h(sig)) self.assertTrue(tx.is_signature_ok(2)) sig_type = SIGHASH_SINGLE | SIGHASH_ANYONECANPAY sig_hash = tx.signature_hash(coinbase_tx.txs_out[0].script, 0, sig_type) self.assertEqual( '2003393d246a7f136692ce7ab819c6eadc54ffea38eb4377ac75d7d461144e75', b2h(to_bytes_32(sig_hash))) sig = sigmake(k0, sig_hash, sig_type) self.assertTrue(sigcheck(k0, sig_hash, sig[:-1])) tx.txs_in[0].script = pycoin_compile(b2h(sig)) self.assertTrue(tx.is_signature_ok(0)) sig_hash = tx.signature_hash(coinbase_tx.txs_out[1].script, 1, sig_type) self.assertEqual( 'e3f469ac88e9f35e8eff0bd8ad4ad3bf899c80eb7645947d60860de4a08a35df', b2h(to_bytes_32(sig_hash))) sig = sigmake(k1, sig_hash, sig_type) self.assertTrue(sigcheck(k1, sig_hash, sig[:-1])) tx.txs_in[1].script = pycoin_compile(b2h(sig)) self.assertTrue(tx.is_signature_ok(1)) sig_hash = tx.signature_hash(coinbase_tx.txs_out[2].script, 2, sig_type) self.assertEqual( 'bacd7c3ab79cad71807312677c1788ad9565bf3c00ab9a153d206494fb8b7e6a', b2h(to_bytes_32(sig_hash))) sig = sigmake(k2, sig_hash, sig_type) self.assertTrue(sigcheck(k2, sig_hash, sig[:-1])) tx.txs_in[2].script = pycoin_compile(b2h(sig)) self.assertTrue(tx.is_signature_ok(2))