commit 52a11137e17d1ca94239d815abc5eac36b3aef00
parent 085944fd037672e264a186cd54d83ac9ca21c308
Author: Jared Tobin <jared@jtobin.io>
Date: Sat, 14 Dec 2024 04:08:45 -0330
lib: s/verify_checksum/verify
Strikes me as having better UX.
Diffstat:
3 files changed, 24 insertions(+), 25 deletions(-)
diff --git a/lib/Data/ByteString/Base32.hs b/lib/Data/ByteString/Base32.hs
@@ -11,7 +11,7 @@ module Data.ByteString.Base32 (
-- not actually base32-related, but convenient to put here
, Encoding(..)
, create_checksum
- , verify_checksum
+ , verify
, valid_hrp
) where
@@ -204,10 +204,13 @@ create_checksum enc hrp dat =
in BS.map code "\EM\DC4\SI\n\ENQ\NUL" -- BS.pack [25, 20, 15, 10, 5, 0]
-verify_checksum :: Encoding -> BS.ByteString -> BS.ByteString -> Bool
-verify_checksum enc hrp dat =
- let bs = hrp_expand hrp <> as_word5 dat
- in polymod bs == case enc of
- Bech32 -> 1
- Bech32m -> _BECH32M_CONST
+verify :: Encoding -> BS.ByteString -> Bool
+verify enc b32 = case BS.elemIndexEnd 0x31 b32 of
+ Nothing -> False
+ Just idx ->
+ let (hrp, BS.drop 1 -> dat) = BS.splitAt idx b32
+ 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
@@ -2,7 +2,7 @@
module Data.ByteString.Bech32 (
encode
- , verify_checksum
+ , verify
) where
import Control.Monad (guard)
@@ -20,18 +20,16 @@ toStrict = BS.toStrict
create_checksum :: BS.ByteString -> BS.ByteString -> BS.ByteString
create_checksum = B32.create_checksum Bech32
--- | Verify a Bech32 checksum, given human-readable and data parts.
+-- | Verify that a bech32 string has a valid checksum.
--
--- >>> let Just bech32 = encode "bc" "my string"
--- >>> verify_checksum "bc" "d4ujqum5wf5kuecmu02w2"
+-- >>> verify "bc1d4ujqum5wf5kuecmu02w2"
-- True
--- >>> verify_checksum "bc" "d4ujquw5wf5kuecmu02w2" -- s/m/w
+-- >>> verify "bc1d4ujquw5wf5kuecmu02w2" -- s/m/w
-- False
-verify_checksum
- :: BS.ByteString -- ^ base255-encoded human-readable part
- -> BS.ByteString -- ^ bech32-encoded data part
+verify
+ :: BS.ByteString -- ^ bech32-encoded bytestring
-> Bool
-verify_checksum = B32.verify_checksum Bech32
+verify = B32.verify Bech32
-- | Encode a base255 human-readable part and input as Bech32.
--
diff --git a/lib/Data/ByteString/Bech32m.hs b/lib/Data/ByteString/Bech32m.hs
@@ -2,7 +2,7 @@
module Data.ByteString.Bech32m (
encode
- , verify_checksum
+ , verify
) where
import Control.Monad (guard)
@@ -20,18 +20,16 @@ toStrict = BS.toStrict
create_checksum :: BS.ByteString -> BS.ByteString -> BS.ByteString
create_checksum = B32.create_checksum Bech32m
--- | Verify a Bech32m checksum, given human-readable and data parts.
+-- | Verify that a bech32m string has a valid checksum.
--
--- >>> let Just bech32m = encode "bc" "my string"
--- >>> verify_checksum "bc" "d4ujqum5wf5kuecwqlxtg"
+-- >>> verify "bc1d4ujqum5wf5kuecwqlxtg"
-- True
--- >>> verify_checksum "bc" "d4ujquw5wf5kuecwqlxtg" -- s/m/w
+-- >>> verify "bc1d4ujquw5wf5kuecwqlxtg" -- s/m/w
-- False
-verify_checksum
- :: BS.ByteString -- ^ base255-encoded human-readable part
- -> BS.ByteString -- ^ bech32-encoded data part
+verify
+ :: BS.ByteString -- ^ bech32m-encoded bytestring
-> Bool
-verify_checksum = B32.verify_checksum Bech32m
+verify = B32.verify Bech32m
-- | Encode a base255 human-readable part and input as Bech32m.
--