def build_transaction(transidsarr, transindexarr, pubeysarr, amountsarr, privatekey): tx_ins = buildinputs(transidsarr, transindexarr) tx_outs = buildoutputs(pubkeysarr, amountsarr) tx_obj = Tx(version=1, tx_ins=tx_ins, tx_outs=tx_outs, locktime=0, testnet=True) hash_type = SIGHASH_ALL z = tx_obj.sig_hash(0, hash_type) pk = PrivateKey(secret=privatekey) sighash = SIGHASH_ALL z = tx_obj.sig_hash(0, sighash) #print(z) sig = pk.sign(z) #print("r: " + str(sig.r)) #print("s: " + str(sig.s)) der = pk.sign(z).der() sig = der + bytes([sighash]) sec = pk.point.sec() tx_obj.tx_ins[0].script_sig = Script([sig, sec]) #print("serialized:") #print(hexlify(Script([sig,sec]).serialize())) #print("----------") return hexlify(tx_obj.serialize())
def test_example_1(self): tx_ins = [] prev_tx = bytes.fromhex( '8be2f69037de71e3bc856a6627ed3e222a7a2d0ce81daeeb54a3aea8db274149') prev_index = 4 tx_ins.append(TxIn(prev_tx, prev_index)) tx_outs = [] h160 = decode_base58('mzx5YhAH9kNHtcN481u6WkjeHjYtVeKVh2') tx_outs.append( TxOut( amount=int(0.38 * 100000000), script_pubkey=p2pkh_script(h160), )) h160 = decode_base58('mnrVtF8DWjMu839VW3rBfgYaAfKk8983Xf') tx_outs.append( TxOut( amount=int(0.1 * 100000000), script_pubkey=p2pkh_script(h160), )) tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True) z = tx_obj.sig_hash(0) pk = PrivateKey(secret=8675309) der = pk.sign(z).der() sig = der + SIGHASH_ALL.to_bytes(1, 'big') sec = pk.point.sec() tx_obj.tx_ins[0].script_sig = Script([sig, sec]) want = '0100000001494127dba8aea354ebae1de80c2d7a2a223eed27666a85bce371de3790f6e28b040000006b483045022100fa3032607b50e8cb05bedc9d43f986f19dedc22e61320b9765061c5cd9c66946022072d514ef637988515bfa59a660596206de68f0ed4090d0a398e70f4d81370dfb012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b67ffffffff0280d54302000000001976a914d52ad7ca9b3d096a38e752c2018e6fbc40cdf26f88ac80969800000000001976a914507b27411ccf7f16f10297de6cef3f291623eddf88ac00000000' self.assertEqual(tx_obj.serialize().hex(), want)
def test_example_6(self): tx_ins = [] prev_tx = bytes.fromhex( '0d6fe5213c0b3291f208cba8bfb59b7476dffacc4e5cb66f6eb20a080843a299') prev_index = 13 tx_ins.append(TxIn(prev_tx, prev_index, Script([]), 0xffffffff)) tx_outs = [] change_amount = int(0.33 * 100000000) change_h160 = decode_base58('mzx5YhAH9kNHtcN481u6WkjeHjYtVeKVh2') change_script = p2pkh_script(change_h160) tx_outs.append(TxOut(amount=change_amount, script_pubkey=change_script)) target_amount = int(0.1 * 100000000) target_h160 = decode_base58('mnrVtF8DWjMu839VW3rBfgYaAfKk8983Xf') target_script = p2pkh_script(target_h160) tx_outs.append(TxOut(amount=target_amount, script_pubkey=target_script)) transaction = Tx(1, tx_ins, tx_outs, 0, testnet=True) z = transaction.sig_hash(0) private_key = PrivateKey(secret=8675309) der = private_key.sign(z).der() sig = der + SIGHASH_ALL.to_bytes(1, 'big') sec = private_key.point.sec() transaction.tx_ins[0].script_sig = Script([sig, sec]) want = '010000000199a24308080ab26e6fb65c4eccfadf76749bb5bfa8cb08f291320b3c21e56f0d0d0000006b4830450221008ed46aa2cf12d6d81065bfabe903670165b538f65ee9a3385e6327d80c66d3b502203124f804410527497329ec4715e18558082d489b218677bd029e7fa306a72236012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b67ffffffff02408af701000000001976a914d52ad7ca9b3d096a38e752c2018e6fbc40cdf26f88ac80969800000000001976a914507b27411ccf7f16f10297de6cef3f291623eddf88ac00000000' self.assertEqual(transaction.serialize().hex(), want)
def check_htlc(node, commitment_tx, secret): tx_in = TxIn(bytes.fromhex(commitment_tx.id()), 2) tx_out = TxOut(amount=commitment_tx.tx_outs[2].amount, script_pubkey=commitment_tx.tx_outs[0].script_pubkey) spendingTx = Tx(1, [tx_in], [tx_out], 0, True) z = spendingTx.sig_hash(0) signature = node.private_key.sign(z).der() + SIGHASH_ALL.to_bytes(1, 'big') combined = Script( [signature, node.public_key.sec(), str.encode(secret), b'1']) + commitment_tx.tx_outs[2].script_pubkey return combined.evaluate(z, None)
def build_transaction2(transidsarr, transindexarr, pubkeysarr, amountsarr, tnet=True): tx_ins = buildinputs(transidsarr, transindexarr) print("ins") print(tx_ins) tx_outs = buildoutputs(pubkeysarr, amountsarr) print("pubkeys") print(pubkeysarr) print("outs") print(tx_outs) tx_obj = Tx(version=1, tx_ins=tx_ins, tx_outs=tx_outs, locktime=0, testnet=tnet) #hash_type = SIGHASH_ALL #z = tx_obj.sig_hash(0, hash_type) #pk = PrivateKey(secret=privatekey) for i in range(len(tx_ins)): sighash = SIGHASH_ALL z = tx_obj.sig_hash(i, sighash) #print("getting sign:") r, s = ardubridge.sign(z) s = int(s) others = N - s if others < s: s = others #print("r: " + str(r)) #print("s: " + str(s)) sig = Signature(int(r), s) der = sig.der() sig = der + bytes([sighash]) #sec = pk.point.sec() #print("public point:") #print(int(pk.point.x.hex(), 16)) #print(int(pk.point.y.hex(), 16)) x, y = ardubridge.getpubkey() if (x == -1 and y == -1): return '-1' #pub = S256Point(53237820045986896539096637357322002537362350769420441605069248472301971758546, 49407176618187043960559197373734381057571970898731550795341045595301080938882) pub = S256Point(int(x), int(y)) sec2 = pub.sec() tx_obj.tx_ins[i].script_sig = Script([sig, sec2]) return hexlify(tx_obj.serialize())
# output address that will receive the sat we have not spend change_h160 = decode_base58('mvEg6eZ3sUApodedYQrkpEPMMALsr1K1k1') change_script = p2pkh_script(change_h160) change_output = TxOut(amount=change_amount, script_pubkey=change_script) # output address that will receive our sat target_amount = int(0.00006 * 100000000) target_h160 = decode_base58('mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv') target_script = p2pkh_script(target_h160) target_output = TxOut(amount=target_amount, script_pubkey=target_script) tx_obj = Tx(1, [tx_in], [change_output, target_output], 0, True) #print(tx_obj).hex() # now we sign the transaction z = tx_obj.sig_hash(0) private_key = PrivateKey(little_endian_to_int( hash256(b'dat_test_private_key'))) der = private_key.sign(z).der() sig = der + SIGHASH_ALL.to_bytes(1, 'big') sec = private_key.point.sec() script_sig = Script([sig, sec]) tx_obj.tx_ins[0].script_sig = script_sig print(tx_obj.serialize().hex()) # end
sat_in_bit = 100000000 fee = 0.0001 * sat_in_bit input_amount = tx_input.value(testnet=True) #creating the target output target_amount = int(0.005 * sat_in_bit) target_h160 = decode_base58(sender_address) target_script = p2pkh_script(target_h160) target_output = TxOut(amount=target_amount, script_pubkey=target_script) #creating the change output change_amount = int(input_amount - target_amount - fee) change_160 = decode_base58(receiver_address) change_script = p2pkh_script(change_160) change_output = TxOut(amount=change_amount, script_pubkey=change_script) #create the Tx object tx_obj = Tx(1, [tx_input], [change_output, target_output], 0, True) #signing the transaction z = tx_obj.sig_hash(0) #get sig_hash of the first input der = private_key.sign(z).der() sig = der + SIGHASH_ALL.to_bytes(1, 'big') sec = public_key.sec() script_sig = Script([sig, sec]) tx_obj.tx_ins[0].script_sig = script_sig print(tx_obj.serialize().hex()) print(sender_address) print(receiver_address)
print(len(h160)) print(hexlify(h160)) tx_outs.append(TxOut( amount=int(t2*.97), script_pubkey=p2pkh_script(h160), )) h160 = decode_base58(taddr2) tx_outs.append(TxOut( amount=int(t2*.03), script_pubkey=p2pkh_script(h160), )) tx_obj = Tx(version=1, tx_ins=tx_ins, tx_outs=tx_outs, locktime=0, testnet=True) # Step 3 hash_type = SIGHASH_ALL z = tx_obj.sig_hash(0, hash_type) pk = PrivateKey(secret=privatekey) # sign with SIGHASH_ALL sighash = SIGHASH_ALL # get the sighash z = tx_obj.sig_hash(0, sighash) # sign with priv to get der der = pk.sign(z).der() # add the sighash as a single byte bytes([sighash]) sig = der + bytes([sighash]) # get the sec from priv sec = pk.point.sec() # create a new script that has sig and sec as the new input sig tx_obj.tx_ins[0].script_sig = Script([sig, sec])