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 debug1(): z = 82736482736928392039492837498273984692387492 pk = PrivateKey(secret=privatekey) r, s = ardubridge.sign(z) sig = pk.sign(z) print("r1: ") print(r) print("s1: ") print(s) print("r2: ") print(sig.r) print("s2: ") print(sig.s)
def test_p2pk_script(self): from ecc import PrivateKey, N from random import randint # public key secret = randint(0, N) pk = PrivateKey(secret) sec = pk.point.sec(compressed=False) # signature z = randint(0, 2**256) sig = pk.sign(z) der = sig.der() + b'\x01' # append SIGHASH_ALL # construct and verify P2PK script script_pubkey = p2pk_script(sec) script_sig = Script([der]) combined_script = script_sig + script_pubkey self.assertTrue(combined_script.evaluate(z))
# 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
def test_sign(self): pk = PrivateKey(randint(0, 2**256)) z = randint(0, 2**256) sig = pk.sign(z) self.assertTrue(pk.point.verify(z, sig))
from ecc import PrivateKey from hashlib import sha256 password = '******' message = 'my message' e = int.from_bytes(sha256(password.encode('utf-8')).digest(), 'big') z = int.from_bytes(sha256(message.encode('utf-8')).digest(), 'big') print(e) print(z) signature = PrivateKey.sign(e, z) print(signature)
from helper import decode_base58, little_endian_to_int, hash256, SIGHASH_ALL from script import p2pkh_script, Script from tx import TxIn, TxOut, Tx from ecc import PrivateKey # our old private key private_key = PrivateKey(little_endian_to_int( hash256(b'dat_test_private_key'))) prev_tx1 = bytes.fromhex( '23f031f63c53f8fbaf58ff5c2df34f60099d0ddb775ecf98dbf5f27fd5c555de') prev_index1 = 0 tx_in1 = TxIn(prev_tx1, prev_index1) tx_outs = [] target_amount = int((0.00100000 * 100000000) - 192) 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_in1], [target_output], 0, True) z1 = tx_obj.sig_hash(0) der1 = private_key.sign(z1).der() sig1 = der1 + SIGHASH_ALL.to_bytes(1, 'big') sec = private_key.point.sec() script_sig1 = Script([sig1, sec]) tx_obj.tx_ins[0].script_sig = script_sig1 print(tx_obj.serialize().hex())
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]) print(hexlify(tx_obj.serialize())) '''der = pk.sign(z).der() sig = der + bytes([hash_type]) sec = pk.point.sec() script_sig = bytes([len(sig)]) + sig + bytes([len(sec)]) + sec script_sig = bytes([len(script_sig)]) + script_sig tx_obj.tx_ins[0].script_sig = Script.parse(script_sig) print(hexlify(tx_obj.serialize()))
dat_redeem_script_serialized = dat_redeem_script_op.serialize() prev_tx = bytes.fromhex( 'f387eba7393e07b4c2134cd29551b7294a992c9bae129b347b64ba6650522acb') prev_index = 0 target_h160 = decode_base58('mvEg6eZ3sUApodedYQrkpEPMMALsr1K1k1') target_script = p2pkh_script(target_h160) target_amount = int(0.00008 * 100000000) target_output = TxOut(amount=target_amount, script_pubkey=target_script) tx_in = TxIn(prev_tx, prev_index) tx_obj = Tx(1, [tx_in], [target_output], 0, True) z = tx_obj.sig_hash(0) private_key1 = PrivateKey(secret1) private_key2 = PrivateKey(secret2) der1 = private_key1.sign(z).der() der2 = private_key2.sign(z).der() sig1 = der1 + SIGHASH_ALL.to_bytes(1, 'big') sig2 = der2 + SIGHASH_ALL.to_bytes(1, 'big') script_sig = Script([0x00, sig1, sig2, dat_redeem_script_serialized]) print('script_sig=:{}'.format(script_sig)) tx_obj.tx_ins[0].script_sig = script_sig print(tx_obj.serialize().hex()) # end