View Source Signet.Transaction.V2 (Signet v1.3.8)

Represents a V2 or EIP-1559 transaction.

Summary

Functions

Adds a signature to a transaction. This overwrites the signature_y_parity, signature_r and signature_s fields.

Decode an RLP-encoded transaction. Note: the signature must have been signed (i.e. properly encoded), not simply encoded for signing.

Build an RLP-encoded transaction. Note: if the transaction does not have a signature set (that is, signature_y_parity, signature_r or signature_s are nil), then we will encode a partial transaction (which can be used for signing).

Recovers a signature from a transaction, if it's been signed. Otherwise returns an error.

Recovers the signer from a given transaction, if it's been signed.

Types

@type t() :: %Signet.Transaction.V2{
  access_list: [{<<_::160>>, [<<_::256>>]}],
  amount: integer(),
  chain_id: integer(),
  data: binary(),
  destination: <<_::160>>,
  gas_limit: integer(),
  max_fee_per_gas: integer(),
  max_priority_fee_per_gas: integer(),
  nonce: integer(),
  signature_r: <<_::256>>,
  signature_s: <<_::256>>,
  signature_y_parity: boolean()
}

Functions

Link to this function

add_signature(transaction, arg)

View Source
Link to this function

add_signature(transaction, v, r, s)

View Source

Adds a signature to a transaction. This overwrites the signature_y_parity, signature_r and signature_s fields.

Examples

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [<<2::160>>, <<3::160>>], 1, 0x01, 0x02, :goerli)
...> |> Signet.Transaction.V2.add_signature(true, <<1::256>>, <<2::256>>)
%Signet.Transaction.V2{
  chain_id: 5,
  nonce: 1,
  max_priority_fee_per_gas: 1000000000,
  max_fee_per_gas: 100000000000,
  gas_limit: 100000,
  destination: <<1::160>>,
  amount: 2,
  data: <<1, 2, 3>>,
  access_list: [<<2::160>>, <<3::160>>],
  signature_y_parity: true,
  signature_r: <<0x01::256>>,
  signature_s: <<0x02::256>>
}

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [<<2::160>>, <<3::160>>], 1, 0x01, 0x02, :goerli)
...> |> Signet.Transaction.V2.add_signature(<<1::256, 2::256, 1::8>>)
%Signet.Transaction.V2{
  chain_id: 5,
  nonce: 1,
  max_priority_fee_per_gas: 1000000000,
  max_fee_per_gas: 100000000000,
  gas_limit: 100000,
  destination: <<1::160>>,
  amount: 2,
  data: <<1, 2, 3>>,
  access_list: [<<2::160>>, <<3::160>>],
  signature_y_parity: true,
  signature_r: <<0x01::256>>,
  signature_s: <<0x02::256>>
}

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [<<2::160>>, <<3::160>>], 1, 0x01, 0x02, :goerli)
...> |> Signet.Transaction.V2.add_signature(<<1::256, 2::256, 27::8>>)
%Signet.Transaction.V2{
  chain_id: 5,
  nonce: 1,
  max_priority_fee_per_gas: 1000000000,
  max_fee_per_gas: 100000000000,
  gas_limit: 100000,
  destination: <<1::160>>,
  amount: 2,
  data: <<1, 2, 3>>,
  access_list: [<<2::160>>, <<3::160>>],
  signature_y_parity: false,
  signature_r: <<0x01::256>>,
  signature_s: <<0x02::256>>
}

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [<<2::160>>, <<3::160>>], 1, 0x01, 0x02, :goerli)
...> |> Signet.Transaction.V2.add_signature(<<1::256, 2::256, 38::8>>)
%Signet.Transaction.V2{
  chain_id: 5,
  nonce: 1,
  max_priority_fee_per_gas: 1000000000,
  max_fee_per_gas: 100000000000,
  gas_limit: 100000,
  destination: <<1::160>>,
  amount: 2,
  data: <<1, 2, 3>>,
  access_list: [<<2::160>>, <<3::160>>],
  signature_y_parity: true,
  signature_r: <<0x01::256>>,
  signature_s: <<0x02::256>>
}

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [<<2::160>>, <<3::160>>], 1, 0x01, 0x02, :goerli)
...> |> Signet.Transaction.V2.add_signature(<<1::256, 2::256, 3838::16>>)
%Signet.Transaction.V2{
  chain_id: 5,
  nonce: 1,
  max_priority_fee_per_gas: 1000000000,
  max_fee_per_gas: 100000000000,
  gas_limit: 100000,
  destination: <<1::160>>,
  amount: 2,
  data: <<1, 2, 3>>,
  access_list: [<<2::160>>, <<3::160>>],
  signature_y_parity: true,
  signature_r: <<0x01::256>>,
  signature_s: <<0x02::256>>
}

Decode an RLP-encoded transaction. Note: the signature must have been signed (i.e. properly encoded), not simply encoded for signing.

Examples

iex> use Signet.Hex
iex> Signet.Transaction.V2.decode(~h[0x02EF0501843B9ACA0085174876E800830186A09400000000000000000000000000000000000000010283010203C0010102])
{:ok, %Signet.Transaction.V2{
  chain_id: 5,
  nonce: 1,
  max_priority_fee_per_gas: 1000000000,
  max_fee_per_gas: 100000000000,
  gas_limit: 100000,
  destination: <<1::160>>,
  amount: 2,
  data: <<1, 2, 3>>,
  access_list: [],
  signature_y_parity: true,
  signature_r: <<0x01::256>>,
  signature_s: <<0x02::256>>
}}

iex> use Signet.Hex
iex> Signet.Transaction.V2.decode(~h[0x02F87F0501843B9ACA0085174876E800830186A09400000000000000000000000000000000000000010283010203F84FF7940000000000000000000000000000000000000002E1A00000000000000000000000000000000000000000000000000000000000000016D6940000000000000000000000000000000000000003C0010102])
{:ok, %Signet.Transaction.V2{
  chain_id: 5,
  nonce: 1,
  max_priority_fee_per_gas: 1000000000,
  max_fee_per_gas: 100000000000,
  gas_limit: 100000,
  destination: <<1::160>>,
  amount: 2,
  data: <<1, 2, 3>>,
  access_list: [{<<2::160>>, [<<22::256>>]}, {<<3::160>>, []}],
  signature_y_parity: true,
  signature_r: <<0x01::256>>,
  signature_s: <<0x02::256>>
}}

iex> use Signet.Hex
iex> Signet.Transaction.V2.decode(~h[0x02F9027382210501843B9ACA00843C2390F1830493E09400AEA4B2242ABC8BB4BB78D537A67A245A7BEC6480B90204DEFF4B240000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000A000000000000000000000000000000000000000000000000000000000000007B0000000000000000000000003B72952436D0DCACFA7D7691C0CF4DE6DD5BAA7E0000000000000000000000003B72952436D0DCACFA7D7691C0CF4DE6DD5BAA7E00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000B2C639C533813F4AA9D7837CAF62653D097FF85000000000000000000000000833589FCD6EDB6E08F4C7C32D4F71B54BDA0291300000000000000000000000000000000000000000000000000000000000AAE6000000000000000000000000000000000000000000000000000000000000AAE60000000000000000000000000000000000000000000000000000000000000000A00000000000000000000000000000000000000000000000000000000002ADA240000000000000000000000000000000000000000000000000000000067D38314000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000C080A0019C8102CB582C309B0D2ABABB6AEB683A8FBC7E2044665F84669F0A73865B9AA03732EB6644FC11E850A0FED8EBE403B4C2F5D1F1AEC961EACCF1F7BAA55615A6])
{:ok, %Signet.Transaction.V2{
  chain_id: 8453,
  nonce: 1,
  max_priority_fee_per_gas: 1000000000,
  max_fee_per_gas: 1008963825,
  gas_limit: 300000,
  destination: ~h[0x00aea4b2242abc8bb4bb78d537a67a245a7bec64],
  amount: 0,
  data: ~h[0xdeff4b240000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000007b0000000000000000000000003b72952436d0dcacfa7d7691c0cf4de6dd5baa7e0000000000000000000000003b72952436d0dcacfa7d7691c0cf4de6dd5baa7e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b2c639c533813f4aa9d7837caf62653d097ff85000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda0291300000000000000000000000000000000000000000000000000000000000aae6000000000000000000000000000000000000000000000000000000000000aae60000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000002ada240000000000000000000000000000000000000000000000000000000067d38314000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000],
  access_list: [],
  signature_y_parity: false,
  signature_r: ~h[0x019c8102cb582c309b0d2ababb6aeb683a8fbc7e2044665f84669f0a73865b9a],
  signature_s: ~h[0x3732eb6644fc11e850a0fed8ebe403b4c2f5d1f1aec961eaccf1f7baa55615a6]
}}

Build an RLP-encoded transaction. Note: if the transaction does not have a signature set (that is, signature_y_parity, signature_r or signature_s are nil), then we will encode a partial transaction (which can be used for signing).

Examples

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [], :goerli)
...> |> Signet.Transaction.V2.encode()
...> |> Signet.Hex.encode_big_hex()
"0x02EC0501843B9ACA0085174876E800830186A09400000000000000000000000000000000000000010283010203C0"

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [], true, <<0x01::256>>, <<0x02::256>>, :goerli)
...> |> Signet.Transaction.V2.encode()
...> |> Signet.Hex.encode_big_hex()
"0x02EF0501843B9ACA0085174876E800830186A09400000000000000000000000000000000000000010283010203C0010102"

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [{<<2::160>>, [<<22::256>>]}, {<<3::160>>, []}], true, <<0x01::256>>, <<0x00, 0x02::248>>, :goerli)
...> |> Signet.Transaction.V2.encode()
...> |> Signet.Hex.encode_big_hex()
"0x02F87F0501843B9ACA0085174876E800830186A09400000000000000000000000000000000000000010283010203F84FF7940000000000000000000000000000000000000002E1A00000000000000000000000000000000000000000000000000000000000000016D6940000000000000000000000000000000000000003C0010102"

iex> use Signet.Hex
iex> %Signet.Transaction.V2{
...>   chain_id: 8453,
...>   nonce: 1,
...>   max_priority_fee_per_gas: 1000000000,
...>   max_fee_per_gas: 1008963825,
...>   gas_limit: 300000,
...>   destination: ~h[0x00aea4b2242abc8bb4bb78d537a67a245a7bec64],
...>   amount: 0,
...>   data: ~h[0xdeff4b240000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000007b0000000000000000000000003b72952436d0dcacfa7d7691c0cf4de6dd5baa7e0000000000000000000000003b72952436d0dcacfa7d7691c0cf4de6dd5baa7e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b2c639c533813f4aa9d7837caf62653d097ff85000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda0291300000000000000000000000000000000000000000000000000000000000aae6000000000000000000000000000000000000000000000000000000000000aae60000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000002ada240000000000000000000000000000000000000000000000000000000067d38314000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000],
...>   access_list: [],
...>   signature_y_parity: false,
...>   signature_r: ~h[0x019c8102cb582c309b0d2ababb6aeb683a8fbc7e2044665f84669f0a73865b9a],
...>   signature_s: ~h[0x3732eb6644fc11e850a0fed8ebe403b4c2f5d1f1aec961eaccf1f7baa55615a6]
...> }
...> |> Signet.Transaction.V2.encode()
...> |> Signet.Hex.encode_big_hex()
"0x02F9027382210501843B9ACA00843C2390F1830493E09400AEA4B2242ABC8BB4BB78D537A67A245A7BEC6480B90204DEFF4B240000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000A000000000000000000000000000000000000000000000000000000000000007B0000000000000000000000003B72952436D0DCACFA7D7691C0CF4DE6DD5BAA7E0000000000000000000000003B72952436D0DCACFA7D7691C0CF4DE6DD5BAA7E00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000B2C639C533813F4AA9D7837CAF62653D097FF85000000000000000000000000833589FCD6EDB6E08F4C7C32D4F71B54BDA0291300000000000000000000000000000000000000000000000000000000000AAE6000000000000000000000000000000000000000000000000000000000000AAE60000000000000000000000000000000000000000000000000000000000000000A00000000000000000000000000000000000000000000000000000000002ADA240000000000000000000000000000000000000000000000000000000067D38314000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000000C080A0019C8102CB582C309B0D2ABABB6AEB683A8FBC7E2044665F84669F0A73865B9AA03732EB6644FC11E850A0FED8EBE403B4C2F5D1F1AEC961EACCF1F7BAA55615A6"

Recovers a signature from a transaction, if it's been signed. Otherwise returns an error.

Examples

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [<<2::160>>, <<3::160>>], true, <<0x01::256>>, <<0x02::256>>, :goerli)
...> |> Signet.Transaction.V2.get_signature()
{:ok, <<1::256, 2::256, 1::8>>}

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [<<2::160>>, <<3::160>>], :goerli)
...> |> Signet.Transaction.V2.get_signature()
{:error, "transaction missing signature"}
Link to this function

new(nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, destination, amount, data, access_list, chain_id \\ nil)

View Source

Constructs a new V2 (EIP-1559) Ethereum transaction.

Examples

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [{<<2::160>>, [<<22::256>>]}, {<<3::160>>, []}], :goerli)
%Signet.Transaction.V2{
  chain_id: 5,
  nonce: 1,
  max_priority_fee_per_gas: 1000000000,
  max_fee_per_gas: 100000000000,
  gas_limit: 100000,
  destination: <<1::160>>,
  amount: 2,
  data: <<1, 2, 3>>,
  access_list: [{<<2::160>>, [<<22::256>>]}, {<<3::160>>, []}],
  signature_y_parity: nil,
  signature_r: nil,
  signature_s: nil
}

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [{<<2::160>>, [<<22::256>>]}, {<<3::160>>, []}], true, <<0x01::256>>, <<0x02::256>>, :goerli)
%Signet.Transaction.V2{
  chain_id: 5,
  nonce: 1,
  max_priority_fee_per_gas: 1000000000,
  max_fee_per_gas: 100000000000,
  gas_limit: 100000,
  destination: <<1::160>>,
  amount: 2,
  data: <<1, 2, 3>>,
  access_list: [{<<2::160>>, [<<22::256>>]}, {<<3::160>>, []}],
  signature_y_parity: true,
  signature_r: <<0x01::256>>,
  signature_s: <<0x02::256>>
}
Link to this function

new(nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, destination, amount, data, access_list, signature_y_parity, signature_r, signature_s, chain_id \\ nil)

View Source
Link to this function

recover_signer(transaction)

View Source

Recovers the signer from a given transaction, if it's been signed.

Examples

iex> {:ok, address} =
...>   Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [<<2::160>>, <<3::160>>], true, <<0x01::256>>, <<0x02::256>>, :goerli)
...>   |> Signet.Transaction.V2.recover_signer()
...> Signet.Hex.to_address(address)
"0xC002Ca628F93e1550b5f30Ed10902A9e7783364B"

iex> Signet.Transaction.V2.new(1, {1, :gwei}, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, [<<2::160>>, <<3::160>>], :goerli)
...> |> Signet.Transaction.V2.recover_signer()
{:error, "transaction missing signature"}