bsv_rpc v1.0.0-alpha3 BsvRpc.Transaction View Source
Functions for Bitcoin transaction manipulation.
Link to this section Summary
Functions
Adds a transaction input to the transaction.
Adds a transaction output to the transaction.
Creates a transaction from a binary blob.
Creates a transaction from a hex blob.
Gets the network fee of the transaction.
Gets the transaction hash.
Gets the transaction id (hash in the hex form).
Generates a P2PKH transaction to send funds to a single address.
Signs a transaction using the private key.
Signs a transaction using the private key.
Gets binary representation of the transaction.
Gets hex representation of the transaction.
Link to this section Types
t()
View Sourcet() :: %BsvRpc.Transaction{ block: binary() | nil, confirmations: non_neg_integer() | nil, hash: binary() | nil, inputs: [BsvRpc.TransactionInput.t()], locktime: non_neg_integer(), outputs: [BsvRpc.TransactionOutput.t()], size: non_neg_integer() | nil, time: DateTime.t() | nil, version: non_neg_integer() }
A Bitcoin transaction.
Link to this section Functions
add_input(transaction, transaction_input)
View Sourceadd_input(BsvRpc.Transaction.t(), BsvRpc.TransactionInput.t()) :: BsvRpc.Transaction.t()
Adds a transaction input to the transaction.
Examples
iex> t = BsvRpc.Transaction.create_from_hex("01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000") iex> t = t |> BsvRpc.Transaction.add_input(%BsvRpc.TransactionInput{ ...> previous_transaction: Base.decode16!("4A5E1E4BAAB89F3A32518A88C31BC87F618F76673E2CC77AB2127B7AFDEDA33B"), ...> previous_output: 0, ...> script_sig: <<>>, ...> sequence: 0xFFFFFFFF ...> }) iex> [_original | [added]] = t.inputs iex> added %BsvRpc.TransactionInput{
previous_transaction: <<74, 94, 30, 75, 170, 184, 159, 58, 50, 81, 138, 136, 195, 27, 200, 127, 97, 143, 118, 103, 62, 44, 199, 122, 178, 18, 123, 122, 253, 237, 163, 59>>,
previous_output: 0,
script_sig: <<>>,
sequence: 0xFFFFFFFF
}
add_output(transaction, transaction_output)
View Sourceadd_output(BsvRpc.Transaction.t(), BsvRpc.TransactionOutput.t()) :: BsvRpc.Transaction.t()
Adds a transaction output to the transaction.
Examples
iex> t = BsvRpc.Transaction.create_from_hex("01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000") iex> t = t |> BsvRpc.Transaction.add_output(%BsvRpc.TransactionOutput{value: 0, script_pubkey: <<0x00, 0x6A>>}) iex> [_original | [added]] = t.outputs iex> added %BsvRpc.TransactionOutput{script_pubkey: <<0x00, 0x6A>>, value: 0}
Creates a transaction from a binary blob.
Examples
iex> tx = "01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000" iex> t = tx |> Base.decode16!() |> BsvRpc.Transaction.create() iex> t.size 204 iex> Base.encode16(t.hash) "4A5E1E4BAAB89F3A32518A88C31BC87F618F76673E2CC77AB2127B7AFDEDA33B"
create_from_hex(hex)
View Sourcecreate_from_hex(String.t()) :: BsvRpc.Transaction.t()
Creates a transaction from a hex blob.
Examples
iex> t = BsvRpc.Transaction.create_from_hex("01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000") iex> t.size 204 iex> Base.encode16(t.hash) "4A5E1E4BAAB89F3A32518A88C31BC87F618F76673E2CC77AB2127B7AFDEDA33B"
Gets the network fee of the transaction.
Gets the transaction hash.
Examples
iex> t = BsvRpc.Transaction.create_from_hex("01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000") iex> BsvRpc.Transaction.hash(t) <<74, 94, 30, 75, 170, 184, 159, 58, 50, 81, 138, 136, 195, 27, 200, 127, 97, 143, 118, 103, 62, 44, 199, 122, 178, 18, 123, 122, 253, 237, 163, 59>>
Gets the transaction id (hash in the hex form).
Examples
iex> t = BsvRpc.Transaction.create_from_hex("01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000") iex> BsvRpc.Transaction.id(t) "4A5E1E4BAAB89F3A32518A88C31BC87F618F76673E2CC77AB2127B7AFDEDA33B"
send_to(to_address, amount, utxos, change_address, sat_per_byte \\ 1)
View SourceGenerates a P2PKH transaction to send funds to a single address.
Arguments
to_address
- Address to send funds to.amount
- Amount in satoshis.utxos
- List of unspent transaction outputs to consume.change_address
- Address to send change to.sat_per_byte
- (Optional) fee in satoshi per byte (dafault: 1).
Examples
iex> {:ok, to} = BsvRpc.Address.create("1wBQpttZsiMtrwgjp2NuGNEyBMPdnzCeA") iex> {:ok, change} = BsvRpc.Address.create("18v4ZTwZAkk7HKECkfutns1bfGVehaXNkW") iex> utxos = [%BsvRpc.UTXO{transaction: Base.decode16!("4A5E1E4BAAB89F3A32518A88C31BC87F618F76673E2CC77AB2127B7AFDEDA33B"), output: 0, value: 5_000_000_000, script_pubkey: <<>>}] iex> BsvRpc.Transaction.send_to(to, 4_000_000_000, utxos, change) {:ok, %BsvRpc.Transaction{
block: nil,
confirmations: nil,
hash: nil,
inputs: [
%BsvRpc.TransactionInput{
previous_output: 0,
previous_transaction: <<74, 94, 30, 75, 170, 184, 159, 58, 50, 81, 138,
136, 195, 27, 200, 127, 97, 143, 118, 103, 62, 44, 199, 122, 178, 18,
123, 122, 253, 237, 163, 59>>,
script_sig: "",
sequence: 4294967295
}
],
locktime: 0,
outputs: [
%BsvRpc.TransactionOutput{
script_pubkey: <<118, 169, 20, 10, 63, 39, 5, 95, 134, 238, 22, 182, 35,
80, 229, 135, 46, 13, 197, 9, 176, 72, 193, 136, 172>>,
value: 4000000000
},
%BsvRpc.TransactionOutput{
script_pubkey: <<118, 169, 20, 86, 209, 229, 225, 200, 165, 160, 64, 184,
37, 55, 2, 13, 124, 118, 184, 15, 15, 111, 242, 136, 172>>,
value: 999999772
}
],
size: nil,
time: nil,
version: 1
}} iex> BsvRpc.Transaction.send_to(to, 5_000_000_000, utxos, change)
sign(tx, keys, utxos, sigtype \\ [:sighash_all, :sighash_forkid])
View Sourcesign( BsvRpc.Transaction.t(), [BsvRpc.PrivateKey.t()] | BsvRpc.PrivateKey.t(), [BsvRpc.UTXO.t() | nil] | BsvRpc.UTXO.t() | nil, BsvRpc.Sighash.t() ) :: {:ok, BsvRpc.Transaction.t()} | {:error, String.t()}
Signs a transaction using the private key.
Examples
iex> {:ok, k} = ExtendedKey.from_string("xprv9s21ZrQH143K42Wyfo4GvDT1QBNSgq5sCBPXr4zaftZr2WKCrgEzdtniz5TvRgXA6V8hi2QrUMG3QTQnqovLp2UBAqsDcaxDUP3YCA61rJV") ...> |> BsvRpc.PrivateKey.create() iex> tx = BsvRpc.Transaction.create_from_hex("0100000001040800A41008F4C353626694DAC1EE5553FBD36B11AC5647528E29C7D6C89BE20000000000FFFFFFFF0200F90295000000001976A9141D7C7B4894BE23A6495B004157F3A1BBA173C52988AC0CF70295000000001976A9141D7C7B4894BE23A6495B004157F3A1BBA173C52988AC00000000") iex> utxo = %BsvRpc.UTXO{script_pubkey: Base.decode16!("76A9141D7C7B4894BE23A6495B004157F3A1BBA173C52988AC"), value: 5000000000, transaction: <<>>, output: 0} iex> {:ok, signed_tx} = BsvRpc.Transaction.sign(tx, k, utxo) iex> [input | []] = signed_tx.inputs iex> Base.encode16(input.script_sig) "4730440220758CB5A38A45687AC87F2637287D8F0214BB3F4455FA55CC66F37A8BD88BD62A022019B8AE768FC3ADAD1B99779E20CF747A9AD9EA339A8FAD24CB8DDFB196457E2741210342E0EB80C57799F22624264E5F7541400B996D3B38CFFFFC12EBDA7AC921DF2F"
sign!(tx, keys, utxos \\ nil, sigtype \\ [:sighash_all, :sighash_forkid])
View Sourcesign!( BsvRpc.Transaction.t(), [BsvRpc.PrivateKey.t()] | BsvRpc.PrivateKey.t(), [BsvRpc.UTXO.t() | nil] | BsvRpc.UTXO.t() | nil, BsvRpc.Sighash.t() ) :: BsvRpc.Transaction.t()
Signs a transaction using the private key.
Raises an exception in case of an error.
Examples
iex> {:ok, k} = ExtendedKey.from_string("xprv9s21ZrQH143K42Wyfo4GvDT1QBNSgq5sCBPXr4zaftZr2WKCrgEzdtniz5TvRgXA6V8hi2QrUMG3QTQnqovLp2UBAqsDcaxDUP3YCA61rJV") ...> |> BsvRpc.PrivateKey.create() iex> tx = BsvRpc.Transaction.create_from_hex("0100000001040800A41008F4C353626694DAC1EE5553FBD36B11AC5647528E29C7D6C89BE20000000000FFFFFFFF0200F90295000000001976A9141D7C7B4894BE23A6495B004157F3A1BBA173C52988AC0CF70295000000001976A9141D7C7B4894BE23A6495B004157F3A1BBA173C52988AC00000000") iex> utxo = %BsvRpc.UTXO{script_pubkey: Base.decode16!("76A9141D7C7B4894BE23A6495B004157F3A1BBA173C52988AC"), value: 5000000000, transaction: <<>>, output: 0} iex> signed_tx = BsvRpc.Transaction.sign!(tx, k, utxo) iex> [input | []] = signed_tx.inputs iex> Base.encode16(input.script_sig) "4730440220758CB5A38A45687AC87F2637287D8F0214BB3F4455FA55CC66F37A8BD88BD62A022019B8AE768FC3ADAD1B99779E20CF747A9AD9EA339A8FAD24CB8DDFB196457E2741210342E0EB80C57799F22624264E5F7541400B996D3B38CFFFFC12EBDA7AC921DF2F"
to_binary(transaction)
View Sourceto_binary(BsvRpc.Transaction.t()) :: binary()
Gets binary representation of the transaction.
Gets hex representation of the transaction.
Examples
iex> tx = "01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000" iex> t = tx |> Base.decode16!() |> BsvRpc.Transaction.create() iex> BsvRpc.Transaction.to_hex(t) "01000000010000000000000000000000000000000000000000000000000000000000000000FFFFFFFF4D04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73FFFFFFFF0100F2052A01000000434104678AFDB0FE5548271967F1A67130B7105CD6A828E03909A67962E0EA1F61DEB649F6BC3F4CEF38C4F35504E51EC112DE5C384DF7BA0B8D578A4C702B6BF11D5FAC00000000"