This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlnd.js
137 lines (128 loc) · 4.39 KB
/
lnd.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
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
const fetch = require('node-fetch')
module.exports.init=function(lndAddress,lnbitsKey){
if(lnbitsKey){return}
var fs = require('fs');
var grpc = require('@grpc/grpc-js');
var lnrpc = grpc.load('private/rpc.proto').lnrpc;
process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA'
var lndCert = fs.readFileSync('private/tls.cert');
var sslCreds = grpc.credentials.createSsl(lndCert);
var macaroonCreds = grpc.credentials.createFromMetadataGenerator(function(args, callback) {
var macaroon = fs.readFileSync("private/admin.macaroon").toString('hex');
var metadata = new grpc.Metadata()
metadata.add('macaroon', macaroon);
callback(null, metadata);
});
var creds = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds);
var lightning = new lnrpc.Lightning(lndAddress, creds);
/*var request = {}
lightning.getInfo(request, function(err, response) {
console.log(err)
console.log(response);
})*/
return lightning
}
module.exports.addInvoice=function(lightning,optionD,callback){
const {cost:value, message:memo,lnbitsKey,lnbitsUrl } = optionD
if (lnbitsKey) {
fetch(`${lnbitsUrl}/api/v1/payments`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Api-Key':lnbitsKey
},
body: JSON.stringify({out: false, amount: value, memo}) // body data type must match "Content-Type" header
}).then(res => {
return res.json()
}).then(res => {
callback(res)
})
return
}
var request = {
type: 0,
}
lightning.newAddress(request, function(err, response) {
if(err){
console.log(err)
return
}
//console.log(response);
var resAddr = response.address
//console.log(resAddr)
console.log("creating invoice----------")
var request = {
memo:memo,
value:value,
fallback_addr:resAddr
}
lightning.addInvoice(request, function(err, response) {
if(err){
console.log(err)
return
}
console.log(response);
response.fallback_addr = resAddr
callback(response)
})
})
}
module.exports.listenInvoice= function(gmail,lightning,db,optionD){
var localDB = require("./db.js")
const {lnbitsKey,lnbitsUrl } = optionD
if (lnbitsKey) {
const es = new EventSource(`${lnbitsUrl}/api/v1/payments/sse?api-key=${lnbitsKey}`)
es.addEventListener("payment-received", function(e) {
const {data} = e
localDB.handlePayment(gmail,db,data.bolt11,"",data.preimage,"",data.amount/1000,optionD)
})
es.addEventListener("error", function(e) {
console.log(e)
})
return
}
var request = {
settle_index: 1,
}
var call = lightning.subscribeInvoices(request)
console.log("listening invoce:")
call.on('data', function(response) {
// A response was received from the server.
//console.log(response);
if(response.settled){
localDB.handlePayment(gmail,db,response.payment_request,"",response.r_preimage,"",response.amt_paid,optionD)
}
});
call.on('status', function(status) {
// The current status of the stream.
});
call.on('end', function() {
// The server has closed the stream.
});
}
module.exports.listenTransaction = function(gmail,lightning,db,optionD){
var localDB = require("./db.js")
const {lnbitsKey } = optionD
if (lnbitsKey) {
return
}
var request = {}
var call = lightning.subscribeTransactions(request)
call.on('data', function(response) {
// A response was received from the server.
//console.log(response);
console.log("got something on chain")
//console.log(response.amount)
//console.log(optionD.cost)
//console.log(response.num_confirmations)
if(response.amount >= optionD.cost && response.num_confirmations >= 1){
localDB.handlePayment(gmail,db,"",response.dest_addresses[0],"",response.tx_hash,response.amount,optionD)
}
});
call.on('status', function(status) {
// The current status of the stream.
});
call.on('end', function() {
// The server has closed the stream.
});
}