yabase/core/encoding

Types

Variants for Base32 encoding.

pub opaque type Base32Variant

Variants for Base58 encoding.

pub opaque type Base58Variant

Variants for Base64 encoding.

pub opaque type Base64Variant

Variants for Base85 encoding.

pub opaque type Base85Variant

Re-export of error.CodecError so the historical import yabase/core/encoding.{type CodecError} shape keeps working. Other error types (Bech32Variant, Bech32Decoded, Base58CheckDecoded) are no longer re-exported — import them directly from yabase/core/error.

pub type CodecError =
  error.CodecError

A decoded value tagged with its detected encoding. Use decoded_encoding/1 and decoded_data/1 to inspect the contents — the constructor is package-private so the representation can evolve (e.g. add a multibase prefix field) without breaking external pattern matches.

pub opaque type Decoded

Represents a supported encoding scheme.

pub opaque type Encoding

A Gleam compilation target. Construct with target_erlang/0 or target_javascript/0 and pass to supports_target/2.

pub opaque type Target

Values

pub fn base10() -> Encoding
pub fn base16() -> Encoding
pub fn base2() -> Encoding
pub fn base32_clockwork() -> Encoding
pub fn base32_crockford() -> Encoding
pub fn base32_crockford_check() -> Encoding
pub fn base32_hex() -> Encoding
pub fn base32_rfc4648() -> Encoding
pub fn base32_z_base32() -> Encoding
pub fn base36() -> Encoding
pub fn base45() -> Encoding
pub fn base58_bitcoin() -> Encoding
pub fn base58_check(version: Int) -> Encoding

Smart constructor for the Base58Check encoding. The version byte is part of the checksummed payload — decode_as rejects any wire whose embedded version does not match, surfacing the mismatch as InvalidChecksum.

pub fn base58_flickr() -> Encoding
pub fn base62() -> Encoding
pub fn base64_dq() -> Encoding
pub fn base64_no_padding() -> Encoding
pub fn base64_standard() -> Encoding
pub fn base64_url_safe() -> Encoding
pub fn base64_url_safe_no_padding() -> Encoding
pub fn base8() -> Encoding
pub fn base85_adobe() -> Encoding
pub fn base85_btoa() -> Encoding
pub fn base85_rfc1924() -> Encoding
pub fn base85_z85() -> Encoding
pub fn base91() -> Encoding
pub fn bech32(hrp: String) -> Encoding

Smart constructor for Bech32 (BIP 173 — the original variant). The hrp (human-readable part) is the protocol-specific prefix emitted before the checksummed payload (e.g. "bc" for Bitcoin mainnet, "npub" for Nostr public keys).

pub fn bech32m(hrp: String) -> Encoding

Smart constructor for Bech32m (BIP 350 — the improved variant). Same wire shape as Bech32 but with a different checksum constant to detect cross-variant decoding.

pub fn decode_as(
  enc: Encoding,
  value: String,
) -> Result(BitArray, error.CodecError)

Decode a string using the specified encoding.

pub fn decoded_data(decoded: Decoded) -> BitArray

The decoded raw bytes.

pub fn decoded_encoding(decoded: Decoded) -> Encoding

The encoding that was auto-detected when this value was decoded.

pub fn encode(
  enc: Encoding,
  data: BitArray,
) -> Result(String, error.CodecError)

Encode data using the specified encoding.

pub fn from_multibase_prefix(
  prefix: String,
) -> Result(Encoding, Nil)

Map a multibase prefix character to its Encoding.

pub fn is_javascript_safe(enc: Encoding) -> Bool

True if the encoding produces correct results on the JavaScript target for inputs of any size.

Encodings whose internals rely on arbitrary-precision integer arithmetic (base8, base10, base32 Crockford / CrockfordCheck, base36, base58 Bitcoin / Flickr, base62) inherit JavaScript’s Number.MAX_SAFE_INTEGER (2^53 - 1) ceiling and may produce incorrect output for inputs that represent integers above that bound — this returns False for them.

Byte-oriented encodings (base2, base16, base32 RFC4648 / Hex / Clockwork / ZBase32, base45, base64 *, base85 *, base91) are correct on both targets — this returns True for them.

pub fn multibase_name(enc: Encoding) -> String

Human-readable name for an Encoding.

pub fn multibase_prefix(enc: Encoding) -> Result(String, Nil)

Map an Encoding to its official multibase prefix character.

pub fn normalise_for_multibase_prefix(
  enc: Encoding,
  encoded: String,
) -> String

Lowercase the dispatcher’s output for encodings whose multibase prefix pins lowercase output (currently Base16 under prefix f).

pub fn supports_target(enc: Encoding, target: Target) -> Bool

True if the encoding works correctly on the given target.

All encodings work on the Erlang target (BEAM has bignum integers). On JavaScript, this delegates to is_javascript_safe/1.

Useful for filtering an Encoding value picked at runtime — for example, after multibase.decode auto-detects the encoding from a prefix supplied by an untrusted source — before running the decoded payload through downstream logic.

pub fn target_erlang() -> Target

The Erlang/BEAM target.

pub fn target_javascript() -> Target

The JavaScript target (Node.js or browser).

Search Document