# `Cartouche.RecoveryBit`
[🔗](https://github.com/zenhive/cartouche/blob/main/lib/cartouche/recovery_bit.ex#L1)

There are a number of ways to look at recovery bits. Either:

* `:base`: In the range `{0,1}`, which are the outputs of a signer library
* `:ethereum`: In the range `{27,28}`, as defined in the yellow paper
* `:eip155`: In the range `{35+chain_id*2,35+chain_id*2+1}`, as defined in EIP-155

This module provides tools between switching through these choices.

# `rec_type`

```elixir
@type rec_type() :: :base | :ethereum | :eip155
```

# `normalize`

```elixir
@spec normalize(non_neg_integer(), rec_type()) :: non_neg_integer() | no_return()
```

Normalizes a binary-encoded signature to the given requested type,
i.e. `:base`, `:ethereum`, or `:eip155`.

## Examples

    iex> Cartouche.RecoveryBit.normalize(28, :ethereum)
    28

    iex> Cartouche.RecoveryBit.normalize(1, :ethereum)
    28

    iex> Cartouche.RecoveryBit.normalize(27, :base)
    0

# `normalize_signature`

```elixir
@spec normalize_signature(Cartouche.signature(), rec_type()) ::
  Cartouche.signature() | no_return()
```

Normalizes a binary-encoded signature to the given requested type,
i.e. `:base`, `:ethereum`, or `:eip155`.

## Examples

    iex> Cartouche.RecoveryBit.normalize_signature(<<1::256, 2::256, 28>>, :ethereum)
    <<1::256, 2::256, 28>>

    iex> Cartouche.RecoveryBit.normalize_signature(<<1::256, 2::256, 1>>, :ethereum)
    <<1::256, 2::256, 28>>

    iex> Cartouche.RecoveryBit.normalize_signature(<<1::256, 2::256, 27>>, :base)
    <<1::256, 2::256, 0>>

# `recover_base`

```elixir
@spec recover_base(non_neg_integer()) :: 0 | 1 | no_return()
```

Normalizes a recovery bit to be either 0 or 1.

## Examples

    iex> Cartouche.RecoveryBit.recover_base(0)
    0

    iex> Cartouche.RecoveryBit.recover_base(1)
    1

    iex> Cartouche.RecoveryBit.recover_base(27)
    0

    iex> Cartouche.RecoveryBit.recover_base(28)
    1

    iex> Cartouche.RecoveryBit.recover_base(2)
    ** (FunctionClauseError) no function clause matching in Cartouche.RecoveryBit.recover_base/1

---

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