Author

Topic: how to sign a rawtransaction (Read 128 times)

jr. member
Activity: 71
Merit: 4
April 25, 2024, 11:31:43 AM
#3
Code:
from fastecdsa import curve, ecdsa, point
from hashlib import sha256

txid = '4a33829b7606d8f3326efdae4580318707c9d68f07b00581209db5308bf491c9'
r = int('218f29a138304bec8bdc08a74f014a472b5053bcbb1f14f525eb08c8d00884a3', 16)
s = int('4c589722a0775fd76227503e8dbf4e4bd51d4dc2745cc5588c23ba7fa2a810f5', 16)
x = int('0afccd21b34449bd18b1ee2f5504b5bc4e2faef14a825dc4b6d0e16a79f5e33b', 16)
y = int('629be62cbde56f8842fa8fd4a33d2656f77ccfc0ad51d9659a7b34fc6c99d840', 16)

Qa = point.Point(x=x, y=y, curve=curve.secp256k1)
hash_bytes = bytes.fromhex(txid)
valid = ecdsa.verify((r, s), hash_bytes, Qa, curve=curve.secp256k1, hashfunc=sha256)
print(valid)

Kindly try this code. But check your transaction hash, public key coordinates, and signature values properly. If everything is good the code will give you true as a result.
legendary
Activity: 3472
Merit: 10611
April 25, 2024, 04:41:43 AM
#2
valid = ecdsa.verify((int(r,16),int(s,16)),bytes.fromhex(txid),Qa,curve=curve.secp256k1,hashfunc=sha256)
print(valid)
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
newbie
Activity: 4
Merit: 1
April 25, 2024, 03:32:57 AM
#1
hello,
I tried a lot of ways to sign a rawtransaction by python,but all way is error.
this is a transaction sended by Bitcoin Core in testnet:
txid: 4a33829b7606d8f3326efdae4580318707c9d68f07b00581209db5308bf491c9
hex: 020000000001017826770251b03004341d1f11f59c2e1aac368cd4f16201e77e2cd38b57abacb30 100000000fdffffff020000000000000000066a04746573748804000000000000160014fa5a153a 8c4d936f990bc54419916c90bb42e92c  024730440220218f29a138304bec8bdc08a74f014a472b5053bcbb1f14f525eb08c8d00884a3022 04c589722a0775fd76227503e8dbf4e4bd51d4dc2745cc5588c23ba7fa2a810f501 21020afccd21b34449bd18b1ee2f5504b5bc4e2faef14a825dc4b6d0e16a79f5e33b00000000

I want to implement transaction signing in python.Now, I can separate the parts:
unsigned transaction: 02000000017826770251b03004341d1f11f59c2e1aac368cd4f16201e77e2cd38b57abacb301000 00000fdffffff020000000000000000066a04746573748804000000000000160014fa5a153a8c4d 936f990bc54419916c90bb42e92c00000000
r:218f29a138304bec8bdc08a74f014a472b5053bcbb1f14f525eb08c8d00884a3
s:4c589722a0775fd76227503e8dbf4e4bd51d4dc2745cc5588c23ba7fa2a810f5
sign_all:01
public_key:020afccd21b34449bd18b1ee2f5504b5bc4e2faef14a825dc4b6d0e16a79f5e33b

I tried to start by verifing the transaction.This is my python code:
from fastecdsa import curve, ecdsa,point
from hashlib import sha256
txid = '4a33829b7606d8f3326efdae4580318707c9d68f07b00581209db5308bf491c9'
r = '218f29a138304bec8bdc08a74f014a472b5053bcbb1f14f525eb08c8d00884a3'
s = '4c589722a0775fd76227503e8dbf4e4bd51d4dc2745cc5588c23ba7fa2a810f5'
x = '0afccd21b34449bd18b1ee2f5504b5bc4e2faef14a825dc4b6d0e16a79f5e33b'
y = '629be62cbde56f8842fa8fd4a33d2656f77ccfc0ad51d9659a7b34fc6c99d840'
Qa = point.Point(x=int(x,16),y=int(y,16),curve=curve.secp256k1)
valid = ecdsa.verify((int(r,16),int(s,16)),bytes.fromhex(txid),Qa,curve=curve.secp256k1,hashfunc=sha256)
print(valid)

The result is False. I don't know where is wrong.
Jump to: