UNPKG

blockchain-api-client

Version:

Blockchain Api Client which maps Results of all supported Blockchain API's into a unified interface

3 lines (2 loc) 11.7 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,r=(t=require("node-fetch"))&&"object"==typeof t&&"default"in t?t.default:t;function e(){e=function(){return t};var t={},r=Object.prototype,n=r.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",a=o.asyncIterator||"@@asyncIterator",c=o.toStringTag||"@@toStringTag";function u(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,e){return t[r]=e}}function s(t,r,e,n){var o=Object.create((r&&r.prototype instanceof l?r:l).prototype),i=new L(n||[]);return o._invoke=function(t,r,e){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(e.method=o,e.arg=i;;){var a=e.delegate;if(a){var c=x(a,e);if(c){if(c===h)continue;return c}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var u=f(t,r,e);if("normal"===u.type){if(n=e.done?"completed":"suspendedYield",u.arg===h)continue;return{value:u.arg,done:e.done}}"throw"===u.type&&(n="completed",e.method="throw",e.arg=u.arg)}}}(t,e,i),o}function f(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=s;var h={};function l(){}function p(){}function v(){}var d={};u(d,i,(function(){return this}));var y=Object.getPrototypeOf,m=y&&y(y(E([])));m&&m!==r&&n.call(m,i)&&(d=m);var g=v.prototype=l.prototype=Object.create(d);function w(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function b(t,r){var e;this._invoke=function(o,i){function a(){return new r((function(e,a){!function e(o,i,a,c){var u=f(t[o],t,i);if("throw"!==u.type){var s=u.arg,h=s.value;return h&&"object"==typeof h&&n.call(h,"__await")?r.resolve(h.__await).then((function(t){e("next",t,a,c)}),(function(t){e("throw",t,a,c)})):r.resolve(h).then((function(t){s.value=t,a(s)}),(function(t){return e("throw",t,a,c)}))}c(u.arg)}(o,i,e,a)}))}return e=e?e.then(a,a):a()}}function x(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,x(t,r),"throw"===r.method))return h;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return h}var n=f(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,h;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,h):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,h)}function T(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function k(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(T,this),this.reset(!0)}function E(t){if(t){var r=t[i];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var e=-1,o=function r(){for(;++e<t.length;)if(n.call(t,e))return r.value=t[e],r.done=!1,r;return r.value=void 0,r.done=!0,r};return o.next=o}}return{next:_}}function _(){return{value:void 0,done:!0}}return p.prototype=v,u(g,"constructor",v),u(v,"constructor",p),p.displayName=u(v,c,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===p||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,v):(t.__proto__=v,u(t,c,"GeneratorFunction")),t.prototype=Object.create(g),t},t.awrap=function(t){return{__await:t}},w(b.prototype),u(b.prototype,a,(function(){return this})),t.AsyncIterator=b,t.async=function(r,e,n,o,i){void 0===i&&(i=Promise);var a=new b(s(r,e,n,o),i);return t.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},w(g),u(g,c,"Generator"),u(g,i,(function(){return this})),u(g,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var r=[];for(var e in t)r.push(e);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=E,L.prototype={constructor:L,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(k),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function e(e,n){return a.type="throw",a.arg=t,r.next=e,n&&(r.method="next",r.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return e("end");if(i.tryLoc<=this.prev){var c=n.call(i,"catchLoc"),u=n.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return e(i.catchLoc,!0);if(this.prev<i.finallyLoc)return e(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return e(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return e(i.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var o=this.tryEntries[e];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,h):this.complete(a)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),h},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),k(e),h}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;k(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:E(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),h}},t}function n(t,r,e,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void e(t)}c.done?r(u):Promise.resolve(u).then(n,o)}function o(t){return function(){var r=this,e=arguments;return new Promise((function(o,i){var a=t.apply(r,e);function c(t){n(a,o,i,c,u,"next",t)}function u(t){n(a,o,i,c,u,"throw",t)}c(void 0)}))}}function i(){return(i=Object.assign?Object.assign.bind():function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t}).apply(this,arguments)}var a,c=function(t){var r=u(t);return{balance:function(t){return"https://api.blockcypher.com/v1/btc/"+r+"/addrs/"+t+"/full"},transactions:function(t,e){return"https://api.blockcypher.com/v1/btc/"+r+"/addrs/"+t+"/full?"+e.toString()},utxo:function(t,e){return"https://api.blockcypher.com/v1/btc/"+r+"/txs/"+t+"/"+e},fees:function(){return"https://api.blockcypher.com/v1/btc/"+r},broadcastTransaction:function(){return"https://api.blockcypher.com/v1/btc/"+r+"/txs/push"}}},u=function(t){return"MAIN"===t?"main":"test3"};!function(t){t.POST="POST",t.GET="GET"}(a||(a={}));var s,f=function(){var t=o(e().mark((function t(n,o){var a,c,u,s,f,l;return e().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return c=(a=o||{}).body,u=a.method,s=a.args,t.next=3,r(n,{method:h(c,u),body:JSON.stringify(c),headers:i({"Content-Type":"application/json"},null==s?void 0:s.headers)});case 3:return f=t.sent,t.next=6,f.json();case 6:return l=t.sent,f.ok||console.error("Error from API, possibly show snackbar",l),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),h=function(t,r){return r||(t?a.POST:a.GET)},l=function(){var t=o(e().mark((function t(r,n){var o;return e().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n&&n.method&&n.method!==a.GET&&(o=r.includes("?")?"&":"?",r=""+r+o+"token="+p),t.abrupt("return",f(r,n));case 2:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),p="2952f06836cf48cc95d998b901c4a4bd ",v=function(){var t=o(e().mark((function t(r,n,o){var a;return e().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",f(r,i({},o,{args:i({},null==o?void 0:o.args,{headers:i({},null==o||null==(a=o.args)?void 0:a.headers,{"x-api-key":"MAIN"===n?d.main:d.test})})})));case 1:case"end":return t.stop()}}),t)})));return function(r,e,n){return t.apply(this,arguments)}}(),d={test:"89156412-0b04-4ed1-aede-d4546b60697c",main:"4d706557-f378-4242-b57f-78b368df0fc2"};!function(t){t[t.TATUM=0]="TATUM",t[t.BLOCKCYPHER=1]="BLOCKCYPHER"}(s||(s={}));var y=function(t){var r=this;this.getProviderFunctions=function(t){switch(t){case s.TATUM:return r.tatum;case s.BLOCKCYPHER:return r.blockCypher;default:return r.tatum}},this.blockCypher={fetcher:{fetchBalance:function(t){return l(c(r.network).balance(t))},fetchTransactions:function(t,e){return l(c(r.network).transactions(t,e))}},mapper:{responseToBalance:function(t){return{incoming:(r=t).total_received,outgoing:r.total_sent,unconfirmedBalance:r.unconfirmed_balance,confirmedBalance:r.final_balance};var r},responseToTransactions:function(t){return t.txs.map((function(t,r){return{blockNumber:t.block_index,fee:t.fees,hash:t.hash,hex:t.hex||"",index:r,inputs:t.inputs.map((function(t){return{prevout:{hash:t.prev_hash,index:t.output_index},sequence:t.sequence,script:t.script,scriptType:t.script_type}})),locktime:t.lock_time,outputs:t.outputs.map((function(t){return{value:t.value,script:t.script,address:t.addresses[0]}})),size:t.size,time:t.received.getTime(),version:t.ver,vsize:t.vsize,total:t.total,witnessHash:t.witness_hash||""}}))}}},this.tatum={fetcher:{fetchBalance:function(t){return v(function(t){return"https://api-eu1.tatum.io/v3/bitcoin/address/balance/"+t}(t),r.network)},fetchTransactions:function(t,e){return v(function(t,r){return"https://api-eu1.tatum.io/v3/bitcoin/transaction/address/"+t+"?"+r.toString()}(t,e),r.network)}},mapper:{responseToTransactions:function(t){return t.map((function(t){return i({},t,{total:void 0})}))},responseToBalance:function(t){return t}}},this.network=t};exports.BitcoinService=function(t){var r=this;this.getBalance=function(){var t=o(e().mark((function t(n,o){var i,a,c;return e().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return i=r.factory.getProviderFunctions(o),a=i.mapper,c=i.fetcher,t.next=3,c.fetchBalance(n);case 3:return t.abrupt("return",a.responseToBalance(t.sent));case 5:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),this.getTransactions=function(){var t=o(e().mark((function t(n,o,i){var a,c,u;return e().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=r.factory.getProviderFunctions(i),c=a.mapper,u=a.fetcher,t.next=3,u.fetchTransactions(n,o);case 3:return t.abrupt("return",c.responseToTransactions(t.sent));case 5:case"end":return t.stop()}}),t)})));return function(r,e,n){return t.apply(this,arguments)}}(),this.factory=new y(t)}; //# sourceMappingURL=blockchain-api-client.cjs.production.min.js.map