Advertisement
Guest User

Untitled

a guest
Feb 14th, 2021
71
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from helper import decode_base58, little_endian_to_int, hash256, SIGHASH_ALL, hash160, h160_to_p2sh_address
  2. from script import p2pkh_script, p2sh_script, Script
  3. from tx import TxIn, TxOut, Tx
  4. from ecc import PrivateKey
  5.  
  6. secret = little_endian_to_int(hash256(b'dat_test_private_key'))
  7.  
  8. private_key = PrivateKey(secret)
  9.  
  10. public_key = print(private_key.point.address(testnet=False)) #1FiiobU54Sja2XB1pqtNzKB2VAkAzm2Ssi
  11. public_key = print(private_key.point.address(testnet=True)) # mvEg6eZ3sUApodedYQrkpEPMMALsr1K1k1
  12.  
  13. secret1 = little_endian_to_int(hash256(b'dat_test_private_key_p2sh1'))
  14.  
  15. secret2 = little_endian_to_int(hash256(b'dat_test_private_key2_p2sh2'))
  16.  
  17. ## first we create the scriptPubKey of the redeemscript or redeem script
  18.  
  19. # we decode our previous pubkey to put them in script commands
  20.  
  21. private_key1 = PrivateKey(secret1)
  22. private_key2 = PrivateKey(secret2)
  23.  
  24. public_key1 = decode_base58('mvEg6eZ3sUApodedYQrkpEPMMALsr1K1k1')
  25. public_key2 = decode_base58('mwjg9Y1jeFdNu7DQBAW7DUbJqj6jMmhd74')
  26.  
  27. #redeem script as op code
  28.  
  29. dat_redeem_script_op = Script([0x52, public_key1, public_key2, 0x52, 0xae])
  30.  
  31. # we serialize it in binary
  32.  
  33. dat_redeem_script_serialized = dat_redeem_script_op.raw_serialize()
  34.  
  35. # hash 160
  36.  
  37. dat_redeem_script_h160 = hash160(dat_redeem_script_serialized)
  38.  
  39. # we get the address
  40.  
  41. dat_redeem_script_address = h160_to_p2sh_address(dat_redeem_script_h160, testnet=True)
  42.  
  43. # get it back in bytes
  44.  
  45. dat_target_h160 = decode_base58(dat_redeem_script_address)
  46.  
  47. # use the result to build our p2sh_script
  48.  
  49. target_script = p2sh_script(dat_target_h160)
  50.  
  51. print(target_script)
  52. print('is p2sh: {}'.format(target_script.is_p2sh_script_pubkey()))
  53. print(dat_redeem_script_h160)
  54. print()
  55. print(dat_redeem_script_address)
  56.  
  57. # UTXO that we gonna receive
  58.  
  59. prev_tx = bytes.fromhex('2adf399966b32e8543ca829263958154d3a03ac7b2a02dd49aae5a9248e636b7')
  60. prev_index = 0
  61.  
  62. # create the txin with the output of the UTXO we have been given
  63.  
  64. tx_in = TxIn(prev_tx, prev_index)
  65.  
  66. # output address that will receive our sat
  67.  
  68. target_amount = int(0.00009 * 100000000)
  69. target_output = TxOut(amount=target_amount, script_pubkey=target_script)
  70.  
  71. tx_obj = Tx(1, [tx_in], [target_output], 0, True)
  72.  
  73. # now we sign the transaction
  74.  
  75. z = tx_obj.sig_hash(0)
  76. private_key = PrivateKey(little_endian_to_int(hash256(b'dat_test_private_key')))
  77. der = private_key.sign(z).der()
  78. sig = der + SIGHASH_ALL.to_bytes(1, 'big')
  79. sec = private_key.point.sec()
  80. script_sig = Script([sig, sec])
  81. tx_obj.tx_ins[0].script_sig = script_sig
  82. print(tx_obj.serialize().hex())
Advertisement
RAW Paste Data Copied
Advertisement