Cartouche.Transaction.V1 (Cartouche v0.2.0)

Copy Markdown View Source

Represents a V1 or "Legacy" (that is, pre-EIP-1559) transaction.

Summary

Functions

Adds a signature to a transaction. This overwrites the [chain_id, 0, 0] fields, as per EIP-155.

Decode an RLP-encoded transaction.

Build an RLP-encoded transaction. Note: transactions can be encoded before they are signed, which uses [chain_id, 0, 0] in the signature fields, otherwise those fields are [v, r, s].

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

Constructs a new V1 (Legacy) Ethereum transaction.

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

Types

t()

@type t() :: %Cartouche.Transaction.V1{
  data: binary(),
  gas_limit: integer(),
  gas_price: integer(),
  nonce: integer(),
  r: integer(),
  s: integer(),
  to: <<_::160>>,
  v: integer(),
  value: integer()
}

Functions

add_signature(transaction, arg)

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

Adds a signature to a transaction. This overwrites the [chain_id, 0, 0] fields, as per EIP-155.

Examples

iex> Cartouche.Transaction.V1.new(1, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, :kovan)
...> |> Cartouche.Transaction.V1.add_signature(<<1::256, 2::256, 3::8>>)
%Cartouche.Transaction.V1{
  nonce: 1,
  gas_price: 100000000000,
  gas_limit: 100000,
  to: <<1::160>>,
  value: 2,
  data: <<1, 2, 3>>,
  v: 3,
  r: 1,
  s: 2
}

decode(trx_enc)

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

Decode an RLP-encoded transaction.

Examples

iex> use Cartouche.Hex
iex> ~h[0xE80185174876E800830186A094000000000000000000000000000000000000000102830102032A8080]
...> |> Cartouche.Transaction.V1.decode()
{:ok, %Cartouche.Transaction.V1{
  nonce: 1,
  gas_price: 100000000000,
  gas_limit: 100000,
  to: <<1::160>>,
  value: 2,
  data: <<1, 2, 3>>,
  v: 42,
  r: 0,
  s: 0
}}

encode(v1)

@spec encode(t()) :: binary()

Build an RLP-encoded transaction. Note: transactions can be encoded before they are signed, which uses [chain_id, 0, 0] in the signature fields, otherwise those fields are [v, r, s].

Examples

iex> Cartouche.Transaction.V1.new(1, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, :kovan)
...> |> Cartouche.Transaction.V1.encode()
...> |> Base.encode16()
"E80185174876E800830186A094000000000000000000000000000000000000000102830102032A8080"

get_signature(v1)

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

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

Examples

iex> Cartouche.Transaction.V1.new(1, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, :kovan)
...> |> Cartouche.Transaction.V1.add_signature(<<1::256, 2::256, 3::8>>)
...> |> Cartouche.Transaction.V1.get_signature()
{:ok, <<1::256, 2::256, 3::8>>}

iex> Cartouche.Transaction.V1.new(1, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, :kovan)
...> |> Cartouche.Transaction.V1.add_signature(<<1::256, 2::256, 0x05f5e0ff::32>>)
...> |> Cartouche.Transaction.V1.get_signature()
{:ok, <<1::256, 2::256, 0x05f5e0ff::32>>}

iex> Cartouche.Transaction.V1.new(1, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, :kovan)
...> |> Cartouche.Transaction.V1.get_signature()
{:error, "transaction missing signature"}

new(nonce, gas_price, gas_limit, to, value, data, chain_id \\ nil)

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

Constructs a new V1 (Legacy) Ethereum transaction.

Examples

iex> Cartouche.Transaction.V1.new(1, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, :kovan)
%Cartouche.Transaction.V1{
  nonce: 1,
  gas_price: 100000000000,
  gas_limit: 100000,
  to: <<1::160>>,
  value: 2,
  data: <<1, 2, 3>>,
  v: 42,
  r: 0,
  s: 0
}

recover_signer(transaction, chain_id)

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

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

Examples

iex> {:ok, address} =
...> Cartouche.Transaction.V1.new(1, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, :kovan)
...> |> Cartouche.Transaction.V1.add_signature(<<1::256, 2::256, 3::8>>)
...> |> Cartouche.Transaction.V1.recover_signer(:kovan)
...> Cartouche.Hex.to_address(address)
"0x47643AC1194d7e8C6d04dD631D456137028bBc1F"

iex> Cartouche.Transaction.V1.new(1, {100, :gwei}, 100_000, <<1::160>>, {2, :wei}, <<1, 2, 3>>, :kovan)
...> |> Cartouche.Transaction.V1.recover_signer(:kovan)
{:error, "transaction missing signature"}