@@ -14,61 +14,60 @@ import qualified Prelude
14
14
15
15
bolt11 :: Model -> [View Action ]
16
16
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
41
22
where
42
- r = st ^. # modelState . # stDoc . # stDocLnPreimage . # fieldOutput
43
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
+
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
44
54
simple x =
45
55
newFieldPairId x
46
56
. DynamicFieldText
47
57
. from @ Prelude. String @ MisoString
48
58
. Prelude. show
49
59
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 =
54
62
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
64
65
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!"
72
71
73
72
pairs :: [FieldPair DynamicField f ] -> [View Action ]
74
73
pairs xs =
@@ -82,16 +81,17 @@ pairs xs =
82
81
pure next
83
82
}
84
83
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]
91
93
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