-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
76 lines (61 loc) · 1.87 KB
/
node.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
package app
import (
"context"
"github.com/c13n-io/c13n-go/lnchat"
"github.com/c13n-io/c13n-go/model"
)
// GetNodes returns all nodes visible in the underlying lightning network.
func (app *App) GetNodes(ctx context.Context) ([]model.Node, error) {
// Propagate the request to the library
nodes, err := app.LNManager.ListNodes(ctx)
if err != nil {
return nil, newErrorf(err, "ListNodes")
}
// Create a slice with the relevant fields
result := make([]model.Node, len(nodes))
for i, u := range nodes {
result[i] = model.Node{
Alias: u.Alias,
Address: u.Address,
}
}
return result, nil
}
// GetNodesByAlias returns the visible nodes, filtered
// based on the provided alias substring.
func (app *App) GetNodesByAlias(ctx context.Context, aliasSubstr string) ([]model.Node, error) {
nodes, err := app.LNManager.ListNodes(ctx)
if err != nil {
return nil, newErrorf(err, "ListNodes")
}
matching := lnchat.ResolveAlias(nodes, aliasSubstr)
result := make([]model.Node, len(matching))
for i, u := range matching {
result[i] = model.Node{
Alias: u.Alias,
Address: u.Address,
}
}
return result, nil
}
// GetNodesByAddress returns the visible nodes, filtered based on address.
func (app *App) GetNodesByAddress(ctx context.Context, address string) ([]model.Node, error) {
nodes, err := app.LNManager.ListNodes(ctx)
if err != nil {
return nil, newErrorf(err, "ListNodes")
}
matching := lnchat.ResolveAddress(nodes, address)
result := make([]model.Node, len(matching))
for i, u := range matching {
result[i] = model.Node{
Alias: u.Alias,
Address: u.Address,
}
}
return result, nil
}
// ConnectNode connects a node as a peer.
func (app *App) ConnectNode(ctx context.Context, address string, hostport string) error {
err := app.LNManager.ConnectNode(ctx, address, hostport)
return newErrorf(err, "ConnectNode: could not create peer connection")
}