Exploring Bitcoin: Signature Hash Types

bitaps.com
4 min readJul 12, 2018

--

The process of signing transaction input consists of 2 stages. The first stage is the formation of a message that will be signed by a private key, the second stage is the calculation of the signature itself. The message to be signed is a 32 byte hash (double sha256) from the transaction template generated in accordance with the signature type.

This article describes all types of signatures. Shows what part of the transaction structure is covered by this type of signature, and briefly describes how the transaction template for each type of signature is generated. For each type provided real example. All hash pre-image (transaction template) examples provided for non-segwit transaction:

0100000003ae8fe99eac2ced7681fdd2aedd25a83ff88fbed347571a2d7cb54aeb85a883f4010000006a473044022012048b6ac38277642e24e012267cf91c22326c3b447d6b4056698f7c298fb36202201139039bb4090a7cfb63c57ecc60d0ec8b7483bf0461a468743022759dc50124012102317ee6cd0c43381825bfe8bb0a51d46195dc36eca6adc7bb4bd2ead6a0909be0ffffffff5704be9a882060ac36f96e204553e92d37c78f831d84dce32832f4c0b919e594000000006a47304402207601fb44eab2eaf87b7988e672657c763d7ba0337cf19a3ba8fcdee55da483ec022067122f2e4aa523b1a081ad2b7a94a90cca16cd2a5214b31649a2e8aaf5565427012102dec5f30310c48e3ced04237a995469c41182383ee403d904ccb19ef85feaafa9ffffffff5704be9a882060ac36f96e204553e92d37c78f831d84dce32832f4c0b919e594010000006a473044022002f924830a1cf8214da7601e17a74d5e197c3f9b043d2964b4b4e99cfaf38f8402201587208194bc578e17d274ce5d7db253ce48d24ddd4f574377535d437351a74f0121032a934955fbdd4601265c801150f999ba6cda536de2dbfcab7d6912c803211b9bffffffff0563fb7805000000001976a914cd2be655e57086668a5be28c02f5563c06a1b47b88aca3738402000000001976a914ddcb7fedaacc18da645a2d7ffdabbc24880e288a88aca71b9a00000000001976a91486165b6dc00d1ae52685ed594f8262f39ec0150c88ac867f0d03000000001976a91421b95ee973b8966ff6cf039969491801cc42f1a088ace7489601000000001976a914c879015ab911026b277e48231a2bf256c6e9492688ac00000000

Examples for segwit transaction:

020000000001029d4d29133abef262970c53713c3fb1aa20bfa9fe1df4b932baf21a7c30ef4bf000000000171600144c54f5eb3063f89cc3b85a6b53b5f894345f368cfeffffffbdb0697236af2157163e6422dd96b33f65e5b8dea8995cdfff4b15a72b61d5a50000000017160014bce7218c126924b6cb203e0ac465647ec8ccee8dfeffffff0285ac1a000000000017a91460c3cfca3e622480cd67a6b7110c73f1de5aff04877f1a0900000000001976a914956b62c9f2dcf00e93bc6422ec9eea427f32353788ac02483045022100f5ac21aebd09acf08495684b3632b2a9a54085e2f46ff2d74822b2c9568cb457022047936507ff77d032a221f0672cfb279bba525f1a71b4eca0563f3422d10e3dc50121034e08b96022a79da60fa9a90f5c5c0a4ae554cb25851cddc00c6a7d3b281b3cd5024830450221009ffda404de89bfcb785de58ad957b7638ff30bb880af99cf4fd196e9e60034aa0220694a0fc09b8e8927f676e4ad7d8043999e8eace3ae17ee3f1007fe977250b618012103ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360e8b1c0800

All image examples provided for this transaction:

SIGHASH_ALL signature type

This is the most commonly used type of signature, signs all inputs and outputs, protecting all elements from modification. Signature scripts are not subject to signature and are always deleted from all inputs before forming a signature hash pre-image template before the signature process begins.

Example for non-segwit transaction, signs input with index 0:

Public key:02317ee6cd0c43381825bfe8bb0a51d46195dc36eca6adc7bb4bd2ead6a0909be0Pre-image:

0000000100000000ac882649e9c656f22b1a23487e276b0211b95a0179c814a9761900000000019648e7ac88a0f142cc011849699903cff66f96b873e95eb92114a9761900000000030d7f86ac880c15c09ef362824f59ed8526e51a0dc06d5b168614a9761900000000009a1ba7ac888a280e8824bcabfd7f2d5a64da18ccaaed7fcbdd14a9761900000000028473a3ac887bb4a1063c56f5028ce25b8a668670e555e62bcd14a97619000000000578fb6305ffffffff000000000194e519b9c0f43228e3dc841d838fc7372de95345206ef936ac6020889abe0457ffffffff000000000094e519b9c0f43228e3dc841d838fc7372de95345206ef936ac6020889abe0457ffffffffe09b90a0d6ead24bbbc7ada6ec36dc9561d4510abbe8bf251838430ccde67e31022100000001f483a885eb4ab57c2d1a5747d3be8ff83fa825ddaed2fd8176ed2cac9ee98fae0300000001
Sighash:d9c85312f47d05449c0f05fb09d726a309d75fd598ce927fbc565f0750412e14

Example for segwit transaction, signs input with index 0:

Public key: 03ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360eAmount: 1142938Pre-image:020000000079ee2e593f194687f6a899d8c691d4d27931c82d5824b89c9c348d415e0880c992651ac89a97aecd0811c1761915a8e2c8f5153d1bdd994a789e6bd86ab7179d4d29133abef262970c53713c3fb1aa20bfa9fe1df4b932baf21a7c30ef4bf00000000003ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360e9a70110000000000feffffffee526ec083b5121f7dd631c5b6ea8bb6e9ac633f9741497403b1615a5f94163b8b1c080001000000Sighash:9b3eda3e7a1c8bb7673d08084389db4fbc30a9bdaf0cd93684405edfd97e9715

SIGHASH_SINGLE signature type

All output excluded from the pre-image template, except one output whose index corresponds to the index of the signing input. All inputs include to pre-image but sequence field for non signing inputs are excluded and can be updated.

If signing input index matches the non-existent output, an exception occurs in which the hash of the template returns the value 1. Signing input with hash from pre-image equally 1 will lead to compromise private key.

Example for non-segwit transaction, signs input with index 1:

Public key:02317ee6cd0c43381825bfe8bb0a51d46195dc36eca6adc7bb4bd2ead6a0909be0Pre-image:

0000000300000000ac888a280e8824bcabfd7f2d5a64da18ccaaed7fcbdd14a9761900000000028473a300ffffffffffffffff0200000000000000000194e519b9c0f43228e3dc841d838fc7372de95345206ef936ac6020889abe0457ffffffffe09b90a0d6ead24bbbc7ada6ec36dc9561d4510abbe8bf251838430ccde67e3102210000000094e519b9c0f43228e3dc841d838fc7372de95345206ef936ac6020889abe0457000000000000000001f483a885eb4ab57c2d1a5747d3be8ff83fa825ddaed2fd8176ed2cac9ee98fae0300000001
Sighash:8a3810a1994d4f956a54e8cdd600c5cbf9b4c278f39552870bcaba2f380a49d2

Example for segwit transaction, signs input with index 0:

Public key:03ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360eAmount: 1142938Pre-image:020000000079ee2e593f194687f6a899d8c691d4d27931c82d5824b89c9c348d415e088000000000000000000000000000000000000000000000000000000000000000009d4d29133abef262970c53713c3fb1aa20bfa9fe1df4b932baf21a7c30ef4bf00000000003ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360e9a70110000000000feffffffdbc1cd31f4cae57f414f7f9c8b23f7fd3e07fbdb0611dbc6680a2e504748f8b98b1c080003000000Sighash:0b4435260ec8d8ad304c6678727a13028118bc8e14990da18dd9202a9f0627c0

SIGHASH_NONE signature type

Signs of all inputs, all outputs are excluded from the pre-image template. Sequence field for non signing inputs are excluded and can be updated. Any output can be changed until another transaction input does not lock outputs with a different type of signature. The use of this type of signature without the combination with another type of signatures that impose a lock on the outputs is unsafe.

Example for non-segwit transaction, signs input with index 0:

Public key:02317ee6cd0c43381825bfe8bb0a51d46195dc36eca6adc7bb4bd2ead6a0909be0Pre-image:00000002000000000000000000000000000194e519b9c0f43228e3dc841d838fc7372de95345206ef936ac6020889abe045700000000000000000094e519b9c0f43228e3dc841d838fc7372de95345206ef936ac6020889abe0457ffffffffe09b90a0d6ead24bbbc7ada6ec36dc9561d4510abbe8bf251838430ccde67e31022100000001f483a885eb4ab57c2d1a5747d3be8ff83fa825ddaed2fd8176ed2cac9ee98fae0300000001Sighash:e18470175dff934e34fb767ec5198aed37d2d1953ad8418fec381961f467fa38

Example for segwit transaction, signs input with index 0:

Public key:03ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360eAmount: 1142938Pre-image:020000000079ee2e593f194687f6a899d8c691d4d27931c82d5824b89c9c348d415e088000000000000000000000000000000000000000000000000000000000000000009d4d29133abef262970c53713c3fb1aa20bfa9fe1df4b932baf21a7c30ef4bf00000000003ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360e9a70110000000000feffffff00000000000000000000000000000000000000000000000000000000000000008b1c080002000000Sighash:d17e502f7e656adc4f17ecaf13bd2e53bdf9a387552b5de3b2b28f6504b95183

SIGHASH_ALL + SIGHASH_ANYONECANPAY flag

Signs all of the outputs but only this one input. Rest inputs excluded. This type allows anyone to add or remove other inputs, so anyone can add funds to the transaction but cannot change destination.

Example for non-segwit transaction, signs input with index 0:

Public key:02317ee6cd0c43381825bfe8bb0a51d46195dc36eca6adc7bb4bd2ead6a0909be0Pre-image:0000008100000000ac882649e9c656f22b1a23487e276b0211b95a0179c814a9761900000000019648e7ac88a0f142cc011849699903cff66f96b873e95eb92114a9761900000000030d7f86ac880c15c09ef362824f59ed8526e51a0dc06d5b168614a9761900000000009a1ba7ac888a280e8824bcabfd7f2d5a64da18ccaaed7fcbdd14a9761900000000028473a3ac887bb4a1063c56f5028ce25b8a668670e555e62bcd14a97619000000000578fb6305ffffffffe09b90a0d6ead24bbbc7ada6ec36dc9561d4510abbe8bf251838430ccde67e3102210000000094e519b9c0f43228e3dc841d838fc7372de95345206ef936ac6020889abe04570100000001Sighash:dc2f3eb93b9bd2a8ce4f1a6f9c834cef1fedeb174317a790d7a07275965b91be

Example for segwit transaction, signs input with index 0:

Public key:03ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360eAmount: 1142938Pre-image:02000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009d4d29133abef262970c53713c3fb1aa20bfa9fe1df4b932baf21a7c30ef4bf00000000003ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360e9a70110000000000feffffffee526ec083b5121f7dd631c5b6ea8bb6e9ac633f9741497403b1615a5f94163b8b1c080081000000Sighash:1cf67c0342fe34ff6b96ba11912586dcbcf391381149d2bf52aa5c22251962db

SIGHASH_SINGLE + SIGHASH_ANYONECANPAY flag

Signs this one input and its corresponding output. Rest input and outputs excluded. Allows anyone to add or remove other inputs and outputs. Signer just locks his part of the transaction.

Example for non-segwit transaction, signs input with index 0:

Public key:02317ee6cd0c43381825bfe8bb0a51d46195dc36eca6adc7bb4bd2ead6a0909be0Pre-image:0000008300000000ac888a280e8824bcabfd7f2d5a64da18ccaaed7fcbdd14a9761900000000028473a300ffffffffffffffff02ffffffffe09b90a0d6ead24bbbc7ada6ec36dc9561d4510abbe8bf251838430ccde67e3102210000000094e519b9c0f43228e3dc841d838fc7372de95345206ef936ac6020889abe04570100000001Sighash:eb1c98cd221f760cffbca140d1a8ed4f5a6b111ddbfbfc8048e313f32cecb567

Example for segwit transaction, signs input with index 0:

Public key:03ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360eAmount: 1142938Pre-image:02000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009d4d29133abef262970c53713c3fb1aa20bfa9fe1df4b932baf21a7c30ef4bf00000000003ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360e9a70110000000000feffffffdbc1cd31f4cae57f414f7f9c8b23f7fd3e07fbdb0611dbc6680a2e504748f8b98b1c080083000000Sighash:f084a27f344d451e7a96ef2492db098b3202fed9c535925d317e3b1ee3fc81d7

SIGHASH_NONE + SIGHASH_ANYONECANPAY flag

Signs just one input all other inputs/outputs excluded.

Example for non-segwit transaction, signs input with index 0:

Public key:02317ee6cd0c43381825bfe8bb0a51d46195dc36eca6adc7bb4bd2ead6a0909be0Pre-image:000000820000000000ffffffffe09b90a0d6ead24bbbc7ada6ec36dc9561d4510abbe8bf251838430ccde67e3102210000000094e519b9c0f43228e3dc841d838fc7372de95345206ef936ac6020889abe04570100000001Sighash:77ddf4cfb29f334f7475115069aa046441d3ba44eac65df72b4492912faee3fd

Example for segwit transaction, signs input with index 0:

Public key:03ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360eAmount: 1142938Pre-image:02000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009d4d29133abef262970c53713c3fb1aa20bfa9fe1df4b932baf21a7c30ef4bf00000000003ca939e4b3f88cc8702212b31dc8ded683329f220c71112a1edb57ff27c68360e9a70110000000000feffffff00000000000000000000000000000000000000000000000000000000000000008b1c080082000000Sighash:3f7a02f92b498169b6d506be620901eb7a392487ecf71ac4096a2bc46c30eb9a

--

--