bech32-1.0.0: Implementation of the Bech32 cryptocurrency address format (BIP 0173).

Copyright© 2017 Marko Bencun 2018-2019 IOHK
LicenseApache-2.0
Safe HaskellNone
LanguageHaskell2010

Codec.Binary.Bech32.Internal

Contents

Description

Implementation of the Bech32 address format.

From an original implementation by Marko Bencun:

sipa/bech32

Synopsis

Encoding & Decoding

encode :: HumanReadablePart -> DataPart -> Either EncodingError Text Source #

Encode a human-readable string and data payload into a Bech32 string.

encodeLenient :: HumanReadablePart -> DataPart -> Text Source #

Like encode but allows output to be longer than 90 characters. This isn't ideal as the error detection becomes worse as string get longer but it's still acceptable.

From BIP-0173:

Even though the chosen code performs reasonably well up to 1023 characters, other designs are preferable for lengths above 89 characters (excluding the separator).

data EncodingError Source #

Represents the set of error conditions that may occur while encoding a Bech32 string.

Constructors

EncodedStringTooLong 

decode :: Text -> Either DecodingError (HumanReadablePart, DataPart) Source #

Decode a Bech32 string into a human-readable part and data part.

decodeLenient :: Text -> Either DecodingError (HumanReadablePart, DataPart) Source #

Like decode but does not enforce a maximum length. See also encodeLenient for details.

data DecodingError Source #

Represents the set of errors that may occur while decoding a Bech32 string with the decode function.

Constructors

StringToDecodeTooLong 
StringToDecodeTooShort 
StringToDecodeHasMixedCase 
StringToDecodeMissingSeparatorChar 
StringToDecodeContainsInvalidChars [CharPosition]

In cases where it is possible to determine the exact locations of erroneous characters, this list will encode those locations. Clients can use this information to provide user feedback. In cases where it isn't possible to reliably determine the locations of erroneous characters, this list will be empty.

checksumLength :: Int Source #

The length of the checksum portion of an encoded string, in bytes.

encodedStringMaxLength :: Int Source #

The maximum length of an encoded string, in bytes. This length includes the human-readable part, the separator character, the encoded data portion, and the checksum.

encodedStringMinLength :: Int Source #

The minimum length of an encoded string, in bytes. This length includes the human-readable part, the separator character, the encoded data portion, and the checksum.

separatorChar :: Char Source #

The separator character. This character appears immediately after the human-readable part and before the data part.

separatorLength :: Int Source #

The length of the separator portion of an encoded string, in bytes.

Data Part

data DataPart Source #

Represents the data part of a Bech32 string, as defined here: https://git.io/fj8FS

dataPartIsValid :: DataPart -> Bool Source #

Returns true iff. the specified DataPart is valid.

dataPartFromBytes :: ByteString -> DataPart Source #

Constructs a DataPart from a ByteString.

This function encodes a ByteString in such a way that guarantees it can be successfully decoded with the dataPartToBytes function:

dataPartToBytes (dataPartFromBytes b) == Just b

dataPartFromText :: Text -> Maybe DataPart Source #

Constructs a DataPart from textual input. All characters in the input must be a member of dataCharList, the set of characters permitted to appear within the data part of a Bech32 string.

Returns Nothing if any character in the input is not a member of dataCharList.

This function guarantees to satisfy the following property:

dataPartFromText (dataPartToText d) == Just d

dataPartFromWords :: [Word5] -> DataPart Source #

Construct a DataPart directly from words.

This function guarantees to satisfy the following properties:

dataPartFromWords (dataPartToWords d) == d
dataPartToWords (dataPartFromWords w) == w

dataPartToBytes :: DataPart -> Maybe ByteString Source #

Attempts to extract a ByteString from a DataPart.

This function guarantees to satisfy the following property:

dataPartToBytes (dataPartFromBytes b) == Just b

dataPartToText :: DataPart -> Text Source #

Converts a DataPart to Text, using the Bech32 character set to render the data.

This function guarantees to satisfy the following property:

dataPartFromText (dataPartToText d) == Just d

dataPartToWords :: DataPart -> [Word5] Source #

Convert a DataPart into words.

dataCharToWord :: Char -> Maybe Word5 Source #

If the specified character is permitted to appear within the data part of a Bech32 string, this function returns that character's corresponding Word5 value. If the specified character is not permitted, or if the specified character is upper-case, returns Nothing.

dataCharFromWord :: Word5 -> Char Source #

Maps the specified Word5 onto a character that is permitted to appear within the data part of a Bech32 string.

dataCharList :: String Source #

A list of all characters that are permitted to appear within the data part of a Bech32 string. See here for more details: https://git.io/fj8FS

Human-Readable Part

humanReadablePartFromText :: Text -> Either HumanReadablePartError HumanReadablePart Source #

Parses the human-readable part of a Bech32 string, as defined here: https://git.io/fj8FS

humanReadablePartToText :: HumanReadablePart -> Text Source #

Get the raw text of the human-readable part of a Bech32 string.

humanReadablePartToWords :: HumanReadablePart -> [Word5] Source #

Convert the specified human-readable part to a list of words.

humanReadablePartMinLength :: Int Source #

The shortest length permitted for the human-readable part of a Bech32 string.

humanReadablePartMaxLength :: Int Source #

The longest length permitted for the human-readable part of a Bech32 string.

humanReadableCharMinBound :: Char Source #

The lower bound of the set of characters permitted to appear within the human-readable part of a Bech32 string.

humanReadableCharMaxBound :: Char Source #

The upper bound of the set of characters permitted to appear within the human-readable part of a Bech32 string.

Bit Manipulation

convertBits :: Functor f => [Word] -> Int -> Int -> Pad f -> f [Word] Source #

Big-endian conversion of a word string from base '2^frombits' to base '2^tobits'. The frombits and twobits parameters must be positive, while '2^frombits' and '2^tobits' must be smaller than the size of Word. Every value in dat must be strictly smaller than '2^frombits'.

data Word5 Source #

Instances
Bounded Word5 Source # 
Instance details

Defined in Codec.Binary.Bech32.Internal

Enum Word5 Source # 
Instance details

Defined in Codec.Binary.Bech32.Internal

Eq Word5 Source # 
Instance details

Defined in Codec.Binary.Bech32.Internal

Methods

(==) :: Word5 -> Word5 -> Bool #

(/=) :: Word5 -> Word5 -> Bool #

Ord Word5 Source # 
Instance details

Defined in Codec.Binary.Bech32.Internal

Methods

compare :: Word5 -> Word5 -> Ordering #

(<) :: Word5 -> Word5 -> Bool #

(<=) :: Word5 -> Word5 -> Bool #

(>) :: Word5 -> Word5 -> Bool #

(>=) :: Word5 -> Word5 -> Bool #

max :: Word5 -> Word5 -> Word5 #

min :: Word5 -> Word5 -> Word5 #

Show Word5 Source # 
Instance details

Defined in Codec.Binary.Bech32.Internal

Methods

showsPrec :: Int -> Word5 -> ShowS #

show :: Word5 -> String #

showList :: [Word5] -> ShowS #

Ix Word5 Source # 
Instance details

Defined in Codec.Binary.Bech32.Internal

word5 :: Integral a => a -> Word5 Source #

Character Manipulation