Skip to content

Instantly share code, notes, and snippets.

@visvirial
Last active June 28, 2017 02:16
Show Gist options
  • Save visvirial/cacbf395e85bd657797b1e5241d31859 to your computer and use it in GitHub Desktop.
Save visvirial/cacbf395e85bd657797b1e5241d31859 to your computer and use it in GitHub Desktop.
Check the balance of your Bitcoin HD wallet at the specified block height.
/**
* @file claim_checker.js
*
* Check the balance of your Bitcoin HD wallet at the specified block height.
* This is usefull for checking what address is capable of receiving Stellar giveaway coins.
*/
const BASE_BLOCK_HEIGHT = 472889;
const HD_GAP_LIMIT = 20;
var async = require('async');
var request = require('request');
var bitcoin = require('bitcoinjs-lib');
if(process.argv.length < 3) {
console.log('usage: node claim_checker.js xpub...');
process.exit(1);
}
var check = function(xpub, callback) {
var idx = 0;
var nEmpty = 0;
async.whilst(function() {
return nEmpty < 20;
}, function(cb) {
var address = xpub.derive(idx).getAddress();
request.get('https://api.blockcypher.com/v1/btc/main/addrs/' + address, function(err, res) {
if(err) {
console.log('E: failed to fetch data from blockexplorer: ' + err);
process.exit(1);
}
var json = JSON.parse(res.body);
if(!json.txrefs) {
nEmpty++;
} else {
nEmpty = 0;
var balance = 0;
json.txrefs.forEach(function(tx) {
if(tx.block_height > BASE_BLOCK_HEIGHT) return;
balance += (tx.tx_input_n < 0 ? +1 : -1) * tx.value;
});
if(balance > 0) {
console.log(address + '\t' + (balance*1e-8).toFixed(8) + 'BTC');
}
}
idx++;
cb();
});
}, function() {
if(typeof callback == 'function') {
callback();
}
});
};
var xpubStr = process.argv[2];
var xpub = bitcoin.HDNode.fromBase58(xpubStr);
async.series([
function(cb) {
check(xpub.derive(0), cb);
},
function(cb) {
check(xpub.derive(1), cb);
},
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment