def test_sign_input(self): private_key = PrivateKey(secret=8675309) tx_ins = [] prev_tx = bytes.fromhex( '0025bc3c0fa8b7eb55b9437fdbd016870d18e0df0ace7bc9864efc38414147c8') tx_ins.append(TxIn(prev_tx, 0)) tx_outs = [] h160 = decode_base58('mzx5YhAH9kNHtcN481u6WkjeHjYtVeKVh2') tx_outs.append( TxOut(amount=int(0.99 * 100000000), script_pubkey=P2PKHScriptPubKey(h160))) h160 = decode_base58('mnrVtF8DWjMu839VW3rBfgYaAfKk8983Xf') tx_outs.append( TxOut(amount=int(0.1 * 100000000), script_pubkey=P2PKHScriptPubKey(h160))) tx = Tx(1, tx_ins, tx_outs, 0, testnet=True) self.assertTrue(tx.sign_input(0, private_key))
def sig_hash_bip143(self, input_index, redeem_script=None, witness_script=None): '''Returns the integer representation of the hash that needs to get signed for index input_index''' # grab the input being signed by looking up the input_index tx_in = self.tx_ins[input_index] # start with the version in 4 bytes, little endian s = int_to_little_endian(self.version, 4) # add the HashPrevouts and HashSequence s += self.hash_prevouts() + self.hash_sequence() # add the previous transaction hash in little endian s += tx_in.prev_tx[::-1] # add the previous transaction index in 4 bytes, little endian s += int_to_little_endian(tx_in.prev_index, 4) # for p2wpkh, we need to compute the ScriptCode if redeem_script: # Exercise 8: for p2sh-p2wpkh, get the hash160 which is the 2nd command of the RedeemScript h160 = redeem_script.commands[1] # the ScriptCode is the P2PKHScriptPubKey created using the hash160 script_code = P2PKHScriptPubKey(h160) else: # get the script pubkey associated with the previous output (remember testnet) script_pubkey = tx_in.script_pubkey(self.testnet) # next get the hash160 in the script_pubkey. for p2wpkh, it's the second command h160 = script_pubkey.commands[1] # finally the ScriptCode is the P2PKHScriptPubKey created using the hash160 script_code = P2PKHScriptPubKey(h160) # add the serialized ScriptCode s += script_code.serialize() # add the value of the input in 8 bytes, little endian s += int_to_little_endian(tx_in.value(testnet=self.testnet), 8) # add the sequence of the input in 4 bytes, little endian s += int_to_little_endian(tx_in.sequence, 4) # add the HashOutputs s += self.hash_outputs() # add the locktime in 4 bytes, little endian s += int_to_little_endian(self.locktime, 4) # add the sighash (SIGHASH_ALL) in 4 bytes, little endian s += int_to_little_endian(SIGHASH_ALL, 4) # hash256 the whole thing, interpret the as a big endian integer using int_to_big_endian return big_endian_to_int(hash256(s))
def test_sign_p2wpkh(self): private_key = PrivateKey(secret=8675309) prev_tx = bytes.fromhex( '6bfa079532dd9fad6cfbf218edc294fdfa7dd0cb3956375bc864577fb36fad97') prev_index = 0 fee = 500 tx_in = TxIn(prev_tx, prev_index) amount = tx_in.value(testnet=True) - fee h160 = decode_base58('mqYz6JpuKukHzPg94y4XNDdPCEJrNkLQcv') tx_out = TxOut(amount=amount, script_pubkey=P2PKHScriptPubKey(h160)) t = Tx(1, [tx_in], [tx_out], 0, testnet=True, segwit=True) self.assertTrue(t.sign_input(0, private_key)) want = '0100000000010197ad6fb37f5764c85b375639cbd07dfafd94c2ed18f2fb6cad9fdd329507fa6b0000000000ffffffff014c400f00000000001976a9146e13971913b9aa89659a9f53d327baa8826f2d7588ac02483045022100feab5b8feefd5e774bdfdc1dc23525b40f1ffaa25a376f8453158614f00fa6cb02204456493d0bc606ebeb3fa008e056bbc96a67cb0c11abcc871bfc2bec60206bf0012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b6700000000' self.assertEqual(t.serialize().hex(), want)
def test_sign_p2sh_p2wpkh(self): private_key = PrivateKey(secret=8675309) redeem_script = private_key.point.p2sh_p2wpkh_redeem_script() prev_tx = bytes.fromhex( '2e19b463bd5c8a3e0f10ae827f5a670f6794fca96394ecf8488321291d1c2ee9') prev_index = 1 fee = 500 tx_in = TxIn(prev_tx, prev_index) amount = tx_in.value(testnet=True) - fee h160 = decode_base58('mqYz6JpuKukHzPg94y4XNDdPCEJrNkLQcv') tx_out = TxOut(amount=amount, script_pubkey=P2PKHScriptPubKey(h160)) t = Tx(1, [tx_in], [tx_out], 0, testnet=True, segwit=True) self.assertTrue( t.sign_input(0, private_key, redeem_script=redeem_script)) want = '01000000000101e92e1c1d29218348f8ec9463a9fc94670f675a7f82ae100f3e8a5cbd63b4192e0100000017160014d52ad7ca9b3d096a38e752c2018e6fbc40cdf26fffffffff014c400f00000000001976a9146e13971913b9aa89659a9f53d327baa8826f2d7588ac0247304402205e3ae5ac9a0e0a16ae04b0678c5732973ce31051ba9f42193e69843e600d84f2022060a91cbd48899b1bf5d1ffb7532f69ab74bc1701a253a415196b38feb599163b012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b6700000000' self.assertEqual(t.serialize().hex(), want)
def address(self, compressed=True, testnet=False): '''Returns the p2pkh address string''' h160 = self.hash160(compressed) from script import P2PKHScriptPubKey return P2PKHScriptPubKey(h160).address(testnet)
def p2pkh_script(self, compressed=True): '''Returns the p2pkh Script object''' h160 = self.hash160(compressed) # avoid circular dependency from script import P2PKHScriptPubKey return P2PKHScriptPubKey(h160)