@@ -7,7 +7,7 @@ module Functora.Bolt11
7
7
decodeBolt11 ,
8
8
Hex (.. ),
9
9
Multiplier (.. ),
10
- Currency (.. ),
10
+ Network (.. ),
11
11
Tag (.. ),
12
12
Bolt11HRP (.. ),
13
13
isPaymentHash ,
@@ -26,6 +26,7 @@ import qualified Data.ByteString.Lazy.Char8 as BL
26
26
import Data.Data (Data )
27
27
import Data.Foldable (foldl' )
28
28
import Data.Maybe (fromMaybe )
29
+ import Data.Ratio ((%) )
29
30
import Data.String (IsString (.. ))
30
31
import Data.Text (Text )
31
32
import qualified Data.Text as T
@@ -86,21 +87,22 @@ isDescription _ = False
86
87
data Multiplier = Milli | Micro | Nano | Pico
87
88
deriving stock (Eq , Ord , Show , Data , Generic )
88
89
89
- data Currency
90
- = Bitcoin
90
+ data Network
91
+ = BitcoinMainnet
91
92
| BitcoinTestnet
92
93
| BitcoinRegtest
94
+ | BitcoinSignet
93
95
deriving stock (Eq , Ord , Show , Data , Generic )
94
96
95
- newtype Bolt11Amount = Bolt11Amount { _getBolt11Amount :: (Int , Multiplier )}
97
+ newtype Bolt11Amount = Bolt11Amount { _getBolt11Amount :: (Integer , Multiplier )}
96
98
deriving newtype (Eq , Ord )
97
99
deriving stock (Data , Generic )
98
100
99
101
instance Show Bolt11Amount where
100
102
show amt = show (bolt11Msats amt)
101
103
102
104
data Bolt11HRP = Bolt11HRP
103
- { bolt11Currency :: Currency ,
105
+ { bolt11Currency :: Network ,
104
106
bolt11Amount :: Maybe Bolt11Amount
105
107
}
106
108
deriving stock (Eq , Ord , Show , Data , Generic )
@@ -113,11 +115,12 @@ data Bolt11 = Bolt11
113
115
}
114
116
deriving stock (Eq , Ord , Show , Data , Generic )
115
117
116
- parseCurrency :: Parser Currency
117
- parseCurrency =
118
- (string " bc" *> pure Bitcoin )
118
+ parseNetwork :: Parser Network
119
+ parseNetwork =
120
+ (string " bcrt" *> pure BitcoinRegtest )
121
+ <|> (string " bc" *> pure BitcoinMainnet )
122
+ <|> (string " tbs" *> pure BitcoinSignet )
119
123
<|> (string " tb" *> pure BitcoinTestnet )
120
- <|> (string " bcrt" *> pure BitcoinRegtest )
121
124
122
125
parseMultiplier :: Parser Multiplier
123
126
parseMultiplier = do
@@ -133,19 +136,15 @@ parseHrpAmount :: Parser Bolt11Amount
133
136
parseHrpAmount = do
134
137
amt <- decimal
135
138
multi <- parseMultiplier
136
- return (Bolt11Amount (amt, multi))
139
+ pure (Bolt11Amount (amt, multi))
137
140
138
141
hrpParser :: Parser Bolt11HRP
139
142
hrpParser = do
140
143
_ <- char ' l'
141
144
_ <- char ' n'
142
- currency <- parseCurrency
143
- mamt <- optional parseHrpAmount
144
- return (Bolt11HRP currency mamt)
145
-
146
- maybeToRight :: b -> Maybe a -> Either b a
147
- maybeToRight _ (Just x) = Right x
148
- maybeToRight y Nothing = Left y
145
+ net <- parseNetwork
146
+ amt <- optional parseHrpAmount
147
+ pure (Bolt11HRP net amt)
149
148
150
149
w5int :: [Word5 ] -> Int
151
150
w5int bytes = foldl' decodeInt 0 (zip [0 .. ] (Prelude. take 7 (reverse bytes)))
@@ -204,7 +203,7 @@ tagsParser ws
204
203
205
204
decodeBolt11 :: Text -> Either String Bolt11
206
205
decodeBolt11 txt = do
207
- (hrp, w5s) <- maybeToRight " error decoding bech32" ( bech32Decode txt)
206
+ (hrp, w5s) <- maybe ( Left " error decoding bech32" ) Right $ bech32Decode txt
208
207
let (timestampBits, rest) = splitAt 7 w5s
209
208
timestamp = w5int timestampBits
210
209
(tags, leftover) = tagsParser rest
@@ -217,10 +216,10 @@ decodeBolt11 txt = do
217
216
multiplierRatio :: Multiplier -> Rational
218
217
multiplierRatio m =
219
218
case m of
220
- Milli -> 1 / 1000
221
- Micro -> 1 / 1000000
222
- Nano -> 1 / 1000000000
223
- Pico -> 1 / 1000000000000
219
+ Milli -> 1 % 1000
220
+ Micro -> 1 % 1000000
221
+ Nano -> 1 % 1000000000
222
+ Pico -> 1 % 1000000000000
224
223
225
224
bolt11Msats :: Bolt11Amount -> MSats
226
225
bolt11Msats (Bolt11Amount (amt, multi)) =
0 commit comments