Skip to content

Instantly share code, notes, and snippets.

@icemilo
Created May 23, 2016 07:52
Show Gist options
  • Save icemilo/3eb6b6dd0cea5942bb3a9214fb8fb077 to your computer and use it in GitHub Desktop.
Save icemilo/3eb6b6dd0cea5942bb3a9214fb8fb077 to your computer and use it in GitHub Desktop.
Fetches Bitcoin addresses from transaction ids using Google Spreadsheet
var requestPromise = require('request-promise');
var _ = require('lodash');
var Promise = require('bluebird');
var GoogleSpreadSheet = require('google-spreadsheet');
var doc = new GoogleSpreadSheet('<GOOGLE_SPREAD_SHEET_ID');
var creds = {
"jwt_secret": "",
"type": "service_account",
"project_id": "",
"private_key_id": "",
"private_key": "",
"client_email": "",
"client_id": "",
"auth_uri": "",
"token_uri": "",
"auth_provider_x509_cert_url": "",
"client_x509_cert_url": ""
};
function getAddressBlockCypher(reference, value) {
return new Promise(function (resolve, reject) {
return requestPromise('https://api.blockcypher.com/v1/btc/main/txs/' + reference).then(function (response) {
var body = JSON.parse(response);
if(_.isUndefined(body)) {
return reject('Undefined response');
}
var address = _.first(_.filter(body.outputs, function(output) {
return output.value === value;
}));
return resolve(address);
}).catch(function (error) {
return reject(error);
});
});
}
function getAddressBlockr(reference, value) {
return new Promise(function (resolve, reject) {
return requestPromise('http://btc.blockr.io/api/v1/tx/info/' + reference).then(function (response) {
var body = JSON.parse(response);
if(_.isUndefined(body)) {
return reject('Undefined response');
}
var address = _.get(_.first(_.filter(body.data.vouts, function(output) {
return output.amount === value;
})), 'address');
return resolve(address);
}).catch(function (error) {
return reject(error);
});
});
}
function getAddressBlockExplorer(reference, value) {
return new Promise(function (resolve, reject) {
return requestPromise('https://blockexplorer.com/api/tx/' + reference).then(function (response) {
var body = JSON.parse(response);
if(_.isUndefined(body)) {
return reject('Undefined response');
}
var address = _.first(_.get(_.first(_.filter(body.vout, function(output) {
return output.value === value;
})), 'scriptPubKey.addresses'));
return resolve(address);
}).catch(function (error) {
return reject(error);
});
});
}
function getAddress(reference, value, valuemBTC) {
return new Promise(function (resolve, reject) {
return getAddressBlockCypher(reference, valuemBTC).then(function (address) {
return resolve(address);
}).catch(function (bcError) {
console.log('Blockcypher error! Failover to Blockr...');
return getAddressBlockr(reference, value).then(function (address) {
return resolve(address);
}).catch(function (blockrError) {
console.log('Blockr error! Failover to BlockExplorer...');
return getAddressBlockExplorer(reference, value).then(function (address) {
return resolve(address);
}).catch(function (beError) {
return reject('ERROR!');
});
});
});
});
}
var sheet;
var auth = doc.useServiceAccountAuth(creds, function (sheet) {
doc.getInfo(function (err, info ) {
sheet = Promise.promisifyAll(info.worksheets[2]);
sheet.getRowsAsync({
offset: 0, //FROM WHICH ROW
limit: 500 //HOW MANY ITEMS?
}).then(function (rows) {
return _.filter(rows, function(row) {
return row.destinationaddress === '' && row.reference !== 'NULL';
});
}).then(function (filteredRows) {
console.log(filteredRows);
console.log('Number of rows to process = ' + filteredRows.length);
return Promise.map(filteredRows, function (row) {
var rowAsync = Promise.promisifyAll(row);
return getAddress(row.reference, row.total + '', row.mtotal + '').then(function (address) {
console.log('Saving address(' + address + ') for reference(' + row.reference + ') ...');
row.destinationaddress = address;
return row.saveAsync();
});
}).then(function () {
return console.log('Done :)');
}).catch(function (error) {
return console.log('Error : ' + error);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment