Skip to content

Commit 26c1234

Browse files
committed
WIP
1 parent f4cd357 commit 26c1234

File tree

1 file changed

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

1 file changed

+53
-31
lines changed

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

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,27 @@ import qualified Data.Text.Encoding as T
99
import qualified Functora.Bolt11 as B11
1010
import Functora.Miso.Prelude
1111
import qualified Functora.Miso.Widgets.FieldPairs as FieldPairs
12+
import qualified Functora.Miso.Widgets.Header as Header
1213
import qualified Functora.Prelude as Prelude
1314
import qualified Prelude
1415

1516
bolt11 :: Model -> [View Action]
1617
bolt11 st =
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
18+
parserWidget rawLn ln
19+
<> (if isRight ln then parserWidget rawLn rh else mempty)
20+
<> parserWidget rawR r
21+
<> fromRight mempty (verifierWidget [rawLn, rawR] <$> rh <*> r)
22+
<> either (const mempty) invoiceWidget ln
23+
<> either
24+
(const mempty)
25+
(\x -> if x == mempty then mempty else preimageWidget x)
26+
r
2227
where
23-
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+
rawLn = st ^. #modelState . #stDoc . #stDocLnInvoice . #fieldOutput
29+
rawR = st ^. #modelState . #stDoc . #stDocLnPreimage . #fieldOutput
30+
ln = first inspect $ B11.decodeBolt11 rawLn
31+
rh = ln >>= parsePreimageHash
32+
r = parsePreimage rawR
2833

2934
parserWidget :: MisoString -> Either MisoString a -> [View Action]
3035
parserWidget src = \case
@@ -41,31 +46,48 @@ verifierWidget src rh r =
4146
else failure "Invoice and preimage mismatch!"
4247

4348
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-
]
49+
invoiceWidget ln =
50+
Header.headerViewer "Invoice Details"
51+
<> pairs
52+
( [ simple "Network" . B11.bolt11Currency $ B11.bolt11HRP ln,
53+
simple "Amount" . B11.bolt11Amount $ B11.bolt11HRP ln,
54+
simple "Timestamp" $ B11.bolt11Timestamp ln
55+
]
56+
<> fmap (simple "Tag") (B11.bolt11Tags ln)
57+
<> [ simple "Signature" $ B11.bolt11Signature ln
58+
]
59+
)
5360
where
61+
simple :: (Show a) => MisoString -> a -> FieldPair DynamicField Identity
5462
simple x =
5563
newFieldPairId x
5664
. DynamicFieldText
5765
. from @Prelude.String @MisoString
5866
. Prelude.show
5967

60-
getPreimage :: MisoString -> Either MisoString ByteString
61-
getPreimage r =
62-
case B16.decode . T.encodeUtf8 $ from @MisoString @Prelude.Text r of
63-
(x, "") -> Right x
68+
preimageWidget :: ByteString -> [View Action]
69+
preimageWidget r =
70+
Header.headerViewer "Preimage Details"
71+
<> pairs
72+
[ simple "Preimage" r,
73+
simple "Preimage Hash" $ sha256Hash r
74+
]
75+
where
76+
simple :: MisoString -> ByteString -> FieldPair DynamicField Identity
77+
simple x =
78+
newFieldPairId x
79+
. DynamicFieldText
80+
. inspect
81+
82+
parsePreimage :: MisoString -> Either MisoString ByteString
83+
parsePreimage rawR =
84+
case B16.decode . T.encodeUtf8 $ from @MisoString @Prelude.Text rawR of
85+
(r, "") -> Right r
6486
res -> Left $ "Bad preimage " <> inspect res
6587

66-
getPreimageHash :: B11.Bolt11 -> Either MisoString ByteString
67-
getPreimageHash b11 =
68-
case find B11.isPaymentHash $ B11.bolt11Tags b11 of
88+
parsePreimageHash :: B11.Bolt11 -> Either MisoString ByteString
89+
parsePreimageHash ln =
90+
case find B11.isPaymentHash $ B11.bolt11Tags ln of
6991
Just (B11.PaymentHash (B11.Hex rh)) -> Right rh
7092
_ -> Left "Bad invoice without preimage hash!"
7193

@@ -83,15 +105,15 @@ pairs xs =
83105

84106
success :: MisoString -> [View Action]
85107
success msg =
86-
addCssClass "app-success"
108+
css "app-success"
87109
$ pairs [newFieldPairId mempty $ DynamicFieldText msg]
88110

89111
failure :: MisoString -> [View Action]
90112
failure msg =
91-
addCssClass "app-failure"
113+
css "app-failure"
92114
$ pairs [newFieldPairId mempty $ DynamicFieldText msg]
93115

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
116+
css :: MisoString -> [View action] -> [View action]
117+
css x = fmap $ \case
118+
Node x0 x1 x2 x3 x4 -> Node x0 x1 x2 (class_ x : x3) x4
97119
html -> html

0 commit comments

Comments
 (0)