@@ -8,9 +8,9 @@ module Functora.Bolt11
8
8
Multiplier (.. ),
9
9
Network (.. ),
10
10
Tag (.. ),
11
- Bolt11HRP (.. ),
11
+ Bolt11Hrp (.. ),
12
+ Bolt11HrpAmt (.. ),
12
13
isPaymentHash ,
13
- isDescription ,
14
14
)
15
15
where
16
16
@@ -73,10 +73,6 @@ isPaymentHash :: Tag -> Bool
73
73
isPaymentHash PaymentHash {} = True
74
74
isPaymentHash _ = False
75
75
76
- isDescription :: Tag -> Bool
77
- isDescription Description {} = True
78
- isDescription _ = False
79
-
80
76
data Multiplier = Milli | Micro | Nano | Pico
81
77
deriving stock (Eq , Ord , Show , Data , Generic )
82
78
@@ -87,18 +83,20 @@ data Network
87
83
| BitcoinSignet
88
84
deriving stock (Eq , Ord , Show , Data , Generic )
89
85
90
- newtype Bolt11Amount = Bolt11Amount { _getBolt11Amount :: (Integer , Multiplier )}
91
- deriving newtype (Eq , Ord )
92
- deriving stock (Data , Generic )
86
+ data Bolt11HrpAmt = Bolt11HrpAmt
87
+ { bolt11HrpAmtNum :: Integer ,
88
+ bolt11HrpAmtMul :: Multiplier
89
+ }
90
+ deriving stock (Eq , Ord , Data , Generic )
93
91
94
- instance Show Bolt11Amount where
95
- show (Bolt11Amount ( amt, mul) ) =
96
- if ( round sat) % 1 /= sat
97
- then show msat <> " Millisatoshi "
92
+ instance Show Bolt11HrpAmt where
93
+ show (Bolt11HrpAmt amt mul) =
94
+ if sat > 1_000_000
95
+ then inspectBtcNum btc <> " BTC "
98
96
else
99
- if sat < 1_000_000
100
- then show sat <> " Satoshi"
101
- else show btc <> " BTC "
97
+ if ( round sat) % 1 == sat
98
+ then inspectBtcNum sat <> " Satoshi"
99
+ else inspectBtcNum msat <> " Millisatoshi "
102
100
where
103
101
btc :: Rational
104
102
btc = (amt % 1 ) * multiplierRatio mul
@@ -107,14 +105,14 @@ instance Show Bolt11Amount where
107
105
msat :: Rational
108
106
msat = sat * 1000
109
107
110
- data Bolt11HRP = Bolt11HRP
111
- { bolt11Network :: Network ,
112
- bolt11Amount :: Maybe Bolt11Amount
108
+ data Bolt11Hrp = Bolt11Hrp
109
+ { bolt11HrpNet :: Network ,
110
+ bolt11HrpAmt :: Maybe Bolt11HrpAmt
113
111
}
114
112
deriving stock (Eq , Ord , Show , Data , Generic )
115
113
116
114
data Bolt11 = Bolt11
117
- { bolt11HRP :: Bolt11HRP ,
115
+ { bolt11Hrp :: Bolt11Hrp ,
118
116
bolt11Timestamp :: Int , -- posix
119
117
bolt11Tags :: [Tag ], -- posix
120
118
bolt11Signature :: Hex
@@ -138,19 +136,19 @@ parseMultiplier = do
138
136
' p' -> pure Pico
139
137
_ -> fail " unhandled case in parseMultiplier"
140
138
141
- parseHrpAmount :: Parser Bolt11Amount
139
+ parseHrpAmount :: Parser Bolt11HrpAmt
142
140
parseHrpAmount = do
143
141
amt <- decimal
144
- multi <- parseMultiplier
145
- pure ( Bolt11Amount ( amt, multi))
142
+ mul <- parseMultiplier
143
+ pure $ Bolt11HrpAmt amt mul
146
144
147
- hrpParser :: Parser Bolt11HRP
145
+ hrpParser :: Parser Bolt11Hrp
148
146
hrpParser = do
149
147
_ <- char ' l'
150
148
_ <- char ' n'
151
149
net <- parseNetwork
152
150
amt <- optional parseHrpAmount
153
- pure (Bolt11HRP net amt)
151
+ pure (Bolt11Hrp net amt)
154
152
155
153
w5int :: [Word5 ] -> Int
156
154
w5int bytes = foldl' decodeInt 0 (zip [0 .. ] (Prelude. take 7 (reverse bytes)))
@@ -225,3 +223,21 @@ multiplierRatio m =
225
223
Micro -> 1 % 1000000
226
224
Nano -> 1 % 1000000000
227
225
Pico -> 1 % 1000000000000
226
+
227
+ inspectBtcNum ::
228
+ forall a b .
229
+ ( From String a ,
230
+ From b Integer ,
231
+ Integral b
232
+ ) =>
233
+ Ratio b ->
234
+ a
235
+ inspectBtcNum =
236
+ inspectRatio
237
+ RatioFormat
238
+ { ratioFormatDoRounding = True ,
239
+ ratioFormatThousandsSeparator = mempty ,
240
+ ratioFormatDecimalPlacesAfterNonZero = Just 12 , -- Pico
241
+ ratioFormatDecimalPlacesTotalLimit = Just 12 , -- Pico
242
+ ratioFormatDecimalPlacesTotalLimitOverflow = DecimalPlacesOverflowExponent
243
+ }
0 commit comments