Cartouche.RecoveryBit (Cartouche v0.2.0)

Copy Markdown View Source

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.

Summary

Functions

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

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

Normalizes a recovery bit to be either 0 or 1.

Types

rec_type()

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

Functions

normalize(recovery_bit, rec_type \\ :eip155)

@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(arg, rec_type \\ :eip155)

@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(v)

@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