View Source ExWeb3 (ExWeb3 v0.1.2)
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>>
@spec eth_hash_message!(binary()) :: keccak_hash() | no_return()
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:
String.length("0x" <> signature <> recovery_id) == 67
Simple wrapper for decoding ABI encoded hex data.
Simple wrapper for decoding hex data.
examples
Examples
iex> ExWeb3.from_hex("aabbcc")
<<0xaa, 0xbb, 0xcc>>
Simple wrapper for decoding hex data.
examples
Examples
iex> ExWeb3.from_prefixed_hex("0xaabbcc")
{:ok, <<0xaa, 0xbb, 0xcc>>}
Simple wrapper for decoding hex data.
examples
Examples
iex> ExWeb3.from_prefixed_hex!("0xaabbcc")
<<0xaa, 0xbb, 0xcc>>
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.
examples
Examples
iex> ExWeb3.hex_to_int!("0x1")
1
iex> ExWeb3.hex_to_int!("0xA")
10
Check if Ethereum login challenge has expired.
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>>
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
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)>>
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
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.
examples
Examples
iex> ExWeb3.to_hex(<<0xaa, 0xbb, 0xcc>>)
"aabbcc"
Encoder from binary to hex data.
examples
Examples
iex> ExWeb3.to_prefixed_hex(<<0xaa, 0xbb, 0xcc>>)
"0xaabbcc"
Verifies a that a given signature for a given digest is valid against a public key.