forked from stakwork/sphinx-keysend-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.js
89 lines (83 loc) · 2.33 KB
/
subscribe.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
const LND = require('./lightning')
const signer = require('./signer')
function subscribeInvoices(onReceive) {
return new Promise(async (resolve, reject) => {
const lightning = await LND.loadLightning()
var call = lightning.subscribeInvoices()
call.on('data', async function (response) {
if (response['state'] !== 'SETTLED') {
return
}
// console.log("IS KEYSEND", response.is_keysend)
if (response.is_keysend) {
parseKeysendInvoice(response, onReceive)
}
});
call.on('status', function (status) {
console.log("Status", status);
// The server is unavailable, trying to reconnect.
if (!(status.code == ERR_CODE_UNAVAILABLE || status.code == ERR_CODE_STREAM_REMOVED)) {
resolve(status);
}
})
call.on('error', function (err) {
console.error('[LND] Error', now, err.code)
if (!(err.code == ERR_CODE_UNAVAILABLE || err.code == ERR_CODE_STREAM_REMOVED)) {
reject(err)
}
})
call.on('end', function () {
console.log(`Closed stream ${now}`);
})
setTimeout(() => {
resolve(null)
}, 100)
})
}
async function parseKeysendInvoice(i, onReceive) {
const recs = i.htlcs && i.htlcs[0] && i.htlcs[0].custom_records
const buf = recs && recs[LND.SPHINX_CUSTOM_RECORD_KEY]
const data = buf && buf.toString()
const value = i && i.value && parseInt(i.value)
if (!data) return
let payload
if (data[0] === '{') {
try {
payload = await parseAndVerifyPayload(data)
} catch (e) { }
}
if (payload) {
const dat = payload
if (value && dat && dat.message) {
dat.message.amount = value // ADD IN TRUE VALUE
}
onReceive(dat)
} else {
console.log("could not validate")
}
}
// VERIFY PUBKEY OF SENDER from sig
async function parseAndVerifyPayload(data) {
let payload
const li = data.lastIndexOf('}')
const msg = data.substring(0, li + 1)
const sig = data.substring(li + 1)
try {
payload = JSON.parse(msg)
if (payload && payload.sender && payload.sender.pub_key) {
let v
if (sig.length === 96 && payload.sender.pub_key) {
v = await signer.verifyAscii(msg, sig, payload.sender.pub_key)
}
if (v && v.valid) {
return payload
}
}
} catch (e) {
console.log(e)
return null
}
}
module.exports = {
subscribeInvoices
}