Skip to content

Commit 75b9e66

Browse files
committed
wasm-client+example: check macaroon perms
1 parent 74875eb commit 75b9e66

File tree

5 files changed

+233
-67
lines changed

5 files changed

+233
-67
lines changed

cmd/wasm-client/go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ module github.com/lightninglabs/lightning-node-connect/cmd/wasm-client
33
require (
44
github.com/btcsuite/btcd/btcec/v2 v2.2.0
55
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
6+
github.com/golang/protobuf v1.5.2
67
github.com/jessevdk/go-flags v1.4.0
7-
github.com/lightninglabs/faraday v0.2.7-alpha.0.20220503144421-cd1e56982f09
8+
github.com/lightninglabs/faraday v0.2.7-alpha.0.20220614135954-0f761430806c
89
github.com/lightninglabs/lightning-node-connect v0.1.9-alpha.0.20220602120524-e9964c685b18
9-
github.com/lightninglabs/loop v0.18.0-beta
10-
github.com/lightninglabs/pool v0.5.6-alpha
11-
github.com/lightningnetwork/lnd v0.15.0-beta.rc3
10+
github.com/lightninglabs/loop v0.19.1-beta.0.20220614171321-490fb352ffe9
11+
github.com/lightninglabs/pool v0.5.6-alpha.0.20220615075127-160ae4594f4a
12+
github.com/lightningnetwork/lnd v0.15.0-beta.rc4
1213
google.golang.org/grpc v1.39.0
1314
gopkg.in/macaroon-bakery.v2 v2.0.1
1415
gopkg.in/macaroon.v2 v2.1.0

cmd/wasm-client/go.sum

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ github.com/btcsuite/btcd/btcutil v1.1.0/go.mod h1:5OapHB7A2hBBWLm48mmw4MOHNJCcUB
8787
github.com/btcsuite/btcd/btcutil v1.1.1 h1:hDcDaXiP0uEzR8Biqo2weECKqEw0uHDZ9ixIWevVQqY=
8888
github.com/btcsuite/btcd/btcutil v1.1.1/go.mod h1:nbKlBMNm9FGsdvKvu0essceubPiAcI57pYBNnsLAa34=
8989
github.com/btcsuite/btcd/btcutil/psbt v1.1.0/go.mod h1:xMuACsIKDzcE3kWMxqK+aLrAWZ8bMdn7YjYEwNs5q8k=
90-
github.com/btcsuite/btcd/btcutil/psbt v1.1.1/go.mod h1:KsGzRAzAdEimzgERpK9Xm+RhuCMvc4j2ctK0BEQ8JV0=
91-
github.com/btcsuite/btcd/btcutil/psbt v1.1.2/go.mod h1:GMJ40RHh0brZmhAKjltvYjakbVg9wQqfH+hZF96jIRE=
9290
github.com/btcsuite/btcd/btcutil/psbt v1.1.4 h1:Edx4AfBn+YPam2KP5AobDitulGp4r1Oibm8oruzkMdI=
9391
github.com/btcsuite/btcd/btcutil/psbt v1.1.4/go.mod h1:9AyU6EQVJ9Iw9zPyNT1lcdHd6cnEZdno5wLu5FY74os=
9492
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
@@ -98,9 +96,9 @@ github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9
9896
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
9997
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
10098
github.com/btcsuite/btcwallet v0.14.0/go.mod h1:KFR1x3ZH7c31i4qA34XIvcsnhrEBLK1SHli52lN8E54=
101-
github.com/btcsuite/btcwallet v0.14.1-0.20220322182735-b0001c262734/go.mod h1:QN2tl1ipATUQRo9RtgvMHLSspqx7QWsj30qL+7AXuAo=
102-
github.com/btcsuite/btcwallet v0.15.1-0.20220512002839-af5562928b70 h1:BkEGO61/bSFNr1xlFTMPrVg2Qw83Bs6gJ5r7PBEGBMo=
10399
github.com/btcsuite/btcwallet v0.15.1-0.20220512002839-af5562928b70/go.mod h1:OQ+KZYSjNxxSIya6uWKquZBJgb8sV86njOj1tzsf0WE=
100+
github.com/btcsuite/btcwallet v0.15.1-0.20220601171836-38f1e7fbb23d h1:wJkFDREcDmctDkgMpSQ3R5i1LURsGWsDauzU3OO7HPI=
101+
github.com/btcsuite/btcwallet v0.15.1-0.20220601171836-38f1e7fbb23d/go.mod h1:zquPhQW95RBsCoSdCz3HxfaWnmxKB6ED3NW1WqFcTIk=
104102
github.com/btcsuite/btcwallet/wallet/txauthor v1.2.1/go.mod h1:/74bubxX5Js48d76nf/TsNabpYp/gndUuJw4chzCmhU=
105103
github.com/btcsuite/btcwallet/wallet/txauthor v1.2.3 h1:M2yr5UlULvpqtxUqpMxTME/pA92Z9cpqeyvAFk9lAg0=
106104
github.com/btcsuite/btcwallet/wallet/txauthor v1.2.3/go.mod h1:T2xSiKGpUkSLCh68aF+FMXmKK9mFqNdHl9VaqOr+JjU=
@@ -498,34 +496,32 @@ github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
498496
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
499497
github.com/lib/pq v1.10.3 h1:v9QZf2Sn6AmjXtQeFpdoq/eaNtYP6IN+7lcrygsIAtg=
500498
github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
501-
github.com/lightninglabs/aperture v0.1.17-beta.0.20220325093943-42b9d4c1be7f/go.mod h1:lDjRKhndRH0CzZQ2m8dWODdqp/ejEW7esb2u2nlvrw4=
502-
github.com/lightninglabs/aperture v0.1.17-beta.0.20220328072456-4a2632d0be38/go.mod h1:lDjRKhndRH0CzZQ2m8dWODdqp/ejEW7esb2u2nlvrw4=
503499
github.com/lightninglabs/aperture v0.1.18-beta/go.mod h1:lDjRKhndRH0CzZQ2m8dWODdqp/ejEW7esb2u2nlvrw4=
504-
github.com/lightninglabs/faraday v0.2.7-alpha.0.20220503144421-cd1e56982f09 h1:4H3TDTZ7QE5RNzXCpxjPIlN3whA35p7xSbinDj3TJtg=
505-
github.com/lightninglabs/faraday v0.2.7-alpha.0.20220503144421-cd1e56982f09/go.mod h1:YyeMuBbsqJ85wT6j1rCmTPMuk5a5Iz+QouiTN6qQiQ0=
500+
github.com/lightninglabs/faraday v0.2.7-alpha.0.20220614135954-0f761430806c h1:p126YmW2I9qwh/Dd751hsW4tHCJ67acIOhwB5gwxW44=
501+
github.com/lightninglabs/faraday v0.2.7-alpha.0.20220614135954-0f761430806c/go.mod h1:YyeMuBbsqJ85wT6j1rCmTPMuk5a5Iz+QouiTN6qQiQ0=
506502
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf h1:HZKvJUHlcXI/f/O0Avg7t8sqkPo78HFzjmeYFl6DPnc=
507503
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf/go.mod h1:vxmQPeIQxPf6Jf9rM8R+B4rKBqLA2AjttNxkFBL2Plk=
508504
github.com/lightninglabs/lndclient v0.15.0-0/go.mod h1:ORS/YFe9hAXlzN/Uj+gvTmrnXEml6yD6dWwzCjpTJyQ=
509-
github.com/lightninglabs/lndclient v0.15.0-2/go.mod h1:bef6exkGOboLNGZpx+nJFspmyV/CSiO/aIeWj9UFqE4=
510-
github.com/lightninglabs/loop v0.18.0-beta h1:Xnx2u96MX/4uIeKei4MmkCEXbSgooS3u9pC/ZqoCNf0=
511-
github.com/lightninglabs/loop v0.18.0-beta/go.mod h1:hOz9syXWEXjYbTKKxK1IsUnmXDiNmCTvc2jIn3wVais=
512-
github.com/lightninglabs/loop/swapserverrpc v1.0.0 h1:kPvN0gVMuwEONtiij+9nlVg65/Ab9fWP09ph0cYu+AE=
513-
github.com/lightninglabs/loop/swapserverrpc v1.0.0/go.mod h1:imy1/sqnb70EEyBKMo4pHwwLBPW8uYahWZ8s+1Xcq1o=
505+
github.com/lightninglabs/lndclient v0.15.0-6/go.mod h1:bQrzrVGpBuaM2c6mapsAdbnhsXJydOU6gFAKvevEzgo=
506+
github.com/lightninglabs/loop v0.19.1-beta.0.20220614171321-490fb352ffe9 h1:iltkEIowOz4pRmMYrX8Jd/Xy6Rtw13UdU9TiICXz9nU=
507+
github.com/lightninglabs/loop v0.19.1-beta.0.20220614171321-490fb352ffe9/go.mod h1:4uRskIR61TH2xf19N1Y6GTEcN4QaWhfnGnhpey7XpEY=
508+
github.com/lightninglabs/loop/swapserverrpc v1.0.1 h1:z99vrpRgQ834FC/C0Qx8HLtE0RX0WBDa1gAu7yOGPsg=
509+
github.com/lightninglabs/loop/swapserverrpc v1.0.1/go.mod h1:imy1/sqnb70EEyBKMo4pHwwLBPW8uYahWZ8s+1Xcq1o=
514510
github.com/lightninglabs/neutrino v0.13.2/go.mod h1:Cv/v8oHiPhuGiGvGgO+rIMhwCwEdsQFu6as840i2afw=
515511
github.com/lightninglabs/neutrino v0.14.1 h1:ALFckeS3CPmWZmX75vxZaWvz2TUebuASH+CR4cqVo18=
516512
github.com/lightninglabs/neutrino v0.14.1/go.mod h1:SV9ccrw2m6t6UvJX8xB//W0Dv+LEwMTbjg4V/Fb5KwU=
517-
github.com/lightninglabs/pool v0.5.6-alpha h1:V2hsAm/zai9whciuHH7rls7U/7XY8mN5OGXC9wG1lWM=
518-
github.com/lightninglabs/pool v0.5.6-alpha/go.mod h1:ffCa2rXiFK7E+AK1Qu3QW8ia1hAZ3fOQy6YbH90aB90=
519-
github.com/lightninglabs/pool/auctioneerrpc v1.0.5 h1:TIPnH3gPQxoN4HQ+3ATO5geTB6kthBtnx3TLROa6QE0=
520-
github.com/lightninglabs/pool/auctioneerrpc v1.0.5/go.mod h1:F9uND5Kpj2eYeYe0RLi8IWQHsRjQ88FUp8itkYmX1Mo=
513+
github.com/lightninglabs/pool v0.5.6-alpha.0.20220615075127-160ae4594f4a h1:wAl7nppliCKiY8uj9WvlkAXr5jXn2penQ1ubS6yhqw4=
514+
github.com/lightninglabs/pool v0.5.6-alpha.0.20220615075127-160ae4594f4a/go.mod h1:ID3ttDyd12Uu72qkMl21VRKheV6MRIanrnRGV30QonE=
515+
github.com/lightninglabs/pool/auctioneerrpc v1.0.7 h1:+a1ynzXAHDlFT4pOeKeAfV38rlwRX3wVdKnGxggtDEQ=
516+
github.com/lightninglabs/pool/auctioneerrpc v1.0.7/go.mod h1:F9uND5Kpj2eYeYe0RLi8IWQHsRjQ88FUp8itkYmX1Mo=
521517
github.com/lightninglabs/protobuf-hex-display v1.4.3-hex-display/go.mod h1:2oKOBU042GKFHrdbgGiKax4xVrFiZu51lhacUZQ9MnE=
522518
github.com/lightningnetwork/lightning-onion v1.0.2-0.20220211021909-bb84a1ccb0c5 h1:TkKwqFcQTGYoI+VEqyxA8rxpCin8qDaYX0AfVRinT3k=
523519
github.com/lightningnetwork/lightning-onion v1.0.2-0.20220211021909-bb84a1ccb0c5/go.mod h1:7dDx73ApjEZA0kcknI799m2O5kkpfg4/gr7N092ojNo=
524520
github.com/lightningnetwork/lnd v0.14.1-beta.0.20220324135938-0dcaa511a249/go.mod h1:Tp3ZxsfioUl6kQ30RrbMqWoZyZ4K+fv/o1lMEU8U7rA=
525-
github.com/lightningnetwork/lnd v0.14.1-beta.0.20220325230756-dceb10144f71/go.mod h1:ocTs4P7UuJrTjl37IyxwSb7/HOzTqF5GX41zFL2guCk=
526-
github.com/lightningnetwork/lnd v0.14.1-beta.0.20220330161355-8072b20d548d/go.mod h1:1u/ZZ1Cy9chz1odyGvgad+y6mA6wd5QNjOVWRXIQTbA=
527-
github.com/lightningnetwork/lnd v0.15.0-beta.rc3 h1:PpKtAPmP7d7NBJZE1OBIBcdhMzw9r6znkQUUj92dTWo=
521+
github.com/lightningnetwork/lnd v0.15.0-beta.rc1/go.mod h1:NzCE1ZGct0YEW9u74TXUpXp5AoUXzpkI5lgrO7x0ugM=
528522
github.com/lightningnetwork/lnd v0.15.0-beta.rc3/go.mod h1:NzCE1ZGct0YEW9u74TXUpXp5AoUXzpkI5lgrO7x0ugM=
523+
github.com/lightningnetwork/lnd v0.15.0-beta.rc4 h1:K5JViCnQ7U2YJiXnTRZy6ErlbhZTe+L849g6UYDXhfs=
524+
github.com/lightningnetwork/lnd v0.15.0-beta.rc4/go.mod h1:OZQDkyg56fhvgIvCgJvKdNR55+3pldN+u4n8lF2m2Sg=
529525
github.com/lightningnetwork/lnd/cert v1.1.1 h1:Nsav0RlIDRbOnzz2Yu69SQlK939IKya3Q2S0mDviIN8=
530526
github.com/lightningnetwork/lnd/cert v1.1.1/go.mod h1:1P46svkkd73oSoeI4zjkVKgZNwGq8bkGuPR8z+5vQUs=
531527
github.com/lightningnetwork/lnd/clock v1.0.1/go.mod h1:KnQudQ6w0IAMZi1SgvecLZQZ43ra2vpDNj7H/aasemg=
@@ -545,8 +541,9 @@ github.com/lightningnetwork/lnd/ticker v1.1.0/go.mod h1:ubqbSVCn6RlE0LazXuBr7/Zi
545541
github.com/lightningnetwork/lnd/tlv v1.0.2/go.mod h1:fICAfsqk1IOsC1J7G9IdsWX1EqWRMqEDCNxZJSKr9C4=
546542
github.com/lightningnetwork/lnd/tlv v1.0.3 h1:0xBZcPuXagP6f7TY/RnLNR4igE21ov6qUdTr5NyvhhI=
547543
github.com/lightningnetwork/lnd/tlv v1.0.3/go.mod h1:dzR/aZetBri+ZY/fHbwV06fNn/3UID6htQzbHfREFdo=
548-
github.com/lightningnetwork/lnd/tor v1.0.0 h1:wvEc7I+Y7IOtPglVP3cVBbYhiVhc7uTd7cMF9gQRzwA=
549544
github.com/lightningnetwork/lnd/tor v1.0.0/go.mod h1:RDtaAdwfAm+ONuPYwUhNIH1RAvKPv+75lHPOegUcz64=
545+
github.com/lightningnetwork/lnd/tor v1.0.1 h1:A11FrpU0Y//g+fA827W4VnjOeoIvExONdchlLX8wYkA=
546+
github.com/lightningnetwork/lnd/tor v1.0.1/go.mod h1:RDtaAdwfAm+ONuPYwUhNIH1RAvKPv+75lHPOegUcz64=
550547
github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 h1:sjOGyegMIhvgfq5oaue6Td+hxZuf3tDC8lAPrFldqFw=
551548
github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796/go.mod h1:3p7ZTf9V1sNPI5H8P3NkTFF4LuwMdPl2DodF60qAKqY=
552549
github.com/ltcsuite/ltcutil v0.0.0-20181217130922-17f3b04680b6/go.mod h1:8Vg/LTOO0KYa/vlHWJ6XZAevPQThGH5sufO0Hrou/lA=
@@ -976,8 +973,9 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
976973
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
977974
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
978975
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
979-
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
980976
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
977+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
978+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
981979
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
982980
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
983981
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

cmd/wasm-client/main.go

Lines changed: 127 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import (
1111
"fmt"
1212
"net/http"
1313
"os"
14+
"regexp"
1415
"runtime/debug"
1516
"strings"
1617
"syscall/js"
1718

1819
"github.com/btcsuite/btcd/btcec/v2"
20+
"github.com/golang/protobuf/proto"
1921
"github.com/jessevdk/go-flags"
2022
"github.com/lightninglabs/faraday/frdrpc"
2123
"github.com/lightninglabs/lightning-node-connect/mailbox"
@@ -35,29 +37,36 @@ import (
3537
"github.com/lightningnetwork/lnd/lnrpc/wtclientrpc"
3638
"github.com/lightningnetwork/lnd/signal"
3739
"google.golang.org/grpc"
40+
"gopkg.in/macaroon-bakery.v2/bakery"
3841
"gopkg.in/macaroon-bakery.v2/bakery/checkers"
3942
"gopkg.in/macaroon.v2"
4043
)
4144

4245
type stubPackageRegistration func(map[string]func(context.Context,
4346
*grpc.ClientConn, string, func(string, error)))
4447

45-
var registrations = []stubPackageRegistration{
46-
lnrpc.RegisterLightningJSONCallbacks,
47-
lnrpc.RegisterStateJSONCallbacks,
48-
autopilotrpc.RegisterAutopilotJSONCallbacks,
49-
chainrpc.RegisterChainNotifierJSONCallbacks,
50-
invoicesrpc.RegisterInvoicesJSONCallbacks,
51-
routerrpc.RegisterRouterJSONCallbacks,
52-
signrpc.RegisterSignerJSONCallbacks,
53-
verrpc.RegisterVersionerJSONCallbacks,
54-
walletrpc.RegisterWalletKitJSONCallbacks,
55-
watchtowerrpc.RegisterWatchtowerJSONCallbacks,
56-
wtclientrpc.RegisterWatchtowerClientJSONCallbacks,
57-
looprpc.RegisterSwapClientJSONCallbacks,
58-
poolrpc.RegisterTraderJSONCallbacks,
59-
frdrpc.RegisterFaradayServerJSONCallbacks,
60-
}
48+
var (
49+
registrations = []stubPackageRegistration{
50+
lnrpc.RegisterLightningJSONCallbacks,
51+
lnrpc.RegisterStateJSONCallbacks,
52+
autopilotrpc.RegisterAutopilotJSONCallbacks,
53+
chainrpc.RegisterChainNotifierJSONCallbacks,
54+
invoicesrpc.RegisterInvoicesJSONCallbacks,
55+
routerrpc.RegisterRouterJSONCallbacks,
56+
signrpc.RegisterSignerJSONCallbacks,
57+
verrpc.RegisterVersionerJSONCallbacks,
58+
walletrpc.RegisterWalletKitJSONCallbacks,
59+
watchtowerrpc.RegisterWatchtowerJSONCallbacks,
60+
wtclientrpc.RegisterWatchtowerClientJSONCallbacks,
61+
looprpc.RegisterSwapClientJSONCallbacks,
62+
poolrpc.RegisterTraderJSONCallbacks,
63+
frdrpc.RegisterFaradayServerJSONCallbacks,
64+
}
65+
66+
perms = getAllMethodPermissions()
67+
68+
jsonCBRegex = regexp.MustCompile("(\\w+)\\.(\\w+)\\.(\\w+)")
69+
)
6170

6271
func main() {
6372
defer func() {
@@ -108,6 +117,8 @@ func main() {
108117
callbacks.Set("wasmClientInvokeRPC", js.FuncOf(wc.InvokeRPC))
109118
callbacks.Set("wasmClientStatus", js.FuncOf(wc.Status))
110119
callbacks.Set("wasmClientGetExpiry", js.FuncOf(wc.GetExpiry))
120+
callbacks.Set("wasmClientHasPerms", js.FuncOf(wc.HasPermissions))
121+
callbacks.Set("wasmClientIsReadOnly", js.FuncOf(wc.IsReadOnly))
111122
js.Global().Set(cfg.NameSpace, callbacks)
112123

113124
for _, registration := range registrations {
@@ -319,33 +330,118 @@ func (w *wasmClient) GetExpiry(_ js.Value, _ []js.Value) interface{} {
319330

320331
return js.ValueOf(expiry.Unix())
321332
}
333+
334+
func (w *wasmClient) IsReadOnly(_ js.Value, _ []js.Value) interface{} {
335+
if w.mac == nil {
336+
log.Errorf("macaroon not obtained yet. IsReadOnly should " +
337+
"only be called once the connection is complete")
338+
return js.ValueOf(false)
339+
}
340+
341+
macOps, err := extractMacaroonOps(w.mac)
342+
if err != nil {
343+
log.Errorf("could not extract macaroon ops: %v", err)
344+
return js.ValueOf(false)
345+
}
346+
347+
// Check that the macaroon contains each of the required permissions
348+
// for the given URI.
349+
return js.ValueOf(isReadOnly(macOps))
350+
}
351+
352+
func (w *wasmClient) HasPermissions(_ js.Value, args []js.Value) interface{} {
322353
if len(args) != 1 {
323-
return js.ValueOf("invalid use of wasmClientExtractExpiry, " +
324-
"need 1 parameters: macaroon string")
354+
return js.ValueOf(false)
355+
}
356+
357+
if w.mac == nil {
358+
log.Errorf("macaroon not obtained yet. HasPermissions should " +
359+
"only be called once the connection is complete")
360+
return js.ValueOf(false)
361+
}
362+
363+
// Convert JSON callback to grpc URI. JSON callbacks are of the form:
364+
// `lnrpc.Lightning.WalletBalance` and the corresponding grpc URI is of
365+
// the form: `/lnrpc.Lightning/WalletBalance`. So to convert the one to
366+
// the other, we first convert all the `.` into `/`. Then we replace the
367+
// first `/` back to a `.` and then we prepend the result with a `/`.
368+
uri := jsonCBRegex.ReplaceAllString(args[0].String(), "/$1.$2/$3")
369+
370+
ops, ok := perms[uri]
371+
if !ok {
372+
log.Errorf("uri %s not found in known permissions list", uri)
373+
return js.ValueOf(false)
374+
}
375+
376+
macOps, err := extractMacaroonOps(w.mac)
377+
if err != nil {
378+
log.Errorf("could not extract macaroon ops: %v", err)
379+
return js.ValueOf(false)
325380
}
326381

327-
parts := strings.Split(args[0].String(), ": ")
328-
if len(parts) != 2 || parts[0] != "Macaroon" {
329-
return js.ValueOf("macaroon missing from auth data")
382+
// Check that the macaroon contains each of the required permissions
383+
// for the given URI.
384+
return js.ValueOf(hasPermissions(macOps, ops))
385+
}
386+
387+
// extractMacaroonOps is a helper function that extracts operations from the
388+
// ID of a macaroon.
389+
func extractMacaroonOps(mac *macaroon.Macaroon) ([]*lnrpc.Op, error) {
390+
rawID := mac.Id()
391+
if rawID[0] != byte(bakery.LatestVersion) {
392+
return nil, fmt.Errorf("invalid macaroon version: %x", rawID)
330393
}
331394

332-
macBytes, err := hex.DecodeString(parts[1])
395+
decodedID := &lnrpc.MacaroonId{}
396+
idProto := rawID[1:]
397+
err := proto.Unmarshal(idProto, decodedID)
333398
if err != nil {
334-
return js.ValueOf(err.Error())
399+
return nil, fmt.Errorf("unable to decode macaroon: %v", err)
335400
}
336401

337-
mac := &macaroon.Macaroon{}
338-
if err := mac.UnmarshalBinary(macBytes); err != nil {
339-
return js.ValueOf(fmt.Sprintf("unable to decode macaroon: %v",
340-
err))
402+
return decodedID.Ops, nil
403+
}
404+
405+
// isReadOnly returns true if the given operations only contain "read" actions.
406+
func isReadOnly(ops []*lnrpc.Op) bool {
407+
for _, op := range ops {
408+
for _, action := range op.Actions {
409+
if action != "read" {
410+
return false
411+
}
412+
}
341413
}
342414

343-
expiry, found := checkers.ExpiryTime(nil, mac.Caveats())
344-
if !found {
345-
return nil
415+
return true
416+
}
417+
418+
// hasPermissions returns true if all the operations in requiredOps can also be
419+
// found in macOps.
420+
func hasPermissions(macOps []*lnrpc.Op, requiredOps []bakery.Op) bool {
421+
// Create a lookup map of the macaroon operations.
422+
macOpsMap := make(map[string]map[string]bool)
423+
for _, op := range macOps {
424+
macOpsMap[op.Entity] = make(map[string]bool)
425+
426+
for _, action := range op.Actions {
427+
macOpsMap[op.Entity][action] = true
428+
}
346429
}
347430

348-
return js.ValueOf(expiry.Unix())
431+
// For each of the required operations, we ensure that the macaroon also
432+
// contains the operation.
433+
for _, op := range requiredOps {
434+
macEntity, ok := macOpsMap[op.Entity]
435+
if !ok {
436+
return false
437+
}
438+
439+
if !macEntity[op.Action] {
440+
return false
441+
}
442+
}
443+
444+
return true
349445
}
350446

351447
// validateArgs checks that the correct keys and callback functions have been

cmd/wasm-client/permissions.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
11
package main
2+
3+
import (
4+
faraday "github.com/lightninglabs/faraday/frdrpcserver/perms"
5+
loopd "github.com/lightninglabs/loop/loopd/perms"
6+
poold "github.com/lightninglabs/pool/perms"
7+
"github.com/lightningnetwork/lnd"
8+
"gopkg.in/macaroon-bakery.v2/bakery"
9+
)
10+
11+
// getAllMethodPermissions returns a merged map of all litd's method
12+
// permissions.
13+
func getAllMethodPermissions() map[string][]bakery.Op {
14+
mapSize := len(lnd.MainRPCServerPermissions()) +
15+
len(faraday.RequiredPermissions) +
16+
len(loopd.RequiredPermissions) + len(poold.RequiredPermissions)
17+
18+
allPerms := make(map[string][]bakery.Op, mapSize)
19+
for key, value := range lnd.MainRPCServerPermissions() {
20+
allPerms[key] = value
21+
}
22+
for key, value := range faraday.RequiredPermissions {
23+
allPerms[key] = value
24+
}
25+
for key, value := range loopd.RequiredPermissions {
26+
allPerms[key] = value
27+
}
28+
for key, value := range poold.RequiredPermissions {
29+
allPerms[key] = value
30+
}
31+
return allPerms
32+
}

0 commit comments

Comments
 (0)