View Source ExWeb3 (ExWeb3 v0.2.1)

Helpers for common operations around EVM networks.

Link to this section Summary

Functions

Similar to :binary.decode_unsigned/1, except we decode <<>> back to 0, which is a common practice in Ethereum, since we cannot have any leading zeros.

Similar to :binary.encode_unsigned/1, except we encode 0 as <<>>, the empty string. This is because the specification says that we cannot have any leading zeros, and so having <<0>> by itself is leading with a zero and prohibited.

Create digest for Ethereum signature for a generic binary data.

Recovers a public key from a given signature structured as a hex string returned by ethers.js e.g

Simple wrapper for decoding ABI encoded hex data.

Simple wrapper for decoding hex data.

Simple wrapper for decoding hex data.

Simple wrapper for decoding hex data.

Generate random expirable challenge for signature.

Get Ethereum address from private or public key.

Get public key from private key.

Convert hex encoded string to integer.

Check if Ethereum login challenge has expired.

Validate if Ethereum challenge digest was signed with a proper wallet.

Returns the keccak sha256 of a given input.

Simply returns the rightmost n bits of a binary.

Simply returns the rightmost n bits of a binary.

Returned a binary padded to given length in bytes. Fails if binary is longer than desired length.

Recovers a public key from a given signature for a given digest. Note, the key is returned in DER format (with a leading 0x04 to indicate that it's an octet-string).

Signs message with private key, returns signature parts {r, s, v}

Simple wrapper to generate hex.

Encoder from binary to hex data.

Verifies a that a given signature for a given digest is valid against a public key.

Link to this section Types

@type keccak_hash() :: binary()

Link to this section Functions

@spec decode_unsigned(binary()) :: non_neg_integer()

Similar to :binary.decode_unsigned/1, except we decode <<>> back to 0, which is a common practice in Ethereum, since we cannot have any leading zeros.

examples

Examples

iex> ExWeb3.decode_unsigned(<<>>)
0
iex> ExWeb3.decode_unsigned(<<5>>)
5
iex> ExWeb3.decode_unsigned(<<76, 75, 64>>)
5_000_000
@spec encode_unsigned(non_neg_integer()) :: binary()

Similar to :binary.encode_unsigned/1, except we encode 0 as <<>>, the empty string. This is because the specification says that we cannot have any leading zeros, and so having <<0>> by itself is leading with a zero and prohibited.

examples

Examples

iex> ExWeb3.encode_unsigned(0)
<<>>
iex> ExWeb3.encode_unsigned(5)
<<5>>
iex> ExWeb3.encode_unsigned(5_000_000)
<<76, 75, 64>>
Link to this function

eth_hash_message!(message)

View Source
@spec eth_hash_message!(binary()) :: keccak_hash() | no_return()

Create digest for Ethereum signature for a generic binary data.

Link to this function

eth_recover(digest, hex_signature)

View Source
@spec eth_recover(binary(), String.t()) :: {:ok, binary()} | {:error, any()}

Recovers a public key from a given signature structured as a hex string returned by ethers.js e.g:

String.length("0x" <> signature <> recovery_id) == 67
Link to this function

eth_sign(message, private_key)

View Source
@spec eth_sign(binary(), binary()) ::
  {:error, atom()} | {:ok, {binary(), binary(), non_neg_integer()}}
@spec from_abi(String.t()) :: {integer(), <<_::256>>, <<_::256>>} | no_return()

Simple wrapper for decoding ABI encoded hex data.

@spec from_hex(String.t()) :: binary()

Simple wrapper for decoding hex data.

examples

Examples

iex> ExWeb3.from_hex("aabbcc")
<<0xaa, 0xbb, 0xcc>>
@spec from_prefixed_hex(String.t()) :: {:ok, binary()} | {:error, :invalid_hex}

Simple wrapper for decoding hex data.

examples

Examples

iex> ExWeb3.from_prefixed_hex("0xaabbcc")
{:ok, <<0xaa, 0xbb, 0xcc>>}
@spec from_prefixed_hex!(String.t()) :: binary()

Simple wrapper for decoding hex data.

examples

Examples

iex> ExWeb3.from_prefixed_hex!("0xaabbcc")
<<0xaa, 0xbb, 0xcc>>
Link to this function

gen_eth_challenge(intention, challenge_expiration \\ 300)

View Source
@spec gen_eth_challenge(String.t(), integer()) :: {String.t(), String.t(), integer()}

Generate random expirable challenge for signature.

@spec get_address(binary()) :: String.t()

Get Ethereum address from private or public key.

@spec get_public_key(binary()) :: binary()

Get public key from private key.

@spec hex_to_int!(String.t()) :: integer() | no_return()

Convert hex encoded string to integer.

examples

Examples

iex> ExWeb3.hex_to_int!("0x1")
1
iex> ExWeb3.hex_to_int!("0xA")
10
Link to this function

is_eth_signed_challenge_expired?(arg1)

View Source
@spec is_eth_signed_challenge_expired?(String.t()) :: boolean()

Check if Ethereum login challenge has expired.

Link to this function

is_eth_signed_challenge_valid?(wallet, challenge, raw_signature)

View Source
@spec is_eth_signed_challenge_valid?(String.t(), String.t(), String.t()) :: boolean()

Validate if Ethereum challenge digest was signed with a proper wallet.

@spec kec(binary()) :: keccak_hash()

Returns the keccak sha256 of a given input.

examples

Examples

iex> ExWeb3.kec("hello world")
<<71, 23, 50, 133, 168, 215, 52, 30, 94, 151, 47, 198, 119, 40, 99,
       132, 248, 2, 248, 239, 66, 165, 236, 95, 3, 187, 250, 37, 76, 176,
       31, 173>>
iex> ExWeb3.kec(<<0x01, 0x02, 0x03>>)
<<241, 136, 94, 218, 84, 183, 160, 83, 49, 140, 212, 30, 32, 147, 34,
       13, 171, 21, 214, 83, 129, 177, 21, 122, 54, 51, 168, 59, 253, 92,
       146, 57>>
@spec mask(integer(), integer()) :: integer()

Simply returns the rightmost n bits of a binary.

examples

Examples

iex> ExWeb3.mask(0b111101111, 6)
0b101111
iex> ExWeb3.mask(0b101101, 3)
0b101
iex> ExWeb3.mask(0b011, 1)
1
iex> ExWeb3.mask(0b011, 0)
0
iex> ExWeb3.mask(0b010, 1)
0
iex> ExWeb3.mask(0b010, 20)
0b010
@spec mask_bitstring(bitstring(), integer()) :: bitstring()

Simply returns the rightmost n bits of a binary.

examples

Examples

iex> ExWeb3.mask_bitstring(<<0b111101111>>, 6)
<<0b101111::size(6)>>
iex> ExWeb3.mask_bitstring(<<0b101101>>, 3)
<<0b101::size(3)>>
iex> ExWeb3.mask_bitstring(<<0b011>>, 1)
<<1::size(1)>>
iex> ExWeb3.mask_bitstring(<<0b011>>, 0)
<<>>
iex> ExWeb3.mask_bitstring(<<0b010>>, 1)
<<0::size(1)>>
iex> ExWeb3.mask_bitstring(<<0b010>>, 20)
<<0, 0, 2::size(4)>>
iex> ExWeb3.mask_bitstring(<<0b010>>, 3)
<<0b010::size(3)>>
iex> ExWeb3.mask_bitstring(<<>>, 3)
<<0b000::size(3)>>
Link to this function

pad(binary, desired_length)

View Source
@spec pad(binary(), integer()) :: binary()

Returned a binary padded to given length in bytes. Fails if binary is longer than desired length.

examples

Examples

iex> ExWeb3.pad(<<9>>, 5)
<<0, 0, 0, 0, 9>>
iex> ExWeb3.pad(<<9>>, 1)
<<9>>
iex> ExWeb3.pad(<<9, 9>>, 1)
** (RuntimeError) Binary too long for padding
Link to this function

recover(digest, signature, recovery_id)

View Source
@spec recover(binary(), binary(), integer()) :: {:ok, binary()} | {:error, String.t()}

Recovers a public key from a given signature for a given digest. Note, the key is returned in DER format (with a leading 0x04 to indicate that it's an octet-string).

Link to this function

sign(message, private_key)

View Source
@spec sign(binary(), binary()) ::
  {:error, atom()} | {:ok, {binary(), binary(), non_neg_integer()}}

Signs message with private key, returns signature parts {r, s, v}

@spec to_hex(binary()) :: String.t()

Simple wrapper to generate hex.

examples

Examples

iex> ExWeb3.to_hex(<<0xaa, 0xbb, 0xcc>>)
"aabbcc"
@spec to_prefixed_hex(binary()) :: String.t()

Encoder from binary to hex data.

examples

Examples

iex> ExWeb3.to_prefixed_hex(<<0xaa, 0xbb, 0xcc>>)
"0xaabbcc"
Link to this function

verify(digest, signature, public_key)

View Source
@spec verify(binary(), binary(), binary()) :: boolean()

Verifies a that a given signature for a given digest is valid against a public key.