epitap

bitcoin tutorial assignment

Apr 11th, 2022
2,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. from bitcoin.wallet import *
  2. from bitcoin.base58 import *
  3. import hashlib*
  4. import bitcoin *
  5. from bitcoin.core import *
  6. from bitcoin.core.script import *
  7. import bitcoin, bitcoin.core, bitcoin.core.script, bitcoin.core.scripteval, bitcoin.wallet
  8. from bitcoin.wallet import *
  9. from bitcoin.signmessage import *
  10.  
  11.  
  12. key = "5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
  13. key2 = "5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcnada"
  14. try:
  15.     secret = CBitcoinSecret(key)
  16.     print(secret)
  17.     secret2 = CBitcoinSecret(key2)
  18. except Base58ChecksumError:
  19.     print("Invalid Base 58 Key")
  20.  
  21.  
  22. key = "5J3mBbAH58CpQ3Y5RNJpUKPE62SQ5tfcvU2JpbnkeyhfsYB1Jcn"
  23. msg = "message to sign"
  24.  
  25. secret = CBitcoinSecret(key)
  26. message = BitcoinMessage(msg)
  27. signature = SignMessage(secret, message)
  28.  
  29. address = P2PKHBitcoinAddress.from_pubkey(secret.pub)
  30. message = BitcoinMessage(msg)
  31. verify = VerifyMessage(address, message, signature)
  32. print('Address: %s' % address)
  33. print('Message: %s' % msg)
  34. print('Signature: %s' % signature)
  35. print('Verified: %s' % verify)
  36. print("\n")
  37. print('Verify using bitcoin core this command:')
  38. print('\n`bitcoin-cli verifymessage %s \'%s\' \'%s\'`\n' % (address, signature.decode('ascii'), msg))
  39.  
  40.  
  41. bitcoin.SelectParams('mainnet')
  42.  
  43. h = hashlib.sha256(b'wallet secret key').digest()
  44. seckey = CBitcoinSecret.from_secret_bytes(h)
  45.  
  46. # Creating Script Pub Key
  47. txin_scriptPubKey = CScript([OP_DUP, OP_HASH160, Hash160(seckey.pub), OP_EQUALVERIFY, OP_CHECKSIG])
  48.  
  49. # Calculate the signature hash for that transaction
  50. sighash = SignatureHash(txin_scriptPubKey, tx, 0, SIGHASH_ALL)
  51. print(sighash)
  52.  
  53.  
  54. bitcoin.SelectParams('mainnet')
  55.  
  56. h = hashlib.sha256(b'wallet secret key').digest()
  57. seckey = CBitcoinSecret.from_secret_bytes(h)
  58.  
  59. # lx corresponds to little endian hex
  60. txid = lx('010862f9588f760b4a007cc316baa38890d5e102dc8cee923aae396260e13cb1')
  61. vout = 0
  62.  
  63. # Create the txin structure, which includes the outpoint
  64. txin = CMutableTxIn(COutPoint(txid, vout))
  65. print(txin)
  66.  
  67. # Creating Script Pub Key
  68. param = [OP_DUP, OP_HASH160, Hash160(seckey.pub), OP_EQUALVERIFY, OP_CHECKSIG]
  69. txin_scriptPubKey = CScript(param)
  70.  
  71. # Create txout object
  72. txout = CMutableTxOut(0.001*COIN, CBitcoinAddress('1CaJbGDKkk2DzWNAgHs6sgMdMJZ9eXvCs7').to_scriptPubKey())
  73.  
  74. # Create unsigned transaction
  75. tx = CMutableTransaction([txin], [txout])
  76.  
  77. sighash = SignatureHash(txin_scriptPubKey, tx, 0, SIGHASH_ALL)
  78.  
  79. sig = seckey.sign(sighash) + bytes([SIGHASH_ALL])
  80.  
  81. txin.scriptSig = CScript([sig, seckey.pub])
  82.  
  83. VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))
  84.  
  85. print(bitcoin.core.b2x(tx.serialize()))
  86.  
Advertisement
Add Comment
Please, Sign In to add comment