@blocto/sdk
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).BloctoProvider=t()}(this,(function(){"use strict";function e(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function t(t){for(var n=1;n<arguments.length;n++){var r=null!=arguments[n]?arguments[n]:{};n%2?e(Object(r),!0).forEach((function(e){a(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):e(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function n(e,t,n,r,o,i,a){try{var s=e[i](a),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,o)}function r(e){return function(){var t=this,r=arguments;return new Promise((function(o,i){var a=e.apply(t,r);function s(e){n(a,o,i,s,c,"next",e)}function c(e){n(a,o,i,s,c,"throw",e)}s(void 0)}))}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var s="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function c(){throw new Error("setTimeout has not been defined")}function u(){throw new Error("clearTimeout has not been defined")}var h=c,l=u;function f(e){if(h===setTimeout)return setTimeout(e,0);if((h===c||!h)&&setTimeout)return h=setTimeout,setTimeout(e,0);try{return h(e,0)}catch(t){try{return h.call(null,e,0)}catch(t){return h.call(this,e,0)}}}"function"==typeof s.setTimeout&&(h=setTimeout),"function"==typeof s.clearTimeout&&(l=clearTimeout);var p,d=[],v=!1,m=-1;function b(){v&&p&&(v=!1,p.length?d=p.concat(d):m=-1,d.length&&w())}function w(){if(!v){var e=f(b);v=!0;for(var t=d.length;t;){for(p=d,d=[];++m<t;)p&&p[m].run();m=-1,t=d.length}p=null,v=!1,function(e){if(l===clearTimeout)return clearTimeout(e);if((l===u||!l)&&clearTimeout)return l=clearTimeout,clearTimeout(e);try{l(e)}catch(t){try{return l.call(null,e)}catch(t){return l.call(this,e)}}}(e)}}function g(e,t){this.fun=e,this.array=t}g.prototype.run=function(){this.fun.apply(null,this.array)};function y(){}var k=y,E=y,T=y,j=y,I=y,x=y,O=y;var A=s.performance||{},_=A.now||A.mozNow||A.msNow||A.oNow||A.webkitNow||function(){return(new Date).getTime()};var P=new Date;var L={nextTick:function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var n=1;n<arguments.length;n++)t[n-1]=arguments[n];d.push(new g(e,t)),1!==d.length||v||f(w)},title:"browser",browser:!0,env:{},argv:[],version:"",versions:{},on:k,addListener:E,once:T,off:j,removeListener:I,removeAllListeners:x,emit:O,binding:function(e){throw new Error("process.binding is not supported")},cwd:function(){return"/"},chdir:function(e){throw new Error("process.chdir is not supported")},umask:function(){return 0},hrtime:function(e){var t=.001*_.call(A),n=Math.floor(t),r=Math.floor(t%1*1e9);return e&&(n-=e[0],(r-=e[1])<0&&(n--,r+=1e9)),[n,r]},platform:"browser",release:{},config:{},uptime:function(){return(new Date-P)/1e3}},R=function(e,t,n,r,o,i,a,s){if("production"!==L.env.NODE_ENV&&void 0===t)throw new Error("invariant requires an error message argument");if(!e){var c;if(void 0===t)c=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var u=[n,r,o,i,a,s],h=0;(c=new Error(t.replace(/%s/g,(function(){return u[h++]})))).name="Invariant Violation"}throw c.framesToPop=1,c}},C=Object.freeze({__proto__:null,default:{}});function S(e,t){for(var n=0,r=e.length-1;r>=0;r--){var o=e[r];"."===o?e.splice(r,1):".."===o?(e.splice(r,1),n++):n&&(e.splice(r,1),n--)}if(t)for(;n--;n)e.unshift("..");return e}var N=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,D=function(e){return N.exec(e).slice(1)};function z(){for(var e="",t=!1,n=arguments.length-1;n>=-1&&!t;n--){var r=n>=0?arguments[n]:"/";if("string"!=typeof r)throw new TypeError("Arguments to path.resolve must be strings");r&&(e=r+"/"+e,t="/"===r.charAt(0))}return(t?"/":"")+(e=S(H(e.split("/"),(function(e){return!!e})),!t).join("/"))||"."}function q(e){var t=B(e),n="/"===X(e,-1);return(e=S(H(e.split("/"),(function(e){return!!e})),!t).join("/"))||t||(e="."),e&&n&&(e+="/"),(t?"/":"")+e}function B(e){return"/"===e.charAt(0)}function M(){var e=Array.prototype.slice.call(arguments,0);return q(H(e,(function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e})).join("/"))}function V(e,t){function n(e){for(var t=0;t<e.length&&""===e[t];t++);for(var n=e.length-1;n>=0&&""===e[n];n--);return t>n?[]:e.slice(t,n-t+1)}e=z(e).substr(1),t=z(t).substr(1);for(var r=n(e.split("/")),o=n(t.split("/")),i=Math.min(r.length,o.length),a=i,s=0;s<i;s++)if(r[s]!==o[s]){a=s;break}var c=[];for(s=a;s<r.length;s++)c.push("..");return(c=c.concat(o.slice(a))).join("/")}function U(e){var t=D(e),n=t[0],r=t[1];return n||r?(r&&(r=r.substr(0,r.length-1)),n+r):"."}function $(e,t){var n=D(e)[2];return t&&n.substr(-1*t.length)===t&&(n=n.substr(0,n.length-t.length)),n}function F(e){return D(e)[3]}var G={extname:F,basename:$,dirname:U,sep:"/",delimiter:":",relative:V,join:M,isAbsolute:B,normalize:q,resolve:z};function H(e,t){if(e.filter)return e.filter(t);for(var n=[],r=0;r<e.length;r++)t(e[r],r,e)&&n.push(e[r]);return n}var J,X="b"==="ab".substr(-1)?function(e,t,n){return e.substr(t,n)}:function(e,t,n){return t<0&&(t=e.length+t),e.substr(t,n)},K=Object.freeze({__proto__:null,resolve:z,normalize:q,isAbsolute:B,join:M,relative:V,sep:"/",delimiter:":",dirname:U,basename:$,extname:F,default:G});function Q(){if(void 0===J){var e=new ArrayBuffer(2),t=new Uint8Array(e),n=new Uint16Array(e);if(t[0]=1,t[1]=2,258===n[0])J="BE";else{if(513!==n[0])throw new Error("unable to figure out endianess");J="LE"}}return J}function W(){return void 0!==s.location?s.location.hostname:""}function Y(){return[]}function Z(){return 0}function ee(){return Number.MAX_VALUE}function te(){return Number.MAX_VALUE}function ne(){return[]}function re(){return"Browser"}function oe(){return void 0!==s.navigator?s.navigator.appVersion:""}function ie(){}function ae(){}function se(){return"/tmp"}var ce=se,ue={EOL:"\n",tmpdir:ce,tmpDir:se,networkInterfaces:ie,getNetworkInterfaces:ae,release:oe,type:re,cpus:ne,totalmem:te,freemem:ee,uptime:Z,loadavg:Y,hostname:W,endianness:Q},he=Object.freeze({__proto__:null,endianness:Q,hostname:W,loadavg:Y,uptime:Z,freemem:ee,totalmem:te,cpus:ne,type:re,release:oe,networkInterfaces:ie,getNetworkInterfaces:ae,arch:function(){return"javascript"},platform:function(){return"browser"},tmpDir:se,tmpdir:ce,EOL:"\n",default:ue});function le(e){return e&&e.default||e}var fe=le(C),pe=le(K),de=le(he);function ve(e){console.log(`[dotenv][DEBUG] ${e}`)}const me=/^\s*([\w.-]+)\s*=\s*(.*)?\s*$/,be=/\\n/g,we=/\r\n|\n|\r/;function ge(e,t){const n=Boolean(t&&t.debug),r={};return e.toString().split(we).forEach((function(e,t){const o=e.match(me);if(null!=o){const e=o[1];let t=o[2]||"";const n=t.length-1,i='"'===t[0]&&'"'===t[n];"'"===t[0]&&"'"===t[n]||i?(t=t.substring(1,n),i&&(t=t.replace(be,"\n"))):t=t.trim(),r[e]=t}else n&&ve(`did not match key and value when parsing line ${t+1}: ${e}`)})),r}function ye(e){var t=document.createElement("iframe");return t.setAttribute("src",e),t.setAttribute("style","width:100vw;height:100vh;position:fixed;top:0;left:0;z-index:1000;border:none;"),t}function ke(e){document.body.appendChild(e)}function Ee(e){e.parentNode.removeChild(e)}var Te={56:"https://bsc-dataseed1.binance.org",97:"https://data-seed-prebsc-1-s1.binance.org:8545"},je={1:"ethereum",4:"ethereum",56:"bsc",97:"bsc"},Ie={1:"mainnet",4:"rinkeby",56:"mainnet",97:"testnet"},xe={1:"https://wallet.blocto.app",4:"https://wallet-testnet.blocto.app",56:"https://wallet.blocto.app",97:"https://wallet-testnet.blocto.app"},Oe=["connect","disconnect","message","chainChanged","accountsChanged"];return{config:function(e){let t=pe.resolve(L.cwd(),".env"),n="utf8",r=!1;var o;e&&(null!=e.path&&(t="~"===(o=e.path)[0]?pe.join(de.homedir(),o.slice(1)):o),null!=e.encoding&&(n=e.encoding),null!=e.debug&&(r=!0));try{const e=ge(fe.readFileSync(t,{encoding:n}),{debug:r});return Object.keys(e).forEach((function(t){Object.prototype.hasOwnProperty.call(L.env,t)?r&&ve(`"${t}" is already defined in \`process.env\` and will not be overwritten`):L.env[t]=e[t]})),{parsed:e}}catch(e){return{error:e}}},parse:ge}.config(),function(){function e(){var t=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=n.chainId,i=n.rpc,s=n.server,c=n.appId;o(this,e),a(this,"isBlocto",!0),a(this,"isConnecting",!1),a(this,"connected",!1),a(this,"chainId",null),a(this,"networkId",null),a(this,"chain",null),a(this,"net",null),a(this,"rpc",null),a(this,"server",null),a(this,"appId",null),a(this,"eventListeners",{}),a(this,"accounts",[]),a(this,"off",removeEventListener),R(r,"'chainId' is required"),"number"==typeof r?this.chainId=r:r.includes("0x")?this.chainId=parseInt(r,16):this.chainId=parseInt(r,10),this.networkId=this.chainId,this.chain=je[this.chainId],this.net=Ie[this.chainId],R(this.chain,"unsupported 'chainId': ".concat(this.chainId)),this.rpc=process.env.RPC||i||Te[this.chainId],R(this.rpc,"'rpc' is required for Ethereum"),this.server=process.env.SERVER||s||xe[this.chainId],this.appId=process.env.APP_ID||c,Oe.forEach((function(e){t.eventListeners[e]=[]}))}var n,s,c,u,h,l,f,p,d,v;return n=e,(s=[{key:"send",value:(v=r(regeneratorRuntime.mark((function e(t,n){return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.t0=!0,e.next=e.t0===n instanceof Function?3:e.t0===("string"==typeof t&&Array.isA)?4:5;break;case 3:return e.abrupt("return",this.sendAsync(t,n));case 4:return e.abrupt("return",this.sendAsync({method:t,params:n}));case 5:return e.abrupt("return",this.sendAsync(t));case 6:case"end":return e.stop()}}),e,this)}))),function(e,t){return v.apply(this,arguments)})},{key:"sendAsync",value:(d=r(regeneratorRuntime.mark((function e(t,n){var r,o=this;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=new Promise((function(e){if(Array.isArray(t)){var n=t.filter((function(e){return"eth_sendTransaction"===e.method})).map((function(e){return e.params[0]})),r=parseInt(1e4*Math.random(),10),i={method:"blocto_sendBatchTransaction",params:n},a=o.request(i),s=t.map((function(e,t){var n=e.method,i=e.params;return"eth_sendTransaction"===n?a:o.request({id:r+t+1,jsonrpc:"2.0",method:n,params:i})}));Promise.allSettled(s).then((function(t){return e(t.map((function(e,t){return{id:r+t+1,jsonrpc:"2.0",result:"fulfilled"===e.status?e.value:void 0,error:"fulfilled"!==e.status?e.value:void 0}})))}))}else o.request(t).then((function(t){return e(null,t)}))})),!n){e.next=5;break}r.then((function(e){return n(null,e)})).catch((function(e){return n(e)})),e.next=6;break;case 5:return e.abrupt("return",r);case 6:case"end":return e.stop()}}),e)}))),function(e,t){return d.apply(this,arguments)})},{key:"request",value:(p=r(regeneratorRuntime.mark((function e(n){var r,o;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!window.ethereum||!window.ethereum.isBlocto){e.next=2;break}return e.abrupt("return",window.ethereum.request(n));case 2:if(this.connected){e.next=5;break}return e.next=5,this.enable();case 5:e.prev=5,r=null,o=null,e.t0=n.method,e.next="eth_requestAccounts"===e.t0?11:"eth_accounts"===e.t0?14:"eth_coinbase"===e.t0?16:"eth_chainId"===e.t0?18:"net_version"===e.t0?21:"eth_sign"===e.t0?24:"blocto_sendBatchTransaction"===e.t0||"eth_sendTransaction"===e.t0?29:"eth_signTransaction"===e.t0||"eth_sendRawTransaction"===e.t0?33:35;break;case 11:return e.next=13,this.fetchAccounts();case 13:this.accounts=e.sent;case 14:return o=this.accounts,e.abrupt("break",38);case 16:return o=this.accounts[0],e.abrupt("break",38);case 18:return o=this.chainId,o="0x".concat(o.toString(16)),e.abrupt("break",38);case 21:return o=this.networkId||this.chainId,o="0x".concat(o.toString(16)),e.abrupt("break",38);case 24:return e.next=26,this.handleSign(n);case 26:return o=(o=e.sent).signature,e.abrupt("break",38);case 29:return e.next=31,this.handleSendTransaction(n);case 31:return o=e.sent,e.abrupt("break",38);case 33:return o=null,e.abrupt("break",38);case 35:return e.next=37,this.handleReadRequests(t({id:1,jsonrpc:"2.0"},n));case 37:r=e.sent;case 38:if(!r){e.next=40;break}return e.abrupt("return",r.result);case 40:return e.abrupt("return",o);case 43:throw e.prev=43,e.t1=e.catch(5),console.error(e.t1),e.t1;case 47:case"end":return e.stop()}}),e,this,[[5,43]])}))),function(e){return p.apply(this,arguments)})},{key:"enable",value:function(){var e=this;return window.ethereum&&window.ethereum.isBlocto?window.ethereum.enable():new Promise((function(t,n){"undefined"==typeof window&&n("Currently only supported in browser");var r=encodeURIComponent(window.location.origin),o=ye("".concat(e.server,"/authn?l6n=").concat(r,"&chain=").concat(e.chain));ke(o);var i=null;i=window.addEventListener("message",(function(r){r.origin===e.server&&("FCL::CHALLENGE::RESPONSE"===r.data.type&&(window.removeEventListener("message",i),Ee(o),e.code=r.data.code,e.connected=!0,e.eventListeners.connect.forEach((function(t){return t(e.chainId)})),e.accounts=[r.data.addr],t(e.accounts)),"FCL::CHALLENGE::CANCEL"===r.data.type&&(window.removeEventListener("message",i),o.parentNode.removeChild(o),n()))}))}))}},{key:"fetchAccounts",value:(f=r(regeneratorRuntime.mark((function e(){var t,n;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,fetch("".concat(this.server,"/api/").concat(this.chain,"/accounts?code=").concat(this.code)).then((function(e){return e.json()}));case 2:return t=e.sent,n=t.accounts,e.abrupt("return",n);case 5:case"end":return e.stop()}}),e,this)}))),function(){return f.apply(this,arguments)})},{key:"handleReadRequests",value:(l=r(regeneratorRuntime.mark((function e(t){return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",fetch(this.rpc,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)}).then((function(e){return e.json()})));case 1:case"end":return e.stop()}}),e,this)}))),function(e){return l.apply(this,arguments)})},{key:"handleSign",value:(h=r(regeneratorRuntime.mark((function e(t){var n;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.params,e.abrupt("return",fetch("".concat(this.server,"/api/").concat(this.chain,"/sign?code=").concat(this.code),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({message:n[1]})}).then((function(e){return e.json()})));case 2:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"handleSendTransaction",value:(u=r(regeneratorRuntime.mark((function e(t){var n,r,o,i=this;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,fetch("".concat(this.server,"/api/").concat(this.chain,"/authz?code=").concat(this.code),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t.params)}).then((function(e){return e.json()}));case 2:if(n=e.sent,r=n.authorizationId,"undefined"!=typeof window){e.next=6;break}throw new Error("Currently only supported in browser");case 6:return ke(o=ye("".concat(this.server,"/authz/").concat(this.chain,"/").concat(r))),e.abrupt("return",new Promise((function(e,t){var n=null;n=setInterval((function(){return fetch("".concat(i.server,"/api/").concat(i.chain,"/authz?authorizationId=").concat(r),{method:"GET",headers:{"Content-Type":"application/json"}}).then((function(e){return e.json()})).then((function(r){var i=r.status,a=r.transactionHash;"APPROVED"===i&&(Ee(o),clearInterval(n),e(a)),"DECLINED"===i&&(Ee(o),clearInterval(n),t("Transaction Canceled"))}))}),1e3)})));case 9:case"end":return e.stop()}}),e,this)}))),function(e){return u.apply(this,arguments)})},{key:"on",value:function(e,t){Oe.includes(e)&&t instanceof Function&&this.eventListeners[e].push(t)}},{key:"removeListener",value:function(e,t){var n=this.eventListeners[e].findIndex(t);-1!==n&&this.eventListener[e].splice(n,1)}}])&&i(n.prototype,s),c&&i(n,c),e}()})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).BloctoProvider=t()}(this,(function(){"use strict";function e(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function t(t){for(var n=1;n<arguments.length;n++){var r=null!=arguments[n]?arguments[n]:{};n%2?e(Object(r),!0).forEach((function(e){a(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):e(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}function n(e,t,n,r,o,i,a){try{var s=e[i](a),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,o)}function r(e){return function(){var t=this,r=arguments;return new Promise((function(o,i){var a=e.apply(t,r);function s(e){n(a,o,i,s,c,"next",e)}function c(e){n(a,o,i,s,c,"throw",e)}s(void 0)}))}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var s="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function c(){throw new Error("setTimeout has not been defined")}function u(){throw new Error("clearTimeout has not been defined")}var h=c,l=u;function f(e){if(h===setTimeout)return setTimeout(e,0);if((h===c||!h)&&setTimeout)return h=setTimeout,setTimeout(e,0);try{return h(e,0)}catch(t){try{return h.call(null,e,0)}catch(t){return h.call(this,e,0)}}}"function"==typeof s.setTimeout&&(h=setTimeout),"function"==typeof s.clearTimeout&&(l=clearTimeout);var p,d=[],v=!1,m=-1;function b(){v&&p&&(v=!1,p.length?d=p.concat(d):m=-1,d.length&&w())}function w(){if(!v){var e=f(b);v=!0;for(var t=d.length;t;){for(p=d,d=[];++m<t;)p&&p[m].run();m=-1,t=d.length}p=null,v=!1,function(e){if(l===clearTimeout)return clearTimeout(e);if((l===u||!l)&&clearTimeout)return l=clearTimeout,clearTimeout(e);try{l(e)}catch(t){try{return l.call(null,e)}catch(t){return l.call(this,e)}}}(e)}}function g(e,t){this.fun=e,this.array=t}g.prototype.run=function(){this.fun.apply(null,this.array)};function y(){}var k=y,E=y,T=y,j=y,I=y,x=y,O=y;var A=s.performance||{},_=A.now||A.mozNow||A.msNow||A.oNow||A.webkitNow||function(){return(new Date).getTime()};var P=new Date;var L={nextTick:function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var n=1;n<arguments.length;n++)t[n-1]=arguments[n];d.push(new g(e,t)),1!==d.length||v||f(w)},title:"browser",browser:!0,env:{},argv:[],version:"",versions:{},on:k,addListener:E,once:T,off:j,removeListener:I,removeAllListeners:x,emit:O,binding:function(e){throw new Error("process.binding is not supported")},cwd:function(){return"/"},chdir:function(e){throw new Error("process.chdir is not supported")},umask:function(){return 0},hrtime:function(e){var t=.001*_.call(A),n=Math.floor(t),r=Math.floor(t%1*1e9);return e&&(n-=e[0],(r-=e[1])<0&&(n--,r+=1e9)),[n,r]},platform:"browser",release:{},config:{},uptime:function(){return(new Date-P)/1e3}},R=function(e,t,n,r,o,i,a,s){if("production"!==L.env.NODE_ENV&&void 0===t)throw new Error("invariant requires an error message argument");if(!e){var c;if(void 0===t)c=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var u=[n,r,o,i,a,s],h=0;(c=new Error(t.replace(/%s/g,(function(){return u[h++]})))).name="Invariant Violation"}throw c.framesToPop=1,c}},C=Object.freeze({__proto__:null,default:{}});function S(e,t){for(var n=0,r=e.length-1;r>=0;r--){var o=e[r];"."===o?e.splice(r,1):".."===o?(e.splice(r,1),n++):n&&(e.splice(r,1),n--)}if(t)for(;n--;n)e.unshift("..");return e}var N=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,D=function(e){return N.exec(e).slice(1)};function z(){for(var e="",t=!1,n=arguments.length-1;n>=-1&&!t;n--){var r=n>=0?arguments[n]:"/";if("string"!=typeof r)throw new TypeError("Arguments to path.resolve must be strings");r&&(e=r+"/"+e,t="/"===r.charAt(0))}return(t?"/":"")+(e=S(H(e.split("/"),(function(e){return!!e})),!t).join("/"))||"."}function q(e){var t=B(e),n="/"===X(e,-1);return(e=S(H(e.split("/"),(function(e){return!!e})),!t).join("/"))||t||(e="."),e&&n&&(e+="/"),(t?"/":"")+e}function B(e){return"/"===e.charAt(0)}function M(){var e=Array.prototype.slice.call(arguments,0);return q(H(e,(function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e})).join("/"))}function V(e,t){function n(e){for(var t=0;t<e.length&&""===e[t];t++);for(var n=e.length-1;n>=0&&""===e[n];n--);return t>n?[]:e.slice(t,n-t+1)}e=z(e).substr(1),t=z(t).substr(1);for(var r=n(e.split("/")),o=n(t.split("/")),i=Math.min(r.length,o.length),a=i,s=0;s<i;s++)if(r[s]!==o[s]){a=s;break}var c=[];for(s=a;s<r.length;s++)c.push("..");return(c=c.concat(o.slice(a))).join("/")}function U(e){var t=D(e),n=t[0],r=t[1];return n||r?(r&&(r=r.substr(0,r.length-1)),n+r):"."}function $(e,t){var n=D(e)[2];return t&&n.substr(-1*t.length)===t&&(n=n.substr(0,n.length-t.length)),n}function F(e){return D(e)[3]}var G={extname:F,basename:$,dirname:U,sep:"/",delimiter:":",relative:V,join:M,isAbsolute:B,normalize:q,resolve:z};function H(e,t){if(e.filter)return e.filter(t);for(var n=[],r=0;r<e.length;r++)t(e[r],r,e)&&n.push(e[r]);return n}var J,X="b"==="ab".substr(-1)?function(e,t,n){return e.substr(t,n)}:function(e,t,n){return t<0&&(t=e.length+t),e.substr(t,n)},K=Object.freeze({__proto__:null,resolve:z,normalize:q,isAbsolute:B,join:M,relative:V,sep:"/",delimiter:":",dirname:U,basename:$,extname:F,default:G});function Q(){if(void 0===J){var e=new ArrayBuffer(2),t=new Uint8Array(e),n=new Uint16Array(e);if(t[0]=1,t[1]=2,258===n[0])J="BE";else{if(513!==n[0])throw new Error("unable to figure out endianess");J="LE"}}return J}function W(){return void 0!==s.location?s.location.hostname:""}function Y(){return[]}function Z(){return 0}function ee(){return Number.MAX_VALUE}function te(){return Number.MAX_VALUE}function ne(){return[]}function re(){return"Browser"}function oe(){return void 0!==s.navigator?s.navigator.appVersion:""}function ie(){}function ae(){}function se(){return"/tmp"}var ce=se,ue={EOL:"\n",tmpdir:ce,tmpDir:se,networkInterfaces:ie,getNetworkInterfaces:ae,release:oe,type:re,cpus:ne,totalmem:te,freemem:ee,uptime:Z,loadavg:Y,hostname:W,endianness:Q},he=Object.freeze({__proto__:null,endianness:Q,hostname:W,loadavg:Y,uptime:Z,freemem:ee,totalmem:te,cpus:ne,type:re,release:oe,networkInterfaces:ie,getNetworkInterfaces:ae,arch:function(){return"javascript"},platform:function(){return"browser"},tmpDir:se,tmpdir:ce,EOL:"\n",default:ue});function le(e){return e&&e.default||e}var fe=le(C),pe=le(K),de=le(he);function ve(e){console.log(`[dotenv][DEBUG] ${e}`)}const me=/^\s*([\w.-]+)\s*=\s*(.*)?\s*$/,be=/\\n/g,we=/\r\n|\n|\r/;function ge(e,t){const n=Boolean(t&&t.debug),r={};return e.toString().split(we).forEach((function(e,t){const o=e.match(me);if(null!=o){const e=o[1];let t=o[2]||"";const n=t.length-1,i='"'===t[0]&&'"'===t[n];"'"===t[0]&&"'"===t[n]||i?(t=t.substring(1,n),i&&(t=t.replace(be,"\n"))):t=t.trim(),r[e]=t}else n&&ve(`did not match key and value when parsing line ${t+1}: ${e}`)})),r}function ye(e){var t=document.createElement("iframe");return t.setAttribute("src",e),t.setAttribute("style","width:100vw;height:100vh;position:fixed;top:0;left:0;z-index:1000;border:none;"),t}function ke(e){document.body.appendChild(e)}function Ee(e){e.parentNode.removeChild(e)}var Te={56:"https://bsc-dataseed1.binance.org",97:"https://data-seed-prebsc-1-s1.binance.org:8545"},je={1:"ethereum",4:"ethereum",56:"bsc",97:"bsc"},Ie={1:"mainnet",4:"rinkeby",56:"mainnet",97:"testnet"},xe={1:"https://wallet.blocto.app",4:"https://wallet-testnet.blocto.app",56:"https://wallet.blocto.app",97:"https://wallet-testnet.blocto.app"},Oe=["connect","disconnect","message","chainChanged","accountsChanged"];return{config:function(e){let t=pe.resolve(L.cwd(),".env"),n="utf8",r=!1;var o;e&&(null!=e.path&&(t="~"===(o=e.path)[0]?pe.join(de.homedir(),o.slice(1)):o),null!=e.encoding&&(n=e.encoding),null!=e.debug&&(r=!0));try{const e=ge(fe.readFileSync(t,{encoding:n}),{debug:r});return Object.keys(e).forEach((function(t){Object.prototype.hasOwnProperty.call(L.env,t)?r&&ve(`"${t}" is already defined in \`process.env\` and will not be overwritten`):L.env[t]=e[t]})),{parsed:e}}catch(e){return{error:e}}},parse:ge}.config(),function(){function e(){var t=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=n.chainId,i=n.rpc,s=n.server,c=n.appId;o(this,e),a(this,"isBlocto",!0),a(this,"isConnecting",!1),a(this,"connected",!1),a(this,"chainId",null),a(this,"networkId",null),a(this,"chain",null),a(this,"net",null),a(this,"rpc",null),a(this,"server",null),a(this,"appId",null),a(this,"eventListeners",{}),a(this,"accounts",[]),a(this,"off",removeEventListener),R(r,"'chainId' is required"),"number"==typeof r?this.chainId=r:r.includes("0x")?this.chainId=parseInt(r,16):this.chainId=parseInt(r,10),this.networkId=this.chainId,this.chain=je[this.chainId],this.net=Ie[this.chainId],R(this.chain,"unsupported 'chainId': ".concat(this.chainId)),this.rpc=process.env.RPC||i||Te[this.chainId],R(this.rpc,"'rpc' is required for Ethereum"),this.server=process.env.SERVER||s||xe[this.chainId],this.appId=process.env.APP_ID||c,Oe.forEach((function(e){t.eventListeners[e]=[]}))}var n,s,c,u,h,l,f,p,d,v;return n=e,(s=[{key:"send",value:(v=r(regeneratorRuntime.mark((function e(t,n){return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.t0=!0,e.next=e.t0===n instanceof Function?3:e.t0===("string"==typeof t&&Array.isA)?4:5;break;case 3:return e.abrupt("return",this.sendAsync(t,n));case 4:return e.abrupt("return",this.sendAsync({method:t,params:n}));case 5:return e.abrupt("return",this.sendAsync(t));case 6:case"end":return e.stop()}}),e,this)}))),function(e,t){return v.apply(this,arguments)})},{key:"sendAsync",value:(d=r(regeneratorRuntime.mark((function e(t,n){var r,o=this;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=new Promise((function(e){if(Array.isArray(t)){var n=t.filter((function(e){return"eth_sendTransaction"===e.method})).map((function(e){return e.params[0]})),r=parseInt(1e4*Math.random(),10),i={method:"blocto_sendBatchTransaction",params:n},a=o.request(i),s=t.map((function(e,t){var n=e.method,i=e.params;return"eth_sendTransaction"===n?a:o.request({id:r+t+1,jsonrpc:"2.0",method:n,params:i})}));Promise.allSettled(s).then((function(t){return e(t.map((function(e,t){return{id:r+t+1,jsonrpc:"2.0",result:"fulfilled"===e.status?e.value:void 0,error:"fulfilled"!==e.status?e.value:void 0}})))}))}else o.request(t).then((function(t){return e(null,t)}))})),!n){e.next=5;break}r.then((function(e){return n(null,e)})).catch((function(e){return n(e)})),e.next=6;break;case 5:return e.abrupt("return",r);case 6:case"end":return e.stop()}}),e)}))),function(e,t){return d.apply(this,arguments)})},{key:"request",value:(p=r(regeneratorRuntime.mark((function e(n){var r,o;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!window.ethereum||!window.ethereum.isBlocto){e.next=2;break}return e.abrupt("return",window.ethereum.request(n));case 2:if(this.connected){e.next=5;break}return e.next=5,this.enable();case 5:e.prev=5,r=null,o=null,e.t0=n.method,e.next="eth_requestAccounts"===e.t0?11:"eth_accounts"===e.t0?14:"eth_coinbase"===e.t0?23:"eth_chainId"===e.t0?25:"net_version"===e.t0?28:"eth_sign"===e.t0?31:"blocto_sendBatchTransaction"===e.t0||"eth_sendTransaction"===e.t0?36:"eth_signTransaction"===e.t0||"eth_sendRawTransaction"===e.t0?40:42;break;case 11:return e.next=13,this.fetchAccounts();case 13:this.accounts=e.sent;case 14:if(!this.accounts.length){e.next=18;break}e.t1=this.accounts,e.next=21;break;case 18:return e.next=20,this.fetchAccounts();case 20:e.t1=e.sent;case 21:return o=e.t1,e.abrupt("break",45);case 23:return o=this.accounts[0],e.abrupt("break",45);case 25:return o=this.chainId,o="0x".concat(o.toString(16)),e.abrupt("break",45);case 28:return o=this.networkId||this.chainId,o="0x".concat(o.toString(16)),e.abrupt("break",45);case 31:return e.next=33,this.handleSign(n);case 33:return o=(o=e.sent).signature,e.abrupt("break",45);case 36:return e.next=38,this.handleSendTransaction(n);case 38:return o=e.sent,e.abrupt("break",45);case 40:return o=null,e.abrupt("break",45);case 42:return e.next=44,this.handleReadRequests(t({id:1,jsonrpc:"2.0"},n));case 44:r=e.sent;case 45:if(!r){e.next=47;break}return e.abrupt("return",r.result);case 47:return e.abrupt("return",o);case 50:throw e.prev=50,e.t2=e.catch(5),console.error(e.t2),e.t2;case 54:case"end":return e.stop()}}),e,this,[[5,50]])}))),function(e){return p.apply(this,arguments)})},{key:"enable",value:function(){var e=this;return window.ethereum&&window.ethereum.isBlocto?window.ethereum.enable():new Promise((function(t,n){"undefined"==typeof window&&n("Currently only supported in browser");var r=encodeURIComponent(window.location.origin),o=ye("".concat(e.server,"/authn?l6n=").concat(r,"&chain=").concat(e.chain));ke(o);var i=null;i=window.addEventListener("message",(function(r){r.origin===e.server&&("FCL::CHALLENGE::RESPONSE"===r.data.type&&(window.removeEventListener("message",i),Ee(o),e.code=r.data.code,e.connected=!0,e.eventListeners.connect.forEach((function(t){return t(e.chainId)})),e.accounts=[r.data.addr],t(e.accounts)),"FCL::CHALLENGE::CANCEL"===r.data.type&&(window.removeEventListener("message",i),o.parentNode.removeChild(o),n()))}))}))}},{key:"fetchAccounts",value:(f=r(regeneratorRuntime.mark((function e(){var t,n;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,fetch("".concat(this.server,"/api/").concat(this.chain,"/accounts?code=").concat(this.code)).then((function(e){return e.json()}));case 2:return t=e.sent,n=t.accounts,this.accounts=n,e.abrupt("return",n);case 6:case"end":return e.stop()}}),e,this)}))),function(){return f.apply(this,arguments)})},{key:"handleReadRequests",value:(l=r(regeneratorRuntime.mark((function e(t){return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",fetch(this.rpc,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)}).then((function(e){return e.json()})));case 1:case"end":return e.stop()}}),e,this)}))),function(e){return l.apply(this,arguments)})},{key:"handleSign",value:(h=r(regeneratorRuntime.mark((function e(t){var n;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.params,e.abrupt("return",fetch("".concat(this.server,"/api/").concat(this.chain,"/sign?code=").concat(this.code),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({message:n[1]})}).then((function(e){return e.json()})));case 2:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"handleSendTransaction",value:(u=r(regeneratorRuntime.mark((function e(t){var n,r,o,i=this;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,fetch("".concat(this.server,"/api/").concat(this.chain,"/authz?code=").concat(this.code),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t.params)}).then((function(e){return e.json()}));case 2:if(n=e.sent,r=n.authorizationId,"undefined"!=typeof window){e.next=6;break}throw new Error("Currently only supported in browser");case 6:return ke(o=ye("".concat(this.server,"/authz/").concat(this.chain,"/").concat(r))),e.abrupt("return",new Promise((function(e,t){var n=null;n=setInterval((function(){return fetch("".concat(i.server,"/api/").concat(i.chain,"/authz?authorizationId=").concat(r),{method:"GET",headers:{"Content-Type":"application/json"}}).then((function(e){return e.json()})).then((function(r){var i=r.status,a=r.transactionHash;"APPROVED"===i&&(Ee(o),clearInterval(n),e(a)),"DECLINED"===i&&(Ee(o),clearInterval(n),t("Transaction Canceled"))}))}),1e3)})));case 9:case"end":return e.stop()}}),e,this)}))),function(e){return u.apply(this,arguments)})},{key:"on",value:function(e,t){Oe.includes(e)&&t instanceof Function&&this.eventListeners[e].push(t)}},{key:"removeListener",value:function(e,t){var n=this.eventListeners[e].findIndex(t);-1!==n&&this.eventListener[e].splice(n,1)}}])&&i(n.prototype,s),c&&i(n,c),e}()})); |
{ | ||
"name": "@blocto/sdk", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"author": "Yang Lin", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -150,3 +150,3 @@ import invariant from 'invariant'; | ||
case 'eth_accounts': | ||
result = this.accounts; | ||
result = this.accounts.length ? this.accounts : await this.fetchAccounts(); | ||
break; | ||
@@ -239,2 +239,3 @@ case 'eth_coinbase': { | ||
).then(response => response.json()); | ||
this.accounts = accounts; | ||
return accounts; | ||
@@ -241,0 +242,0 @@ } |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
97803
2274