forked from stakwork/sphinx-keysend-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
67 lines (60 loc) · 1.56 KB
/
router.js
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
var ByteBuffer = require('bytebuffer')
var LND = require('./lightning')
var path = require('path')
var grpc = require('grpc');
const env = process.env.NODE_ENV || 'production'
const config = require(__dirname + '/config.json')[env]
var routerClient;
const loadRouter = () => {
if (routerClient) {
return routerClient
} else {
try {
var credentials = LND.loadCredentials('router.macaroon')
var descriptor = grpc.load("proto/router.proto");
var router = descriptor.routerrpc
routerClient = new router.Router(config.node_ip + ':' + config.lnd_port, credentials);
return routerClient
} catch (e) {
throw e
}
}
}
const buildRoute = (dests, amt) => { // dests: array of hex strings
return new Promise(async (resolve, reject) => {
let router = loadRouter()
try {
const options = {
hop_pubkeys: dests.map(d=>ByteBuffer.fromHex(d)),
final_cltv_delta: 144,
amt_msat: amt ? amt*1000 : 3000,
}
router.buildRoute(options, function (err, route) {
if (err) {
reject(err)
} else {
resolve(route)
}
})
} catch(e) {
reject(e)
}
})
}
const sendToRoute = (opts) => { // dests: array of hex strings
return new Promise(async (resolve, reject) => {
let router = loadRouter()
try {
router.sendToRoute(opts, function (err, res) {
if (err) {
reject(err)
} else {
resolve(res)
}
})
} catch(e) {
reject(e)
}
})
}
module.exports={buildRoute,sendToRoute,loadRouter}