bitcoin-elixir v0.0.1 Bitcoin.Tx

Functions associated with a Bitcoin transaction.

Transaction structure is defined in Bitcoin.Protocol.Messages.Tx

Summary

Functions

Returns the fee that given transaction offers (in satoshis)

Calculates hash of the transaction

Calculate hash for signing. sighash_type is provided as a byte value

Sum of all previous outputs values (in satoshis)

Sum of all transaction outputs (in satoshis)

Validate transaction. Currently it checks if

Types

hash()
hash() :: Bitcoin.t_hash

Functions

fee(tx, opts \\ %{})
fee(Bitcoin.Protocol.Messages.Tx.t, map) :: integer

Returns the fee that given transaction offers (in satoshis).

Fee equals sum of all outputs in sum of all inputs.

Calculates hash of the transaction.

sighash(tx, input_number, sub_script, sighash_type)

Calculate hash for signing. sighash_type is provided as a byte value.

Do note that this is “almost sighash”. It’s missing the last sha256 hash, that’s because :crypto.verify/5 function does one more sha256 before checking signature.

total_input_value(tx, opts \\ %{})
total_input_value(Bitcoin.Protocol.Messages.Tx.t, map) :: integer

Sum of all previous outputs values (in satoshis)

total_output_value(tx, opts \\ %{})
total_output_value(Bitcoin.Protocol.Messages.Tx.t, map) :: integer

Sum of all transaction outputs (in satoshis)

validate(tx, opts \\ %{})
validate(Bitcoin.Protocol.Messages.Tx.t, map) ::
  :ok |
  {:error, term}

Validate transaction. Currently it checks if

  • previous outputs exists
  • scripts execute with proper result
  • sum of previous output values is greater or equal than the sum of outputs

opts can be used to provide validation context:

  • :block - if transaction is inside a block, this is used to check for previous outputs that may be present there which are not yet in storage
  • :previous_outputs - already fetched previous outputs in the form of a map %{{hash, index} => pk_script}, currently used for tests, may be used for optimizations later