Skip to content

Commit 46be634

Browse files
committed
WIP
1 parent d7a8ef3 commit 46be634

File tree

3 files changed

+48
-32
lines changed

3 files changed

+48
-32
lines changed

ghcjs/lightning-verifier/src/App/Widgets/Bolt11.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ invoiceWidget ln =
5858
Header.headerViewer "Invoice Details"
5959
<> pairs
6060
( [ pair "Network"
61-
$ case B11.bolt11Currency $ B11.bolt11HRP ln of
61+
$ case B11.bolt11HrpNet $ B11.bolt11Hrp ln of
6262
B11.BitcoinMainnet -> "Bitcoin Mainnet"
6363
B11.BitcoinTestnet -> "Bitcoin Testnet"
6464
B11.BitcoinRegtest -> "Bitcoin Regtest"
6565
B11.BitcoinSignet -> "Bitcoin Signet",
6666
pair "Amount"
6767
. maybe "0" defShow
68-
. B11.bolt11Amount
69-
$ B11.bolt11HRP ln,
68+
. B11.bolt11HrpAmt
69+
$ B11.bolt11Hrp ln,
7070
pair "Timestamp"
7171
. inspect
7272
$ B11.bolt11Timestamp ln

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

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module Functora.Bolt11
88
Multiplier (..),
99
Network (..),
1010
Tag (..),
11-
Bolt11HRP (..),
11+
Bolt11Hrp (..),
12+
Bolt11HrpAmt (..),
1213
isPaymentHash,
13-
isDescription,
1414
)
1515
where
1616

@@ -73,10 +73,6 @@ isPaymentHash :: Tag -> Bool
7373
isPaymentHash PaymentHash {} = True
7474
isPaymentHash _ = False
7575

76-
isDescription :: Tag -> Bool
77-
isDescription Description {} = True
78-
isDescription _ = False
79-
8076
data Multiplier = Milli | Micro | Nano | Pico
8177
deriving stock (Eq, Ord, Show, Data, Generic)
8278

@@ -87,18 +83,20 @@ data Network
8783
| BitcoinSignet
8884
deriving stock (Eq, Ord, Show, Data, Generic)
8985

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)
9391

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"
9896
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"
102100
where
103101
btc :: Rational
104102
btc = (amt % 1) * multiplierRatio mul
@@ -107,14 +105,14 @@ instance Show Bolt11Amount where
107105
msat :: Rational
108106
msat = sat * 1000
109107

110-
data Bolt11HRP = Bolt11HRP
111-
{ bolt11Network :: Network,
112-
bolt11Amount :: Maybe Bolt11Amount
108+
data Bolt11Hrp = Bolt11Hrp
109+
{ bolt11HrpNet :: Network,
110+
bolt11HrpAmt :: Maybe Bolt11HrpAmt
113111
}
114112
deriving stock (Eq, Ord, Show, Data, Generic)
115113

116114
data Bolt11 = Bolt11
117-
{ bolt11HRP :: Bolt11HRP,
115+
{ bolt11Hrp :: Bolt11Hrp,
118116
bolt11Timestamp :: Int, -- posix
119117
bolt11Tags :: [Tag], -- posix
120118
bolt11Signature :: Hex
@@ -138,19 +136,19 @@ parseMultiplier = do
138136
'p' -> pure Pico
139137
_ -> fail "unhandled case in parseMultiplier"
140138

141-
parseHrpAmount :: Parser Bolt11Amount
139+
parseHrpAmount :: Parser Bolt11HrpAmt
142140
parseHrpAmount = do
143141
amt <- decimal
144-
multi <- parseMultiplier
145-
pure (Bolt11Amount (amt, multi))
142+
mul <- parseMultiplier
143+
pure $ Bolt11HrpAmt amt mul
146144

147-
hrpParser :: Parser Bolt11HRP
145+
hrpParser :: Parser Bolt11Hrp
148146
hrpParser = do
149147
_ <- char 'l'
150148
_ <- char 'n'
151149
net <- parseNetwork
152150
amt <- optional parseHrpAmount
153-
pure (Bolt11HRP net amt)
151+
pure (Bolt11Hrp net amt)
154152

155153
w5int :: [Word5] -> Int
156154
w5int bytes = foldl' decodeInt 0 (zip [0 ..] (Prelude.take 7 (reverse bytes)))
@@ -225,3 +223,21 @@ multiplierRatio m =
225223
Micro -> 1 % 1000000
226224
Nano -> 1 % 1000000000
227225
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+
}

pub/functora/src/test/Functora/Bolt11Spec.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ goodSamples :: [(Text, Bolt11)]
88
goodSamples =
99
[ ( "lnbc1pvjluezsp5zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zygspp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmwwd5kgetjypeh2ursdae8g6twvus8g6rfwvs8qun0dfjkxaq9qrsgq357wnc5r2ueh7ck6q93dj32dlqnls087fxdwk8qakdyafkq3yap9us6v52vjjsrvywa6rt52cm9r9zqt8r2t7mlcwspyetp5h2tztugp9lfyql",
1010
Bolt11
11-
{ bolt11HRP =
12-
Bolt11HRP
13-
{ bolt11Network = BitcoinMainnet,
14-
bolt11Amount = Nothing
11+
{ bolt11Hrp =
12+
Bolt11Hrp
13+
{ bolt11HrpNet = BitcoinMainnet,
14+
bolt11HrpAmt = Nothing
1515
},
1616
bolt11Timestamp = 1496314658,
1717
bolt11Tags =

0 commit comments

Comments
 (0)