Skip to content

Commit eb6246f

Browse files
committed
using bech32 package
1 parent 465fff8 commit eb6246f

File tree

5 files changed

+18
-271
lines changed

5 files changed

+18
-271
lines changed

pub/functora/functora.cabal

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ common pkg-bolt11
251251
, attoparsec
252252
, base
253253
, base16-bytestring
254+
, bech32
254255
, bytestring
255256
, text
256257

@@ -348,7 +349,6 @@ library bolt11
348349
exposed: True
349350
visibility: public
350351
exposed-modules:
351-
Functora.Bech32
352352
Functora.Bolt11
353353
Functora.Denomination
354354

@@ -401,7 +401,6 @@ test-suite functora-test
401401
other-modules:
402402
Functora.Aes
403403
Functora.AesOrphan
404-
Functora.Bech32
405404
Functora.Bolt11
406405
Functora.Cfg
407406
Functora.CfgOrphan

pub/functora/src/bolt11/Functora/Bech32.hs

Lines changed: 0 additions & 252 deletions
This file was deleted.

pub/functora/src/bolt11/Functora/Bolt11.hs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ module Functora.Bolt11
1515
)
1616
where
1717

18+
import Codec.Binary.Bech32 (Word5)
19+
import qualified Codec.Binary.Bech32.Internal as Bech32
1820
import Control.Applicative
1921
import Data.Attoparsec.Text
22+
import Data.Bifunctor (first)
2023
import Data.Bits (shiftL, (.|.))
2124
import Data.ByteString (ByteString)
2225
import qualified Data.ByteString as BS
@@ -32,7 +35,6 @@ import Data.Text (Text)
3235
import qualified Data.Text as T
3336
import Data.Text.Encoding (decodeUtf8)
3437
import qualified Data.Text.Encoding as T
35-
import Functora.Bech32 (Word5 (..), bech32Decode, toBase256)
3638
import Functora.Denomination (Denomination (toMsats), MSats, btc)
3739
import GHC.Generics (Generic)
3840
import Prelude
@@ -74,7 +76,7 @@ data Tag
7476
| OnchainFallback Hex -- TODO: address type
7577
| ExtraRouteInfo
7678
| FeatureBits [Word5]
77-
deriving stock (Eq, Ord, Show, Data, Generic)
79+
deriving stock (Eq, Ord, Show, Generic)
7880

7981
isPaymentHash :: Tag -> Bool
8082
isPaymentHash PaymentHash {} = True
@@ -113,7 +115,7 @@ data Bolt11 = Bolt11
113115
bolt11Tags :: [Tag], -- posix
114116
bolt11Signature :: Hex
115117
}
116-
deriving stock (Eq, Ord, Show, Data, Generic)
118+
deriving stock (Eq, Ord, Show, Generic)
117119

118120
parseNetwork :: Parser Network
119121
parseNetwork =
@@ -149,11 +151,11 @@ hrpParser = do
149151
w5int :: [Word5] -> Int
150152
w5int bytes = foldl' decodeInt 0 (zip [0 ..] (Prelude.take 7 (reverse bytes)))
151153
where
152-
decodeInt !n (i, UnsafeWord5 byte) =
153-
n .|. fromIntegral byte `shiftL` (i * 5)
154+
decodeInt !n (i, byte) =
155+
n .|. fromEnum byte `shiftL` (i * 5)
154156

155157
w5bs :: [Word5] -> ByteString
156-
w5bs = BS.pack . fromMaybe (error "what") . toBase256
158+
w5bs = BS.pack . fromMaybe (error "what") . Bech32.toBase256
157159

158160
w5txt :: [Word5] -> Text
159161
w5txt = decodeUtf8 . w5bs
@@ -164,15 +166,15 @@ tagParser ws@[_] = (Nothing, ws)
164166
tagParser ws@[_, _] = (Nothing, ws) -- appease the compiler warning gods
165167
tagParser ws
166168
| length ws < 8 = (Nothing, ws)
167-
tagParser ws@(UnsafeWord5 typ : d1 : d2 : rest)
169+
tagParser ws@(typ : d1 : d2 : rest)
168170
| length rest < 7 = (Nothing, ws)
169171
| otherwise = (Just tag, leftovers)
170172
where
171173
dataLen = w5int [d1, d2]
172174
(dat, leftovers) = Prelude.splitAt dataLen rest
173175
datBs = Hex (w5bs dat)
174176
tag =
175-
case typ of
177+
case fromEnum typ of
176178
1 -> PaymentHash datBs
177179
16 -> PaymentSecret datBs
178180
23 -> DescriptionHash datBs -- (w5bs dat)
@@ -188,29 +190,28 @@ tagParser ws@(UnsafeWord5 typ : d1 : d2 : rest)
188190
data MSig
189191
= Sig [Word5]
190192
| Unk [Word5]
191-
deriving stock (Eq, Ord, Show, Data, Generic)
193+
deriving stock (Eq, Ord, Show, Generic)
192194

193195
tagsParser :: [Word5] -> ([Tag], MSig)
194196
tagsParser ws
195197
| length ws == 104 = ([], Sig ws)
196198
| otherwise =
197199
let (mtag, rest) = tagParser ws
198-
first fn (a, b) = (fn a, b)
199200
in maybe
200201
([], Unk rest)
201202
(\tag -> first (tag :) (tagsParser rest))
202203
mtag
203204

204205
decodeBolt11 :: Text -> Either String Bolt11
205206
decodeBolt11 txt = do
206-
(hrp, w5s) <- maybe (Left "error decoding bech32") Right $ bech32Decode txt
207-
let (timestampBits, rest) = splitAt 7 w5s
207+
(hrp, w5s) <- first show $ Bech32.decodeLenient txt
208+
let (timestampBits, rest) = splitAt 7 $ Bech32.dataPartToWords w5s
208209
timestamp = w5int timestampBits
209210
(tags, leftover) = tagsParser rest
210211
sig <- case leftover of
211-
Sig ws -> maybe (Left "corrupt") Right (toBase256 ws)
212+
Sig ws -> maybe (Left "corrupt") Right (Bech32.toBase256 ws)
212213
Unk left -> Left ("corrupt, leftover: " ++ show (Hex (w5bs left)))
213-
parsedHrp <- parseOnly hrpParser hrp
214+
parsedHrp <- parseOnly hrpParser $ Bech32.humanReadablePartToText hrp
214215
Right (Bolt11 parsedHrp timestamp tags (Hex (BS.pack sig)))
215216

216217
multiplierRatio :: Multiplier -> Rational

0 commit comments

Comments
 (0)