forked from Siko91/BitBoxDemo-23Oct18
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcoin.js
40 lines (33 loc) · 1.22 KB
/
bitcoin.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
module.exports = { checkIsPaid : checkIsPaid }
const BITBOXSDK = require('bitbox-sdk/lib/bitbox-sdk').default;
const BITBOX = new BITBOXSDK;
const xpub = 'xpub661MyMwAqRbcGDjqdcZX3HDMnCifGDC9cEGVxGTP8ee5TApDEeeZbqXA4Vf9h9BnsLGawNAPRFmAZYm4pbwAKjD4G2CD9sEJrLKVdfhn8gn'
var derivation_path_idx = 0;
var orders = [];
function checkIsPaid(id, price, paidCallback, notPaidCallback) {
// id is unique for client-product (productName + IP addr)
var order = orders.find( order => order.id === id );
if (typeof order === 'undefined') {
order = { id: id, addr_idx: derivation_path_idx };
derivation_path_idx++;
orders.push(order);
}
var address = BITBOX.Address.fromXPub(xpub, '0/'+order.addr_idx);
(async () => {
try {
var details = await BITBOX.Address.details(address);
var total = details.totalReceived;
if (details.unconfirmedBalance > 0) {
total += details.unconfirmedBalance;
}
if (total < price) {
notPaidCallback(address);
}
else {
paidCallback();
}
} catch(error) {
console.error(error)
}
})()
}