@@ -15,8 +15,11 @@ module Functora.Bolt11
15
15
)
16
16
where
17
17
18
+ import Codec.Binary.Bech32 (Word5 )
19
+ import qualified Codec.Binary.Bech32.Internal as Bech32
18
20
import Control.Applicative
19
21
import Data.Attoparsec.Text
22
+ import Data.Bifunctor (first )
20
23
import Data.Bits (shiftL , (.|.) )
21
24
import Data.ByteString (ByteString )
22
25
import qualified Data.ByteString as BS
@@ -32,7 +35,6 @@ import Data.Text (Text)
32
35
import qualified Data.Text as T
33
36
import Data.Text.Encoding (decodeUtf8 )
34
37
import qualified Data.Text.Encoding as T
35
- import Functora.Bech32 (Word5 (.. ), bech32Decode , toBase256 )
36
38
import Functora.Denomination (Denomination (toMsats ), MSats , btc )
37
39
import GHC.Generics (Generic )
38
40
import Prelude
@@ -74,7 +76,7 @@ data Tag
74
76
| OnchainFallback Hex -- TODO: address type
75
77
| ExtraRouteInfo
76
78
| FeatureBits [Word5 ]
77
- deriving stock (Eq , Ord , Show , Data , Generic )
79
+ deriving stock (Eq , Ord , Show , Generic )
78
80
79
81
isPaymentHash :: Tag -> Bool
80
82
isPaymentHash PaymentHash {} = True
@@ -113,7 +115,7 @@ data Bolt11 = Bolt11
113
115
bolt11Tags :: [Tag ], -- posix
114
116
bolt11Signature :: Hex
115
117
}
116
- deriving stock (Eq , Ord , Show , Data , Generic )
118
+ deriving stock (Eq , Ord , Show , Generic )
117
119
118
120
parseNetwork :: Parser Network
119
121
parseNetwork =
@@ -149,11 +151,11 @@ hrpParser = do
149
151
w5int :: [Word5 ] -> Int
150
152
w5int bytes = foldl' decodeInt 0 (zip [0 .. ] (Prelude. take 7 (reverse bytes)))
151
153
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 )
154
156
155
157
w5bs :: [Word5 ] -> ByteString
156
- w5bs = BS. pack . fromMaybe (error " what" ) . toBase256
158
+ w5bs = BS. pack . fromMaybe (error " what" ) . Bech32. toBase256
157
159
158
160
w5txt :: [Word5 ] -> Text
159
161
w5txt = decodeUtf8 . w5bs
@@ -164,15 +166,15 @@ tagParser ws@[_] = (Nothing, ws)
164
166
tagParser ws@ [_, _] = (Nothing , ws) -- appease the compiler warning gods
165
167
tagParser ws
166
168
| length ws < 8 = (Nothing , ws)
167
- tagParser ws@ (UnsafeWord5 typ : d1 : d2 : rest)
169
+ tagParser ws@ (typ : d1 : d2 : rest)
168
170
| length rest < 7 = (Nothing , ws)
169
171
| otherwise = (Just tag, leftovers)
170
172
where
171
173
dataLen = w5int [d1, d2]
172
174
(dat, leftovers) = Prelude. splitAt dataLen rest
173
175
datBs = Hex (w5bs dat)
174
176
tag =
175
- case typ of
177
+ case fromEnum typ of
176
178
1 -> PaymentHash datBs
177
179
16 -> PaymentSecret datBs
178
180
23 -> DescriptionHash datBs -- (w5bs dat)
@@ -188,29 +190,28 @@ tagParser ws@(UnsafeWord5 typ : d1 : d2 : rest)
188
190
data MSig
189
191
= Sig [Word5 ]
190
192
| Unk [Word5 ]
191
- deriving stock (Eq , Ord , Show , Data , Generic )
193
+ deriving stock (Eq , Ord , Show , Generic )
192
194
193
195
tagsParser :: [Word5 ] -> ([Tag ], MSig )
194
196
tagsParser ws
195
197
| length ws == 104 = ([] , Sig ws)
196
198
| otherwise =
197
199
let (mtag, rest) = tagParser ws
198
- first fn (a, b) = (fn a, b)
199
200
in maybe
200
201
([] , Unk rest)
201
202
(\ tag -> first (tag : ) (tagsParser rest))
202
203
mtag
203
204
204
205
decodeBolt11 :: Text -> Either String Bolt11
205
206
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
208
209
timestamp = w5int timestampBits
209
210
(tags, leftover) = tagsParser rest
210
211
sig <- case leftover of
211
- Sig ws -> maybe (Left " corrupt" ) Right (toBase256 ws)
212
+ Sig ws -> maybe (Left " corrupt" ) Right (Bech32. toBase256 ws)
212
213
Unk left -> Left (" corrupt, leftover: " ++ show (Hex (w5bs left)))
213
- parsedHrp <- parseOnly hrpParser hrp
214
+ parsedHrp <- parseOnly hrpParser $ Bech32. humanReadablePartToText hrp
214
215
Right (Bolt11 parsedHrp timestamp tags (Hex (BS. pack sig)))
215
216
216
217
multiplierRatio :: Multiplier -> Rational
0 commit comments