Bitcoin Forum
May 04, 2024, 09:07:55 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: how to sign a rawtransaction  (Read 97 times)
myfy (OP)
Newbie
*
Offline Offline

Activity: 4
Merit: 1


View Profile
April 25, 2024, 08: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.
1714856875
Hero Member
*
Offline Offline

Posts: 1714856875

View Profile Personal Message (Offline)

Ignore
1714856875
Reply with quote  #2

1714856875
Report to moderator
1714856875
Hero Member
*
Offline Offline

Posts: 1714856875

View Profile Personal Message (Offline)

Ignore
1714856875
Reply with quote  #2

1714856875
Report to moderator
It is a common myth that Bitcoin is ruled by a majority of miners. This is not true. Bitcoin miners "vote" on the ordering of transactions, but that's all they do. They can't vote to change the network rules.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
pooya87
Legendary
*
Offline Offline

Activity: 3444
Merit: 10546



View Profile
April 25, 2024, 09: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

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!