-
Notifications
You must be signed in to change notification settings - Fork 3
/
getClaims.js
61 lines (56 loc) · 1.82 KB
/
getClaims.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
'use strict'
const Promise = require('bluebird')
const bitcoin = require('bitcoin-promise');
const rp = require('request-promise');
const request = require('request');
let client;
async function getClaims (height, gclient) {
return new Promise(async (resolve, reject) => {
try {
client = gclient;
let blockHash = await client.getBlockHash(height).then(blockHash => {return blockHash}).catch( err => reject(err));
let block = await client.getBlock(blockHash).then(block => {return block}).catch( err => reject(err));
let claims = await getClaimsForTxes(block.tx,height); // should return an array of claims, decoded if possible.
resolve(claims);
} catch (err) {
return reject(err)
}
})
}
async function getClaimsForTxes(txes,height) {
return new Promise(async (resolve, reject) => {
try {
let claimsArr = [];
let i=0;
for (let tx of txes) {
let claims_tx = await client.getClaimsForTx(tx).then(claims => {return claims}).catch( err => reject(err));
if(claims_tx != null){for (let claim of claims_tx) {
claim['height'] = height;
let d_claim = await getValue(tx,claim['nOut']);
if(d_claim !== 'error when decoding claims' && claim['value']){
claim['value'] = JSON.parse(d_claim);
claimsArr.push(claim);
}else{
claim['value'] = { error: 'non_decodable' }
claimsArr.push(claim);
}
}}
}
resolve(claimsArr);
} catch (err) {
return reject(err)
}
})
}
async function getValue(tx, i){
return new Promise(async (resolve, reject) => {
rp(`http://localhost:5000/claim_decode/${tx}/${i}`)
.then(function (htmlString) {
resolve(htmlString);
})
.catch(function (err) {
reject(err);
});
})
}
module.exports = exports = getClaims