View Source Web3.Type.Address (web3ex v0.1.0)
The address (40 (hex) characters / 160 bits / 20 bytes) is derived from the public key (128 (hex) characters / 512 bits / 64 bytes) which is derived from the private key (64 (hex) characters / 256 bits / 32 bytes).
The address is actually the last 40 characters of the keccak-256 hash of the public key with 0x
appended.
Copy from Blockscout
Link to this section Summary
Functions
Casts term
to t/0
.
Dumps the binary hash to :binary
(bytea
) format used in database.
Callback implementation for Ecto.Type.embed_as/1
.
Callback implementation for Ecto.Type.equal?/2
.
Loads the binary hash from the database.
The underlying database type: binary
. binary
is used because no Postgres integer type is 20 bytes long.
Validates a hexadecimal encoded string to see if it conforms to an address.
Link to this section Types
@type t() :: %Web3.Type.Hash{byte_count: 20, bytes: <<_::160>>}
A 20-byte hash of the address public key.
Link to this section Functions
Casts term
to t/0
.
If the term
is already in t/0
, then it is returned
iex> Web3.Type.Address.cast(
...> %Web3.Type.Hash{
...> byte_count: 20,
...> bytes: <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
...> }
...> )
{
:ok,
%Web3.Type.Hash{
byte_count: 20,
bytes: <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
}
}
If the term
is an non_neg_integer
, then it is converted to t/0
iex> Web3.Type.Address.cast(0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed)
{
:ok,
%Web3.Type.Hash{
byte_count: 20,
bytes: <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
}
}
If the non_neg_integer
is too large, then :error
is returned.
iex> Web3.Type.Address.cast(0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b)
:error
If the term
is a String.t
that starts with 0x
, then is converted to an integer and then to t/0
.
iex> Web3.Type.Address.cast("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed")
{
:ok,
%Web3.Type.Hash{
byte_count: 20,
bytes: <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
}
}
While non_neg_integers
don't have to be the correct width (because zero padding it difficult with numbers),
String.t
format must always have 40 digits after the 0x
base prefix.
iex> Web3.Type.Address.cast("0x0")
:error
Dumps the binary hash to :binary
(bytea
) format used in database.
If the field from the struct is t/0
, then it succeeds
iex> Web3.Type.Address.dump(
...> %Web3.Type.Hash{
...> byte_count: 20,
...> bytes: <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
...> }
...> )
{:ok, <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>}
If the field from the struct is an incorrect format such as Web3.Type.Hash.t/0
, :error
is returned
iex> Web3.Type.Address.dump(
...> %Web3.Type.Hash{
...> byte_count: 32,
...> bytes: <<0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b ::
...> big-integer-size(32)-unit(8)>>
...> }
...> )
:error
Callback implementation for Ecto.Type.embed_as/1
.
Callback implementation for Ecto.Type.equal?/2
.
Loads the binary hash from the database.
If the binary hash is the correct format, it is returned.
iex> Web3.Type.Address.load(
...> <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
...> )
{
:ok,
%Web3.Type.Hash{
byte_count: 20,
bytes: <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
}
}
If the binary hash is an incorrect format, such as if an Web3.Type.Hash
field is loaded, :error
is returned.
iex> Web3.Type.Address.load(
...> <<0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b :: big-integer-size(32)-unit(8)>>
...> )
:error
@spec type() :: :binary
The underlying database type: binary
. binary
is used because no Postgres integer type is 20 bytes long.
@spec validate(String.t()) :: {:ok, String.t()} | {:error, :invalid_length | :invalid_characters | :invalid_checksum}
Validates a hexadecimal encoded string to see if it conforms to an address.
error-descriptions
Error Descriptions
:invalid_characters
- String used non-hexadecimal characters:invalid_checksum
- Mixed-case string didn't pass EIP-55 checksum:invalid_length
- Addresses are expected to be 40 hex characters long
example
Example
iex> Web3.Type.Address.validate("0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d")
{:ok, "0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232d"}
iex> Web3.Type.Address.validate("0xc1912fEE45d61C87Cc5EA59DaE31190FFFFf232H")
{:error, :invalid_characters}