Signature Hash and signature hash types
Before we get deep into signature hashing, its only good that we speak a little bit about what digital signatures are.
In simple terms digital signatures are techniques that are used to validate authenticity and integrity of a message. It's the digital equivalent of a handwritten signature or a stamped seal, but digital signature offers much more security than others.
Digital signatures can provide evidence of origin, identity and status of electronic documents, transactions or digital messages. Signers can also use them to acknowledge informed consent.
In many countries including the United States, digital signatures are considered legally binding in the same way as traditional handwritten signatures.
In the case of Bitcoin, digital signatures are applied to transactions themselves. The signature implies a commitment by the signer to specific transaction data. This signature applies to the whole transaction (inputs, outputs, and other transaction fields). The signature can also commit to only a subset of data in a transaction and this is where sighash flags comes in, with each signature signing (committing) different parts of the transaction.
Signature hashing is one of the core elements of the digital system. This process involves transforming data of any size into a fixed-size output.
Sighash, short for the signature hash, is a mechanism used in the bitcoin protocol to ensure the integrity of transactions. Sighash is an important aspect of the Bitcoin protocol, and it plays a crucial role in ensuring the security and reliability of the network.
The purpose of sighash is to ensure that a transaction can only be modified by the person who created it. How? By including a signature, or hash, of the transaction in the input of the transaction. it is good to note that this signature is created using the private key of the person who created the transaction and is then included in the transaction's input.
Sighash also plays an important role in ensuring the security of the Bitcoin protocol by preventing "double spending"
Double spending occurs when a person attempts to spend the same Bitcoin twice. This can happen if a person creates a transaction and then modifies it before it is included in the blockchain.
By including a signature of the transaction, sighash ensures that any changes to the transaction would result in an invalid signature, preventing the transaction from being accepted by the network and thus preventing double-spending.
Another application where we find the importance of sighash is in the implementation of P2SH (Pay-to-Script-Hash)
In addition to the basic sighash mechanism, there is also the concept of sighash flags. A signature hash (sighash) flag is a small part of each input in a transaction that determines which parts of the transaction become immutable once a signature has been added to the transaction. Sighash flag provides additional options for signing transactions; for example, these flags allow users to specify how the transaction should be hashed, thus providing a higher level of flexibility and control over the transaction. It is good to point out that every signature has a SIGHASH flag, and the flag can be different from input to input. A transaction with three signed inputs may have three signatures with different sighash flags, each signature signing (committing) different parts of the signature.
There are three different sighash flags available in the Bitcoin protocol: ALL, NONE, SINGLE; and a modifier flag: ANYONECANPAY, which can be combined with each of the flags. Each of them has different properties, and use cases, and the choice of the flag used can have a big impact on the security and flexibility of transactions.
1. SIGHASH_ALL: This is the default option. Almost all transactions use the sighash flag SIGHASH_ALL, Here, the entire transaction is hashed, meaning that each input's signature is only valid if all other inputs and outputs remain unchanged. This provides the highest level of security as it ensures that all inputs and outputs are locked and cannot be modified. This differs from other sighash flags, which allows for an input signature to remain valid even if other inputs or outputs are altered.
- SIGHASH_NONE: This flag allows for the highest level of flexibility, but also gives the lowest level of security. Sighash_none signs all inputs and no outputs, so that output can be changed without invalidating the signature. However, if any inputs are changed, then the signature becomes invalidated.
- SIGHASH_SINGLE: This flag allows for partial signing of the transaction, where only one output is locked, and the others can be modified. The only output signed is the one corresponding to the input being signed i.e, the output with the same output index number as the input being signed. This ensures that nobody can change a particular signer's part of the transaction but allows other signers to change their part of the transaction.
SIGHASH_ANYONECANPAY:
There is one major difference between ANYONECANPAY flag and the other three flags which is the fact that this flag ensures that only the input at hand is signed, while the other three sighash flags ensure that all inputs are signed. SIGHASH_ANYONECANPAY
SIGHASH_ALL|SIGHASH_ANYONECANPAY: For this, all the outputs are signed along side this only one input. It also gives one the ability to add or remove other inputs so that anyone can contribute additional duffs, but they cannot change how many duffs are sent nor where they go.
SIGHASH_NONE|SIGHASH_ANYONECANPAY: For this case, only one input is signed, giving anyone the ability to add or remove other i
nputs or outputs.SIGHASH_SINGLE|SIGHASH_ANYONECANPAY: Signs this one input and its corresponding output. It also allows anyone to add or remove other inputs.
The choice of sighash flag is important as it determines the level of flexibility and security of a transaction. SIGHASH_ALL provides the highest level of security as it ensures that all inputs and outputs are locked and cannot be modified. SIGHASH_NONE, on the other hand, provides the highest level of flexibility, but also the lowest level of security, as it allows for the output to be modified. SIGHASH_SINGLE provides a balance between security and flexibility.
The good thing is that for every transaction, all inputs are signed, and because each input is signed, a transaction with multiple inputs can have multiple signature hash types signing different parts of the transaction.
For example, a single-input transaction signed with 'NONE' could have its output changed by the miner who adds it to the blockchain. On the other hand, if a two-input transaction has one input signed with 'NONE' and one input signed with 'ALL', the 'ALL' signer can choose where to spend the duffs without consulting the 'NONE' signer---but nobody else can modify the transaction.
And that's all for now, but just before you go, as a software developer, what if I told you there is a platform that offers you the opportunity to learn even more about sighash, bitcoin development, and lightning development, and you get paid while doing that?
I guess you haven't heard about Qala.
Qala is a program designed to train the next generation of African bitcoin and lightning developers. It goes beyond surface learning and takes you neck deep into the core of bitcoin and lightning development. Qala is a project-based learning program. As a participant, you get to work on diverse projects like open source, personal projects, etc.
Go now to qala.dev and apply to begin your journey in this world of freedom money.
until next time/article, Obrigado.
References:
https://wiki.bitcoinsv.io/index.php/SIGHASH_flags
https://dashcore.readme.io/docs/core-guide-transactions-signature-hash-types