Copyright | © 2017 Marko Bencun 2019-2020 IOHK |
---|---|
License | Apache-2.0 |
Safe Haskell | None |
Language | Haskell2010 |
Implementation of the Bech32 address format.
Based on an original implementation by Marko Bencun.
Synopsis
- encode :: HumanReadablePart -> DataPart -> Either EncodingError Text
- decode :: Text -> Either DecodingError (HumanReadablePart, DataPart)
- data EncodingError = EncodedStringTooLong
- data DecodingError
- data DataPart
- dataPartFromWords :: [Word5] -> DataPart
- dataPartToWords :: DataPart -> [Word5]
- dataPartFromBytes :: ByteString -> DataPart
- dataPartToBytes :: DataPart -> Maybe ByteString
- dataPartFromText :: Text -> Maybe DataPart
- dataPartToText :: DataPart -> Text
- data HumanReadablePart
- humanReadablePartFromText :: Text -> Either HumanReadablePartError HumanReadablePart
- humanReadablePartToText :: HumanReadablePart -> Text
- data HumanReadablePartError
- newtype CharPosition = CharPosition Int
- data Word5
- encodeLenient :: HumanReadablePart -> DataPart -> Text
- decodeLenient :: Text -> Either DecodingError (HumanReadablePart, DataPart)
- dataCharList :: String
- humanReadablePartMinLength :: Int
- humanReadablePartMaxLength :: Int
- humanReadableCharMinBound :: Char
- humanReadableCharMaxBound :: Char
- encodedStringMaxLength :: Int
- encodedStringMinLength :: Int
- separatorChar :: Char
Basic Usage
Encoding
encode :: HumanReadablePart -> DataPart -> Either EncodingError Text Source #
Encode a Bech32 string from a human-readable prefix and data payload.
Example
>>>
import Prelude
>>>
import Codec.Binary.Bech32
>>>
import Data.Text.Encoding
First, prepare a human-readable prefix:
>>>
Right prefix = humanReadablePartFromText "example"
Next, prepare a data payload:
>>>
messageToEncode = "Lorem ipsum dolor sit amet!"
>>>
dataPart = dataPartFromBytes $ encodeUtf8 messageToEncode
Finally, produce a Bech32 string:
>>>
encode prefix dataPart
Right "example1f3hhyetdyp5hqum4d5sxgmmvdaezqumfwssxzmt9wsss9un3cx"
Decoding
decode :: Text -> Either DecodingError (HumanReadablePart, DataPart) Source #
Decode a Bech32 string into a human-readable prefix and data payload.
Example
>>>
import Prelude
>>>
import Codec.Binary.Bech32
>>>
import Data.Text.Encoding
First, decode the input:
>>>
input = "example1f3hhyetdyp5hqum4d5sxgmmvdaezqumfwssxzmt9wsss9un3cx"
>>>
Right (prefix, dataPart) = decode input
Next, examine the decoded human-readable prefix:
>>>
humanReadablePartToText prefix
"example"
Finally, examine the decoded data payload:
>>>
decodeUtf8 <$> dataPartToBytes dataPart
Just "Lorem ipsum dolor sit amet!"
Error Handling
Encoding
data EncodingError Source #
Represents the set of error conditions that may occur while encoding a Bech32 string.
EncodedStringTooLong | The resultant encoded string would be longer than
|
Instances
Eq EncodingError Source # | |
Defined in Codec.Binary.Bech32.Internal (==) :: EncodingError -> EncodingError -> Bool # (/=) :: EncodingError -> EncodingError -> Bool # | |
Show EncodingError Source # | |
Defined in Codec.Binary.Bech32.Internal showsPrec :: Int -> EncodingError -> ShowS # show :: EncodingError -> String # showList :: [EncodingError] -> ShowS # |
Decoding
data DecodingError Source #
Represents the set of errors that may occur while decoding a Bech32
string with the decode
function.
StringToDecodeTooLong | The string to decode is longer than |
StringToDecodeTooShort | The string to decode is shorter than |
StringToDecodeHasMixedCase | The string to decode contains both upper case and lower case characters. |
StringToDecodeMissingSeparatorChar | The string to decode is missing the separator character, specified
by |
StringToDecodeContainsInvalidChars [CharPosition] | The string to decode contains one or more invalid characters. 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. |
Instances
Eq DecodingError Source # | |
Defined in Codec.Binary.Bech32.Internal (==) :: DecodingError -> DecodingError -> Bool # (/=) :: DecodingError -> DecodingError -> Bool # | |
Show DecodingError Source # | |
Defined in Codec.Binary.Bech32.Internal showsPrec :: Int -> DecodingError -> ShowS # show :: DecodingError -> String # showList :: [DecodingError] -> ShowS # |
Core Types
Data Payloads
Represents the data part of a Bech32 string, as defined here: https://git.io/fj8FS
Conversion to and from Words
dataPartFromWords :: [Word5] -> DataPart Source #
Construct a DataPart
directly from a list of words.
This function guarantees to satisfy the following properties:
dataPartFromWords (dataPartToWords d) == d dataPartToWords (dataPartFromWords w) == w
dataPartToWords :: DataPart -> [Word5] Source #
Unpack a DataPart
into a list of its constituent words.
This function guarantees to satisfy the following properties:
dataPartFromWords (dataPartToWords d) == d dataPartToWords (dataPartFromWords w) == w
Conversion to and from Bytes
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
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
Conversion to and from Text
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
dataPartToText :: DataPart -> Text Source #
Human-Readable Prefixes
data HumanReadablePart Source #
Represents the human-readable part of a Bech32 string, as defined here: https://git.io/fj8FS
Instances
Eq HumanReadablePart Source # | |
Defined in Codec.Binary.Bech32.Internal (==) :: HumanReadablePart -> HumanReadablePart -> Bool # (/=) :: HumanReadablePart -> HumanReadablePart -> Bool # | |
Show HumanReadablePart Source # | |
Defined in Codec.Binary.Bech32.Internal showsPrec :: Int -> HumanReadablePart -> ShowS # show :: HumanReadablePart -> String # showList :: [HumanReadablePart] -> ShowS # | |
Semigroup HumanReadablePart Source # | |
Defined in Codec.Binary.Bech32.Internal (<>) :: HumanReadablePart -> HumanReadablePart -> HumanReadablePart # sconcat :: NonEmpty HumanReadablePart -> HumanReadablePart # stimes :: Integral b => b -> HumanReadablePart -> HumanReadablePart # | |
Monoid HumanReadablePart Source # | |
Defined in Codec.Binary.Bech32.Internal |
Conversion to and from Text
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.
Error Handling
data HumanReadablePartError Source #
Represents the set of error conditions that may occur while parsing the human-readable part of a Bech32 string.
HumanReadablePartTooShort | The human-readable part is shorter than
|
HumanReadablePartTooLong | The human-readable part is longer than
|
HumanReadablePartContainsInvalidChars [CharPosition] | The human-readable part contains one or more characters whose values
are less than |
Instances
Eq HumanReadablePartError Source # | |
Defined in Codec.Binary.Bech32.Internal | |
Show HumanReadablePartError Source # | |
Defined in Codec.Binary.Bech32.Internal showsPrec :: Int -> HumanReadablePartError -> ShowS # show :: HumanReadablePartError -> String # showList :: [HumanReadablePartError] -> ShowS # | |
Exception HumanReadablePartError Source # | |
Additional Types
Character Positions
newtype CharPosition Source #
The zero-based position of a character in a string, counting from the left.
Values of this type are typically used to reflect the positions of errors.
See DecodingError
.
Instances
Eq CharPosition Source # | |
Defined in Codec.Binary.Bech32.Internal (==) :: CharPosition -> CharPosition -> Bool # (/=) :: CharPosition -> CharPosition -> Bool # | |
Ord CharPosition Source # | |
Defined in Codec.Binary.Bech32.Internal compare :: CharPosition -> CharPosition -> Ordering # (<) :: CharPosition -> CharPosition -> Bool # (<=) :: CharPosition -> CharPosition -> Bool # (>) :: CharPosition -> CharPosition -> Bool # (>=) :: CharPosition -> CharPosition -> Bool # max :: CharPosition -> CharPosition -> CharPosition # min :: CharPosition -> CharPosition -> CharPosition # | |
Show CharPosition Source # | |
Defined in Codec.Binary.Bech32.Internal showsPrec :: Int -> CharPosition -> ShowS # show :: CharPosition -> String # showList :: [CharPosition] -> ShowS # |
Data Words
Represents a data word of 5 bits in width.
Each character in the data portion of a Bech32 string encodes exactly 5 bits of data.
Construction and Deconstruction
Use the toEnum
and fromEnum
functions to construct and deconstruct
Word5
values.
Packing Words into Data Payloads
Use the dataPartFromWords
and dataPartToWords
functions to pack and
unpack Word5
values into and out of data payloads.
Advanced Usage
Encoding and Decoding with Greater Leniency
encodeLenient :: HumanReadablePart -> DataPart -> Text Source #
Like encode
but allows output to be longer than 90 characters.
This isn't ideal, as Bech32 error detection becomes worse as strings get longer, but it may be useful in certain circumstances.
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)."
decodeLenient :: Text -> Either DecodingError (HumanReadablePart, DataPart) Source #
Like decode
but does not enforce a maximum length.
See also encodeLenient
for details.
Fundamental Constants
Data Payloads
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 Prefixes
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.
Encoded Strings
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 in an encoded string.