This repository has been archived by the owner on Oct 31, 2019. It is now read-only.
forked from offerm/swap-resolver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resolver.go
522 lines (411 loc) · 12.5 KB
/
resolver.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
package main
// TODO: seperate resolver RPC into a standalone package
// TODO: properly handle the time locks - Important!!!!
import (
"fmt"
"log"
"net"
"sync"
"encoding/hex"
"crypto/rand"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "github.com/ExchangeUnion/lnd/lnrpc"
pbp2p "github.com/ExchangeUnion/swap-resolver/swapp2p"
"github.com/urfave/cli"
"os"
"github.com/davecgh/go-spew/spew"
"time"
"github.com/dchest/uniuri"
"github.com/btcsuite/btcutil"
"path/filepath"
"google.golang.org/grpc/credentials"
"crypto/sha256"
"github.com/ExchangeUnion/lnd/lnrpc"
)
type hashResolverServer struct {
p2pServer *P2PServer
mu sync.Mutex // protects data structure
}
type role int
const (
defaultTLSCertFilename = "tls.cert"
defaultMacaroonFilename = "admin.macaroon"
defaultRpcPort = "10009"
defaultRpcHostPort = "localhost:" + defaultRpcPort
Maker role = iota + 1
Taker
)
type deal struct {
// Maker or Taker ?
myRole role
// global order it in XU network
orderId string
takerDealId string
takerAmount int64
// takerCoin is the name of the coin the taker is expecting to get
takerCoin pbp2p.CoinType
takerPubKey string
makerDealId string
makerAmount int64
// makerCoin is the name of the coin the maker is expecting to get
makerCoin pbp2p.CoinType
makerPubKey string
hash [32]byte
preImage [32]byte
createTime time.Time
executeTime time.Time
competionTime time.Time
}
var (
//tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
//certFile = flag.String("cert_file", "", "The TLS cert file")
//keyFile = flag.String("key_file", "", "The TLS key file")
//port = flag.Int("port", 10000, "The server port")
//Commit stores the current commit hash of this build. This should be
//set using -ldflags during compilation.
Commit string
deals []*deal
defaultLndDir = btcutil.AppDataDir("lnd", false)
defaultTLSCertPath = filepath.Join(defaultLndDir, defaultTLSCertFilename)
defaultMacaroonPath = filepath.Join(defaultLndDir, defaultMacaroonFilename)
)
// GetFeature returns the feature at the given point.
func (s *hashResolverServer) ResolveHash(ctx context.Context, req *pb.ResolveRequest) (*pb.ResolveResponse, error) {
var deal *deal
log.Printf("ResolveHash stating with for hash: %v amount %v ",req.Hash, req.Amount)
for _, d := range deals{
if hex.EncodeToString(d.hash[:]) == req.Hash{
deal = d
break
}
}
if deal == nil{
log.Printf("Something went wrong. Can't find deal in hashResolverServer: %v ",req.Hash)
return nil, fmt.Errorf("Something went wrong. Can't find deal in hashResolverServer")
}
// If I'm the taker I need to forward the payment to the other chanin
// TODO: check that I got the right amount before sending out the agreed amount
if deal.myRole == Taker{
log.Printf("Taker code")
cmdLnd := s.p2pServer.lnBTC
switch deal.makerCoin{
case pbp2p.CoinType_BTC:
case pbp2p.CoinType_LTC:
cmdLnd = s.p2pServer.lnLTC
}
lncctx := context.Background()
resp, err := cmdLnd.SendPaymentSync(lncctx,&lnrpc.SendRequest{
DestString:deal.makerPubKey,
Amt:deal.makerAmount,
PaymentHash:deal.hash[:],
})
if err != nil{
err = fmt.Errorf("Got error sending %d %v by taker - %v",
deal.makerAmount,deal.makerCoin.String(),err)
log.Printf(err.Error())
return nil, err
}
if resp.PaymentError != ""{
err = fmt.Errorf("Got PaymentError sending %d %v by taker - %v",
deal.makerAmount,deal.makerCoin.String(), resp.PaymentError)
log.Printf(err.Error())
return nil, err
}
log.Printf("sendPayment response from maker to taker:%v",spew.Sdump(resp))
return &pb.ResolveResponse{
Preimage: hex.EncodeToString(resp.PaymentPreimage[:]),
}, nil
}
// If we are here we are the maker
log.Printf("Maker code")
return &pb.ResolveResponse{
Preimage: hex.EncodeToString(deal.preImage[:]),
}, nil
}
func newServer(p2pServer *P2PServer) *hashResolverServer {
s := &hashResolverServer{
p2pServer: p2pServer,
}
return s
}
type P2PServer struct {
xuPeer pbp2p.P2PClient
lnLTC lnrpc.LightningClient
lnBTC lnrpc.LightningClient
mu sync.Mutex // protects data structure
}
// TakeOrder is called to initiate a swap between maker and taker
// it is a temporary service needed until the integration with XUD
// intended to be called from CLI to simulate order taking by taker
func (s *P2PServer) TakeOrder(ctx context.Context, req *pbp2p.TakeOrderReq) (*pbp2p.TakeOrderResp, error){
log.Printf("TakeOrder (maker) stating with %v: ",spew.Sdump(req))
ctxt, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var (
info *lnrpc.GetInfoResponse
err error
)
switch req.TakerCoin{
case pbp2p.CoinType_BTC:
info, err = s.lnBTC.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
if err != nil{
return nil,fmt.Errorf("Can't getInfo from BTC LND - %v", err)
}
case pbp2p.CoinType_LTC:
info, err = s.lnLTC.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
if err != nil{
return nil,fmt.Errorf("Can't getInfo from LTC LND - %v", err)
}
}
log.Printf("%v getinfo:%v",req.TakerCoin.String(),spew.Sdump(info))
spew.Sdump(info)
newDeal := deal{
myRole: Taker,
orderId: req.Orderid,
takerDealId: uniuri.New(),
takerAmount: req.TakerAmount,
takerCoin: req.TakerCoin,
takerPubKey: info.IdentityPubkey,
makerAmount: req.MakerAmount,
makerCoin: req.MakerCoin,
createTime: time.Now(),
}
log.Printf("Suggesting deal to peer. deal data %v: ",spew.Sdump(newDeal))
suggestDealResp, err := s.xuPeer.SuggestDeal(ctx, &pbp2p.SuggestDealReq{
Orderid: newDeal.orderId,
TakerCoin: newDeal.takerCoin,
TakerAmount: newDeal.takerAmount,
TakerDealId: newDeal.takerDealId,
TakerPubkey: newDeal.takerPubKey,
MakerCoin: newDeal.makerCoin,
MakerAmount: newDeal.makerAmount,
})
if err != nil{
err = fmt.Errorf("SuggestDeal failed with %v", err)
return nil, err
}
newDeal.makerDealId = suggestDealResp.MakerDealId
newDeal.makerPubKey = suggestDealResp.MakerPubkey
copy(newDeal.hash[:],suggestDealResp.RHash[:32])
log.Printf("Deal Agreed with maker. deal data %v: ",spew.Sdump(newDeal))
// TODO: protect data structure
deals = append(deals, &newDeal)
newDeal.executeTime = time.Now()
swapResp, err := s.xuPeer.Swap(ctx, &pbp2p.SwapReq{
MakerDealId: suggestDealResp.MakerDealId,
})
if err != nil{
err = fmt.Errorf("Swap failed with %v", err)
return nil, err
}
ret := &pbp2p.TakeOrderResp{
RPreimage: swapResp.RPreimage,
}
return ret, nil
}
// SuggestDeal is called by the taker to inform the maker that he
// would like to execute a swap. The maker may reject the request
// for now, the maker can only accept/reject and can't rediscuss the
// deal or suggest partial amount. If accepted the maker should respond
// with a hash that would be used for teh swap.
func (s *P2PServer) SuggestDeal(ctx context.Context, req *pbp2p.SuggestDealReq) (*pbp2p.SuggestDealResp, error){
log.Printf("SuggestDeal (taker) stating with %v: ",spew.Sdump(req))
ctxt, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var (
info *lnrpc.GetInfoResponse
err error
)
switch req.MakerCoin{
case pbp2p.CoinType_BTC:
info, err = s.lnBTC.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
if err != nil{
return nil,fmt.Errorf("Can't getInfo from BTC LND - %v", err)
}
case pbp2p.CoinType_LTC:
info, err = s.lnLTC.GetInfo(ctxt, &lnrpc.GetInfoRequest{})
if err != nil{
return nil,fmt.Errorf("Can't getInfo from LTC LND - %v", err)
}
}
log.Printf("%v getinfo:%v",req.TakerCoin.String(),spew.Sdump(info))
spew.Sdump(info)
newDeal := deal{
myRole: Maker,
orderId: req.Orderid,
takerDealId: req.TakerDealId,
takerAmount: req.TakerAmount,
takerCoin: req.TakerCoin,
takerPubKey: req.TakerPubkey,
makerPubKey: info.IdentityPubkey,
makerAmount: req.MakerAmount,
makerCoin: req.MakerCoin,
makerDealId: uniuri.New(),
hash: [32]byte{
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
},
createTime: time.Now(),
}
// create preimage and a hash for the deal
if _, err := rand.Read(newDeal.preImage[:]); err != nil {
return nil, fmt.Errorf("Cant create preimage - %v", err)
}
newDeal.hash = sha256.Sum256(newDeal.preImage[:])
deals = append(deals, &newDeal)
ret := pbp2p.SuggestDealResp{
Orderid: newDeal.orderId,
RHash: newDeal.hash[:],
MakerDealId:newDeal.makerDealId,
MakerPubkey:newDeal.makerPubKey,
}
return &ret, nil
}
// Swap initiates the swap. It is called by the taker to confirm that
// he has the hash and confirm the deal.
func (s *P2PServer) Swap(ctx context.Context, req *pbp2p.SwapReq) (*pbp2p.SwapResp, error) {
var deal *deal
log.Printf("Swap (maker) stating with: %v ",spew.Sdump(req))
for _, d := range deals{
if d.makerDealId == req.MakerDealId{
deal = d
break
}
}
if deal == nil{
return nil, fmt.Errorf("Something went wrong. Can't find maker deal in swap")
}
cmdLnd := s.lnLTC
switch deal.makerCoin{
case pbp2p.CoinType_BTC:
case pbp2p.CoinType_LTC:
cmdLnd = s.lnBTC
}
lncctx := context.Background()
resp, err := cmdLnd.SendPaymentSync(lncctx,&lnrpc.SendRequest{
DestString:deal.takerPubKey,
Amt:deal.takerAmount,
PaymentHash:deal.hash[:],
})
if err != nil{
return nil, fmt.Errorf("Got error sending %d %v by maker - %v",
deal.takerAmount,deal.takerCoin.String(),err)
}
if resp.PaymentError != ""{
return nil, fmt.Errorf("Got error sending %d %v by maker - %v",
deal.takerAmount,deal.takerCoin.String(), resp.PaymentError)
}
log.Printf("sendPayment response:%v",spew.Sdump(resp))
ret := &pbp2p.SwapResp{
RPreimage: deal.preImage[:],
}
return ret, nil
}
func newP2PServer(xuPeer pbp2p.P2PClient, lnLTC lnrpc.LightningClient, lnBTC lnrpc.LightningClient) *P2PServer {
s := &P2PServer{
xuPeer: xuPeer,
lnLTC: lnLTC,
lnBTC: lnBTC,
}
return s
}
func fatal(err error) {
fmt.Fprintf(os.Stderr, "[lncli] %v\n", err)
os.Exit(1)
}
func getClient(ctx *cli.Context) (pbp2p.P2PClient, func()) {
conn := getPeerConn(ctx, false)
cleanUp := func() {
conn.Close()
}
return pbp2p.NewP2PClient(conn), cleanUp
}
func getPeerConn(ctx *cli.Context, skipMacaroons bool) *grpc.ClientConn {
var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure())
conn, err := grpc.Dial(ctx.GlobalString("peer"), opts...)
if err != nil {
fatal(err)
}
return conn
}
func main() {
app := cli.NewApp()
app.Name = "resolver"
app.Version = fmt.Sprintf("%s commit=%s", "0.0.1", Commit)
app.Usage = "Use me to simulate order taking by the taker"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "listen",
Value: "localhost:10000",
Usage: "An ip:port to listen for peer connections",
},
cli.StringFlag{
Name: "peer",
Value: "a.b.com:10000",
Usage: "A host:port of peer xu daemon",
},
cli.StringFlag{
Name: "lnd-rpc-ltc",
Value: "localhost:10001",
Usage: "RPC host:port of LND connected to LTC chain",
},
cli.StringFlag{
Name: "lnd-rpc-btc",
Value: "localhost:10002",
Usage: "RPC host:port of LND connected to LTC chain",
},
}
app.Action = func(c *cli.Context) error {
log.Printf("Server starting")
lnLTC, close := getLNDClient(c,"lnd-rpc-ltc")
defer close()
lnBTC, close := getLNDClient(c,"lnd-rpc-btc")
defer close()
xuPeer, close := getClient(c)
defer close()
log.Printf("Got peer connection")
listen := c.GlobalString("listen")
lis, err := net.Listen("tcp", listen)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
log.Printf("listening on %s", listen)
var opts []grpc.ServerOption
grpcServer := grpc.NewServer(opts...)
p2pServer := newP2PServer(xuPeer, lnLTC, lnBTC)
pb.RegisterHashResolverServer(grpcServer, newServer(p2pServer))
pbp2p.RegisterP2PServer(grpcServer, p2pServer)
log.Printf("Server ready")
grpcServer.Serve(lis)
return nil
}
if err := app.Run(os.Args); err != nil {
fatal(err)
}
}
func getLNDClient(ctx *cli.Context, name string) (lnrpc.LightningClient, func()) {
conn := getLNDClientConn(ctx, false, name)
cleanUp := func() {
conn.Close()
}
return lnrpc.NewLightningClient(conn), cleanUp
}
func getLNDClientConn(ctx *cli.Context, skipMacaroons bool, name string) *grpc.ClientConn {
creds, err := credentials.NewClientTLSFromFile(defaultTLSCertPath, "")
if err != nil {
fatal(err)
}
// Create a dial options array.
opts := []grpc.DialOption{
grpc.WithTransportCredentials(creds),
}
conn, err := grpc.Dial(ctx.String(name), opts...)
if err != nil {
fatal(err)
}
return conn
}