Base58 encoding and decoding using the Bitcoin alphabet.
Solana addresses, transaction signatures, and blockhashes are Base58
strings over the Bitcoin alphabet
(123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz — no 0,
O, I, or l). Leading zero bytes encode as leading 1 characters,
one per byte, matching the reference implementations.
Pure functions with no dependencies; used by X402.Solana and
X402.Scheme.ExactSVM.
Summary
Functions
Decodes a Base58 string, returning :error for invalid characters.
Encodes a binary as a Base58 string.
Types
@type encoded() :: String.t()
A Base58-encoded string.
Functions
Decodes a Base58 string, returning :error for invalid characters.
Examples
iex> X402.Base58.decode("2g")
{:ok, "a"}
iex> X402.Base58.decode("a3gV")
{:ok, "bbb"}
iex> X402.Base58.decode("1111111111")
{:ok, <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>}
iex> X402.Base58.decode("")
{:ok, ""}
iex> X402.Base58.decode("invalid0base58")
:error
iex> X402.Base58.decode(:not_a_string)
:error
Encodes a binary as a Base58 string.
Test vectors are the Bitcoin Core base58_encode_decode.json suite.
Examples
iex> X402.Base58.encode("")
""
iex> X402.Base58.encode(<<0x61>>)
"2g"
iex> X402.Base58.encode(<<0x62, 0x62, 0x62>>)
"a3gV"
iex> X402.Base58.encode(<<0x51, 0x6B, 0x6F, 0xCD, 0x0F>>)
"ABnLTmg"
iex> X402.Base58.encode("simply a long string")
"2cFupjhnEsSn59qHXstmK2ffpLv2"
iex> X402.Base58.encode(<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>)
"1111111111"