View Source BitcoinLib.Transaction (BitcoinLib v0.2.0-pre1)

Based on https://learnmeabitcoin.com/technical/transaction-data#fields

Link to this section Summary

Functions

Checks if a transaction is NOT signed

Converts a bitstring into a %Transaction{}

Encodes a transaction into binary format

Signs a transaction and transforms it into a binary that can be sent to the network

Link to this section Functions

Link to this function

check_if_unsigned(transaction)

View Source
@spec check_if_unsigned(%BitcoinLib.Transaction{
  inputs: term(),
  locktime: term(),
  outputs: term(),
  version: term()
}) :: boolean()

Checks if a transaction is NOT signed

examples

Examples

iex> %BitcoinLib.Transaction{ ...> inputs: [ ...> %BitcoinLib.Transaction.Input{ ...> txid: "39bc5c3b33d66ce3d7852a7942331e3ec10f8ba50f225fc41fb5dfa523239a27", ...> vout: 0, ...> script_sig: [], ...> sequence: 4294967295 ...> } ...> ] ...> } ...> |> BitcoinLib.Transaction.check_if_unsigned true

Link to this function

decode(encoded_transaction)

View Source
@spec decode(bitstring()) ::
  {:ok,
   %BitcoinLib.Transaction{
     inputs: term(),
     locktime: term(),
     outputs: term(),
     version: term()
   }}
  | {:error, binary()}

Converts a bitstring into a %Transaction{}

examples

Examples

iex> <<0x01000000017b1eabe0209b1fe794124575ef807057c77ada2138ae4fa8d6c4de0398a14f3f0000000000ffffffff01f0ca052a010000001976a914cbc20a7664f2f69e5355aa427045bc15e7c6c77288ac00000000::680>> ...> |> BitcoinLib.Transaction.decode() {

:ok,
%BitcoinLib.Transaction{
  version: 1,
  inputs: [
    %BitcoinLib.Transaction.Input{
      txid: "3f4fa19803dec4d6a84fae3821da7ac7577080ef75451294e71f9b20e0ab1e7b",
      vout: 0,
      script_sig: [],
      sequence: 4294967295
    }
  ],
  outputs: [
    %BitcoinLib.Transaction.Output{
      value: 4999990000,
      script_pub_key: [
        %BitcoinLib.Script.Opcodes.Stack.Dup{},
        %BitcoinLib.Script.Opcodes.Crypto.Hash160{},
        %BitcoinLib.Script.Opcodes.Data{value: <<0xcbc20a7664f2f69e5355aa427045bc15e7c6c772::160>>},
        %BitcoinLib.Script.Opcodes.BitwiseLogic.EqualVerify{},
        %BitcoinLib.Script.Opcodes.Crypto.CheckSig{script: <<0x76a914cbc20a7664f2f69e5355aa427045bc15e7c6c77288ac::200>>}
      ]
    }
  ],
  locktime: 0
}

}

@spec encode(%BitcoinLib.Transaction{
  inputs: term(),
  locktime: term(),
  outputs: term(),
  version: term()
}) ::
  binary()

Encodes a transaction into binary format

based on https://medium.com/@bitaps.com/exploring-bitcoin-signing-the-p2pkh-input-b8b4d5c4809c#50a6

examples

Examples

iex> transaction = %BitcoinLib.Transaction{ ...> inputs: [ ...> %BitcoinLib.Transaction.Input{ ...> script_sig: nil, ...> sequence: 0xFFFFFFFF, ...> txid: "5e2383defe7efcbdc9fdd6dba55da148b206617bbb49e6bb93fce7bfbb459d44", ...> vout: 1 ...> } ...> ], ...> outputs: [ ...> %BitcoinLib.Transaction.Output{ ...> script_pub_key: [ ...> %BitcoinLib.Script.Opcodes.Stack.Dup{}, ...> %BitcoinLib.Script.Opcodes.Crypto.Hash160{}, ...> %BitcoinLib.Script.Opcodes.Data{value: <<0xf86f0bc0a2232970ccdf4569815db500f1268361::160>>}, ...> %BitcoinLib.Script.Opcodes.BitwiseLogic.EqualVerify{}, ...> %BitcoinLib.Script.Opcodes.Crypto.CheckSig{} ...> ], ...> value: 129000000 ...> } ...> ], ...> locktime: 0, ...> version: 1 ...> } ...> BitcoinLib.Transaction.encode(transaction) <<0x0100000001449d45bbbfe7fc93bbe649bb7b6106b248a15da5dbd6fdc9bdfc7efede83235e0100000000ffffffff014062b007000000001976a914f86f0bc0a2232970ccdf4569815db500f126836188ac00000000::680>>

Link to this function

sign_and_encode(transaction, private_key)

View Source
@spec sign_and_encode(
  %BitcoinLib.Transaction{
    inputs: term(),
    locktime: term(),
    outputs: term(),
    version: term()
  },
  %BitcoinLib.Key.PrivateKey{
    chain_code: term(),
    depth: term(),
    fingerprint: term(),
    index: term(),
    key: term(),
    parent_fingerprint: term()
  }
) :: binary()

Signs a transaction and transforms it into a binary that can be sent to the network

examples

Examples

iex> private_key = BitcoinLib.Key.PrivateKey.from_mnemonic_phrase( ...> "rally celery split order almost twenty ignore record legend learn chaos decade" ...> ) ...> %BitcoinLib.Transaction{ ...> version: 1, ...> inputs: [ ...> %BitcoinLib.Transaction.Input{ ...> txid: "e4c226432a9319d603b2ed1fa609bffe4cd91f89b3176a9e73b19f7891a92bb6", ...> vout: 0, ...> sequence: 0xFFFFFFFF, ...> script_sig: <<0x76A914AFC3E518577316386188AF748A816CD14CE333F288AC::200>> |> BitcoinLib.Script.parse() ...> } ...> ], ...> outputs: [%BitcoinLib.Transaction.Output{ ...> script_pub_key: <<0x76a9283265393261373463333431393661303236653839653061643561633431386366393430613361663288ac::360>> |> BitcoinLib.Script.parse(), ...> value: 10_000} ...> ], ...> locktime: 0 ...> } ...> |> BitcoinLib.Transaction.sign_and_encode(private_key) "0100000001b62ba991789fb1739e6a17b3891fd94cfebf09a61fedb203d619932a4326c2e4000000006a47304402207d2ff650acf4bd2f413dc04ded50fbbfc315bcb0aa97636b3c4caf55333d1c6a02207590f62363b2263b3d9b65dad3cd56e840e0d61dc0feab8f7e7956831c7e5103012102702ded1cca9816fa1a94787ffc6f3ace62cd3b63164f76d227d0935a33ee48c3ffffffff0110270000000000002d76a9283265393261373463333431393661303236653839653061643561633431386366393430613361663288ac00000000"