1
1
module App.Widgets.Bolt11
2
2
( bolt11Viewer ,
3
- evalBolt11 ,
3
+ makeBolt11Viewer ,
4
+ mergeBolt11Viewers ,
4
5
)
5
6
where
6
7
7
8
import App.Types
8
9
import qualified Bitcoin.Address as Btc
9
10
import qualified Data.Aeson as A
10
- import Data.Bitraversable (bimapM )
11
11
import qualified Data.ByteString.Base16 as B16
12
12
import qualified Data.ByteString.Lazy as BL
13
13
import qualified Data.Text.Encoding as T
@@ -88,36 +88,34 @@ inspectTimestamp =
88
88
. Prelude. fromInteger
89
89
. from @ Int @ Integer
90
90
91
- evalBolt11 :: (MonadIO m ) => StDoc Unique -> m (StDoc Unique )
92
- evalBolt11 st = do
93
- lnFields <-
94
- if rawLn == mempty
95
- then pure $ Right mempty
96
- else bimapM plainM (mapM identityToUnique . invoiceFields) ln
97
- preFields <-
98
- if rawR == mempty
99
- then pure $ Right mempty
100
- else bimapM plainM (mapM identityToUnique . preimageFields rawR) r
101
- verifierFields <-
102
- if any @ [MisoString ] (== mempty ) [rawLn, rawR]
103
- then pure $ Right mempty
104
- else case verifyPreimage <$> rh <*> r of
105
- Left {} -> pure $ Right mempty
106
- Right x -> bimapM plainM plainM x
107
- pure
108
- $ st
109
- & # stDocSuccessViewer
110
- %~ mergeFieldPairs (fromRight mempty verifierFields)
111
- & # stDocFailureViewer
112
- %~ mergeFieldPairs
113
- ( fromLeft mempty lnFields
114
- <> fromLeft mempty preFields
115
- <> fromLeft mempty verifierFields
116
- )
117
- & # stDocLnInvoiceViewer
118
- %~ mergeFieldPairs (fromRight mempty lnFields)
119
- & # stDocLnPreimageViewer
120
- %~ mergeFieldPairs (fromRight mempty preFields)
91
+ makeBolt11Viewer :: StDoc Identity -> StDoc Identity
92
+ makeBolt11Viewer st =
93
+ let lnFields =
94
+ if rawLn == mempty
95
+ then pure mempty
96
+ else bimap plain invoiceFields ln
97
+ preFields =
98
+ if rawR == mempty
99
+ then pure mempty
100
+ else bimap plain (preimageFields rawR) r
101
+ verifierFields =
102
+ if any @ [MisoString ] (== mempty ) [rawLn, rawR]
103
+ then pure mempty
104
+ else case verifyPreimage <$> rh <*> r of
105
+ Left {} -> pure mempty
106
+ Right x -> bimap plain plain x
107
+ in st
108
+ & # stDocSuccessViewer
109
+ .~ fromRight mempty verifierFields
110
+ & # stDocFailureViewer
111
+ .~ ( fromLeft mempty lnFields
112
+ <> fromLeft mempty preFields
113
+ <> fromLeft mempty verifierFields
114
+ )
115
+ & # stDocLnInvoiceViewer
116
+ .~ fromRight mempty lnFields
117
+ & # stDocLnPreimageViewer
118
+ .~ fromRight mempty preFields
121
119
where
122
120
rawLn :: MisoString
123
121
rawLn = st ^. # stDocLnInvoice . # fieldOutput
@@ -133,9 +131,21 @@ evalBolt11 st = do
133
131
r :: Either MisoString ByteString
134
132
r = parsePreimage rawR
135
133
136
- plainM :: (MonadIO m ) => MisoString -> m [FieldPair DynamicField Unique ]
137
- plainM =
138
- fmap (: mempty ) . newFieldPair mempty . DynamicFieldText
134
+ mergeBolt11Viewers :: (Foldable1 f ) => StDoc f -> StDoc f -> StDoc f
135
+ mergeBolt11Viewers next prev =
136
+ prev
137
+ & # stDocSuccessViewer
138
+ %~ mergeFieldPairs (stDocSuccessViewer next)
139
+ & # stDocFailureViewer
140
+ %~ mergeFieldPairs (stDocFailureViewer next)
141
+ & # stDocLnInvoiceViewer
142
+ %~ mergeFieldPairs (stDocLnInvoiceViewer next)
143
+ & # stDocLnPreimageViewer
144
+ %~ mergeFieldPairs (stDocLnPreimageViewer next)
145
+
146
+ plain :: MisoString -> [FieldPair DynamicField Identity ]
147
+ plain =
148
+ (: mempty ) . newFieldPairId mempty . DynamicFieldText
139
149
140
150
parsePreimage :: MisoString -> Either MisoString ByteString
141
151
parsePreimage rawR =
0 commit comments