When trying to implement OP_CHECKSIG
, I am unable to figure out how to do the actual signature verification in python (python3)
I obtained the signature, public key, and hashed message digest of a transaction, and now I want to verify the signature.
message = '692678553d1b85ccf87d4d4443095f276cdf600f2bb7dd44f6effbd7458fd4c2'
pubkey = '042e930f39ba62c6534ee98ed20ca98959d34aa9e057cda01cfd422c6bab3667b76426529382c23f42b9b08d7832d4fee1d6b437a8526e59667ce9c4e9dcebcabb'
signature = '30450221009908144ca6539e09512b9295c8a27050d478fbb96f8addbc3d075544dc41328702201aa528be2b907d316d2da068dd9eb1e23243d97e444d59290d2fddf25269ee0e'
Since python-ecdsa
requires 64 byte input for creating the verifying key, I removed the first byte myself.
pubkey = pubkey[2:]
I also extracted r
and s
from the DER encoded signature, and concatenated these to create a 64 byte signature as python-ecdsa
requires.
r = '9908144ca6539e09512b9295c8a27050d478fbb96f8addbc3d075544dc413287'
s = '1aa528be2b907d316d2da068dd9eb1e23243d97e444d59290d2fddf25269ee0e'
sig = r + s
However when I try to verify the message, I get a BadSignatureError
signifying that the verification failed.
from ecdsa import VerifyingKey, SECP256k1
vk = VerifyingKey.from_string(bytes.fromhex(pubkey), curve=SECP256k1)
vk.verify(bytes.fromhex(sig), bytes.fromhex(message))
I am looking for a way to verify signatures that I have extracted from bitcoin transactions.
Thank you.