BSV.Sig (BSV v2.1.0) View Source
Module for signing and verifying Bitcoin transactions.
Signing a transaction in Bitcoin first involves computing a transaction
preimage. A BSV.Sig.sighash_flag/0 is used to indicate which parts of the
transaction are included in the preimage.
| Flag | Value with SIGHASH_FORKID | Value without SIGHASH_FORKID | Description |
|---|---|---|---|
SIGHASH_ALL | 0x41 / 0100 0001 | 0x01 / 0000 0001 | Sign all inputs and outputs |
SIGHASH_NONE | 0x42 / 0100 0010 | 0x02 / 0000 0010 | Sign all inputs and no outputs |
SIGHASH_SINGLE | 0x43 / 0100 0011 | 0x03 / 0000 0011 | Sign all inputs and single output |
| `SIGHASH_ALL | ANYONECANPAY` | 0xC1 / 1100 0001 | 0x81 / 1000 0001 | Sign single input and all outputs |
| `SIGHASH_NONE | ANYONECANPAY` | 0xC2 / 1100 0010 | 0x82 / 1000 0010 | Sign single input and no outputs |
| `SIGHASH_SINGLE | ANYONECANPAY` | 0xC3 / 1100 0011 | 0x83 / 1000 0011 | Sign single input and single output |
Once the preimage is constructed, it is double hashed using the SHA-256
algorithm and then used to calculate the ECDSA signature. The resulting
DER-encoded signature is appended with the sighash flag.
Link to this section Summary
Functions
Returns the preimage for the given transaction. Must also specify the
BSV.TxIn.vin/0 of the context input, the BSV.TxOut.t/0 that is being
spent, and the BSV.Sig.sighash_flag/0.
Computes a double SHA256 hash of the preimage of the given transaction. Must
also specify the BSV.TxIn.vin/0 of the context input, the BSV.TxOut.t/0
that is being spent, and the BSV.Sig.sighash_flag/0.
Returns the BSV.Sig.sighash_flag/0 of the given sighash type.
Signs the sighash of the given transaction using the given PrivKey. Must also
specify the BSV.TxIn.vin/0 of the context input, the BSV.TxOut.t/0
that is being spent, and the BSV.Sig.sighash_flag/0.
Verifies the signature against the sighash of the given transaction using the
specified PubKey. Must also specify the BSV.TxIn.vin/0 of the context
input, the BSV.TxOut.t/0 that is being spent.
Link to this section Types
Link to this section Functions
Specs
preimage(BSV.Tx.t(), BSV.TxIn.vin(), BSV.TxOut.t(), sighash_flag()) :: preimage()
Returns the preimage for the given transaction. Must also specify the
BSV.TxIn.vin/0 of the context input, the BSV.TxOut.t/0 that is being
spent, and the BSV.Sig.sighash_flag/0.
BSV transactions require the SIGHASH_FORKID flag which results in a preimage
according the algorithm proposed in BIP-143.
The legacy preimage algorithm is supported by this library.
Specs
sighash(BSV.Tx.t(), BSV.TxIn.vin(), BSV.TxOut.t(), sighash_flag()) :: sighash()
Computes a double SHA256 hash of the preimage of the given transaction. Must
also specify the BSV.TxIn.vin/0 of the context input, the BSV.TxOut.t/0
that is being spent, and the BSV.Sig.sighash_flag/0.
Specs
sighash_flag(atom()) :: sighash_flag()
Returns the BSV.Sig.sighash_flag/0 of the given sighash type.
Examples
iex> Sig.sighash_flag(:default)
0x41
Specs
sign(BSV.Tx.t(), BSV.TxIn.vin(), BSV.TxOut.t(), BSV.PrivKey.t(), keyword()) :: signature()
Signs the sighash of the given transaction using the given PrivKey. Must also
specify the BSV.TxIn.vin/0 of the context input, the BSV.TxOut.t/0
that is being spent, and the BSV.Sig.sighash_flag/0.
The returned DER-encoded signature is appended with the sighash flag.
Specs
verify(signature(), BSV.Tx.t(), BSV.TxIn.vin(), BSV.TxOut.t(), BSV.PubKey.t()) :: boolean() | :error
Verifies the signature against the sighash of the given transaction using the
specified PubKey. Must also specify the BSV.TxIn.vin/0 of the context
input, the BSV.TxOut.t/0 that is being spent.