BSV-ex v0.4.1 BSV.Transaction View Source

Module for the construction, parsing and serialization of Bitcoin transactions.

Link to this section Summary

Types

t()

Bitcoin Transaction

Functions

Adds the given input to the transaction. Resets the signatures for all inputs.

Adds the given output to the transaction. Resets the signatures for all inputs.

Specifies the change address for the given transaction. Resets the signatures for all inputs.

Returns the change output of the given transaction.

Returns the fee for the given transaction. If the fee has already been set using f:BSV.Transaction.set_fee/2, then that figure is returned. Otherwise a fee is calculated based on the result of f:BSV.Transaction.get_size/1.

Returns the sum from all inputs of the given transaction.

Returns the sum from all outputs of the given transaction.

Returns the size of the given transaction. Where any inputs are without a signed script, it's size is estimated assuming a P2PKH input.

Returns the given transaction's txid, which is a double SHA-256 hash of the serialized transaction, reversed.

Parse the given binary into a transaction. Returns a tuple containing the transaction input and the remaining binary data.

Serialises the given transaction into a binary.

Sets the fee for the given transaction. Resets the signatures for all inputs.

Signs the transaction using the given private key or list of keys. Each input is iterrated over verifying that the key can sign the input.

Adds the given input or list of inputs to the transaction. Each input must be complete with a spendable UTXO or the function will raise an error. Resets the signatures for all inputs.

Creates a P2PKH output using the given address and spend amount, and adds the output to the transaction. Resets the signatures for all inputs.

Link to this section Types

Link to this type

t()

View Source
t() :: %BSV.Transaction{
  change_index: nil,
  change_script: nil,
  fee: nil,
  inputs: list(),
  lock_time: integer(),
  outputs: list(),
  version: integer()
}

Bitcoin Transaction

Link to this section Functions

Link to this function

add_input(tx, input)

View Source
add_input(t(), BSV.Transaction.Input.t()) :: t()

Adds the given input to the transaction. Resets the signatures for all inputs.

Examples

iex> tx = %BSV.Transaction{}
...> |> BSV.Transaction.add_input(%BSV.Transaction.Input{})
iex> length(tx.inputs) == 1
true
Link to this function

add_output(tx, output)

View Source
add_output(t(), BSV.Transaction.Output.t()) :: t()

Adds the given output to the transaction. Resets the signatures for all inputs.

Examples

iex> tx = %BSV.Transaction{}
...> |> BSV.Transaction.add_output(%BSV.Transaction.Output{})
iex> length(tx.outputs) == 1
true
Link to this function

change_to(tx, address)

View Source
change_to(t(), BSV.Address.t() | binary()) :: t()

Specifies the change address for the given transaction. Resets the signatures for all inputs.

Examples

iex> %BSV.Transaction{}
...> |> BSV.Transaction.spend_from(%BSV.Transaction.Input{utxo: %BSV.Transaction.Output{satoshis: 100000}})
...> |> BSV.Transaction.spend_to("1B8j21Ym6QbJQ6kRvT1N7pvdBN2qPDhqij", 75000)
...> |> BSV.Transaction.change_to("1G26ZnsXQpL9cdqCKE6vViMdW9QwRQTcTJ")
...> |> BSV.Transaction.get_change_output
%BSV.Transaction.Output{
  satoshis: 24887,
  script: %BSV.Script{
    chunks: [
      :OP_DUP,
      :OP_HASH160,
      <<164, 190, 242, 205, 108, 224, 228, 253, 144, 102, 35, 209, 230, 33, 135, 143, 211, 21, 79, 82>>,
      :OP_EQUALVERIFY,
      :OP_CHECKSIG
    ]
  }
}
Link to this function

get_change_output(tx)

View Source
get_change_output(t()) :: BSV.Transaction.Output.t()

Returns the change output of the given transaction.

Returns the fee for the given transaction. If the fee has already been set using f:BSV.Transaction.set_fee/2, then that figure is returned. Otherwise a fee is calculated based on the result of f:BSV.Transaction.get_size/1.

Examples

iex> %BSV.Transaction{}
...> |> BSV.Transaction.set_fee(500)
...> |> BSV.Transaction.get_fee
500

iex> %BSV.Transaction{}
...> |> BSV.Transaction.spend_from(%BSV.Transaction.Input{utxo: %BSV.Transaction.Output{satoshis: 100000}})
...> |> BSV.Transaction.spend_to("1B8j21Ym6QbJQ6kRvT1N7pvdBN2qPDhqij", 75000)
...> |> BSV.Transaction.get_fee
96
Link to this function

get_input_sum(tx)

View Source
get_input_sum(t()) :: integer()

Returns the sum from all inputs of the given transaction.

Examples

iex> inputs = [
...>   %BSV.Transaction.Input{utxo: %BSV.Transaction.Output{satoshis: 1575}},
...>   %BSV.Transaction.Input{utxo: %BSV.Transaction.Output{satoshis: 3000}}
...> ]
...>
iex> BSV.Transaction.spend_from(%BSV.Transaction{}, inputs)
...> |> BSV.Transaction.get_input_sum
4575
Link to this function

get_output_sum(tx)

View Source
get_output_sum(t()) :: integer()

Returns the sum from all outputs of the given transaction.

Examples

iex> %BSV.Transaction{}
...> |> BSV.Transaction.spend_to("15KgnG69mTbtkx73vNDNUdrWuDhnmfCxsf", 5000)
...> |> BSV.Transaction.spend_to("15KgnG69mTbtkx73vNDNUdrWuDhnmfCxsf", 1325)
...> |> BSV.Transaction.get_output_sum
6325
Link to this function

get_size(tx)

View Source
get_size(t()) :: integer()

Returns the size of the given transaction. Where any inputs are without a signed script, it's size is estimated assuming a P2PKH input.

Examples

iex> %BSV.Transaction{}
...> |> BSV.Transaction.spend_from(%BSV.Transaction.Input{utxo: %BSV.Transaction.Output{satoshis: 100000}})
...> |> BSV.Transaction.spend_to("1B8j21Ym6QbJQ6kRvT1N7pvdBN2qPDhqij", 75000)
...> |> BSV.Transaction.get_size
192
Link to this function

get_txid(tx)

View Source
get_txid(t()) :: String.t()

Returns the given transaction's txid, which is a double SHA-256 hash of the serialized transaction, reversed.

Examples

iex> %BSV.Transaction{}
...> |> BSV.Transaction.spend_to("1B8j21Ym6QbJQ6kRvT1N7pvdBN2qPDhqij", 72000)
...> |> BSV.Transaction.get_txid
"c8e8f4951eb08f9e6e12b92da30b0b9a0849202dcbb5ac35e13acc91b8c4de6d"
Link to this function

is_coinbase(transaction)

View Source
is_coinbase(t()) :: boolean()
Link to this function

parse(data, options \\ [])

View Source
parse(binary(), keyword()) :: {t(), binary()}

Parse the given binary into a transaction. Returns a tuple containing the transaction input and the remaining binary data.

Options

The accepted options are:

  • :encoding - Optionally decode the binary with either the :base64 or :hex encoding scheme.

Examples

BSV.Transaction.parse(data)
{%BSV.Trasaction{}, ""}
Link to this function

serialize(tx, options \\ [])

View Source
serialize(t(), keyword()) :: binary()

Serialises the given transaction into a binary.

Options

The accepted options are:

  • :encode - Optionally encode the returned binary with either the :base64 or :hex encoding scheme.

Examples

BSV.Transaction.Input.serialize(input)
<<binary>>
Link to this function

set_fee(tx, fee)

View Source
set_fee(t(), integer()) :: t()

Sets the fee for the given transaction. Resets the signatures for all inputs.

Signs the transaction using the given private key or list of keys. Each input is iterrated over verifying that the key can sign the input.

Link to this function

spend_from(tx, input)

View Source
spend_from(t(), BSV.Transaction.Input.t() | list()) :: t()

Adds the given input or list of inputs to the transaction. Each input must be complete with a spendable UTXO or the function will raise an error. Resets the signatures for all inputs.

Link to this function

spend_to(tx, address, satoshis)

View Source
spend_to(t(), BSV.Address.t() | binary(), integer()) :: t()

Creates a P2PKH output using the given address and spend amount, and adds the output to the transaction. Resets the signatures for all inputs.

Examples

iex> tx = %BSV.Transaction{}
...> |> BSV.Transaction.spend_to("15KgnG69mTbtkx73vNDNUdrWuDhnmfCxsf", 1000)
iex> length(tx.outputs) == 1
true