From 9497437ce57a8e0fc6760c7e743be85d9f880de8 Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Thu, 27 Jul 2023 03:50:00 +0000 Subject: [PATCH 1/5] Repair broken help strings. Various help strings were broken in ebf77a2e07d3da9b6f14dd7729a8b359b019ca04, perhaps by an overly-ambitious search and replace. :D --- bech32/app/Main.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bech32/app/Main.hs b/bech32/app/Main.hs index 3f14bcb..a3f330b 100644 --- a/bech32/app/Main.hs +++ b/bech32/app/Main.hs @@ -129,10 +129,10 @@ hrpArgument = argument (eitherReader reader) $ mconcat , helpDoc $ Just $ vsep [ pretty "An optional human-readable prefix (e.g. 'addr')." , indent 2 $ pretty - "- When provided, the input pretty is decoded from various encoding \ + "- When provided, the input text is decoded from various encoding \ \formats and re-encoded to bech32 using the given prefix." , indent 2 $ pretty - "- When omitted, the input pretty is decoded from bech32 to base16." + "- When omitted, the input text is decoded from bech32 to base16." ] ] where From b46285c7ff091436d66e5786a29723de1fbb7cf3 Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Thu, 27 Jul 2023 03:54:31 +0000 Subject: [PATCH 2/5] Wrap long lines to meet coding standard for this repository. (Maximum length 80 characters.) --- bech32/app/Main.hs | 42 ++++++++++++++++++-------- bech32/test/AppSpec.hs | 4 +-- bech32/test/Codec/Binary/Bech32Spec.hs | 13 ++++++-- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/bech32/app/Main.hs b/bech32/app/Main.hs index a3f330b..6131706 100644 --- a/bech32/app/Main.hs +++ b/bech32/app/Main.hs @@ -91,23 +91,37 @@ parse = customExecParser (prefs showHelpOnEmpty) parser , footerDoc $ Just $ vsep [ hsep [ pretty "Supported encoding formats:" - , indent 0 $ pretty "Base16, Bech32 & Base58." + , indent 0 $ pretty "Base16, Bech32 & Base58." ] , pretty "" , pretty "Examples:" - , indent 2 $ hsep [annotate underlined $ pretty "To", pretty "Bech32:"] - , indent 4 $ annotate bold $ pretty "$ bech32 base16_ <<< 706174617465" - , indent 4 $ pretty "base16_1wpshgct5v5r5mxh0" + , indent 2 + $ hsep [annotate underlined $ pretty "To", pretty "Bech32:"] + , indent 4 + $ annotate bold + $ pretty "$ bech32 base16_ <<< 706174617465" + , indent 4 + $ pretty "base16_1wpshgct5v5r5mxh0" , pretty "" - , indent 4 $ annotate bold $ pretty "$ bech32 base58_ <<< Ae2tdPwUPEYy" - , indent 4 $ pretty "base58_1p58rejhd9592uusa8pzj2" + , indent 4 + $ annotate bold + $ pretty "$ bech32 base58_ <<< Ae2tdPwUPEYy" + , indent 4 + $ pretty "base58_1p58rejhd9592uusa8pzj2" , pretty "" - , indent 4 $ annotate bold $ pretty "$ bech32 new_prefix <<< old_prefix1wpshgcg2s33x3" - , indent 4 $ pretty "new_prefix1wpshgcgeak9mv" + , indent 4 + $ annotate bold + $ pretty "$ bech32 new_prefix <<< old_prefix1wpshgcg2s33x3" + , indent 4 + $ pretty "new_prefix1wpshgcgeak9mv" , pretty "" - , indent 2 $ hsep [annotate underlined $ pretty "From", pretty "Bech32:"] - , indent 4 $ annotate bold $ pretty "$ bech32 <<< base16_1wpshgct5v5r5mxh0" - , indent 4 $ pretty "706174617465" + , indent 2 + $ hsep [annotate underlined $ pretty "From", pretty "Bech32:"] + , indent 4 + $ annotate bold + $ pretty "$ bech32 <<< base16_1wpshgct5v5r5mxh0" + , indent 4 + $ pretty "706174617465" ] ] @@ -196,8 +210,10 @@ detectEncoding str guard (Bech32.separatorChar `elem` str) pure Bech32 where - datapart = reverse . takeWhile (/= Bech32.separatorChar) . reverse $ str - humanpart = takeWhile (/= Bech32.separatorChar) str + datapart = + reverse . takeWhile (/= Bech32.separatorChar) . reverse $ str + humanpart = + takeWhile (/= Bech32.separatorChar) str alpha = filter isLetter str resembleBase58 = do diff --git a/bech32/test/AppSpec.hs b/bech32/test/AppSpec.hs index a7c9bc2..1107a9f 100644 --- a/bech32/test/AppSpec.hs +++ b/bech32/test/AppSpec.hs @@ -66,8 +66,8 @@ base16 = fromUtf8 . convertToBase Base16 . utf8 bech32 :: Text -> String -> String bech32 txt = T.unpack . Bech32.encodeLenient hrp . dataPartFromBytes . utf8 where - hrp = either (error . ("Error while parsing Bech32: " <>) . show) id $ humanReadablePartFromText txt - + hrp = either (error . ("Error while parsing Bech32: " <>) . show) id + $ humanReadablePartFromText txt base58 :: String -> String base58 = fromUtf8 . encodeBase58 bitcoinAlphabet . utf8 diff --git a/bech32/test/Codec/Binary/Bech32Spec.hs b/bech32/test/Codec/Binary/Bech32Spec.hs index a02dacc..b8874dc 100644 --- a/bech32/test/Codec/Binary/Bech32Spec.hs +++ b/bech32/test/Codec/Binary/Bech32Spec.hs @@ -103,7 +103,8 @@ spec = do -- test that a corrupted checksum fails decoding. let (hrp, rest) = T.breakOnEnd (T.singleton separatorChar) checksum - let (first, rest') = fromMaybe (error "empty rest") $ T.uncons rest + let (first, rest') = + fromMaybe (error "empty rest") $ T.uncons rest let checksumCorrupted = (hrp `T.snoc` chr (ord first `xor` 1)) `T.append` rest' @@ -188,7 +189,10 @@ spec = do it "length > maximum" $ do let hrpUnpacked = "ca" let hrpLength = length hrpUnpacked - let hrp = either (error . ("Error while parsing Bech32: " <>) . show) id $ humanReadablePartFromText (T.pack hrpUnpacked) + let hrp = either + (error . ("Error while parsing Bech32: " <>) . show) + id + $ humanReadablePartFromText (T.pack hrpUnpacked) let maxDataLength = Bech32.encodedStringMaxLength - Bech32.checksumLength - Bech32.separatorLength - hrpLength @@ -198,7 +202,10 @@ spec = do `shouldBe` Left Bech32.EncodedStringTooLong it "hrp lowercased" $ do - let hrp = either (error . ("Error while parsing Bech32: " <>) . show) id $ humanReadablePartFromText "HRP" + let hrp = either + (error . ("Error while parsing Bech32: " <>) . show) + id + $ humanReadablePartFromText "HRP" Bech32.encode hrp mempty `shouldBe` Right "hrp1vhqs52" describe "Arbitrary Bech32String" $ From 0db724307fd120c631ecb19465c9320d9cc7a49f Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Thu, 27 Jul 2023 03:56:36 +0000 Subject: [PATCH 3/5] Use explicit imports for `PrettyPrinter` in `app/Main`. --- bech32/app/Main.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bech32/app/Main.hs b/bech32/app/Main.hs index 6131706..f9c5ae8 100644 --- a/bech32/app/Main.hs +++ b/bech32/app/Main.hs @@ -49,7 +49,9 @@ import Options.Applicative import Paths_bech32 ( version ) import Prettyprinter + ( annotate, hsep, indent, pretty, vsep ) import Prettyprinter.Render.Terminal + ( bold, underlined ) import System.IO ( BufferMode (..), Handle, hSetBuffering, stderr, stdin, stdout ) From 00beb2ec26f4fe74cbd95fc063b8bcd6e9d5a710 Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Thu, 27 Jul 2023 04:11:38 +0000 Subject: [PATCH 4/5] Extract out common unsafe HRP parsing code into utility function. --- bech32/test/Codec/Binary/Bech32Spec.hs | 27 ++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/bech32/test/Codec/Binary/Bech32Spec.hs b/bech32/test/Codec/Binary/Bech32Spec.hs index b8874dc..c2debb8 100644 --- a/bech32/test/Codec/Binary/Bech32Spec.hs +++ b/bech32/test/Codec/Binary/Bech32Spec.hs @@ -189,10 +189,7 @@ spec = do it "length > maximum" $ do let hrpUnpacked = "ca" let hrpLength = length hrpUnpacked - let hrp = either - (error . ("Error while parsing Bech32: " <>) . show) - id - $ humanReadablePartFromText (T.pack hrpUnpacked) + let hrp = unsafeHumanReadablePartFromText (T.pack hrpUnpacked) let maxDataLength = Bech32.encodedStringMaxLength - Bech32.checksumLength - Bech32.separatorLength - hrpLength @@ -202,10 +199,7 @@ spec = do `shouldBe` Left Bech32.EncodedStringTooLong it "hrp lowercased" $ do - let hrp = either - (error . ("Error while parsing Bech32: " <>) . show) - id - $ humanReadablePartFromText "HRP" + let hrp = unsafeHumanReadablePartFromText "HRP" Bech32.encode hrp mempty `shouldBe` Right "hrp1vhqs52" describe "Arbitrary Bech32String" $ @@ -691,8 +685,7 @@ instance Arbitrary HumanReadablePart where arbitrary = do len <- choose (1, 10) chars <- replicateM len arbitrary - let hrp = either (error . ("Error while parsing Bech32: " <>) . show) id - $ humanReadablePartFromText + let hrp = unsafeHumanReadablePartFromText $ T.pack $ getHumanReadableChar <$> chars return hrp @@ -790,3 +783,17 @@ isLeft' :: Show e => Either e a -> Bool isLeft' = \case Left e -> show e `deepseq` True Right _ -> False + +-------------------------------------------------------------------------------- +-- Utilities +-------------------------------------------------------------------------------- + +-- | Unsafely parses a human-readable prefix from text. +-- +-- Throws a run-time error if the given text could not be parsed as a +-- human-readable prefix. +-- +unsafeHumanReadablePartFromText :: Text -> HumanReadablePart +unsafeHumanReadablePartFromText + = either (error . ("Error while parsing Bech32: " <>) . show) id + . humanReadablePartFromText From 8c370685088847db32a9ec66ab7fd3e366a60c03 Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Thu, 27 Jul 2023 04:14:29 +0000 Subject: [PATCH 5/5] Remove extraneous blank lines. --- bech32/test/Codec/Binary/Bech32Spec.hs | 1 - 1 file changed, 1 deletion(-) diff --git a/bech32/test/Codec/Binary/Bech32Spec.hs b/bech32/test/Codec/Binary/Bech32Spec.hs index c2debb8..b1b862a 100644 --- a/bech32/test/Codec/Binary/Bech32Spec.hs +++ b/bech32/test/Codec/Binary/Bech32Spec.hs @@ -155,7 +155,6 @@ spec = do humanReadablePartFromText hrp `shouldBe` Left invalidError - it "Lengths are checked correctly." $ property $ \(HumanReadablePartWithSuspiciousLength hrp) -> let lo = humanReadablePartMinLength