Skip to content

Commit 0983c67

Browse files
committed
unicode refactoring wip
1 parent 390e775 commit 0983c67

File tree

25 files changed

+198
-224
lines changed

25 files changed

+198
-224
lines changed

ghcjs/lightning-verifier/src/App/Types.hs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,18 @@ import Functora.Miso.Prelude
3636
import Functora.Miso.Types as X
3737
import Functora.Money hiding (Currency, Money, Text)
3838
import qualified Functora.Prelude as Prelude
39-
import qualified Miso.String as MS
4039
import qualified Paths_lightning_verifier as Paths
4140
import qualified Text.URI as URI
4241
import qualified Text.URI.QQ as URI
43-
import qualified Prelude
4442

4543
data Model = Model
4644
{ modelFav :: OpenedOrClosed,
4745
modelMenu :: OpenedOrClosed,
4846
modelLinks :: OpenedOrClosed,
4947
modelLoading :: Bool,
5048
modelState :: St Unique,
51-
modelFavMap :: Map MisoString Fav,
52-
modelFavName :: Field MisoString Unique,
49+
modelFavMap :: Map Unicode Fav,
50+
modelFavName :: Field Unicode Unique,
5351
modelUriViewer :: [FieldPair DynamicField Unique],
5452
modelProducerQueue :: TChan (InstantOrDelayed (Model -> JSM Model)),
5553
modelConsumerQueue :: TChan (InstantOrDelayed (Model -> JSM Model))
@@ -65,7 +63,7 @@ data Action
6563

6664
data St f = St
6765
{ stKm :: Aes.Km,
68-
stIkm :: Field MisoString f,
66+
stIkm :: Field Unicode f,
6967
stDoc :: StDoc f,
7068
stPre :: Field DynamicField f,
7169
stScreen :: Screen,
@@ -91,9 +89,9 @@ data StDoc f = StDoc
9189
{ stDocFieldPairs :: [FieldPair DynamicField f],
9290
stDocSuccessViewer :: [FieldPair DynamicField f],
9391
stDocFailureViewer :: [FieldPair DynamicField f],
94-
stDocLnPreimage :: Field MisoString f,
92+
stDocLnPreimage :: Field Unicode f,
9593
stDocLnPreimageViewer :: [FieldPair DynamicField f],
96-
stDocLnInvoice :: Field MisoString f,
94+
stDocLnInvoice :: Field Unicode f,
9795
stDocLnInvoiceViewer :: [FieldPair DynamicField f]
9896
}
9997
deriving stock (Generic)
@@ -230,7 +228,7 @@ stQuery st = do
230228

231229
stUri :: (MonadThrow m) => Model -> m URI
232230
stUri st = do
233-
uri <- mkURI $ fromMisoString @Prelude.Text baseUri
231+
uri <- mkURI $ from @Unicode @Prelude.Text baseUri
234232
qxs <- stQuery . uniqueToIdentity $ st ^. #modelState
235233
pure
236234
$ uri
@@ -256,18 +254,18 @@ compressViewers st =
256254
pair
257255
& #fieldPairKey
258256
. #fieldInput
259-
.~ Identity (mempty :: MisoString)
257+
.~ Identity (mempty :: Unicode)
260258
& #fieldPairKey
261259
. #fieldOutput
262-
.~ (mempty :: MisoString)
260+
.~ (mempty :: Unicode)
263261
& #fieldPairValue
264262
. #fieldInput
265-
.~ Identity (mempty :: MisoString)
263+
.~ Identity (mempty :: Unicode)
266264
& #fieldPairValue
267265
. #fieldOutput
268266
.~ DynamicFieldText mempty
269267

270-
baseUri :: MisoString
268+
baseUri :: Unicode
271269
#ifdef GHCID
272270
baseUri =
273271
"http://localhost:8080"
@@ -286,10 +284,10 @@ setScreenAction =
286284
. Instant
287285
. setScreenPure
288286

289-
vsn :: MisoString
287+
vsn :: Unicode
290288
vsn =
291-
MS.intercalate "."
292-
. fmap (toMisoString @Prelude.String . Prelude.show)
289+
intercalate "."
290+
. fmap inspect
293291
$ Version.versionBranch Paths.version
294292

295293
usd :: CurrencyInfo
@@ -318,7 +316,7 @@ apkLink :: URI
318316
apkLink =
319317
either impureThrow id
320318
. URI.mkURI
321-
. fromMisoString @Prelude.Text
319+
. from @Unicode @Prelude.Text
322320
$ "https://github.com/functora/functora.github.io/releases/download/lightning-verifier-v"
323321
<> vsn
324322
<> "/lightning-verifier-v"

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

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import qualified Bitcoin.Address as Btc
1010
import qualified Data.Aeson as A
1111
import qualified Data.ByteString.Base16 as B16
1212
import qualified Data.ByteString.Lazy as BL
13-
import qualified Data.Text.Lazy as TL
1413
import qualified Functora.Bolt11 as B11
1514
import Functora.Miso.Prelude
1615
import qualified Functora.Miso.Widgets.FieldPairs as FieldPairs
@@ -65,7 +64,7 @@ pairs st header optic =
6564
FieldPairs.argsAction = PushUpdate . Instant
6665
}
6766

68-
pair :: MisoString -> MisoString -> FieldPair DynamicField Identity
67+
pair :: Unicode -> Unicode -> FieldPair DynamicField Identity
6968
pair x =
7069
newFieldPairId x
7170
. DynamicFieldText
@@ -76,12 +75,12 @@ success = css "app-success"
7675
failure :: [View Action] -> [View Action]
7776
failure = css "app-failure"
7877

79-
css :: MisoString -> [View action] -> [View action]
78+
css :: Unicode -> [View action] -> [View action]
8079
css x = fmap $ \case
8180
Node x0 x1 x2 x3 x4 -> Node x0 x1 x2 (class_ x : x3) x4
8281
html -> html
8382

84-
inspectTimestamp :: Int -> MisoString
83+
inspectTimestamp :: Int -> Unicode
8584
inspectTimestamp =
8685
inspect
8786
. posixSecondsToUTCTime
@@ -99,7 +98,7 @@ makeBolt11Viewer st =
9998
then pure mempty
10099
else bimap plain (preimageFields rawR) r
101100
verifierFields =
102-
if any @[MisoString] (== mempty) [rawLn, rawR]
101+
if any @[Unicode] (== mempty) [rawLn, rawR]
103102
then pure mempty
104103
else case verifyPreimage <$> rh <*> r of
105104
Left {} -> pure mempty
@@ -117,18 +116,18 @@ makeBolt11Viewer st =
117116
& #stDocLnPreimageViewer
118117
.~ fromRight mempty preFields
119118
where
120-
rawLn :: MisoString
119+
rawLn :: Unicode
121120
rawLn = st ^. #stDocLnInvoice . #fieldOutput
122-
rawR :: MisoString
121+
rawR :: Unicode
123122
rawR = st ^. #stDocLnPreimage . #fieldOutput
124-
ln :: Either MisoString B11.Bolt11
123+
ln :: Either Unicode B11.Bolt11
125124
ln =
126-
first (mappend "Bad invoice - " . toMisoString @Prelude.String)
125+
first (mappend "Bad invoice - " . from @Prelude.String @Unicode)
127126
. B11.decodeBolt11
128-
$ fromMisoString @Prelude.Text rawLn
129-
rh :: Either MisoString ByteString
127+
$ from @Unicode @Prelude.Text rawLn
128+
rh :: Either Unicode ByteString
130129
rh = ln >>= parsePreimageHash
131-
r :: Either MisoString ByteString
130+
r :: Either Unicode ByteString
132131
r = parsePreimage rawR
133132

134133
mergeBolt11Viewers :: (Foldable1 f) => StDoc f -> StDoc f -> StDoc f
@@ -143,21 +142,17 @@ mergeBolt11Viewers next prev =
143142
& #stDocLnPreimageViewer
144143
%~ mergeFieldPairs (stDocLnPreimageViewer next)
145144

146-
plain :: MisoString -> [FieldPair DynamicField Identity]
145+
plain :: Unicode -> [FieldPair DynamicField Identity]
147146
plain =
148147
(: mempty) . newFieldPairId mempty . DynamicFieldText
149148

150-
parsePreimage :: MisoString -> Either MisoString ByteString
149+
parsePreimage :: Unicode -> Either Unicode ByteString
151150
parsePreimage rawR =
152-
case B16.decode $ fromMisoString @ByteString rawR of
151+
case B16.decode $ encodeUtf8 rawR of
153152
(r, "") -> Right r
154-
(_, e) ->
155-
Left
156-
. toMisoString @Prelude.String
157-
$ "Bad preimage - non hex leftover "
158-
<> Prelude.show e
153+
(_, e) -> Left $ "Bad preimage - non hex leftover " <> inspect @Unicode e
159154

160-
parsePreimageHash :: B11.Bolt11 -> Either MisoString ByteString
155+
parsePreimageHash :: B11.Bolt11 -> Either Unicode ByteString
161156
parsePreimageHash ln =
162157
case find B11.isPaymentHash $ B11.bolt11Tags ln of
163158
Just (B11.PaymentHash (B11.Hex rh)) -> Right rh
@@ -166,7 +161,7 @@ parsePreimageHash ln =
166161
verifyPreimage ::
167162
ByteString ->
168163
ByteString ->
169-
Either MisoString MisoString
164+
Either Unicode Unicode
170165
verifyPreimage rh r =
171166
if rh == sha256Hash r
172167
then Right "The preimage matches the invoice"
@@ -181,11 +176,7 @@ invoiceFields ln =
181176
B11.BitcoinRegtest -> "Bitcoin Regtest"
182177
B11.BitcoinSignet -> "Bitcoin Signet",
183178
pair "Amount"
184-
. maybe
185-
"0"
186-
( toMisoString @Prelude.String
187-
. B11.inspectBolt11HrpAmt
188-
)
179+
. maybe "0" B11.inspectBolt11HrpAmt
189180
. B11.bolt11HrpAmt
190181
$ B11.bolt11Hrp ln,
191182
pair "Created At"
@@ -196,7 +187,6 @@ invoiceFields ln =
196187
>>= invoiceFieldsTag ln
197188
)
198189
<> [ pair "Signature"
199-
. toMisoString @TL.Text
200190
. B11.inspectHex
201191
$ B11.bolt11SigVal sig,
202192
pair "Pubkey Recovery Flag"
@@ -221,36 +211,39 @@ invoiceFieldsTag ln = \case
221211
B11.OnchainFallback x ->
222212
pure
223213
. pair "Onchain Fallback"
224-
. toMisoString @ByteString
214+
. either impureThrow id
215+
. decodeUtf8Strict @Unicode @ByteString
225216
$ Btc.renderAddress x
226217
B11.ExtraRouteInfo x ->
227218
pure
228219
. pair "Extra Routing Info"
229-
. toMisoString @BL.ByteString
220+
. either impureThrow id
221+
. decodeUtf8Strict @Unicode @BL.ByteString
230222
$ A.encode x
231223
B11.Features x ->
232224
pure
233225
. pair "Feature Bits"
234-
. toMisoString @Prelude.Text
235226
$ B11.inspectFeatures x
236227
B11.UnknownTag {} -> mempty
237228
B11.UnparsedTag {} -> mempty
238229
where
239-
w5s :: MisoString -> [B11.Word5] -> [FieldPair DynamicField Identity]
230+
w5s :: Unicode -> [B11.Word5] -> [FieldPair DynamicField Identity]
240231
w5s x =
241232
pure
242233
. pair x
243234
. inspect
244235
. fmap fromEnum
245-
hex :: MisoString -> B11.Hex -> [FieldPair DynamicField Identity]
236+
hex :: Unicode -> B11.Hex -> [FieldPair DynamicField Identity]
246237
hex x =
247238
pure
248239
. pair x
249-
. toMisoString @TL.Text
250240
. B11.inspectHex
251241

252-
preimageFields :: MisoString -> ByteString -> [FieldPair DynamicField Identity]
242+
preimageFields :: Unicode -> ByteString -> [FieldPair DynamicField Identity]
253243
preimageFields rawR r =
254244
[ pair "Preimage" rawR,
255-
pair "Preimage Hash" . inspect @ByteString $ sha256Hash r
245+
pair "Preimage Hash"
246+
. either impureThrow id
247+
. decodeUtf8Strict @Unicode @ByteString
248+
$ sha256Hash r
256249
]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ decrypt st =
5050

5151
decryptDoc :: Model -> JSM Model
5252
decryptDoc st@Model {modelState = St {stCpt = Nothing}} = do
53-
Jsm.popupText @MisoString "Nothing to decrypt!"
53+
Jsm.popupText @Unicode "Nothing to decrypt!"
5454
pure st
5555
decryptDoc st@Model {modelState = St {stCpt = Just cpt}} = do
5656
let ikm = st ^. #modelState . #stIkm . #fieldOutput

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fav st =
112112
then saveAction
113113
else Jsm.enterOrEscapeBlur uid code
114114

115-
makeFavName :: Model -> MisoString
115+
makeFavName :: Model -> Unicode
116116
makeFavName =
117117
MS.toUpper
118118
. MS.strip
@@ -127,7 +127,7 @@ favItems st =
127127
$ st
128128
^. #modelFavMap
129129

130-
favItem :: Model -> MisoString -> Fav -> View Action
130+
favItem :: Model -> Unicode -> Fav -> View Action
131131
favItem st label Fav {favUri = uri} =
132132
Grid.bigCell
133133
[ Button.raised

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ screenWidget st@Model {modelState = St {stScreen = Converter}} =
173173
<> Bolt11.bolt11Viewer st
174174

175175
pasteWidget ::
176-
MisoString ->
177-
((Maybe MisoString -> JSM ()) -> JSM ()) ->
178-
ATraversal' Model (Field MisoString Unique) ->
176+
Unicode ->
177+
((Maybe Unicode -> JSM ()) -> JSM ()) ->
178+
ATraversal' Model (Field Unicode Unique) ->
179179
Maybe (Field.OptsWidget Model Action)
180180
pasteWidget icon selector optic =
181181
Just
@@ -185,14 +185,14 @@ pasteWidget icon selector optic =
185185
$ \prev -> do
186186
selector $ \case
187187
Nothing ->
188-
Jsm.popupText @MisoString "Failure!"
188+
Jsm.popupText @Unicode "Failure!"
189189
Just res -> do
190190
Misc.pushActionQueue prev
191191
. Instant
192192
$ pure
193193
. (& cloneTraversal optic . #fieldOutput .~ res)
194194
. (& cloneTraversal optic . #fieldInput . #uniqueValue .~ res)
195-
Jsm.popupText @MisoString "Success!"
195+
Jsm.popupText @Unicode "Success!"
196196
pure prev
197197

198198
tosWidget :: View Action

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ menu st =
9393
void
9494
$ JS.global
9595
^. JS.js1
96-
("printCurrentPage" :: MisoString)
97-
("lightning-verifier" :: MisoString)
96+
("printCurrentPage" :: Unicode)
97+
("lightning-verifier" :: Unicode)
9898
pure next
9999
)
100100
& IconButton.setAttributes
@@ -109,7 +109,7 @@ menu st =
109109
( PushUpdate
110110
. Instant
111111
. Jsm.shareText
112-
. toMisoString @Prelude.String
112+
. from @Prelude.String @Unicode
113113
. either impureThrow URI.renderStr
114114
$ stUri st
115115
)
@@ -291,7 +291,7 @@ menu st =
291291
[ x
292292
]
293293

294-
defFavName :: Model -> MisoString
294+
defFavName :: Model -> Unicode
295295
defFavName st =
296296
if isJust (st ^. #modelState . #stCpt)
297297
|| (st ^. #modelState . #stIkm . #fieldOutput /= mempty)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ newDonateDoc = do
128128
qr :: FieldPair a b -> FieldPair a b
129129
qr = (& #fieldPairValue . #fieldType .~ FieldTypeQrCode)
130130

131-
exampleBtcAddress :: MisoString
131+
exampleBtcAddress :: Unicode
132132
exampleBtcAddress = "bc1qa3qk8d4mxl6qkpvahl5xvg6c5k33kmuwvt9v8q"
133133

134-
exampleXmrAddress :: MisoString
134+
exampleXmrAddress :: Unicode
135135
exampleXmrAddress =
136136
"48sTw2TvjuWKkaomi9J7gLExRUJLJCvUHLrbf8M8qmayQ9zkho1GYdCXVtpTPawNWH7mNS49N4E6HNDF95dtggMMCigrVyG"
137137

138-
exampleDonationText :: MisoString
138+
exampleDonationText :: Unicode
139139
exampleDonationText =
140140
"I'm Functora, the creator of this software. If you're enjoying it, a donation would be greatly appreciated. Sincerely yours, Functora."

0 commit comments

Comments
 (0)