# `Cartouche.Transaction.V1`
[🔗](https://github.com/zenhive/cartouche/blob/main/lib/cartouche/transaction.ex#L8)

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

# `t`

```elixir
@type t() :: %Cartouche.Transaction.V1{
  data: binary(),
  gas_limit: integer(),
  gas_price: integer(),
  nonce: integer(),
  r: integer(),
  s: integer(),
  to: &lt;&lt;_::160&gt;&gt;,
  v: integer(),
  value: integer()
}
```

# `add_signature`

```elixir
@spec add_signature(t(), &lt;&lt;_::512, _::_*8&gt;&gt;) :: 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`

```elixir
@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`

```elixir
@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`

```elixir
@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`

```elixir
@spec new(
  integer(),
  integer() | {integer(), :wei | :gwei} | nil,
  integer(),
  &lt;&lt;_::160&gt;&gt;,
  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`

```elixir
@spec recover_signer(t(), atom() | integer()) ::
  {:ok, &lt;&lt;_::160&gt;&gt;} | {: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"}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
