@@ -9,22 +9,27 @@ import qualified Data.Text.Encoding as T
9
9
import qualified Functora.Bolt11 as B11
10
10
import Functora.Miso.Prelude
11
11
import qualified Functora.Miso.Widgets.FieldPairs as FieldPairs
12
+ import qualified Functora.Miso.Widgets.Header as Header
12
13
import qualified Functora.Prelude as Prelude
13
14
import qualified Prelude
14
15
15
16
bolt11 :: Model -> [View Action ]
16
17
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
22
27
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
28
33
29
34
parserWidget :: MisoString -> Either MisoString a -> [View Action ]
30
35
parserWidget src = \ case
@@ -41,31 +46,48 @@ verifierWidget src rh r =
41
46
else failure " Invoice and preimage mismatch!"
42
47
43
48
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
+ )
53
60
where
61
+ simple :: (Show a ) => MisoString -> a -> FieldPair DynamicField Identity
54
62
simple x =
55
63
newFieldPairId x
56
64
. DynamicFieldText
57
65
. from @ Prelude. String @ MisoString
58
66
. Prelude. show
59
67
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
64
86
res -> Left $ " Bad preimage " <> inspect res
65
87
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
69
91
Just (B11. PaymentHash (B11. Hex rh)) -> Right rh
70
92
_ -> Left " Bad invoice without preimage hash!"
71
93
@@ -83,15 +105,15 @@ pairs xs =
83
105
84
106
success :: MisoString -> [View Action ]
85
107
success msg =
86
- addCssClass " app-success"
108
+ css " app-success"
87
109
$ pairs [newFieldPairId mempty $ DynamicFieldText msg]
88
110
89
111
failure :: MisoString -> [View Action ]
90
112
failure msg =
91
- addCssClass " app-failure"
113
+ css " app-failure"
92
114
$ pairs [newFieldPairId mempty $ DynamicFieldText msg]
93
115
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
97
119
html -> html
0 commit comments