Skip to content

Commit 465fff8

Browse files
committed
wip
1 parent 2d7a6b2 commit 465fff8

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ invoiceWidget ln =
5959
<> pairs
6060
( [ pair "Network"
6161
$ case B11.bolt11Currency $ B11.bolt11HRP ln of
62-
B11.Bitcoin -> "Bitcoin Mainnet"
62+
B11.BitcoinMainnet -> "Bitcoin Mainnet"
6363
B11.BitcoinTestnet -> "Bitcoin Testnet"
64-
B11.BitcoinRegtest -> "Bitcoin Regtest",
64+
B11.BitcoinRegtest -> "Bitcoin Regtest"
65+
B11.BitcoinSignet -> "Bitcoin Signet",
6566
pair "Amount"
6667
. maybe "0" defShow
6768
. B11.bolt11Amount

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Functora.Bolt11
77
decodeBolt11,
88
Hex (..),
99
Multiplier (..),
10-
Currency (..),
10+
Network (..),
1111
Tag (..),
1212
Bolt11HRP (..),
1313
isPaymentHash,
@@ -26,6 +26,7 @@ import qualified Data.ByteString.Lazy.Char8 as BL
2626
import Data.Data (Data)
2727
import Data.Foldable (foldl')
2828
import Data.Maybe (fromMaybe)
29+
import Data.Ratio ((%))
2930
import Data.String (IsString (..))
3031
import Data.Text (Text)
3132
import qualified Data.Text as T
@@ -86,21 +87,22 @@ isDescription _ = False
8687
data Multiplier = Milli | Micro | Nano | Pico
8788
deriving stock (Eq, Ord, Show, Data, Generic)
8889

89-
data Currency
90-
= Bitcoin
90+
data Network
91+
= BitcoinMainnet
9192
| BitcoinTestnet
9293
| BitcoinRegtest
94+
| BitcoinSignet
9395
deriving stock (Eq, Ord, Show, Data, Generic)
9496

95-
newtype Bolt11Amount = Bolt11Amount {_getBolt11Amount :: (Int, Multiplier)}
97+
newtype Bolt11Amount = Bolt11Amount {_getBolt11Amount :: (Integer, Multiplier)}
9698
deriving newtype (Eq, Ord)
9799
deriving stock (Data, Generic)
98100

99101
instance Show Bolt11Amount where
100102
show amt = show (bolt11Msats amt)
101103

102104
data Bolt11HRP = Bolt11HRP
103-
{ bolt11Currency :: Currency,
105+
{ bolt11Currency :: Network,
104106
bolt11Amount :: Maybe Bolt11Amount
105107
}
106108
deriving stock (Eq, Ord, Show, Data, Generic)
@@ -113,11 +115,12 @@ data Bolt11 = Bolt11
113115
}
114116
deriving stock (Eq, Ord, Show, Data, Generic)
115117

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)
119123
<|> (string "tb" *> pure BitcoinTestnet)
120-
<|> (string "bcrt" *> pure BitcoinRegtest)
121124

122125
parseMultiplier :: Parser Multiplier
123126
parseMultiplier = do
@@ -133,19 +136,15 @@ parseHrpAmount :: Parser Bolt11Amount
133136
parseHrpAmount = do
134137
amt <- decimal
135138
multi <- parseMultiplier
136-
return (Bolt11Amount (amt, multi))
139+
pure (Bolt11Amount (amt, multi))
137140

138141
hrpParser :: Parser Bolt11HRP
139142
hrpParser = do
140143
_ <- char 'l'
141144
_ <- 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)
149148

150149
w5int :: [Word5] -> Int
151150
w5int bytes = foldl' decodeInt 0 (zip [0 ..] (Prelude.take 7 (reverse bytes)))
@@ -204,7 +203,7 @@ tagsParser ws
204203

205204
decodeBolt11 :: Text -> Either String Bolt11
206205
decodeBolt11 txt = do
207-
(hrp, w5s) <- maybeToRight "error decoding bech32" (bech32Decode txt)
206+
(hrp, w5s) <- maybe (Left "error decoding bech32") Right $ bech32Decode txt
208207
let (timestampBits, rest) = splitAt 7 w5s
209208
timestamp = w5int timestampBits
210209
(tags, leftover) = tagsParser rest
@@ -217,10 +216,10 @@ decodeBolt11 txt = do
217216
multiplierRatio :: Multiplier -> Rational
218217
multiplierRatio m =
219218
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
224223

225224
bolt11Msats :: Bolt11Amount -> MSats
226225
bolt11Msats (Bolt11Amount (amt, multi)) =

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ goodSamples =
1010
Bolt11
1111
{ bolt11HRP =
1212
Bolt11HRP
13-
{ bolt11Currency = Bitcoin,
13+
{ bolt11Currency = BitcoinMainnet,
1414
bolt11Amount = Nothing
1515
},
1616
bolt11Timestamp = 1496314658,
@@ -28,5 +28,7 @@ goodSamples =
2828

2929
spec :: Spec
3030
spec = do
31-
it "goodSamples" . forM_ goodSamples . uncurry $
32-
\lhs rhs -> decodeBolt11 lhs `shouldBe` Right rhs
31+
it "goodSamples"
32+
. forM_ goodSamples
33+
. uncurry
34+
$ \lhs rhs -> decodeBolt11 lhs `shouldBe` Right rhs

0 commit comments

Comments
 (0)