bech32

Pure Haskell bech32 and bech32m encodings.
git clone git://git.ppad.tech/bech32.git
Log | Files | Refs | LICENSE

commit 66e1fd833407935e5c0cc74b172d17837c67faa3
parent 0559452ec1000ca71597ae7b6521c6e76fde5bc8
Author: Jared Tobin <jared@jtobin.io>
Date:   Fri, 13 Dec 2024 07:03:55 -0330

lib: improve verify_checksum ux, docs

Diffstat:
Mlib/Data/ByteString/Base32.hs | 2+-
Mlib/Data/ByteString/Bech32.hs | 28++++++++++++++++++++++------
Mlib/Data/ByteString/Bech32m.hs | 28++++++++++++++++++++++------
Mppad-bech32.cabal | 5++---
4 files changed, 47 insertions(+), 16 deletions(-)

diff --git a/lib/Data/ByteString/Base32.hs b/lib/Data/ByteString/Base32.hs @@ -206,7 +206,7 @@ create_checksum enc hrp dat = verify_checksum :: Encoding -> BS.ByteString -> BS.ByteString -> Bool verify_checksum enc hrp dat = - let bs = hrp_expand hrp <> dat + let bs = hrp_expand hrp <> as_word5 dat in polymod bs == case enc of Bech32 -> 1 Bech32m -> _BECH32M_CONST diff --git a/lib/Data/ByteString/Bech32.hs b/lib/Data/ByteString/Bech32.hs @@ -17,15 +17,31 @@ toStrict :: BSB.Builder -> BS.ByteString toStrict = BS.toStrict . BE.toLazyByteStringWith (BE.safeStrategy 128 BE.smallChunkSize) mempty -verify_checksum :: BS.ByteString -> BS.ByteString -> Bool -verify_checksum = B32.verify_checksum Bech32 - --- XX no need for this to be here create_checksum :: BS.ByteString -> BS.ByteString -> BS.ByteString create_checksum = B32.create_checksum Bech32 --- base255 -> bech32m -encode :: BS.ByteString -> BS.ByteString -> Maybe BS.ByteString +-- | Verify a Bech32 checksum, given human-readable and data parts. +-- +-- >>> let Just bech32 = encode "bc" "my string" +-- >>> verify_checksum "bc" "d4ujqum5wf5kuecmu02w2" +-- True +-- >>> verify_checksum "bc" "d4ujquw5wf5kuecmu02w2" -- s/m/w +-- False +verify_checksum + :: BS.ByteString -- ^ base255-encoded human-readable part + -> BS.ByteString -- ^ bech32-encoded data part + -> Bool +verify_checksum = B32.verify_checksum Bech32 + +-- | Encode a base255 human-readable part and input as Bech32. +-- +-- >>> let Just bech32 = encode "bc" "my string" +-- >>> bech32 +-- "bc1d4ujqum5wf5kuecmu02w2" +encode + :: BS.ByteString -- ^ base255-encoded human-readable part + -> BS.ByteString -- ^ base255-encoded data part + -> Maybe BS.ByteString -- ^ bech32-encoded bytestring encode hrp (B32.encode -> dat) = do guard (B32.valid_hrp hrp) let check = create_checksum hrp (B32.as_word5 dat) diff --git a/lib/Data/ByteString/Bech32m.hs b/lib/Data/ByteString/Bech32m.hs @@ -17,15 +17,31 @@ toStrict :: BSB.Builder -> BS.ByteString toStrict = BS.toStrict . BE.toLazyByteStringWith (BE.safeStrategy 128 BE.smallChunkSize) mempty -verify_checksum :: BS.ByteString -> BS.ByteString -> Bool -verify_checksum = B32.verify_checksum Bech32m - --- XX no need for this to be here create_checksum :: BS.ByteString -> BS.ByteString -> BS.ByteString create_checksum = B32.create_checksum Bech32m --- base255 -> bech32m -encode :: BS.ByteString -> BS.ByteString -> Maybe BS.ByteString +-- | Verify a Bech32m checksum, given human-readable and data parts. +-- +-- >>> let Just bech32m = encode "bc" "my string" +-- >>> verify_checksum "bc" "d4ujqum5wf5kuecwqlxtg" +-- True +-- >>> verify_checksum "bc" "d4ujquw5wf5kuecwqlxtg" -- s/m/w +-- False +verify_checksum + :: BS.ByteString -- ^ base255-encoded human-readable part + -> BS.ByteString -- ^ bech32-encoded data part + -> Bool +verify_checksum = B32.verify_checksum Bech32m + +-- | Encode a base255 human-readable part and input as Bech32m. +-- +-- >>> let Just bech32m = encode "bc" "my string" +-- >>> bech32m +-- "bc1d4ujqum5wf5kuecwqlxtg" +encode + :: BS.ByteString -- ^ base255-encoded human-readable part + -> BS.ByteString -- ^ base255-encoded data part + -> Maybe BS.ByteString -- ^ bech32-encoded bytestring encode hrp (B32.encode -> dat) = do guard (B32.valid_hrp hrp) let check = create_checksum hrp (B32.as_word5 dat) diff --git a/ppad-bech32.cabal b/ppad-bech32.cabal @@ -22,10 +22,9 @@ library hs-source-dirs: lib ghc-options: -Wall - other-modules: - Data.ByteString.Base32 exposed-modules: - Data.ByteString.Bech32 + Data.ByteString.Base32 + , Data.ByteString.Bech32 , Data.ByteString.Bech32m build-depends: base >= 4.9 && < 5