@@ -4,45 +4,94 @@ module App.Widgets.Bolt11
4
4
where
5
5
6
6
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
8
10
import Functora.Miso.Prelude
9
11
import qualified Functora.Miso.Widgets.FieldPairs as FieldPairs
12
+ import qualified Functora.Prelude as Prelude
10
13
import qualified Prelude
11
14
12
15
bolt11 :: Model -> [View Action ]
13
16
bolt11 st =
14
17
if ln == mempty
15
18
then mempty
16
- else case Bolt11 . decodeBolt11 ln of
19
+ else case B11 . decodeBolt11 ln of
17
20
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
28
26
]
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
+ )
31
41
where
32
- -- r = st ^. #modelState . #stDoc . #stDocLnPreimage . #fieldOutput
42
+ r = st ^. # modelState . # stDoc . # stDocLnPreimage . # fieldOutput
33
43
ln = st ^. # modelState . # stDoc . # stDocLnInvoice . # fieldOutput
34
44
simple x =
35
45
newFieldPairId x
36
46
. DynamicFieldText
37
47
. from @ Prelude. String @ MisoString
38
48
. 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
0 commit comments