Skip to content

Commit 6c3a9e2

Browse files
committed
wip
1 parent ff4be92 commit 6c3a9e2

File tree

4 files changed

+86
-25
lines changed

4 files changed

+86
-25
lines changed

ghcjs/lightning-verifier/lightning-verifier.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ common pkg
9696
, async
9797
, barbies
9898
, base
99+
, base16-bytestring
99100
, base64-bytestring
100101
, binary
101102
, bytestring

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

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,94 @@ module App.Widgets.Bolt11
44
where
55

66
import App.Types
7-
import qualified Functora.Bolt11 as Bolt11
7+
import qualified Data.ByteString.Base16 as B16
8+
import qualified Data.Text.Encoding as T
9+
import qualified Functora.Bolt11 as B11
810
import Functora.Miso.Prelude
911
import qualified Functora.Miso.Widgets.FieldPairs as FieldPairs
12+
import qualified Functora.Prelude as Prelude
1013
import qualified Prelude
1114

1215
bolt11 :: Model -> [View Action]
1316
bolt11 st =
1417
if ln == mempty
1518
then mempty
16-
else case Bolt11.decodeBolt11 ln of
19+
else case B11.decodeBolt11 ln of
1720
Left e ->
18-
widget
19-
[ newFieldPairId mempty
20-
. DynamicFieldText
21-
$ from @Prelude.String @MisoString e
22-
]
23-
Right b11 ->
24-
widget
25-
$ [ simple "Network" . Bolt11.bolt11Currency $ Bolt11.bolt11HRP b11,
26-
simple "Amount" . Bolt11.bolt11Amount $ Bolt11.bolt11HRP b11,
27-
simple "Timestamp" $ Bolt11.bolt11Timestamp b11
21+
red
22+
$ pairs
23+
[ newFieldPairId mempty
24+
. DynamicFieldText
25+
$ from @Prelude.String @MisoString e
2826
]
29-
<> fmap (simple "Tag") (Bolt11.bolt11Tags b11)
30-
<> [simple "Signature" $ Bolt11.bolt11Signature b11]
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+
)
3141
where
32-
-- r = st ^. #modelState . #stDoc . #stDocLnPreimage . #fieldOutput
42+
r = st ^. #modelState . #stDoc . #stDocLnPreimage . #fieldOutput
3343
ln = st ^. #modelState . #stDoc . #stDocLnInvoice . #fieldOutput
3444
simple x =
3545
newFieldPairId x
3646
. DynamicFieldText
3747
. from @Prelude.String @MisoString
3848
. Prelude.show
39-
widget xs =
40-
FieldPairs.fieldPairsViewer
41-
FieldPairs.Args
42-
{ FieldPairs.argsModel = xs,
43-
FieldPairs.argsOptic = id,
44-
FieldPairs.argsAction =
45-
\fun -> PushUpdate . Instant $ \next -> do
46-
void $ fun xs
47-
pure next
48-
}
49+
50+
verifier :: MisoString -> B11.Tag -> [View Action]
51+
verifier "" _ =
52+
mempty
53+
verifier r (B11.PaymentHash (B11.Hex expected)) =
54+
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
64+
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]
72+
73+
pairs :: [FieldPair DynamicField f] -> [View Action]
74+
pairs xs =
75+
FieldPairs.fieldPairsViewer
76+
FieldPairs.Args
77+
{ FieldPairs.argsModel = xs,
78+
FieldPairs.argsOptic = id,
79+
FieldPairs.argsAction =
80+
\fun -> PushUpdate . Instant $ \next -> do
81+
void $ fun xs
82+
pure next
83+
}
84+
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
91+
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

ghcjs/lightning-verifier/static/app.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ textarea {
3333
height: 56px;
3434
}
3535

36+
.app-success > div > span {
37+
color: white;
38+
background-color: #018786 !important;
39+
}
40+
41+
.app-failure > div > span {
42+
color: white;
43+
background-color: #b00020 !important;
44+
}
45+
3646
@media print {
3747
body {
3848
color-adjust: exact !important;

ghcjs/miso-widgets/src/Functora/Miso/Widgets/Field.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ plain out widget =
753753
("height", "auto"),
754754
("padding-top", "8px"),
755755
("padding-bottom", "8px"),
756+
("border-radius", "4px"),
756757
("line-height", "150%")
757758
]
758759
]

0 commit comments

Comments
 (0)