Skip to content

Commit d2c507c

Browse files
committed
bolt11 wip
1 parent 27d58e2 commit d2c507c

File tree

11 files changed

+108
-18
lines changed

11 files changed

+108
-18
lines changed

ghcjs/lightning-verifier/default.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
let
22
# functora = ../..;
33
functora = fetchTarball {
4-
url = "https://github.com/functora/functora.github.io/archive/03df5e6d39d0fdc31a53cc350e1125118b4a12ca.tar.gz";
5-
sha256 = "0pad1981bpnl1szvlhgl00ai9ikz9nsl4yv6mxxv0q46j10bdchz";
4+
url = "https://github.com/functora/functora.github.io/archive/27d58e2f02ed0d17c94e3346f7d45731f8f6b06c.tar.gz";
5+
sha256 = "0mh2fygiclggyl31jhjbgzxpbl933nx10xmz3xl1798bpc0wl2j2";
66
};
77
legacy = fetchTarball {
88
url = "https://github.com/functora/functora.github.io/archive/7a06c850a579058f495ca2d7e5e2f8682ea0998f.tar.gz";

ghcjs/lightning-verifier/lightning-verifier.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ common pkg
2727
other-modules:
2828
App.Misc
2929
App.Types
30+
App.Widgets.Bolt11
3031
App.Widgets.Decrypt
3132
App.Widgets.Fav
3233
App.Widgets.Main
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module App.Widgets.Bolt11
2+
( bolt11,
3+
)
4+
where
5+
6+
import App.Types
7+
import qualified Functora.Bolt11 as Bolt11
8+
import Functora.Miso.Prelude
9+
import qualified Functora.Miso.Widgets.FieldPairs as FieldPairs
10+
import qualified Prelude
11+
12+
bolt11 :: Model -> [View Action]
13+
bolt11 st =
14+
if ln == mempty
15+
then mempty
16+
else case Bolt11.decodeBolt11 ln of
17+
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
28+
]
29+
<> fmap (simple "Tag") (Bolt11.bolt11Tags b11)
30+
<> [simple "Signature" $ Bolt11.bolt11Signature b11]
31+
where
32+
-- r = st ^. #modelState . #stDoc . #stDocLnPreimage . #fieldOutput
33+
ln = st ^. #modelState . #stDoc . #stDocLnInvoice . #fieldOutput
34+
simple x = newFieldPairId x . DynamicFieldText . inspect
35+
widget xs =
36+
FieldPairs.fieldPairsViewer
37+
FieldPairs.Args
38+
{ FieldPairs.argsModel = xs,
39+
FieldPairs.argsOptic = id,
40+
FieldPairs.argsAction =
41+
\fun -> PushUpdate . Instant $ \next -> do
42+
void $ fun xs
43+
pure next
44+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module App.Widgets.Main (mainWidget) where
22

33
import App.Types
4+
import qualified App.Widgets.Bolt11 as Bolt11
45
import qualified App.Widgets.Decrypt as Decrypt
56
import qualified App.Widgets.Menu as Menu
67
import qualified Functora.Miso.Css as Css
@@ -141,6 +142,7 @@ screenWidget st@Model {modelState = St {stScreen = Converter}} =
141142
.~ "Preimage"
142143
)
143144
]
145+
<> Bolt11.bolt11 st
144146

145147
tosWidget :: View Action
146148
tosWidget =

ghcjs/miso-widgets/src/Functora/Miso/Types.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Functora.Miso.Types
88
newUniqueDuplicator,
99
Field (..),
1010
newField,
11+
newFieldId,
1112
newRatioField,
1213
newTextField,
1314
newPasswordField,
@@ -21,6 +22,7 @@ module Functora.Miso.Types
2122
userFieldType,
2223
FieldPair (..),
2324
newFieldPair,
25+
newFieldPairId,
2426
Currency (..),
2527
newCurrency,
2628
Money (..),
@@ -139,6 +141,16 @@ newField typ output newInput = do
139141
fieldModalState = Closed
140142
}
141143

144+
newFieldId :: FieldType -> a -> Field a Identity
145+
newFieldId typ output =
146+
Field
147+
{ fieldType = typ,
148+
fieldInput = mempty,
149+
fieldOutput = output,
150+
fieldAllowCopy = True,
151+
fieldModalState = Closed
152+
}
153+
142154
newRatioField :: (MonadIO m) => Rational -> m (Field Rational Unique)
143155
newRatioField output =
144156
newField FieldTypeNumber output inspectRatioDef
@@ -251,6 +263,18 @@ newFieldPair key val =
251263
<$> newTextField key
252264
<*> newDynamicField val
253265

266+
newFieldPairId :: MisoString -> DynamicField -> FieldPair DynamicField Identity
267+
newFieldPairId key val =
268+
FieldPair
269+
(newFieldId FieldTypeText key)
270+
( newFieldId
271+
( case val of
272+
DynamicFieldNumber {} -> FieldTypeNumber
273+
DynamicFieldText {} -> FieldTypeText
274+
)
275+
val
276+
)
277+
254278
data Currency f = Currency
255279
{ currencyInput :: Field MisoString f,
256280
currencyOutput :: CurrencyInfo,

ghcjs/miso-widgets/src/Functora/Miso/Widgets/Field.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,9 @@ constTextField txt opts action =
702702
-- TODO : support optional copying widgets
703703
--
704704
dynamicFieldViewer ::
705-
forall model action.
705+
forall model action f.
706706
((model -> JSM model) -> action) ->
707-
Field DynamicField Unique ->
707+
Field DynamicField f ->
708708
[View action]
709709
dynamicFieldViewer action value =
710710
case value ^. #fieldType of

ghcjs/miso-widgets/src/Functora/Miso/Widgets/FieldPairs.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ import qualified Functora.Miso.Widgets.Field as Field
1212
import qualified Functora.Miso.Widgets.Grid as Grid
1313
import qualified Material.Typography as Typography
1414

15-
data Args model action = Args
15+
data Args model action f = Args
1616
{ argsModel :: model,
17-
argsOptic :: ATraversal' model [FieldPair DynamicField Unique],
17+
argsOptic :: ATraversal' model [FieldPair DynamicField f],
1818
argsAction :: (model -> JSM model) -> action
1919
}
2020
deriving stock (Generic)
2121

22-
fieldPairsViewer :: Args model action -> [View action]
22+
fieldPairsViewer :: Args model action f -> [View action]
2323
fieldPairsViewer args@Args {argsOptic = optic} = do
2424
item <- fromMaybe mempty $ args ^? #argsModel . cloneTraversal optic
2525
fieldPairViewer args item
2626

2727
fieldPairViewer ::
28-
Args model action ->
29-
FieldPair DynamicField Unique ->
28+
Args model action f ->
29+
FieldPair DynamicField f ->
3030
[View action]
3131
fieldPairViewer args pair =
3232
( if k == mempty
@@ -71,14 +71,14 @@ fieldPairViewer args pair =
7171
then Grid.bigCell
7272
else Grid.mediumCell
7373

74-
fieldPairsEditor :: Args model action -> [View action]
74+
fieldPairsEditor :: Args model action Unique -> [View action]
7575
fieldPairsEditor args@Args {argsModel = st, argsOptic = optic} = do
7676
idx <- fst <$> zip [0 ..] (fromMaybe mempty $ st ^? cloneTraversal optic)
7777
fieldPairEditor args idx
7878

7979
fieldPairEditor ::
8080
forall model action.
81-
Args model action ->
81+
Args model action Unique ->
8282
Int ->
8383
[View action]
8484
fieldPairEditor Args {argsModel = st, argsOptic = optic, argsAction = action} idx =

ghcjs/overlays.nix

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@
171171
{};
172172
functora-ghcjs =
173173
self.callCabal2nix
174-
"functora-ghcjs" "${functora}/pub/functora/src" {};
174+
"functora-ghcjs"
175+
(
176+
pkgs.nix-gitignore.gitignoreSourcePure
177+
./../.gitignore
178+
./../pub/functora/src
179+
)
180+
{};
175181
}
176182
);
177183
});

pub/functora/src/bolt11/Functora/Bech32.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ import Data.Bits
4040
)
4141
import qualified Data.ByteString as B
4242
import Data.Char (toUpper)
43+
import qualified Data.Data as D
4344
import Data.Foldable (foldl')
4445
import Data.Functor.Identity (Identity, runIdentity)
4546
import Data.Ix (Ix (..))
4647
import Data.Text (Text)
4748
import qualified Data.Text as T
4849
import qualified Data.Text.Encoding as E
4950
import Data.Word (Word8)
51+
import GHC.Generics (Generic)
5052
import Prelude
5153

5254
-- | Bech32 human-readable string.
@@ -66,6 +68,7 @@ type Data = [Word8]
6668
newtype Word5
6769
= UnsafeWord5 Word8
6870
deriving newtype (Eq, Ord, Num)
71+
deriving stock (D.Data, Generic)
6972

7073
instance Show Word5 where
7174
show (UnsafeWord5 w8) = show w8

pub/functora/src/bolt11/Functora/Bolt11.hs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import qualified Data.ByteString as BS
2323
import qualified Data.ByteString.Base16 as B16
2424
import qualified Data.ByteString.Builder as BS
2525
import qualified Data.ByteString.Lazy.Char8 as BL
26+
import Data.Data (Data)
2627
import Data.Foldable (foldl')
2728
import Data.Maybe (fromMaybe)
2829
import Data.String (IsString (..))
@@ -32,10 +33,12 @@ import Data.Text.Encoding (decodeUtf8)
3233
import qualified Data.Text.Encoding as T
3334
import Functora.Bech32 (Word5 (..), bech32Decode, toBase256)
3435
import Functora.Denomination (Denomination (toMsats), MSats, btc)
36+
import GHC.Generics (Generic)
3537
import Prelude
3638

3739
newtype Hex = Hex {getHex :: ByteString}
3840
deriving newtype (Eq, Ord)
41+
deriving stock (Data, Generic)
3942

4043
instance Show Hex where
4144
show (Hex bs) = BL.unpack (BS.toLazyByteString (BS.byteStringHex bs))
@@ -70,7 +73,7 @@ data Tag
7073
| OnchainFallback Hex -- TODO: address type
7174
| ExtraRouteInfo
7275
| FeatureBits [Word5]
73-
deriving stock (Eq, Ord, Show)
76+
deriving stock (Eq, Ord, Show, Data, Generic)
7477

7578
isPaymentHash :: Tag -> Bool
7679
isPaymentHash PaymentHash {} = True
@@ -81,16 +84,17 @@ isDescription Description {} = True
8184
isDescription _ = False
8285

8386
data Multiplier = Milli | Micro | Nano | Pico
84-
deriving stock (Eq, Ord, Show)
87+
deriving stock (Eq, Ord, Show, Data, Generic)
8588

8689
data Currency
8790
= Bitcoin
8891
| BitcoinTestnet
8992
| BitcoinRegtest
90-
deriving stock (Eq, Ord, Show)
93+
deriving stock (Eq, Ord, Show, Data, Generic)
9194

9295
newtype Bolt11Amount = Bolt11Amount {_getBolt11Amount :: (Int, Multiplier)}
9396
deriving newtype (Eq, Ord)
97+
deriving stock (Data, Generic)
9498

9599
instance Show Bolt11Amount where
96100
show amt = show (bolt11Msats amt)
@@ -99,15 +103,15 @@ data Bolt11HRP = Bolt11HRP
99103
{ bolt11Currency :: Currency,
100104
bolt11Amount :: Maybe Bolt11Amount
101105
}
102-
deriving stock (Eq, Ord, Show)
106+
deriving stock (Eq, Ord, Show, Data, Generic)
103107

104108
data Bolt11 = Bolt11
105109
{ bolt11HRP :: Bolt11HRP,
106110
bolt11Timestamp :: Int, -- posix
107111
bolt11Tags :: [Tag], -- posix
108112
bolt11Signature :: Hex
109113
}
110-
deriving stock (Eq, Ord, Show)
114+
deriving stock (Eq, Ord, Show, Data, Generic)
111115

112116
parseCurrency :: Parser Currency
113117
parseCurrency =
@@ -185,7 +189,7 @@ tagParser ws@(UnsafeWord5 typ : d1 : d2 : rest)
185189
data MSig
186190
= Sig [Word5]
187191
| Unk [Word5]
188-
deriving stock (Eq, Ord, Show)
192+
deriving stock (Eq, Ord, Show, Data, Generic)
189193

190194
tagsParser :: [Word5] -> ([Tag], MSig)
191195
tagsParser ws

0 commit comments

Comments
 (0)