Skip to content

Commit f4cd357

Browse files
committed
bolt11 wip
1 parent 6c3a9e2 commit f4cd357

File tree

1 file changed

+57
-57
lines changed
  • ghcjs/lightning-verifier/src/App/Widgets

1 file changed

+57
-57
lines changed

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

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,60 @@ import qualified Prelude
1414

1515
bolt11 :: Model -> [View Action]
1616
bolt11 st =
17-
if ln == mempty
18-
then mempty
19-
else case B11.decodeBolt11 ln of
20-
Left e ->
21-
red
22-
$ pairs
23-
[ newFieldPairId mempty
24-
. DynamicFieldText
25-
$ from @Prelude.String @MisoString e
26-
]
27-
Right b11 ->
28-
( case B11.bolt11Tags b11 >>= verifier r of
29-
[] | r /= mempty -> failure "Bad invoice without preimage hash!"
30-
xs -> xs
31-
)
32-
<> ( pairs
33-
$ [ simple "Network" . B11.bolt11Currency $ B11.bolt11HRP b11,
34-
simple "Amount" . B11.bolt11Amount $ B11.bolt11HRP b11,
35-
simple "Timestamp" $ B11.bolt11Timestamp b11
36-
]
37-
<> fmap (simple "Tag") (B11.bolt11Tags b11)
38-
<> [ simple "Signature" $ B11.bolt11Signature b11
39-
]
40-
)
17+
parserWidget ln b11
18+
<> (if isRight b11 then parserWidget ln rh else mempty)
19+
<> parserWidget hexR r
20+
<> fromRight mempty (verifierWidget [ln, hexR] <$> rh <*> r)
21+
<> either (const mempty) invoiceWidget b11
4122
where
42-
r = st ^. #modelState . #stDoc . #stDocLnPreimage . #fieldOutput
4323
ln = st ^. #modelState . #stDoc . #stDocLnInvoice . #fieldOutput
24+
b11 = first inspect $ B11.decodeBolt11 ln
25+
rh = b11 >>= getPreimageHash
26+
hexR = st ^. #modelState . #stDoc . #stDocLnPreimage . #fieldOutput
27+
r = getPreimage hexR
28+
29+
parserWidget :: MisoString -> Either MisoString a -> [View Action]
30+
parserWidget src = \case
31+
Left e | src /= mempty -> failure e
32+
_ -> mempty
33+
34+
verifierWidget :: [MisoString] -> ByteString -> ByteString -> [View Action]
35+
verifierWidget src rh r =
36+
if any (== mempty) src
37+
then mempty
38+
else
39+
if rh == sha256Hash r
40+
then success "Invoice and preimage match!"
41+
else failure "Invoice and preimage mismatch!"
42+
43+
invoiceWidget :: B11.Bolt11 -> [View Action]
44+
invoiceWidget b11 =
45+
pairs
46+
$ [ simple "Network" . B11.bolt11Currency $ B11.bolt11HRP b11,
47+
simple "Amount" . B11.bolt11Amount $ B11.bolt11HRP b11,
48+
simple "Timestamp" $ B11.bolt11Timestamp b11
49+
]
50+
<> fmap (simple "Tag") (B11.bolt11Tags b11)
51+
<> [ simple "Signature" $ B11.bolt11Signature b11
52+
]
53+
where
4454
simple x =
4555
newFieldPairId x
4656
. DynamicFieldText
4757
. from @Prelude.String @MisoString
4858
. Prelude.show
4959

50-
verifier :: MisoString -> B11.Tag -> [View Action]
51-
verifier "" _ =
52-
mempty
53-
verifier r (B11.PaymentHash (B11.Hex expected)) =
60+
getPreimage :: MisoString -> Either MisoString ByteString
61+
getPreimage r =
5462
case B16.decode . T.encodeUtf8 $ from @MisoString @Prelude.Text r of
55-
(rbs, "") -> do
56-
let provided = sha256Hash rbs
57-
if expected == provided
58-
then success "Invoice and preimage match!"
59-
else failure "Invoice and preimage mismatch!"
60-
res ->
61-
failure $ "Bad preimage " <> inspect res
62-
verifier _ _ =
63-
mempty
63+
(x, "") -> Right x
64+
res -> Left $ "Bad preimage " <> inspect res
6465

65-
success :: MisoString -> [View Action]
66-
success msg =
67-
green $ pairs [newFieldPairId mempty $ DynamicFieldText msg]
68-
69-
failure :: MisoString -> [View Action]
70-
failure msg =
71-
red $ pairs [newFieldPairId mempty $ DynamicFieldText msg]
66+
getPreimageHash :: B11.Bolt11 -> Either MisoString ByteString
67+
getPreimageHash b11 =
68+
case find B11.isPaymentHash $ B11.bolt11Tags b11 of
69+
Just (B11.PaymentHash (B11.Hex rh)) -> Right rh
70+
_ -> Left "Bad invoice without preimage hash!"
7271

7372
pairs :: [FieldPair DynamicField f] -> [View Action]
7473
pairs xs =
@@ -82,16 +81,17 @@ pairs xs =
8281
pure next
8382
}
8483

85-
red :: [View action] -> [View action]
86-
red = fmap $ \case
87-
Node x0 x1 x2 x3 x4 ->
88-
Node x0 x1 x2 (class_ "app-failure" : x3) x4
89-
html ->
90-
html
84+
success :: MisoString -> [View Action]
85+
success msg =
86+
addCssClass "app-success"
87+
$ pairs [newFieldPairId mempty $ DynamicFieldText msg]
88+
89+
failure :: MisoString -> [View Action]
90+
failure msg =
91+
addCssClass "app-failure"
92+
$ pairs [newFieldPairId mempty $ DynamicFieldText msg]
9193

92-
green :: [View action] -> [View action]
93-
green = fmap $ \case
94-
Node x0 x1 x2 x3 x4 ->
95-
Node x0 x1 x2 (class_ "app-success" : x3) x4
96-
html ->
97-
html
94+
addCssClass :: MisoString -> [View action] -> [View action]
95+
addCssClass css = fmap $ \case
96+
Node x0 x1 x2 x3 x4 -> Node x0 x1 x2 (class_ css : x3) x4
97+
html -> html

0 commit comments

Comments
 (0)