Skip to content

Commit a758520

Browse files
committed
can parse bolt11 onchain fallback address with both ghc and ghcjs
1 parent f4bcd1d commit a758520

File tree

8 files changed

+94
-10
lines changed

8 files changed

+94
-10
lines changed

ghcjs/lightning-verifier/default.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ in rec {
122122
--compilation_level ADVANCED_OPTIMIZATIONS \
123123
--externs ${app}/bin/${label}.jsexe/all.js.externs \
124124
--externs ${./static}/app.js \
125+
--externs ${./static/hs-bitcoin-hash.js} \
126+
--externs ${./static/hs-bitcoin-keys.js} \
125127
--externs ${./static}/material-components-web.min.js \
126128
--externs ${./static}/material-components-web-elm.min.js \
127129
--output_wrapper "%output%//# sourceMappingURL=all.js.map" \

ghcjs/lightning-verifier/lightning-verifier.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ common pkg
9999
, base16-bytestring
100100
, base64-bytestring
101101
, binary
102+
, bitcoin-address
102103
, bytestring
103104
, containers
104105
, functora-ghcjs

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module App.Widgets.Bolt11
44
where
55

66
import App.Types
7+
import qualified Bitcoin.Address as Btc
78
import qualified Data.ByteString.Base16 as B16
89
import qualified Data.Text.Encoding as T
910
import qualified Functora.Bolt11 as B11
@@ -90,18 +91,28 @@ invoiceTagWidget = \case
9091
B11.PaymentHash x -> hex "Preimage Hash" x
9192
B11.PaymentSecret x -> hex "Payment Secret" x
9293
B11.Description x -> pure . pair "Description" $ inspect x
94+
B11.AdditionalMetadata x -> w5s "Additional Metadata" x
9395
B11.PayeePubkey x -> hex "Payee Pubkey" x
9496
B11.DescriptionHash x -> hex "Description Hash" x
9597
B11.Expiry x -> pure . pair "Expiry" $ inspect x
9698
B11.MinFinalCltvExpiry x -> pure . pair "Min Final CLTV Expiry" $ inspect x
97-
B11.OnchainFallback -> mempty
99+
B11.OnchainFallback x -> do
100+
--
101+
-- TODO : do not ignore failure?
102+
--
103+
txt <- either (const mempty) pure . decodeUtf8' $ Btc.renderAddress x
104+
pure $ pair "Onchain Fallback" $ from @Prelude.Text @MisoString txt
98105
B11.ExtraRouteInfo -> mempty
99-
B11.FeatureBits x ->
100-
pure
101-
. pair "Feature Bits"
102-
. from @Prelude.String @MisoString
103-
$ Prelude.show x
106+
B11.FeatureBits x -> w5s "Feature Bits" x
107+
B11.UnknownTag {} -> mempty
108+
B11.UnparsedTag {} -> mempty
104109
where
110+
w5s :: MisoString -> [B11.Word5] -> [FieldPair DynamicField Identity]
111+
w5s x =
112+
pure
113+
. pair x
114+
. inspect
115+
. fmap fromEnum
105116
hex :: MisoString -> B11.Hex -> [FieldPair DynamicField Identity]
106117
hex x =
107118
pure

ghcjs/lightning-verifier/static/hs-bitcoin-hash.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ghcjs/lightning-verifier/static/hs-bitcoin-keys.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ghcjs/overlays.nix

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,32 @@
135135
ver = "1.7.16";
136136
sha256 = "Eu6I/ZARmObjCfpymyybQDPpakMcqCf3W9gYrT7AR3g=";
137137
} {};
138+
bech32 = self.callHackageDirect {
139+
pkg = "bech32";
140+
ver = "1.1.1";
141+
sha256 = "3bzizRrab+AmN3RQoDbbGIr8mc/z0qf7Ki4dsyjD5dM=";
142+
} {};
143+
bitcoin-address = self.callHackageDirect {
144+
pkg = "bitcoin-address";
145+
ver = "0.1";
146+
sha256 = "s4v3i6pV02anbYgLqAsbeX3ypzUXhSOqMrkbFxV/jj0=";
147+
} {};
148+
bitcoin-hash = self.callHackageDirect {
149+
pkg = "bitcoin-hash";
150+
ver = "0.1";
151+
sha256 = "c18kUghhJB0GjLxHHrgJ8djtNlQXbsJcbnM61zvHwzY=";
152+
} {};
153+
secp256k1-haskell = self.callHackageDirect {
154+
pkg = "secp256k1-haskell";
155+
ver = "0.6.1";
156+
sha256 = "R4zG6prtWN2bX++hMhVt4VMQoyAVbBZnkMcMA0zFoiQ=";
157+
} {};
158+
base16 = doJailbreak (self.callHackageDirect {
159+
pkg = "base16";
160+
ver = "0.3.2.0";
161+
sha256 = "EziiFX/1e105Y7zbO0DLwF6yCzYv4IX/vF9l7B55o7Y=";
162+
} {});
163+
libsecp256k1 = pkgs.secp256k1;
138164
#
139165
# Local
140166
#
@@ -178,6 +204,24 @@
178204
./../pub/functora/src
179205
)
180206
{};
207+
bitcoin-script =
208+
self.callCabal2nix
209+
"bitcoin-script"
210+
(
211+
pkgs.nix-gitignore.gitignoreSourcePure
212+
./../pub/haskell-bitcoin-script/.gitignore
213+
./../pub/haskell-bitcoin-script
214+
)
215+
{};
216+
bitcoin-keys =
217+
self.callCabal2nix
218+
"bitcoin-keys"
219+
(
220+
pkgs.nix-gitignore.gitignoreSourcePure
221+
./../pub/hs-bitcoin-keys/.gitignore
222+
./../pub/hs-bitcoin-keys/bitcoin-keys
223+
)
224+
{};
181225
}
182226
);
183227
});

pub/haskell-bitcoin-script/src/Data/Bitcoin/Script.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE LambdaCase #-}
3+
{-# LANGUAGE OverloadedStrings #-}
24

35
module Data.Bitcoin.Script
46
( decode,

pub/hs-bitcoin-keys/bitcoin-keys/lib/Bitcoin/Keys/GHC.hs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE DerivingStrategies #-}
23
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
34

@@ -51,7 +52,14 @@ instance Show Prv where
5152
-- @
5253
prvRaw :: Prv -> B.ByteString
5354
{-# INLINE prvRaw #-}
54-
prvRaw (Prv (K.SecKey x)) = x
55+
prvRaw (Prv x) = getSecKey x
56+
57+
#if MIN_VERSION_secp256k1_haskell(1,0,0)
58+
getSecKey :: K.SecKey -> B.ByteString
59+
getSecKey (K.SecKey x) = x
60+
#else
61+
getSecKey = K.getSecKey
62+
#endif
5563

5664
-- | Construct a 'Prv' key from its raw 32 bytes (big-endian).
5765
--
@@ -153,14 +161,23 @@ newtype Tweak = Tweak K.Tweak
153161
deriving newtype (Eq)
154162

155163
instance Ord Tweak where
156-
compare (Tweak (K.Tweak a)) (Tweak (K.Tweak b)) = compare a b
164+
compare (Tweak a) (Tweak b) = compare (getTweak a) (getTweak b)
165+
166+
#if MIN_VERSION_secp256k1_haskell(1,0,0)
167+
getTweak :: K.Tweak -> B.ByteString
168+
getTweak (K.Tweak x) = x
169+
#else
170+
getTweak = K.getTweak
171+
#endif
157172

158173
-- | Big-endian base-16.
159174
instance Show Tweak where
160-
showsPrec n (Tweak (K.Tweak x)) =
175+
showsPrec n (Tweak x) =
161176
showParen (n > 10) $
162177
showString "Tweak "
163-
. mappend (BL8.unpack (BB.toLazyByteString (BB.byteStringHex x)))
178+
. mappend
179+
( BL8.unpack (BB.toLazyByteString (BB.byteStringHex (getTweak x)))
180+
)
164181

165182
-- | Construct a 'Tweak' from its raw 32 bytes (big-endian).
166183
--
@@ -171,5 +188,10 @@ parseTweak x = do
171188
guard (B.length x == 32)
172189
Tweak <$> K.tweak x
173190

191+
#if MIN_VERSION_secp256k1_haskell(1,0,0)
174192
withCtx :: (K.Ctx -> a) -> a
175193
withCtx f = Unsafe.unsafePerformIO $ K.withContext (pure . f)
194+
#else
195+
withCtx :: a -> a
196+
withCtx = id
197+
#endif

0 commit comments

Comments
 (0)