-
Notifications
You must be signed in to change notification settings - Fork 2
/
lnd.go
248 lines (222 loc) · 6.36 KB
/
lnd.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
package mock
import (
"crypto/tls"
"crypto/x509"
"fmt"
"strings"
"github.com/lightningnetwork/lnd/macaroons"
"github.com/buger/jsonparser"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/lightningnetwork/lnd/lnrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"gopkg.in/macaroon.v2"
)
// CreateLndContainer create's an lnd container with a given name
// and no channels
func (c LightningMocker) CreateLndContainer(name string) (ctn LndContainer, err error) {
ctn.c = &c
ctn.PortMap = c.portsToMap([]int{10009, 8080, 9735})
created, err := c.CreateContainer(&container.Config{
Image: "ghcr.io/xplorfin/lnd:latest",
Env: EnvArgs(),
Tty: false,
Entrypoint: []string{"./start-lnd.sh"},
Labels: c.GetSessionLabels(),
}, &container.HostConfig{
PortBindings: ctn.PortMap.NatMap(),
NetworkMode: NetworkName,
Mounts: []mount.Mount{
{
Source: "shared",
Target: "/rpc",
Type: mount.TypeVolume,
VolumeOptions: &mount.VolumeOptions{
Labels: c.GetSessionLabels(),
},
},
{
Source: fmt.Sprintf("%s-lnd", name),
Target: "/root/.lnd",
Type: mount.TypeVolume,
VolumeOptions: &mount.VolumeOptions{
Labels: c.GetSessionLabels(),
},
},
},
}, nil, nil, name)
if err != nil {
return ctn, err
}
ctn.id = created.ID
err = c.ContainerStart(c.Ctx, ctn.id, types.ContainerStartOptions{})
if err != nil {
return ctn, err
}
c.PrintContainerLogs(created.ID)
return ctn, nil
}
// LndContainer object contains methods that allow us to interact with a created
// lnd container
type LndContainer struct {
// id of the current docker container
id string
// the lightning mocker object
c *LightningMocker
// address of lnd wallet
address string
// PortMap is the mapping of ports to the host binding
PortMap PortMap
}
// Address gets the address of the user
func (l *LndContainer) Address() (address string, err error) {
if l.address == "" {
// because we don't know when the lnd server will start, we need to keep trying until we get an address
hasAddress := false
counter := 0
for !hasAddress {
counter++
if counter > 100 {
return l.address, err
}
// TODO we might be able to replace the hostname here with the container command
rawAddress, err := l.c.Exec(l.id, append(LnCLIPrefix(), "newaddress", "np2wkh"))
if err != nil {
return "", err
}
hasAddress = rawAddress.ExitCode == 0
if hasAddress {
l.address, _ = jsonparser.GetString([]byte(rawAddress.StdOut), "address")
}
}
}
return l.address, err
}
// GetPubKey of instance
func (l *LndContainer) GetPubKey() (pubKey string, err error) {
info, err := l.c.Exec(l.id, append(LnCLIPrefix(), "getinfo"))
if err != nil {
return pubKey, err
}
pubKey, err = jsonparser.GetString([]byte(info.StdOut), "identity_pubkey")
if err != nil {
return pubKey, err
}
return pubKey, err
}
// Connect connects to the lightning peer
func (l *LndContainer) Connect(pubKey, host string) (err error) {
// we use address to make sure the wallet is unlocked
// TODO: clean this up
_, err = l.Address()
if err != nil {
return err
}
_, err = l.c.Exec(l.id, append(LnCLIPrefix(), "connect", fmt.Sprintf("%s@%s", pubKey, host)))
return err
}
// WaitForCondition waits for a custom condition from info. Useful for verifying chain states
// Use with caution. Loop will not terminate until checker is true
func (l *LndContainer) WaitForCondition(checker func(res *lnrpc.GetInfoResponse) bool) (err error) {
client, err := l.RPCClient()
if err != nil {
return err
}
for {
// this never finishes
res, err := client.GetInfo(l.c.Ctx, &lnrpc.GetInfoRequest{})
if err != nil {
return err
}
if checker(res) {
break
}
}
return err
}
// WaitForSync waits for a the chain to be synced
func (l *LndContainer) WaitForSync(chain, graph bool) (err error) {
return l.WaitForCondition(func(res *lnrpc.GetInfoResponse) bool {
return (!chain || res.SyncedToChain) && (!graph || res.SyncedToGraph)
})
}
// OpenChannel connects to the peer and broadcasts a channel
// opening transaction to the mempool.
// Note: blocks must be mined for channel to be established
func (l *LndContainer) OpenChannel(pubKey, host string, amount int) (err error) {
err = l.Connect(pubKey, host)
if err != nil {
return err
}
err = l.WaitForSync(true, false)
if err != nil {
return err
}
// open the channel
_, err = l.c.Exec(l.id, append(LnCLIPrefix(),
"openchannel", fmt.Sprintf("--node_key=%s", pubKey), fmt.Sprintf("--local_amt=%d", amount)))
return err
}
// GetTLSCert gets the tls cert for LndContainer
func (l *LndContainer) GetTLSCert() (cert *tls.Config, rawCert string, err error) {
rawResult, err := l.c.Exec(l.id, []string{"cat", "/root/.lnd/tls.cert"})
if err != nil {
return cert, rawCert, err
}
rawCert = rawResult.StdOut
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM([]byte(rawCert)) {
return cert, rawCert, err
}
cert = &tls.Config{
InsecureSkipVerify: true,
RootCAs: cp,
}
return cert, rawCert, err
}
// GetAdminMacaroon fetches the admin macaroon from LndContainer
func (l *LndContainer) GetAdminMacaroon() (mac *macaroon.Macaroon, err error) {
rawMacaroonRes, err := l.c.Exec(l.id, []string{"base64", "/root/.lnd/data/chain/bitcoin/simnet/admin.macaroon"})
if err != nil {
return mac, err
}
rawMac := strings.ReplaceAll(rawMacaroonRes.StdOut, "\n", "")
decoded, err := macaroon.Base64Decode([]byte(rawMac))
if err != nil {
return nil, err
}
mac = &macaroon.Macaroon{}
err = mac.UnmarshalBinary(decoded)
if err != nil {
return nil, err
}
return mac, err
}
// GrpcConnection generates a grpc connection to a
func (l *LndContainer) GrpcConnection() (conn *grpc.ClientConn, err error) {
cert, _, err := l.GetTLSCert()
if err != nil {
return nil, err
}
mac, err := l.GetAdminMacaroon()
if err != nil {
return nil, err
}
return grpc.DialContext(
l.c.Ctx,
fmt.Sprintf("localhost:%d",
l.PortMap.GetHostPort(10009)),
grpc.WithTransportCredentials(credentials.NewTLS(cert)),
grpc.WithPerRPCCredentials(macaroons.NewMacaroonCredential(mac)),
)
}
// RPCClient gets an authenticated
func (l *LndContainer) RPCClient() (rpcClient lnrpc.LightningClient, err error) {
grpcConn, err := l.GrpcConnection()
if err != nil {
return nil, err
}
return lnrpc.NewLightningClient(grpcConn), nil
}