signing a raw transaction with python

1 reply 461 views
0xHashMember
Posts: 21 · Reputation: 201
#1May 30, 2021, 06:42 PM
hey everyone, I've been trying to figure out how to sign a raw transaction using Python, but I keep hitting errors. Here's the transaction I got from Bitcoin Core on testnet: txid: 4a33829b7606d8f3326efdae4580318707c9d68f07b00581209db5308bf491c9 hex: 020000000001017826770251b03004341d1f11f59c2e1aac368cd4f16201e77e2cd38b57abacb30 100000000fdffffff020000000000000000066a04746573748804000000000000160014fa5a153a 8c4d936f990bc54419916c90bb42e92c 024730440220218f29a138304bec8bdc08a74f014a472b5053bcbb1f14f525eb08c8d00884a3022 04c589722a0775fd76227503e8dbf4e4bd51d4dc2745cc5588c23ba7fa2a810f501 21020afccd21b34449bd18b1ee2f5504b5bc4e2faef14a825dc4b6d0e16a79f5e33b00000000 I wanna get the signing part down in Python. So far, I've managed to break it down into: unsigned transaction: 02000000017826770251b03004341d1f11f59c2e1aac368cd4f16201e77e2cd38b57abacb301000 00000fdffffff020000000000000000066a04746573748804000000000000160014fa5a153a8c4d 936f990bc54419916c90bb42e92c00000000 r:218f29a138304bec8bdc08a74f014a472b5053bcbb1f14f525eb08c8d00884a3 s:4c589722a0775fd76227503e8dbf4e4bd51d4dc2745cc5588c23ba7fa2a810f5 sign_all:01 public_key:020afccd21b34449bd18b1ee2f5504b5bc4e2faef14a825dc4b6d0e16a79f5e33b I started off by trying to verify the transaction with this Python code: from fastecdsa import curve, ecdsa, point from hashlib import sha256 txid = '4a33829b7606d8f3326efdae4580318707c9d68f07b00581209db5308bf491c9' r = '218f29a138304bec8bdc08a74f014a472b5053bcbb1f14f525eb08c8d00884a3' s = '4c589722a0775fd76227503e8dbf4e4bd51d4dc2745cc5588c23ba7fa2a810f5' x = '0afccd21b34449bd18b1ee2f5504b5bc4e2faef14a825dc4b6d0e16a79f5e33b' y = '629be62
4 Reply Quote Share
im_apeHero Member
Posts: 629 · Reputation: 3824
#2May 30, 2021, 09:05 PM
The "digest" that is used in signing and subsequently for verifying is not the txid, it is double SHA256 hash of a special serialization of the transaction that depends on the type of the output that was being spent. For example since in this case the output that is being spent is a P2WPKH the steps to compute the digest (ie. sighah) are explained here: https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki
4 Reply Quote Share

Related topics