Advertisement
Guest User

Untitled

a guest
May 26th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. const API_KEY = "af462d6b6a9814ebd17fcc53e7316afb";
  2.  
  3. function readFromAddress(address, success, failure) {
  4. function toByteArray(hex) {
  5. return hex.match(/.{1,2}/g).map(function(h) { return parseInt(h, 16) });
  6. }
  7. function toHexString(bytes) {
  8. return bytes.map(function(b) { return b.toString(16) }).join('');
  9. }
  10.  
  11. function parseResponse(responseText) {
  12. var response = JSON.parse(responseText).filter(function(transaction) {
  13. return transaction.receiver_addresses.contains(address);
  14. }).map(function(transaction) {
  15. var payload = toByteArray(transaction.hex);
  16. var version = (payload[0] >> 5);
  17. var command = (payload[0] & ((1 << 5) - 1));
  18. var data = payload.slice(1);
  19. if (command & parseInt('10000', 2)) {
  20. var counter = parseInt(toHexString(data.slice(0, 4)), 16);
  21. var string = data.slice(4);
  22. return {counter: counter, command: command, payload: string};
  23. } else {
  24. return null;
  25. }
  26. }).filter(function(transaction) {
  27. return transaction != null
  28. }).sort(function(a, b) {
  29. return a.counter - b.counter;
  30. });
  31.  
  32. var output = [], buffer = [];
  33. for (var i = 0; i < reseponse.length; i++) {
  34. if (response.command & 1) {
  35. buffer = [];
  36. }
  37. buffer = buffer.concat(response.payload);
  38. if (response.command & 2) {
  39. output.push(new Uint8Array(buffer));
  40. }
  41. }
  42. return output;
  43. }
  44.  
  45. var promise = null;
  46. if (typeof success == 'undefined') {
  47. // Try to load a promise
  48. if (typeof Promise != 'undefined') { // ECMAScript 6 Promises
  49. promise = new Promise();
  50. } else if (typeof jQuery != 'undefined') { // jQuery
  51. promise = jQuery.Deferred();
  52. } else if (typeof $q != 'undefined') { // AngularJS and Q
  53. promise = $q.defer();
  54. } else {
  55. console.warn("No promise engine detected and no success callback received. Ignoring call...");
  56. return;
  57. }
  58. }
  59. var xmlHttp = new XMLHttpRequest();
  60. xmlHttp.onreadystatechange = function() {
  61. if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
  62. if (promise != null) {
  63. promise.resolve(parseResponse(xmlHttp.responseText));
  64. } else if (success) {
  65. success(parseResponse(xmlHttp.responseText));
  66. }
  67. } else {
  68. if (promise != null) {
  69. promise.reject("Received HTTP status: " + xmlHttp.status + ", " + xmlHttp.responseText);
  70. } else if (failure) {
  71. failure("Received HTTP status: " + xmlHttp.status + ", " + xmlHttp.responseText);
  72. }
  73. }
  74. };
  75. xmlHttp.open("GET", "https://api.chain.com/v2/bitcoin/addresses/" + address + "/op-returns?api-key=" + API_KEY);
  76. xmlHttp.send(null);
  77. }
  78.  
  79. function main() {
  80. if (process.argv.length < 2) {
  81. console.error("Usage: reader.js <receiving address>");
  82. } else {
  83. console.log(readFromAddress(process.argv[1]));
  84. }
  85. }
  86.  
  87. if (typeof window == 'undefined' && require.main === module) { // Node.js
  88. main();
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement