-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
204 lines (170 loc) · 5.14 KB
/
setup.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
package lightauth
import (
"context"
"github.com/lightningnetwork/lnd/macaroons"
"gopkg.in/macaroon.v2"
"io"
"io/ioutil"
"log"
"os"
"github.com/BurntSushi/toml"
"github.com/lightningnetwork/lnd/lnrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
var (
clientStore map[string]*Path
serverStore map[string]*Route
conn *grpc.ClientConn
lightningClient lnrpc.LightningClient
lightningClientStream lnrpc.Lightning_SendPaymentClient
lightningServerStream lnrpc.Lightning_SubscribeInvoicesClient
database DataProvider
)
// Record is an interface that superclasses all entities stored in a permanent store
type Record interface {
save() error
}
// DataProvider is an interface that specifies the methods required to store data
type DataProvider interface {
Create(Record) (string, error)
Edit(Record)
GetServerData() (map[string]*Route, error)
GetClientData() (map[string]*Path, error)
}
// RouteInfo is the bare fields that details a route
type RouteInfo struct {
Name string
Fee int
MaxInvoices int
Mode string
Period string
}
type tomlConfig struct {
ServerAddr string
CAFile string
ServerHostOverride string
MacaroonPath string
Routes map[string]*RouteInfo
}
func startRPCClient() (tomlConfig, error) {
var conf tomlConfig
if _, err := toml.DecodeFile("lightauth.toml", &conf); err != nil {
log.Fatalf("Lightauth error: Could not parse lightauth.toml: %v\n", err)
}
var opts []grpc.DialOption
creds, err := credentials.NewClientTLSFromFile(conf.CAFile, conf.ServerHostOverride)
if err != nil {
log.Fatalf("Lightauth error: Failed to create TLS credentials: %v\n", err)
}
opts = append(opts, grpc.WithTransportCredentials(creds))
b, err := ioutil.ReadFile(conf.MacaroonPath)
if err != nil {
return conf, err
}
mac := &macaroon.Macaroon{}
if err = mac.UnmarshalBinary(b); err != nil {
return conf, err
}
cred := macaroons.NewMacaroonCredential(mac)
opts = append(opts, grpc.WithPerRPCCredentials(cred))
conn, err = grpc.Dial(conf.ServerAddr, opts...)
if err != nil {
log.Fatalf("Lightauth error: Failed to start grpc connection: %v\n", err)
}
lightningClient = lnrpc.NewLightningClient(conn)
return conf, nil
}
// StartClientConnection is used to initiate the connection with the LDN node on a client's behalf.
func StartClientConnection(db DataProvider) *grpc.ClientConn {
database = db
_, err := startRPCClient()
if err != nil {
log.Fatalf("Lightauth error: Failed to start client: %v\n", err)
}
clientStore, err = db.GetClientData()
if err != nil {
log.Fatalf("Lightauth error: could not fetch data from store: %v\n", err)
}
ctxb := context.Background()
lightningClientStream, err = lightningClient.SendPayment(ctxb)
if err != nil {
log.Fatalf("Lightauth error: Failed to start lightning client stream: %v\n", err)
}
go func() {
for {
paymentResponse, err := lightningClientStream.Recv()
if err == io.EOF {
return
}
if err != nil {
log.Fatalf("Lightauth error: There was an error receiving data from the lightning client stream: %v\n", err)
}
if paymentResponse != nil {
if paymentResponse.PaymentError != "" {
log.Printf("Lightauth error: Lightning payment contains an error: %v\n", paymentResponse.PaymentError)
} else {
confirmInvoiceSettled(paymentResponse.PaymentPreimage)
}
}
}
}()
return conn
}
// StartServerConnection is used to initiate the connection with the LDN node on a server's behalf.
// It requires lightauth.toml to be populated with the connection params and
// the routes.
func StartServerConnection(db DataProvider) *grpc.ClientConn {
database = db
conf, err := startRPCClient()
if err != nil {
log.Fatalf("Lightauth error: Failed to start client: %v\n", err)
}
serverStore, err = db.GetServerData()
if err != nil {
log.Fatalf("Lightauth error: could not fetch data from store: %v\n", err)
}
for _, v := range conf.Routes {
if _, exists := serverStore[v.Name]; !exists {
// TODO: Delete from store those routes not in toml
r := &Route{
Clients: make(map[string]*Client),
RouteInfo: RouteInfo{
Name: v.Name,
Fee: v.Fee,
MaxInvoices: v.MaxInvoices,
Mode: v.Mode,
Period: v.Period,
},
}
err := r.save()
if err != nil {
os.Exit(1)
}
serverStore[v.Name] = r
}
}
ctxb := context.Background()
lightningServerStream, err = lightningClient.SubscribeInvoices(ctxb, &lnrpc.InvoiceSubscription{})
if err != nil {
log.Fatalf("Lightauth error: Failed to start lightning client stream: %v\n%v\n", conf, err)
}
go func() {
for {
invoiceUpdate, err := lightningServerStream.Recv()
if err == io.EOF {
return
}
if err != nil {
log.Printf("Lightauth error: There was an error receiving data from the lightning client stream: %v\n", err)
}
if invoiceUpdate != nil && invoiceUpdate.Settled {
err := updateInvoice(invoiceUpdate.PaymentRequest)
if err != nil {
// TODO: Serious error: we have been notified of a payment but we can't save it in database. EXCEPTIONAL
}
}
}
}()
return conn
}