elm-cardano/bech32/1.0.0

Bech32.Encode

Encoding arbitrary binary data to Bech32. A Bech32 string consists of:

  • The human-readable part (a.k.a prefix), which is intended to convey the type of data, or anything else that is relevant to the reader. This part MUST contain 1 to 83 US-ASCII characters, with each character having a value in the range [33-126].
  • The separator, which is always "1". Note that "1" is allowed inside the human-readable part and the last one in the string is the separator.
  • The data part, which is at least 6 characters long and only consists of alphanumeric characters excluding "1", "b", "i", and "o".

The last 6 characters of the data-part are an actual checksum of the payload and the prefix, which ensures integrity of the whole string. It also provides bases for an error-detection algorithm, which isn't implemented yet as part of this library.

Importantly, the checksum and the error-detection are most optimal for strings of length smaller than 90 characters. Generating longer strings work, but decreases the efficiency of the checksum in detecting corrupted strings.

{ prefix : String
, data : Bytes
}
-> Result EncodeFailure String

Encode a data payload as Bech32 using the given prefix. Note that we have the following property:

Result.andThen Bech32.decode (Bech32.encode { prefix, data }) == Ok({ prefix, data })
= UnexpectedCharacterInPrefix { culprit : Char }
| PrefixTooShort
{ minimum : Int
, currentLength : Int
}

Failures that might occur when encoding a value. Can be mostly ignored for well-known prefixes.

  • UnexpectedCharacterInPrefix: triggered when the prefix contains characters outside of the unicode range [33; 126]. That is, when it contains characters other than:

    !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~