BSV-ex v0.4.1 BSV.Transaction View Source
Module for the construction, parsing and serialization of Bitcoin transactions.
Link to this section Summary
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
Bitcoin Transaction
Link to this section Functions
add_input(tx, input)
View Sourceadd_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
add_output(tx, output)
View Sourceadd_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
change_to(tx, address)
View Sourcechange_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
]
}
}
get_change_output(tx)
View Sourceget_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
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
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
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
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"
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{}, ""}
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>>
Sets the fee for the given transaction. Resets the signatures for all inputs.
sign(tx, key)
View Sourcesign( t(), BSV.KeyPair.t() | BSV.Extended.PrivateKey.t() | {binary(), binary()} | binary() | list() ) :: t()
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.
spend_from(tx, input)
View Sourcespend_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.
spend_to(tx, address, satoshis)
View Sourcespend_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