elm-cardano/bech32/1.0.0

Bech32.Decode

Decoding Bech32 strings to binary data. 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.

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

Decode a Bech32 string into its internal constituents. Note that we have the following property:

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

Failures that might occur when decoding a value.

= ExcessPadding
| NonZeroPadding

Failure to decode words into bytes; should never occur in practice because Elm's Bytes are well-formed by construction