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.

FlagValue with SIGHASH_FORKIDValue without SIGHASH_FORKIDDescription
SIGHASH_ALL0x41 / 0100 00010x01 / 0000 0001Sign all inputs and outputs
SIGHASH_NONE0x42 / 0100 00100x02 / 0000 0010Sign all inputs and no outputs
SIGHASH_SINGLE0x43 / 0100 00110x03 / 0000 0011Sign all inputs and single output
`SIGHASH_ALLANYONECANPAY`0xC1 / 1100 00010x81 / 1000 0001Sign single input and all outputs
`SIGHASH_NONEANYONECANPAY`0xC2 / 1100 00100x82 / 1000 0010Sign single input and no outputs
`SIGHASH_SINGLEANYONECANPAY`0xC3 / 1100 00110x83 / 1000 0011Sign 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

Types

Sighash preimage

Sighash

Sighash flag

Signature

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

Specs

preimage() :: binary()

Sighash preimage

Specs

sighash() :: <<_::256>>

Sighash

Specs

sighash_flag() :: integer()

Sighash flag

Specs

signature() :: binary()

Signature

DER-encoded signature with the sighash flag appended.

Link to this section Functions

Link to this function

preimage(tx, vin, txout, sighash_type)

View Source

Specs

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.

Link to this function

sighash(tx, vin, txout, sighash_type \\ 65)

View Source

Specs

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.

Link to this macro

sighash_all?(sighash_flag)

View Source (macro)
Link to this macro

sighash_anyone_can_pay?(sighash_flag)

View Source (macro)
Link to this function

sighash_flag(sighash_type \\ :default)

View Source

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
Link to this macro

sighash_forkid?(sighash_flag)

View Source (macro)
Link to this macro

sighash_none?(sighash_flag)

View Source (macro)
Link to this macro

sighash_single?(sighash_flag)

View Source (macro)
Link to this function

sign(tx, vin, txout, priv_key, opts \\ [])

View Source

Specs

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.

Link to this function

verify(signature, tx, vin, txout, pubkey)

View Source

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.