forked from lightninglabs/lightning-node-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
630 lines (525 loc) · 16.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//go:build js
package main
import (
"context"
"crypto/tls"
"encoding/hex"
"errors"
"fmt"
"net/http"
"os"
"regexp"
"runtime/debug"
"strings"
"sync"
"syscall/js"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/jessevdk/go-flags"
faraday "github.com/lightninglabs/faraday/frdrpcserver/perms"
"github.com/lightninglabs/lightning-node-connect/mailbox"
"github.com/lightninglabs/lightning-terminal/litclient"
"github.com/lightninglabs/lightning-terminal/perms"
loop "github.com/lightninglabs/loop/loopd/perms"
pool "github.com/lightninglabs/pool/perms"
tap "github.com/lightninglabs/taproot-assets/perms"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons"
"github.com/lightningnetwork/lnd/signal"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"gopkg.in/macaroon-bakery.v2/bakery"
"gopkg.in/macaroon-bakery.v2/bakery/checkers"
"gopkg.in/macaroon.v2"
)
var (
// initMu is to be used to guard global variables at initialisation
// time.
initMu sync.Mutex
permsMgr *perms.Manager
jsonCBRegex = regexp.MustCompile("(\\w+)\\.(\\w+)\\.(\\w+)")
)
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("Recovered in f: %v", r)
debug.PrintStack()
}
}()
// Initialise any global variables if they have not yet been
// initialised.
if err := initGlobals(); err != nil {
exit(err)
}
// Parse command line flags.
cfg := config{}
parser := flags.NewParser(&cfg, flags.Default)
parser.SubcommandsOptional = true
_, err := parser.Parse()
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
exit(err)
}
if err != nil {
exit(err)
}
// Hook interceptor for os signals.
shutdownInterceptor, err := signal.Intercept()
if err != nil {
exit(err)
}
logWriter := build.NewRotatingLogWriter()
SetupLoggers(logWriter, shutdownInterceptor)
err = build.ParseAndSetDebugLevels(cfg.DebugLevel, logWriter)
if err != nil {
exit(err)
}
wc, err := newWasmClient(&cfg)
if err != nil {
exit(fmt.Errorf("config validation error: %v", err))
}
// Setup JS callbacks.
callbacks := make(map[string]interface{})
callbacks["wasmClientIsReady"] = js.FuncOf(wc.IsReady)
callbacks["wasmClientConnectServer"] = js.FuncOf(wc.ConnectServer)
callbacks["wasmClientIsConnected"] = js.FuncOf(wc.IsConnected)
callbacks["wasmClientDisconnect"] = js.FuncOf(wc.Disconnect)
callbacks["wasmClientInvokeRPC"] = js.FuncOf(wc.InvokeRPC)
callbacks["wasmClientStatus"] = js.FuncOf(wc.Status)
callbacks["wasmClientGetExpiry"] = js.FuncOf(wc.GetExpiry)
callbacks["wasmClientHasPerms"] = js.FuncOf(wc.HasPermissions)
callbacks["wasmClientIsReadOnly"] = js.FuncOf(wc.IsReadOnly)
callbacks["wasmClientIsCustom"] = js.FuncOf(wc.IsCustom)
// Check if a JS object for the namespace already exists on the global
// scope.
nsObj := js.Global().Get(cfg.NameSpace)
if !isEmptyObject(nsObj) {
// If it exists, set the callbacks on the existing object. This
// prevents overwriting the existing object, removing any
// vars/funcs that may have been set by the user.
for cb, name := range callbacks {
nsObj.Set(cb, name)
}
} else {
// If not, create the JS object and set the callbacks.
js.Global().Set(cfg.NameSpace, js.ValueOf(callbacks))
}
for _, registration := range litclient.Registrations {
registration(wc.registry)
}
log.Debugf("WASM client ready for connecting")
select {
case <-shutdownInterceptor.ShutdownChannel():
log.Debugf("Shutting down WASM client")
_ = wc.Disconnect(js.ValueOf(nil), nil)
log.Debugf("Shutdown of WASM client complete")
}
}
// initGlobals initialises any global variables that have not yet been
// initialised.
func initGlobals() error {
initMu.Lock()
defer initMu.Unlock()
if permsMgr != nil {
return nil
}
var err error
permsMgr, err = perms.NewManager(true)
if err != nil {
return err
}
permsMgr.RegisterSubServer("taproot-assets", tap.RequiredPermissions, tap.MacaroonWhitelist(false, false))
permsMgr.RegisterSubServer("loop", loop.RequiredPermissions, nil)
permsMgr.RegisterSubServer("pool", pool.RequiredPermissions, nil)
permsMgr.RegisterSubServer("faraday", faraday.RequiredPermissions, nil)
return err
}
type wasmClient struct {
cfg *config
lndConn *grpc.ClientConn
statusChecker func() mailbox.ClientStatus
mac *macaroon.Macaroon
registry map[string]func(context.Context, *grpc.ClientConn,
string, func(string, error))
}
func newWasmClient(cfg *config) (*wasmClient, error) {
if cfg.NameSpace == "" {
return nil, fmt.Errorf("a non-empty namespace is required")
}
return &wasmClient{
cfg: cfg,
lndConn: nil,
registry: make(map[string]func(context.Context,
*grpc.ClientConn, string, func(string, error))),
}, nil
}
func (w *wasmClient) IsReady(_ js.Value, _ []js.Value) interface{} {
// This will always return true. So as soon as this method is called
// successfully the JS part knows the WASM instance is fully started up
// and ready to connect.
return js.ValueOf(true)
}
func (w *wasmClient) ConnectServer(_ js.Value, args []js.Value) interface{} {
if len(args) != 5 {
return js.ValueOf("invalid use of wasmClientConnectServer, " +
"need 5 parameters: server, isDevServer, " +
"pairingPhrase, localStaticPrivKey, remoteStaticPubKey")
}
mailboxServer := args[0].String()
isDevServer := args[1].Bool()
pairingPhrase := args[2].String()
localStatic := args[3].String()
remoteStatic := args[4].String()
// Check that the correct arguments and config combinations have been
// provided.
err := validateArgs(w.cfg, localStatic, remoteStatic)
if err != nil {
exit(err)
}
// Parse the key arguments.
localPriv, remotePub, err := parseKeys(
w.cfg.OnLocalPrivCreate, localStatic, remoteStatic,
)
if err != nil {
exit(err)
}
// Disable TLS verification for the REST connections if this is a dev
// server.
if isDevServer {
defaultHttpTransport := http.DefaultTransport.(*http.Transport)
defaultHttpTransport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
// Since the connection function is blocking, we need to spin it off
// in another goroutine here. See https://pkg.go.dev/syscall/js#FuncOf.
go func() {
var err error
statusChecker, lndConnect, err := mailbox.NewClientWebsocketConn(
mailboxServer, pairingPhrase, localPriv, remotePub,
func(key *btcec.PublicKey) error {
return callJsCallback(
w.cfg.OnRemoteKeyReceive,
hex.EncodeToString(
key.SerializeCompressed(),
),
)
}, func(data []byte) error {
parts := strings.Split(string(data), ": ")
if len(parts) != 2 || parts[0] != "Macaroon" {
return fmt.Errorf("authdata does " +
"not contain a macaroon")
}
macBytes, err := hex.DecodeString(parts[1])
if err != nil {
return err
}
mac := &macaroon.Macaroon{}
err = mac.UnmarshalBinary(macBytes)
if err != nil {
return fmt.Errorf("unable to decode "+
"macaroon: %v", err)
}
w.mac = mac
return callJsCallback(
w.cfg.OnAuthData, string(data),
)
},
)
if err != nil {
exit(err)
}
w.statusChecker = statusChecker
w.lndConn, err = lndConnect()
if err != nil {
exit(err)
}
log.Debugf("WASM client connected to RPC")
}()
return nil
}
func (w *wasmClient) IsConnected(_ js.Value, _ []js.Value) interface{} {
return js.ValueOf(w.lndConn != nil)
}
func (w *wasmClient) Disconnect(_ js.Value, _ []js.Value) interface{} {
if w.lndConn != nil {
if err := w.lndConn.Close(); err != nil {
log.Errorf("Error closing RPC connection: %v", err)
}
w.lndConn = nil
}
return nil
}
func (w *wasmClient) Status(_ js.Value, _ []js.Value) interface{} {
if w.statusChecker == nil {
return nil
}
return js.ValueOf(w.statusChecker().String())
}
func (w *wasmClient) InvokeRPC(_ js.Value, args []js.Value) interface{} {
if len(args) != 3 {
return js.ValueOf("invalid use of wasmClientInvokeRPC, " +
"need 3 parameters: rpcName, request, callback")
}
if w.lndConn == nil {
return js.ValueOf("RPC connection not ready")
}
rpcName := args[0].String()
requestJSON := args[1].String()
jsCallback := args[len(args)-1:][0]
method, ok := w.registry[rpcName]
if !ok {
return js.ValueOf("rpc with name " + rpcName + " not found")
}
go func() {
log.Infof("Calling '%s' on RPC with request %s",
rpcName, requestJSON)
cb := func(resultJSON string, err error) {
if err != nil {
jsCallback.Invoke(js.ValueOf(err.Error()))
} else {
jsCallback.Invoke(js.ValueOf(resultJSON))
}
}
ctx := context.Background()
method(ctx, w.lndConn, requestJSON, cb)
<-ctx.Done()
}()
return nil
}
func (w *wasmClient) GetExpiry(_ js.Value, _ []js.Value) interface{} {
if w.mac == nil {
log.Errorf("macaroon not obtained yet. GetExpiry should " +
"only be called once the connection is complete")
return nil
}
expiry, found := checkers.ExpiryTime(nil, w.mac.Caveats())
if !found {
return nil
}
return js.ValueOf(expiry.Unix())
}
func (w *wasmClient) IsReadOnly(_ js.Value, _ []js.Value) interface{} {
if w.mac == nil {
log.Errorf("macaroon not obtained yet. IsReadOnly should " +
"only be called once the connection is complete")
return js.ValueOf(false)
}
macOps, err := extractMacaroonOps(w.mac)
if err != nil {
log.Errorf("could not extract macaroon ops: %v", err)
return js.ValueOf(false)
}
// Check that the macaroon contains each of the required permissions
// for the given URI.
return js.ValueOf(isReadOnly(macOps))
}
func (w *wasmClient) IsCustom(_ js.Value, _ []js.Value) interface{} {
if w.mac == nil {
log.Errorf("macaroon not obtained yet. IsCustom should " +
"only be called once the connection is complete")
return js.ValueOf(false)
}
macOps, err := extractMacaroonOps(w.mac)
if err != nil {
log.Errorf("could not extract macaroon ops: %v", err)
return js.ValueOf(false)
}
// We consider a session type to be "custom" if it has any permissions
// with the "uri" entity.
var isCustom bool
for _, op := range macOps {
if op.Entity == macaroons.PermissionEntityCustomURI {
isCustom = true
break
}
}
return js.ValueOf(isCustom)
}
func (w *wasmClient) HasPermissions(_ js.Value, args []js.Value) interface{} {
if len(args) != 1 {
return js.ValueOf(false)
}
if w.mac == nil {
log.Errorf("macaroon not obtained yet. HasPermissions should " +
"only be called once the connection is complete")
return js.ValueOf(false)
}
// Convert JSON callback to grpc URI. JSON callbacks are of the form:
// `lnrpc.Lightning.WalletBalance` and the corresponding grpc URI is of
// the form: `/lnrpc.Lightning/WalletBalance`. So to convert the one to
// the other, we first convert all the `.` into `/`. Then we replace the
// first `/` back to a `.` and then we prepend the result with a `/`.
uri := jsonCBRegex.ReplaceAllString(args[0].String(), "/$1.$2/$3")
ops, ok := permsMgr.URIPermissions(uri)
if !ok {
log.Errorf("uri %s not found in known permissions list", uri)
return js.ValueOf(false)
}
macOps, err := extractMacaroonOps(w.mac)
if err != nil {
log.Errorf("could not extract macaroon ops: %v", err)
return js.ValueOf(false)
}
// Check that the macaroon contains each of the required permissions
// for the given URI.
return js.ValueOf(hasPermissions(uri, macOps, ops))
}
// extractMacaroonOps is a helper function that extracts operations from the
// ID of a macaroon.
func extractMacaroonOps(mac *macaroon.Macaroon) ([]*lnrpc.Op, error) {
rawID := mac.Id()
if rawID[0] != byte(bakery.LatestVersion) {
return nil, fmt.Errorf("invalid macaroon version: %x", rawID)
}
decodedID := &lnrpc.MacaroonId{}
idProto := rawID[1:]
err := proto.Unmarshal(idProto, decodedID)
if err != nil {
return nil, fmt.Errorf("unable to decode macaroon: %v", err)
}
return decodedID.Ops, nil
}
// isReadOnly returns true if the given operations only contain "read" actions.
func isReadOnly(ops []*lnrpc.Op) bool {
for _, op := range ops {
for _, action := range op.Actions {
if action != "read" {
return false
}
}
}
return true
}
// hasPermissions returns true if all the operations in requiredOps can also be
// found in macOps.
func hasPermissions(uri string, macOps []*lnrpc.Op,
requiredOps []bakery.Op) bool {
// Create a lookup map of the macaroon operations.
macOpsMap := make(map[string]map[string]bool)
for _, op := range macOps {
macOpsMap[op.Entity] = make(map[string]bool)
for _, action := range op.Actions {
macOpsMap[op.Entity][action] = true
// We account here for the special case where the
// operation gives access to an entire URI. This is the
// case when the Entity is equal to the "uri" keyword
// and when the Action is equal to the URI that access
// is being granted to.
if op.Entity == macaroons.PermissionEntityCustomURI &&
action == uri {
return true
}
}
}
// For each of the required operations, we ensure that the macaroon also
// contains the operation.
for _, op := range requiredOps {
macEntity, ok := macOpsMap[op.Entity]
if !ok {
return false
}
if !macEntity[op.Action] {
return false
}
}
return true
}
// validateArgs checks that the correct keys and callback functions have been
// provided.
func validateArgs(cfg *config, localPrivKey, remotePubKey string) error {
if remotePubKey != "" && localPrivKey == "" {
return errors.New("cannot set remote pub key if local priv " +
"key is not also set")
}
if localPrivKey == "" && cfg.OnLocalPrivCreate == "" {
return errors.New("OnLocalPrivCreate must be defined if a " +
"local key is not provided")
}
if remotePubKey == "" && cfg.OnRemoteKeyReceive == "" {
return errors.New("OnRemoteKeyReceive must be defined if a " +
"remote key is not provided")
}
return nil
}
// parseKeys parses the given keys from their string format and calls callback
// functions where appropriate. NOTE: This function assumes that the parameter
// combinations have been checked by validateArgs.
func parseKeys(onLocalPrivCreate, localPrivKey, remotePubKey string) (
keychain.SingleKeyECDH, *btcec.PublicKey, error) {
var (
localStaticKey keychain.SingleKeyECDH
remoteStaticKey *btcec.PublicKey
)
switch {
// This is a new session for which a local key has not yet been derived,
// so we generate a new key and call the onLocalPrivCreate callback so
// that this key can be persisted.
case localPrivKey == "" && remotePubKey == "":
privKey, err := btcec.NewPrivateKey()
if err != nil {
return nil, nil, err
}
localStaticKey = &keychain.PrivKeyECDH{PrivKey: privKey}
err = callJsCallback(
onLocalPrivCreate,
hex.EncodeToString(privKey.Serialize()),
)
if err != nil {
return nil, nil, err
}
// A local private key has been provided, so parse it.
case remotePubKey == "":
privKeyByte, err := hex.DecodeString(localPrivKey)
if err != nil {
return nil, nil, err
}
privKey, _ := btcec.PrivKeyFromBytes(privKeyByte)
localStaticKey = &keychain.PrivKeyECDH{PrivKey: privKey}
// Both local private key and remote public key have been provided,
// so parse them both into the appropriate types.
default:
// Both local and remote are set.
localPrivKeyBytes, err := hex.DecodeString(localPrivKey)
if err != nil {
return nil, nil, err
}
privKey, _ := btcec.PrivKeyFromBytes(localPrivKeyBytes)
localStaticKey = &keychain.PrivKeyECDH{PrivKey: privKey}
remoteKeyBytes, err := hex.DecodeString(remotePubKey)
if err != nil {
return nil, nil, err
}
remoteStaticKey, err = btcec.ParsePubKey(remoteKeyBytes)
if err != nil {
return nil, nil, err
}
}
return localStaticKey, remoteStaticKey, nil
}
func callJsCallback(callbackName string, value string) error {
// callbackName can be in the form of "namespace.callbackName" or just
// "callbackName". If it is in the former form, we need to get the
// namespace object and call the callback on that object.
jsScope := js.Global()
parts := strings.Split(callbackName, ".")
if len(parts) > 1 {
jsScope = jsScope.Get(parts[0])
callbackName = parts[1]
}
retValue := jsScope.Call(callbackName, value)
if isEmptyObject(retValue) || isEmptyObject(retValue.Get("err")) {
return nil
}
return fmt.Errorf(retValue.Get("err").String())
}
func isEmptyObject(value js.Value) bool {
return value.IsNull() || value.IsUndefined()
}
func exit(err error) {
// We use the fmt package for this error statement here instead of the
// logger so that we can use this exit function before the logger has
// been initialised since this would result in a panic.
fmt.Printf("Error running wasm client: %v\n", err)
os.Exit(1)
}