Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const API_KEY = "af462d6b6a9814ebd17fcc53e7316afb";
- function readFromAddress(address, success, failure) {
- function toByteArray(hex) {
- return hex.match(/.{1,2}/g).map(function(h) { return parseInt(h, 16) });
- }
- function toHexString(bytes) {
- return bytes.map(function(b) { return b.toString(16) }).join('');
- }
- function parseResponse(responseText) {
- var response = JSON.parse(responseText).filter(function(transaction) {
- return transaction.receiver_addresses.contains(address);
- }).map(function(transaction) {
- var payload = toByteArray(transaction.hex);
- var version = (payload[0] >> 5);
- var command = (payload[0] & ((1 << 5) - 1));
- var data = payload.slice(1);
- if (command & parseInt('10000', 2)) {
- var counter = parseInt(toHexString(data.slice(0, 4)), 16);
- var string = data.slice(4);
- return {counter: counter, command: command, payload: string};
- } else {
- return null;
- }
- }).filter(function(transaction) {
- return transaction != null
- }).sort(function(a, b) {
- return a.counter - b.counter;
- });
- var output = [], buffer = [];
- for (var i = 0; i < reseponse.length; i++) {
- if (response.command & 1) {
- buffer = [];
- }
- buffer = buffer.concat(response.payload);
- if (response.command & 2) {
- output.push(new Uint8Array(buffer));
- }
- }
- return output;
- }
- var promise = null;
- if (typeof success == 'undefined') {
- // Try to load a promise
- if (typeof Promise != 'undefined') { // ECMAScript 6 Promises
- promise = new Promise();
- } else if (typeof jQuery != 'undefined') { // jQuery
- promise = jQuery.Deferred();
- } else if (typeof $q != 'undefined') { // AngularJS and Q
- promise = $q.defer();
- } else {
- console.warn("No promise engine detected and no success callback received. Ignoring call...");
- return;
- }
- }
- var xmlHttp = new XMLHttpRequest();
- xmlHttp.onreadystatechange = function() {
- if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
- if (promise != null) {
- promise.resolve(parseResponse(xmlHttp.responseText));
- } else if (success) {
- success(parseResponse(xmlHttp.responseText));
- }
- } else {
- if (promise != null) {
- promise.reject("Received HTTP status: " + xmlHttp.status + ", " + xmlHttp.responseText);
- } else if (failure) {
- failure("Received HTTP status: " + xmlHttp.status + ", " + xmlHttp.responseText);
- }
- }
- };
- xmlHttp.open("GET", "https://api.chain.com/v2/bitcoin/addresses/" + address + "/op-returns?api-key=" + API_KEY);
- xmlHttp.send(null);
- }
- function main() {
- if (process.argv.length < 2) {
- console.error("Usage: reader.js <receiving address>");
- } else {
- console.log(readFromAddress(process.argv[1]));
- }
- }
- if (typeof window == 'undefined' && require.main === module) { // Node.js
- main();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement