|
| 1 | +//go:build mobile |
| 2 | +// +build mobile |
| 3 | + |
| 4 | +package mobile |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/btcsuite/btcd/btcec/v2" |
| 10 | + "github.com/lightningnetwork/lnd/keychain" |
| 11 | + "github.com/lightninglabs/lightning-node-connect/mailbox" |
| 12 | + "google.golang.org/grpc" |
| 13 | +) |
| 14 | + |
| 15 | +// connectMailbox tries to establish a connection through LNC using the given |
| 16 | +// connect phrase and the mailbox server. |
| 17 | +func ConnectMailbox(ctx context.Context, |
| 18 | + connectPhrase []string, mailboxServerAddr string) (grpc.ClientConnInterface, error) { |
| 19 | + |
| 20 | + var mnemonicWords [mailbox.NumPassphraseWords]string |
| 21 | + copy(mnemonicWords[:], connectPhrase) |
| 22 | + passphrase := mailbox.PassphraseMnemonicToEntropy(mnemonicWords) |
| 23 | + |
| 24 | + privKey, err := btcec.NewPrivateKey() |
| 25 | + if err != nil { |
| 26 | + return nil, err |
| 27 | + } |
| 28 | + ecdh := &keychain.PrivKeyECDH{PrivKey: privKey} |
| 29 | + |
| 30 | + connData := mailbox.NewConnData(ecdh, nil, passphrase[:], nil, nil, nil) |
| 31 | + |
| 32 | + transportConn, err := mailbox.NewClient(ctx, connData) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + |
| 37 | + noiseConn := mailbox.NewNoiseGrpcConn(connData) |
| 38 | + |
| 39 | + dialOpts := []grpc.DialOption{ |
| 40 | + grpc.WithContextDialer(transportConn.Dial), |
| 41 | + grpc.WithTransportCredentials(noiseConn), |
| 42 | + grpc.WithPerRPCCredentials(noiseConn), |
| 43 | + grpc.WithBlock(), |
| 44 | + } |
| 45 | + |
| 46 | + return grpc.DialContext(ctx, mailboxServerAddr, dialOpts...) |
| 47 | +} |
0 commit comments