Signatures
A Transaction Signature serves as strong evidence that the transaction has been authorized by the holder of the private key corresponding to an associated public key. This document provides information about the structure, serialization and pre-image format for these signatures.
Data Structure
The TransactionSignature
class extends the Signature
class, inheriting its basic properties of r
and s
, which are components of the ECDSA (Elliptic Curve Digital Signature Algorithm) signature. Additionally, it introduces a scope
variable which is specific to the context of transaction signing, indicating the parts of the transaction the signature commits to.
The class defines several static readonly properties representing the SIGHASH types for the scope
:
SIGHASH_ALL
: The signature commits to all outputs of the transaction, ensuring none can be changed without invalidating the signature.SIGHASH_NONE
: The signature commits to no outputs, allowing others to add outputs to the transaction.SIGHASH_SINGLE
: The signature commits to exactly one output, the one with the same index as the input the signature is for. This allows for independent adjustment of outputs in multi-output transactions.SIGHASH_ANYONECANPAY
: Modifies the behavior of the other types by only committing to the current input, allowing others to add or remove other inputs.SIGHASH_FORKID
: Currently always enabled in BSV.
Serialization and Formats
The TransactionSignature
class includes methods for serializing and deserializing transaction signatures, useful for referencing them within scripts, among other things.
format()
: This static method prepares the transaction data for signing by creating a preimage according to the specified SIGHASH type. It considers various components like inputs, outputs, the transaction sequence, and lock time. Useful for generating the data that will be signed to produce a transaction signature.fromChecksigFormat()
: Deserializes a script stack element into aTransactionSignature
instance, useful for parsing signatures from raw transaction data.toChecksigFormat()
: Serializes the signature back into a format that can be embedded in a script. This includes converting the ECDSA components (r
ands
) into DER format, followed by thescope
(SIGHASH flags) to indicate which components are signed.
The TransactionSignature
encapsulates the complexity of transaction signing and signature serialization. You can make use of the static .format()
method to compute the signatures you need, enabling a wide variety of applications.
Using the Signature Preimage Formatter
Here's an example of formatting a signature preimage for use in a Transaction:
First, we set up the data we wanted to sign. Then, we created a pre-image, hashed it, and signed it with a private key using ECSA. Then, we created a new TransactionSignature
instance with the ECDSA signature, so that we could convert it into Checksig format. At this point, you could use the transaction signature in a script to help unlock a UTXO, enabling you to unlock and spend the associated Bitcoins or other digital assets.
Last updated