-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathInvo.js
113 lines (102 loc) · 3.44 KB
/
Invo.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
var crypto = require('crypto');
var lightningPayReq = require('bolt11');
export class Invo {
constructor(redis, bitcoindrpc, lightning) {
this._redis = redis;
this._bitcoindrpc = bitcoindrpc;
this._lightning = lightning;
this._decoded = false;
this._bolt11 = false;
this._isPaid = null;
}
setInvoice(bolt11) {
this._bolt11 = bolt11;
}
async getIsMarkedAsPaidInDatabase() {
if (!this._bolt11) throw new Error('bolt11 is not provided');
const decoded = lightningPayReq.decode(this._bolt11);
let paymentHash = false;
for (const tag of decoded.tags) {
if (tag.tagName === 'payment_hash') {
paymentHash = tag.data;
}
}
if (!paymentHash) throw new Error('Could not find payment hash in invoice tags');
return await this._getIsPaymentHashMarkedPaidInDatabase(paymentHash);
}
async markAsPaidInDatabase() {
if (!this._bolt11) throw new Error('bolt11 is not provided');
const decoded = lightningPayReq.decode(this._bolt11);
let paymentHash = false;
for (const tag of decoded.tags) {
if (tag.tagName === 'payment_hash') {
paymentHash = tag.data;
}
}
if (!paymentHash) throw new Error('Could not find payment hash in invoice tags');
return await this._setIsPaymentHashPaidInDatabase(paymentHash, decoded.satoshis);
}
async markAsUnpaidInDatabase() {
if (!this._bolt11) throw new Error('bolt11 is not provided');
const decoded = lightningPayReq.decode(this._bolt11);
let paymentHash = false;
for (const tag of decoded.tags) {
if (tag.tagName === 'payment_hash') {
paymentHash = tag.data;
}
}
if (!paymentHash) throw new Error('Could not find payment hash in invoice tags');
return await this._setIsPaymentHashPaidInDatabase(paymentHash, false);
}
async _setIsPaymentHashPaidInDatabase(paymentHash, settleAmountSat) {
if (settleAmountSat) {
return await this._redis.set('ispaid_' + paymentHash, settleAmountSat);
} else {
return await this._redis.del('ispaid_' + paymentHash);
}
}
async _getIsPaymentHashMarkedPaidInDatabase(paymentHash) {
return await this._redis.get('ispaid_' + paymentHash);
}
async getPreimage() {
if (!this._bolt11) throw new Error('bolt11 is not provided');
const decoded = lightningPayReq.decode(this._bolt11);
let paymentHash = false;
for (const tag of decoded.tags) {
if (tag.tagName === 'payment_hash') {
paymentHash = tag.data;
}
}
if (!paymentHash) throw new Error('Could not find payment hash in invoice tags');
return await this._redis.get('preimage_for_' + paymentHash);
}
async savePreimage(preimageHex) {
const paymentHashHex = require('crypto').createHash('sha256').update(Buffer.from(preimageHex, 'hex')).digest('hex');
const key = 'preimage_for_' + paymentHashHex;
await this._redis.set(key, preimageHex);
await this._redis.expire(key, 3600 * 24 * 30); // 1 month
}
makePreimageHex() {
let buffer = crypto.randomBytes(32);
return buffer.toString('hex');
}
/**
* Queries LND ofr all user invoices
*
* @return {Promise<array>}
*/
async listInvoices() {
return new Promise((resolve, reject) => {
this._lightning.listInvoices(
{
num_max_invoices: 99000111,
reversed: true,
},
function (err, response) {
if (err) return reject(err);
resolve(response);
},
);
});
}
}