View Source Signet.Util (Signet v0.1.0-rc1)

Link to this section Summary

Functions

Decodes a hex string, specifically requiring that the string begins with 0x and allows mixed-case typing.

Decodes hex, allowing it to either by "0x..." or <<1::160>>.

Encodes a number as a binary representation of a certain number of bytes.

Encodes a hex string, adding a 0x prefix.

Returns an Ethereum address from a given DER-encoded public key.

Pads a binary to a given length

Parses a chain id, which can be given as an integer or an atom of a known network.

Converts a number to wei, possibly from gwei, etc.

Link to this section Functions

Decodes a hex string, specifically requiring that the string begins with 0x and allows mixed-case typing.

examples

Examples

iex> Signet.Util.decode_hex("0x1122")

iex> Signet.Util.decode_hex("0x1")

iex> Signet.Util.decode_hex("0xGG") :error

Decodes hex, allowing it to either by "0x..." or <<1::160>>.

Note: a hex-printed string, in this case, must start with 0x,

  otherwise it will be interpreted as its ASCII values.

examples

Examples

iex> Signet.Util.decode_hex_input!("0x55")
<<0x55>>

iex> Signet.Util.decode_hex_input!(<<0x55>>)
<<0x55>>

Encodes a number as a binary representation of a certain number of bytes.

examples

Examples

iex> Signet.Util.encode_bytes(257, 4) <<0, 0, 1, 1>>

iex> Signet.Util.encode_bytes(nil, 4) nil

Link to this function

encode_hex(hex, short \\ false)

View Source

Encodes a hex string, adding a 0x prefix.

Note: if short is set, then any leading zeros will be stripped.

examples

Examples

iex> Signet.Util.encode_hex(<<0x11, 0x22>>) "0x1122"

iex> Signet.Util.encode_hex(<<0xc>>) "0x0C"

iex> Signet.Util.encode_hex(<<0xc>>, true) "0xC"

iex> Signet.Util.encode_hex(<<0x0>>, true) "0x0"

Link to this function

get_eth_address(public_key)

View Source

Returns an Ethereum address from a given DER-encoded public key.

examples

Examples

iex> public_key = Signet.Util.decode_hex!("0x0422") iex> Signet.Util.get_eth_address(public_key) |> Signet.Util.encode_hex() "0x759F1AFDC24ABA433A3E18B683F8C04A6EAA69F0"

See Signet.Hash.keccak/1.

Pads a binary to a given length

examples

Examples

iex> Signet.Util.pad(<<1, 2>>, 2) <<1, 2>>

iex> Signet.Util.pad(<<1, 2>>, 4) <<0, 0, 1, 2>>

iex> Signet.Util.pad(<<1, 2>>, 1) ** (FunctionClauseError) no function clause matching in Signet.Util.pad/2

Link to this function

parse_chain_id(chain_id)

View Source

Parses a chain id, which can be given as an integer or an atom of a known network.

examples

Examples

iex> Signet.Util.parse_chain_id(5)
5

iex> Signet.Util.parse_chain_id(:goerli)
5
@spec to_wei(integer() | {integer(), :gwei}) :: number()

Converts a number to wei, possibly from gwei, etc.

examples

Examples

iex> Signet.Util.to_wei(100)
100

iex> Signet.Util.to_wei({100, :gwei})
100000000000