View Source Signet.Util (Signet v1.3.8)

Summary

Functions

Checksums an Ethereum address per EIP-55, the result is a string-encoded version of the address.

Decodes hex if the size is exactly one 20-byte address, otherwise raises.

decode_hex(arg) deprecated

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

decode_hex!(hex) deprecated

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>>.

Decodes hex to an integer.

Decodes hex but requires the result be a given set of bytes, or otherwise raises.

decode_word!(hex) deprecated

Decodes hex if the size is exactly one 32-byte word, otherwise raises.

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.

Returns the nibbles of a binary as a list.

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.

Functions

Link to this function

checksum_address(address)

View Source

Checksums an Ethereum address per EIP-55, the result is a string-encoded version of the address.

Examples

iex> Signet.Util.checksum_address("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
"0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"

iex> Signet.Util.checksum_address("0xFB6916095CA1DF60BB79CE92CE3EA74C37C5D359")
"0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"

iex> Signet.Util.checksum_address("0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb")
"0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"

iex> Signet.Util.checksum_address("0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb")
"0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"
This function is deprecated. Use Signet.Hex.decode_address!/2 instead.
@spec decode_address!(String.t()) :: <<_::160>> | no_return()

Decodes hex if the size is exactly one 20-byte address, otherwise raises.

Examples

iex> Signet.Util.decode_address!("0x00112233445566778899aabbccddeeff00112233") <<0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00,0x11,0x22,0x33>>

iex> Signet.Util.decode_address!("0x1122") ** (RuntimeError) mismatch byte size. expected 20, got: 2

This function is deprecated. Use Signet.Hex.decode_hex/1 instead.
@spec decode_hex(String.t()) :: {:ok, binary()} | :error

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

Examples

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

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

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

This function is deprecated. Use Signet.Hex.decode_hex!/1 instead.
@spec decode_hex!(String.t()) :: binary() | no_return()

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

Similar to decode_hex/1, but raises on error

Examples

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

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

iex> Signet.Util.decode_hex!("0xGG") ** (RuntimeError) invalid hex

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

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

iex> Signet.Util.decode_hex_input!(<<0x55>>)
<<0x55>>
This function is deprecated. Use Signet.Hex.decode_hex_number!/2 instead.
@spec decode_hex_number!(String.t()) :: integer() | no_return()

Decodes hex to an integer.

Examples

iex> Signet.Util.decode_hex_number!("0x11223344") 0x11223344

Link to this function

decode_sized_hex!(hex, size)

View Source
This function is deprecated. Use Signet.Hex.decode_sized!/2 instead.
@spec decode_sized_hex!(String.t(), integer()) :: binary() | no_return()

Decodes hex but requires the result be a given set of bytes, or otherwise raises.

Examples

iex> Signet.Util.decode_sized_hex!("0x1122", 2) <<0x11, 0x22>>

iex> Signet.Util.decode_sized_hex!("0x1122", 3) ** (RuntimeError) mismatch byte size. expected 3, got: 2

iex> Signet.Util.decode_sized_hex!("0xGG", 3) ** (RuntimeError) invalid hex

This function is deprecated. Use Signet.Hex.decode_word!/2 instead.
@spec decode_word!(String.t()) :: <<_::256>> | no_return()

Decodes hex if the size is exactly one 32-byte word, otherwise raises.

Examples

iex> Signet.Util.decode_word!("0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff") <<0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff>>

iex> Signet.Util.decode_word!("0x1122") ** (RuntimeError) mismatch byte size. expected 32, got: 2

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

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
This function is deprecated. Use Signet.Hex.encode_short_hex/1 instead.

Encodes a hex string, adding a 0x prefix.

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

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

iex> use Signet.Hex iex> public_key = ~h[0x0422] iex> Signet.Util.get_eth_address(public_key) ...> |> Signet.Hex.encode_hex() "0x759f1afdc24aba433a3e18b683f8c04a6eaa69f0"

See Signet.Hash.keccak/1.

Returns the nibbles of a binary as a list.

Examples

iex> Signet.Util.nibbles(<<0xF5,0xE6,0xD0>>)
[0xF, 0x5, 0xE, 0x6, 0xD, 0x0]

Pads a binary to a given length

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

iex> Signet.Util.parse_chain_id(5)
5

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

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

Examples

iex> Signet.Util.to_wei(100)
100

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