6

This page describes SIGHASH flags:

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.

But how can I extract the SIGHASH flags from the signature?

11

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.

2
  • How does it work for ANYONE_CAN_PAY? Is it 0x81, similarly, for the others is it 0x81, 0x83? – Jus12 Jan 6 '18 at 14:57
  • @Jus12 Yes, SIGHASH_ANYONECANPAY is bitwise ANDed (same as added in this case) to one of the other three SIGHASH types. – Christopher Gurnee Jan 11 '18 at 1:03
3

This struct appears to describe the SIGHASH flags:

/** Signature hash types/flags */
enum
{
    SIGHASH_ALL = 1,
    SIGHASH_NONE = 2,
    SIGHASH_SINGLE = 3,
    SIGHASH_ANYONECANPAY = 0x80,
};

and it's encoded as the last byte. Note that SIGHASH_ANYONECANPAY can appear in conjunction with any of the three previous flags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.