def VerifyMessage(address, message, sig): sig = base64.b64decode(sig) hash = message.GetHash() pubkey = CPubKey.recover_compact(hash, sig) return str(P2PKHBitcoinAddress.from_pubkey(pubkey)) == str(address)
def __init__(self, key: CBitcoinSecret, address_type: str = "p2pkh"): self.key_index = None self.type = address_type self.key = key if address_type == "p2pkh": self.address = P2PKHBitcoinAddress.from_pubkey(self.key.pub) elif address_type == "p2sh": self.address = P2SHBitcoinAddress.from_redeemScript( CScript([self.key.pub, OP_CHECKSIG])) elif address_type == "p2wpkh": key_hash = Hash160(self.key.pub) script_pub_key = CScript([OP_0, key_hash]) self.address = P2WPKHBitcoinAddress.from_scriptPubKey( script_pub_key) elif address_type == "p2wsh": self.witness_program = CScript([self.key.pub, OP_CHECKSIG]) script_hash = hashlib.sha256(self.witness_program).digest() script_pub_key = CScript([OP_0, script_hash]) self.address = P2WSHBitcoinAddress.from_scriptPubKey( script_pub_key) else: raise UnsupportedAddressTypeError() self.value = 0 self.txid = None self.vout = None
def print_verbose(signature, key, msg): secret = CBitcoinSecret(key) address = P2PKHBitcoinAddress.from_pubkey(secret.pub) message = BitcoinMessage(msg) print('Address: %s' % address) print('Message: %s' % msg) print('Signature: %s' % signature) print('Verified: %s' % VerifyMessage(address, message, signature)) print('\nTo verify using bitcoin core:') print('\n`bitcoin-cli verifymessage %s \'%s\' \'%s\'`\n' % (address, signature.decode('ascii'), msg))
def test_from_invalid_pubkeys(self): """Create P2PKHBitcoinAddress's from invalid pubkeys""" # first test with accept_invalid=True def T(invalid_pubkey, expected_str_addr): addr = P2PKHBitcoinAddress.from_pubkey(invalid_pubkey, accept_invalid=True) self.assertEqual(str(addr), expected_str_addr) T(x(''), '1HT7xU2Ngenf7D4yocz2SAcnNLW7rK8d4E') T( x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c72' ), '1L9V4NXbNtZsLjrD3nkU7gtEYLWRBWXLiZ') # With accept_invalid=False we should get CBitcoinAddressError's with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_pubkey(x('')) with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_pubkey( x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c72' )) with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_pubkey( CPubKey( x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c72' )))
# There's also a corresponding x() convenience function that takes big-endian # hex and converts it to bytes. txid = lx('7e195aa3de827814f172c362fcf838d92ba10e3f9fdd9c3ecaf79522b311b22d') vout = 0 # Create the txin structure, which includes the outpoint. The scriptSig # defaults to being empty. txin = CMutableTxIn(COutPoint(txid, vout)) # We also need the scriptPubKey of the output we're spending because # SignatureHash() replaces the transaction scriptSig's with it. # # Here we'll create that scriptPubKey from scratch using the pubkey that # corresponds to the secret key we generated above. txin_scriptPubKey = \ P2PKHBitcoinAddress.from_pubkey(seckey.pub).to_scriptPubKey() # Create the txout. This time we create the scriptPubKey from a Bitcoin # address. txout = CMutableTxOut( 0.001 * COIN, CBitcoinAddress('1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8').to_scriptPubKey()) # Create the unsigned transaction. tx = CMutableTransaction([txin], [txout]) # Calculate the signature hash for that transaction. sighash = SignatureHash(txin_scriptPubKey, tx, 0, SIGHASH_ALL) # Now sign it. We have to append the type of signature we want to the end, in # this case the usual SIGHASH_ALL.
def T(invalid_pubkey: bytes, expected_str_addr: str) -> None: addr = P2PKHBitcoinAddress.from_pubkey(invalid_pubkey, accept_invalid=True) self.assertEqual(str(addr), expected_str_addr)
def T(pubkey: bytes, expected_str_addr: str) -> None: addr = P2PKHBitcoinAddress.from_pubkey(pubkey) self.assertEqual(str(addr), expected_str_addr)
def T(pubkey, expected_str_addr): addr = P2PKHBitcoinAddress.from_pubkey(pubkey) self.assertEqual(str(addr), expected_str_addr)