Cartouche.Transaction.V4 (Cartouche v0.2.0)

Copy Markdown View Source

Represents a V4 or EIP-7702 set-code transaction.

EIP-7702 transactions use transaction type 0x04 and extend the EIP-1559 field set with an authorization list. Each authorization entry is a {chain_id, address, nonce, y_parity, r, s} tuple signed over 0x05 || rlp([chain_id, address, nonce]).

Examples

iex> use Cartouche.Hex
iex> authorization = {
...>   1,
...>   ~h[0x0000000000000000000000000000000000000002],
...>   7,
...>   false,
...>   <<1::256>>,
...>   <<2::256>>
...> }
iex> transaction =
...>   Cartouche.Transaction.V4.new(
...>     1,
...>     {1, :gwei},
...>     {100, :gwei},
...>     100_000,
...>     ~h[0x0000000000000000000000000000000000000001],
...>     {2, :wei},
...>     <<1, 2, 3>>,
...>     [],
...>     [authorization],
...>     :mainnet
...>   )
...>   |> Cartouche.Transaction.V4.add_signature(<<3::256, 4::256, 0>>)
iex> {:ok, decoded} = transaction |> Cartouche.Transaction.V4.encode() |> Cartouche.Transaction.V4.decode()
iex> decoded == transaction
true

Summary

Functions

Adds an authorization signature from a packed binary (r <> s <> v).

Adds an outer transaction signature from a packed binary (r <> s <> v).

Returns the EIP-7702 authorization signing hash.

Returns the EIP-7702 authorization signing payload.

Decode an RLP-encoded EIP-7702 transaction.

Build an RLP-encoded EIP-7702 transaction.

Returns a packed authorization signature (r <> s <> y_parity).

Recovers a packed outer transaction signature (r <> s <> y_parity).

Returns the transaction hash for signed or unsigned encoded bytes.

Recovers the EOA that signed an authorization tuple.

Recovers the signer from a signed EIP-7702 transaction.

Signs the outer EIP-7702 transaction.

Signs an EIP-7702 authorization tuple.

Returns the outer transaction signing payload.

Types

authorization()

@type authorization() ::
  {non_neg_integer(), <<_::160>>, non_neg_integer(), boolean(), <<_::256>>,
   <<_::256>>}

authorization_input()

@type authorization_input() ::
  authorization()
  | {non_neg_integer(), <<_::160>>, non_neg_integer(), nil, nil, nil}

t()

@type t() :: %Cartouche.Transaction.V4{
  access_list: [{<<_::160>>, [<<_::256>>]}],
  amount: non_neg_integer(),
  authorization_list: [authorization()],
  chain_id: non_neg_integer(),
  data: binary(),
  destination: <<_::160>>,
  gas_limit: non_neg_integer(),
  max_fee_per_gas: non_neg_integer() | nil,
  max_priority_fee_per_gas: non_neg_integer() | nil,
  nonce: non_neg_integer(),
  signature_r: <<_::256>> | nil,
  signature_s: <<_::256>> | nil,
  signature_y_parity: boolean() | nil
}

tx_input()

@type tx_input() ::
  t()
  | %Cartouche.Transaction.V4{
      access_list: list() | nil,
      amount: non_neg_integer() | nil,
      authorization_list: list() | nil,
      chain_id: non_neg_integer() | nil,
      data: binary() | nil,
      destination: <<_::160>> | nil,
      gas_limit: non_neg_integer() | nil,
      max_fee_per_gas: non_neg_integer() | nil,
      max_priority_fee_per_gas: non_neg_integer() | nil,
      nonce: non_neg_integer() | nil,
      signature_r: nil,
      signature_s: nil,
      signature_y_parity: nil
    }

unsigned_authorization()

@type unsigned_authorization() :: {non_neg_integer(), <<_::160>>, non_neg_integer()}

Functions

add_authorization_signature(arg1, arg2)

@spec add_authorization_signature(authorization_input(), <<_::520, _::_*8>>) ::
  authorization()

Adds an authorization signature from a packed binary (r <> s <> v).

add_signature(transaction, arg)

@spec add_signature(t(), <<_::520, _::_*8>>) :: t()

Adds an outer transaction signature from a packed binary (r <> s <> v).

authorization_hash(authorization)

@spec authorization_hash(unsigned_authorization() | authorization()) :: <<_::256>>

Returns the EIP-7702 authorization signing hash.

authorization_signing_payload(authorization)

@spec authorization_signing_payload(unsigned_authorization() | authorization()) ::
  binary()

Returns the EIP-7702 authorization signing payload.

decode(arg1)

@spec decode(binary()) :: {:ok, t()} | {:error, String.t()}

Decode an RLP-encoded EIP-7702 transaction.

encode(transaction)

@spec encode(tx_input()) :: binary()

Build an RLP-encoded EIP-7702 transaction.

get_authorization_signature(arg)

@spec get_authorization_signature(authorization()) ::
  {:ok, binary()} | {:error, String.t()}

Returns a packed authorization signature (r <> s <> y_parity).

get_signature(v4)

@spec get_signature(t()) :: {:ok, binary()} | {:error, String.t()}

Recovers a packed outer transaction signature (r <> s <> y_parity).

hash(transaction)

@spec hash(t() | binary()) :: <<_::256>>

Returns the transaction hash for signed or unsigned encoded bytes.

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

@spec new(
  non_neg_integer(),
  non_neg_integer() | {non_neg_integer(), :wei | :gwei} | nil,
  non_neg_integer() | {non_neg_integer(), :wei | :gwei} | nil,
  non_neg_integer(),
  <<_::160>>,
  non_neg_integer() | {non_neg_integer(), :wei | :gwei},
  binary(),
  list(),
  [authorization()],
  atom() | integer() | nil
) :: t()

Constructs an unsigned EIP-7702 transaction.

recover_authority(authorization)

@spec recover_authority(authorization()) :: {:ok, <<_::160>>} | {:error, String.t()}

Recovers the EOA that signed an authorization tuple.

recover_signer(transaction)

@spec recover_signer(t()) :: {:ok, <<_::160>>} | {:error, String.t()}

Recovers the signer from a signed EIP-7702 transaction.

sign(transaction, signer \\ Default)

@spec sign(t(), GenServer.name()) :: {:ok, t()} | {:error, String.t()}

Signs the outer EIP-7702 transaction.

sign_authorization(authorization, signer \\ Default)

@spec sign_authorization(unsigned_authorization(), GenServer.name()) ::
  {:ok, authorization()} | {:error, String.t()}

Signs an EIP-7702 authorization tuple.

signing_payload(transaction)

@spec signing_payload(t()) :: binary()

Returns the outer transaction signing payload.