How can I extract the SIGHASH flags from the signature?

本文详细解析了比特币交易中的签名过程及SIGHASH类型的作用。解释了如何通过OP_CHECKSIG运算符来选择性地签署交易的部分内容,并介绍了SIGHASH类型的序列化方式及其在保护交易免受篡改方面的重要性。

OP_CHECKSIG extracts a non-stack argument from each signature it evaluates, allowing the signer to decide which parts of the transaction to sign. Since the signature protects those parts of the transaction from modification, this lets signers selectively choose to let other people modify their transactions.


Answer:

The SIGHASH type is serialized as a single byte and then simply appended to the DER-encoded signature.


Example of a typical P2PKH scriptSig:

304402206e3729f021476102a06ea453cea0a26cb9c096cca641efc4229c1111ed3a96fd022037dce1456a93f53d3e868c789b1b750a48a4c1110cd5b7049779b5f4f3c8b62001 03ff1104b46b2141df1948dd0df2223720a3a471ec57404cace47063843a699a0f
The second element above is the public key, serialized as per  SEC 1's Elliptic-Curve-Point-to-Octet-String Conversion . The first element is the DER encoded signature, with a 0x01 appended to specify it's a SIGHASH_ALL (whose values you've already found). To break the signature string down further, if you're interested:

30 <- a sequence
44 <- of length 0x44
  02 <- an integer
  20 <- of length 0x20
    6e3729f021476102a06ea453cea0a26cb9c096cca641efc4229c1111ed3a96fd <- r
  02 <- an integer
  20 <- of length 0x20
    37dce1456a93f53d3e868c789b1b750a48a4c1110cd5b7049779b5f4f3c8b620 <- s
01 <- SIGHASH type (not part of the DER encoding)
The SIGHASH type is also temporarily appended to the transaction before it is signed so that the signature precludes its modification, see  this answer  for more details.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值