Module Tezos_crypto.Base58

Prefixed Base58Check encodings

module Prefix : sig ... end
type data = ..

An extensible sum-type for decoded data: one case per known "prefix". See for instance Hash.Block_hash.Hash or Environment.Ed25519.Public_key_hash.

type 'a encoding = private {
  1. prefix : string;
  2. length : int;
  3. encoded_prefix : string;
  4. encoded_length : int;
  5. to_raw : 'a -> string;
  6. of_raw : string -> 'a option;
  7. wrap : 'a -> data;
}

Abstract representation of registered encodings. The type parameter is the type of the encoded data, for instance Hash.Block_hash.t.

val register_encoding : prefix:string -> length:int -> to_raw:('a -> string) -> of_raw:(string -> 'a option) -> wrap:('a -> data) -> 'a encoding

Register a new encoding. The function might raise Invalid_arg if the provided prefix overlaps with a previously registered prefix. The to_raw and of_raw are the ad-hoc serialisation/deserialisation for the data. The wrap should wrap the deserialised value into the extensible sum-type data (see the generic function decode).

The registered of_raw returns None, without calling the provided one, on a raw payload whose length is not length: decoding a checksum-valid string with the right prefix but a payload of the wrong length fails instead of raising.

val check_encoded_prefix : 'a encoding -> string -> int -> unit

Checks that an encoding has a certain prefix and length.

val max_encoded_size : unit -> int

max_encoded_size () is the length of the longest Base58Check encoding currently registered. Note that the result depends on the set of encodings registered at the time of the call.

module Alphabet : sig ... end
val simple_encode : ?alphabet:Alphabet.t -> 'a encoding -> 'a -> string

Encoder for a given kind of data.

val simple_decode : ?alphabet:Alphabet.t -> 'a encoding -> string -> 'a option

Decoder for a given kind of data. It returns None when the decoded data does not start with the expected prefix.

Inputs longer than the encoding's own encoded_length — which a valid input for that specific encoding can never exceed — are rejected before decoding.

val decode : ?alphabet:Alphabet.t -> string -> data option

Generic decoder. It returns None when the decoded data does not start with a registered prefix.

Inputs are bounded to max_encoded_size, the longest currently registered encoding — which no valid decode of any registered type can ever exceed — and, independently, to encoded_max_length. Inputs longer than these bounds are rejected before decoding.

Completion of partial Base58Check value

val register_resolver : 'a encoding -> (string -> 'a list Lwt.t) -> unit

Register a (global) resolver for a previously registered kind of data.

val complete : ?alphabet:Alphabet.t -> string -> string list Lwt.t

Try to complete a prefix of a Base58Check encoded data, by using the previously registered resolver associated to this kind of data. Note that a prefix of n characters of a Base58-encoded value provides at least n/2 bytes of a prefix of the original value.

Low-level: distinct registering function for economic protocol

module Make (C : sig ... end) : sig ... end

See src/environment/v1/base58.mli for an inlined documentation.

module Internal_for_tests : sig ... end

Low-level Base58Check encodings

val safe_encode : ?alphabet:Alphabet.t -> string -> string

Base58Check-encoding/decoding functions (with error detections).

val safe_decode : ?alphabet:Alphabet.t -> string -> string option

Inputs longer than encoded_max_length are rejected before decoding.

val raw_encode : ?alphabet:Alphabet.t -> string -> string

Base58-encoding/decoding functions (without error detections).

val raw_decode : ?alphabet:Alphabet.t -> string -> string option

Inputs longer than encoded_max_length are rejected before decoding, without attempting to decode them.

val encoded_max_length : int

Upper bound on the length of an input accepted by the decoders by default, and on the encoded length of any encoding that register_encoding accepts.