@blocto/sdk
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -1643,3 +1643,4 @@ function _defineProperty(obj, key, value) { | ||
rpc = _ref.rpc, | ||
server = _ref.server; | ||
server = _ref.server, | ||
appId = _ref.appId; | ||
@@ -1666,2 +1667,4 @@ _classCallCheck(this, BloctoProvider); | ||
_defineProperty(this, "appId", null); | ||
browser(chainId, "'chainId' is required"); | ||
@@ -1684,2 +1687,3 @@ | ||
this.server = process.env.SERVER || server || CHAIN_ID_SERVER_MAPPING[this.chainId]; | ||
this.appId = process.env.APP_ID || appId; | ||
} | ||
@@ -1696,59 +1700,67 @@ | ||
case 0: | ||
_context.prev = 0; | ||
if (!(window.ethereum && window.ethereum.isBlocto)) { | ||
_context.next = 2; | ||
break; | ||
} | ||
return _context.abrupt("return", window.ethereum.request(payload)); | ||
case 2: | ||
_context.prev = 2; | ||
response = null; | ||
result = null; | ||
_context.t0 = payload.method; | ||
_context.next = _context.t0 === "eth_accounts" ? 6 : _context.t0 === "eth_coinbase" ? 10 : _context.t0 === "eth_chainId" ? 15 : _context.t0 === "net_version" ? 18 : _context.t0 === "eth_sign" ? 21 : _context.t0 === "eth_sendTransaction" ? 26 : _context.t0 === "eth_signTransaction" ? 30 : _context.t0 === "eth_sendRawTransaction" ? 30 : 32; | ||
_context.next = _context.t0 === "eth_accounts" ? 8 : _context.t0 === "eth_coinbase" ? 12 : _context.t0 === "eth_chainId" ? 17 : _context.t0 === "net_version" ? 20 : _context.t0 === "eth_sign" ? 23 : _context.t0 === "eth_sendTransaction" ? 28 : _context.t0 === "eth_signTransaction" ? 32 : _context.t0 === "eth_sendRawTransaction" ? 32 : 34; | ||
break; | ||
case 6: | ||
_context.next = 8; | ||
case 8: | ||
_context.next = 10; | ||
return this.fetchAccounts(); | ||
case 8: | ||
case 10: | ||
result = _context.sent; | ||
return _context.abrupt("break", 35); | ||
return _context.abrupt("break", 37); | ||
case 10: | ||
_context.next = 12; | ||
case 12: | ||
_context.next = 14; | ||
return this.fetchAccounts(); | ||
case 12: | ||
case 14: | ||
accounts = _context.sent; | ||
result = accounts[0]; | ||
return _context.abrupt("break", 35); | ||
return _context.abrupt("break", 37); | ||
case 15: | ||
case 17: | ||
result = this.chainId; | ||
result = "0x" + result.toString(16); | ||
return _context.abrupt("break", 35); | ||
return _context.abrupt("break", 37); | ||
case 18: | ||
case 20: | ||
result = this.networkId || this.chainId; | ||
result = "0x" + result.toString(16); | ||
return _context.abrupt("break", 35); | ||
return _context.abrupt("break", 37); | ||
case 21: | ||
_context.next = 23; | ||
case 23: | ||
_context.next = 25; | ||
return this.handleSign(payload); | ||
case 23: | ||
case 25: | ||
result = _context.sent; | ||
result = result.signature; | ||
return _context.abrupt("break", 35); | ||
return _context.abrupt("break", 37); | ||
case 26: | ||
_context.next = 28; | ||
case 28: | ||
_context.next = 30; | ||
return this.handleSendTransaction(payload); | ||
case 28: | ||
case 30: | ||
result = _context.sent; | ||
return _context.abrupt("break", 35); | ||
return _context.abrupt("break", 37); | ||
case 30: | ||
case 32: | ||
result = null; | ||
return _context.abrupt("break", 35); | ||
return _context.abrupt("break", 37); | ||
case 32: | ||
_context.next = 34; | ||
case 34: | ||
_context.next = 36; | ||
return this.handleReadRequests(_objectSpread2({ | ||
@@ -1759,8 +1771,8 @@ id: 1, | ||
case 34: | ||
case 36: | ||
response = _context.sent; | ||
case 35: | ||
case 37: | ||
if (!response) { | ||
_context.next = 37; | ||
_context.next = 39; | ||
break; | ||
@@ -1771,8 +1783,8 @@ } | ||
case 37: | ||
case 39: | ||
return _context.abrupt("return", result); | ||
case 40: | ||
_context.prev = 40; | ||
_context.t1 = _context["catch"](0); | ||
case 42: | ||
_context.prev = 42; | ||
_context.t1 = _context["catch"](2); | ||
console.error(_context.t1); // this.emit("error", error); | ||
@@ -1782,3 +1794,3 @@ | ||
case 44: | ||
case 46: | ||
case "end": | ||
@@ -1788,3 +1800,3 @@ return _context.stop(); | ||
} | ||
}, _callee, this, [[0, 40]]); | ||
}, _callee, this, [[2, 42]]); | ||
})); | ||
@@ -1803,2 +1815,6 @@ | ||
if (window.ethereum && window.ethereum.isBlocto) { | ||
return window.ethereum.enable(); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
@@ -1805,0 +1821,0 @@ if (typeof window === "undefined") reject("Currently only supported in browser"); |
@@ -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 c=e[i](a),s=c.value}catch(e){return void n(e)}c.done?t(s):Promise.resolve(s).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 c(e){n(a,o,i,c,s,"next",e)}function s(e){n(a,o,i,c,s,"throw",e)}c(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 c="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function s(){throw new Error("setTimeout has not been defined")}function u(){throw new Error("clearTimeout has not been defined")}var l=s,h=u;function f(e){if(l===setTimeout)return setTimeout(e,0);if((l===s||!l)&&setTimeout)return l=setTimeout,setTimeout(e,0);try{return l(e,0)}catch(t){try{return l.call(null,e,0)}catch(t){return l.call(this,e,0)}}}"function"==typeof c.setTimeout&&(l=setTimeout),"function"==typeof c.clearTimeout&&(h=clearTimeout);var p,d=[],m=!1,v=-1;function b(){m&&p&&(m=!1,p.length?d=p.concat(d):v=-1,d.length&&w())}function w(){if(!m){var e=f(b);m=!0;for(var t=d.length;t;){for(p=d,d=[];++v<t;)p&&p[v].run();v=-1,t=d.length}p=null,m=!1,function(e){if(h===clearTimeout)return clearTimeout(e);if((h===u||!h)&&clearTimeout)return h=clearTimeout,clearTimeout(e);try{h(e)}catch(t){try{return h.call(null,e)}catch(t){return h.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 E=y,T=y,j=y,k=y,O=y,x=y,I=y;var A=c.performance||{},P=A.now||A.mozNow||A.msNow||A.oNow||A.webkitNow||function(){return(new Date).getTime()};var C=new Date;var N={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||m||f(w)},title:"browser",browser:!0,env:{},argv:[],version:"",versions:{},on:E,addListener:T,once:j,off:k,removeListener:O,removeAllListeners:x,emit:I,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*P.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-C)/1e3}},_=function(e,t,n,r,o,i,a,c){if("production"!==N.env.NODE_ENV&&void 0===t)throw new Error("invariant requires an error message argument");if(!e){var s;if(void 0===t)s=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,c],l=0;(s=new Error(t.replace(/%s/g,(function(){return u[l++]})))).name="Invariant Violation"}throw s.framesToPop=1,s}},S=Object.freeze({__proto__:null,default:{}});function R(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 L=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,D=function(e){return L.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=R(F(e.split("/"),(function(e){return!!e})),!t).join("/"))||"."}function B(e){var t=V(e),n="/"===X(e,-1);return(e=R(F(e.split("/"),(function(e){return!!e})),!t).join("/"))||t||(e="."),e&&n&&(e+="/"),(t?"/":"")+e}function V(e){return"/"===e.charAt(0)}function q(){var e=Array.prototype.slice.call(arguments,0);return B(F(e,(function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e})).join("/"))}function M(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,c=0;c<i;c++)if(r[c]!==o[c]){a=c;break}var s=[];for(c=a;c<r.length;c++)s.push("..");return(s=s.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 G(e){return D(e)[3]}var J={extname:G,basename:$,dirname:U,sep:"/",delimiter:":",relative:M,join:q,isAbsolute:V,normalize:B,resolve:z};function F(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 H,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:B,isAbsolute:V,join:q,relative:M,sep:"/",delimiter:":",dirname:U,basename:$,extname:G,default:J});function Q(){if(void 0===H){var e=new ArrayBuffer(2),t=new Uint8Array(e),n=new Uint16Array(e);if(t[0]=1,t[1]=2,258===n[0])H="BE";else{if(513!==n[0])throw new Error("unable to figure out endianess");H="LE"}}return H}function W(){return void 0!==c.location?c.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!==c.navigator?c.navigator.appVersion:""}function ie(){}function ae(){}function ce(){return"/tmp"}var se=ce,ue={EOL:"\n",tmpdir:se,tmpDir:ce,networkInterfaces:ie,getNetworkInterfaces:ae,release:oe,type:re,cpus:ne,totalmem:te,freemem:ee,uptime:Z,loadavg:Y,hostname:W,endianness:Q},le=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:ce,tmpdir:se,EOL:"\n",default:ue});function he(e){return e&&e.default||e}var fe=he(S),pe=he(K),de=he(le);function me(e){console.log(`[dotenv][DEBUG] ${e}`)}const ve=/^\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(ve);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&&me(`did not match key and value when parsing line ${t+1}: ${e}`)})),r}function ye(e){return new Promise((function(t){return setTimeout(t,e)}))}({config:function(e){let t=pe.resolve(N.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(N.env,t)?r&&me(`"${t}" is already defined in \`process.env\` and will not be overwritten`):N.env[t]=e[t]})),{parsed:e}}catch(e){return{error:e}}},parse:ge}).config();var Ee="width:100vw;height:100vh;position:fixed;top:0;left:0;z-index:1000;border:none;",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"},ke={1:"mainnet",4:"rinkeby",56:"mainnet",97:"testnet"},Oe={1:"https://wallet.blocto.app",4:"https://wallet-testnet.blocto.app",56:"https://wallet.blocto.app",97:"https://wallet-testnet.blocto.app"};return function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=t.chainId,r=t.rpc,i=t.server;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),_(n,"'chainId' is required"),"number"==typeof n?this.chainId=n:n.includes("0x")?this.chainId=parseInt(n,16):this.chainId=parseInt(n,10),this.networkId=this.chainId,this.chain=je[this.chainId],this.net=ke[this.chainId],_(this.chain,"unsupported 'chainId': ".concat(this.chainId)),this.rpc=process.env.RPC||r||Te[this.chainId],_(this.rpc,"'rpc' is required for Ethereum"),this.server=process.env.SERVER||i||Oe[this.chainId]}var n,c,s,u,l,h,f,p;return n=e,(c=[{key:"request",value:(p=r(regeneratorRuntime.mark((function e(n){var r,o,i;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=null,o=null,e.t0=n.method,e.next="eth_accounts"===e.t0?6:"eth_coinbase"===e.t0?10:"eth_chainId"===e.t0?15:"net_version"===e.t0?18:"eth_sign"===e.t0?21:"eth_sendTransaction"===e.t0?26:"eth_signTransaction"===e.t0||"eth_sendRawTransaction"===e.t0?30:32;break;case 6:return e.next=8,this.fetchAccounts();case 8:return o=e.sent,e.abrupt("break",35);case 10:return e.next=12,this.fetchAccounts();case 12:return i=e.sent,o=i[0],e.abrupt("break",35);case 15:return o="0x"+(o=this.chainId).toString(16),e.abrupt("break",35);case 18:return o="0x"+(o=this.networkId||this.chainId).toString(16),e.abrupt("break",35);case 21:return e.next=23,this.handleSign(n);case 23:return o=(o=e.sent).signature,e.abrupt("break",35);case 26:return e.next=28,this.handleSendTransaction(n);case 28:return o=e.sent,e.abrupt("break",35);case 30:return o=null,e.abrupt("break",35);case 32:return e.next=34,this.handleReadRequests(t({id:1,jsonrpc:"2.0"},n));case 34:r=e.sent;case 35:if(!r){e.next=37;break}return e.abrupt("return",r.result);case 37:return e.abrupt("return",o);case 40:throw e.prev=40,e.t1=e.catch(0),console.error(e.t1),e.t1;case 44:case"end":return e.stop()}}),e,this,[[0,40]])}))),function(e){return p.apply(this,arguments)})},{key:"enable",value:function(){var e=this;return new Promise((function(t,n){"undefined"==typeof window&&n("Currently only supported in browser");var r=document.createElement("iframe");r.setAttribute("src","".concat(e.server,"/authn?l6n=").concat(encodeURIComponent(window.location.origin),"&chain=").concat(e.chain)),r.setAttribute("style",Ee),document.body.appendChild(r);var o=null;o=window.addEventListener("message",(function(i){i.origin===e.server&&("FCL::CHALLENGE::RESPONSE"===i.data.type&&(window.removeEventListener("message",o),r.parentNode.removeChild(r),e.code=i.data.code,e.connected=!0,t([i.data.addr])),"FCL::CHALLENGE::CANCEL"===i.data.type&&(window.removeEventListener("message",o),r.parentNode.removeChild(r),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:(h=r(regeneratorRuntime.mark((function e(t){return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,fetch(this.rpc,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)}).then((function(e){return e.json()}));case 2:return e.abrupt("return",e.sent);case 3:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"handleSign",value:(l=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.next=3,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 3:return e.abrupt("return",e.sent);case 4:case"end":return e.stop()}}),e,this)}))),function(e){return l.apply(this,arguments)})},{key:"handleSendTransaction",value:(u=r(regeneratorRuntime.mark((function e(n){var r,o,i,a,c,s,u,l,h;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.params,e.next=3,fetch("".concat(this.server,"/api/").concat(this.chain,"/estimatePoint?code=").concat(this.code),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r[0])}).then((function(e){return e.json()}));case 3:return o=e.sent,i=o.cost,e.next=7,fetch("".concat(this.server,"/api/").concat(this.chain,"/authz?code=").concat(this.code),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t(t({},r[0]),{},{point:i}))}).then((function(e){return e.json()}));case 7:if(a=e.sent,c=a.authorizationId,"undefined"!=typeof window){e.next=11;break}throw new Error("Currently only supported in browser");case 11:(s=document.createElement("iframe")).setAttribute("src","".concat(this.server,"/authz/").concat(this.chain,"/").concat(c)),s.setAttribute("style",Ee),document.body.appendChild(s);case 15:return e.next=18,fetch("".concat(this.server,"/api/").concat(this.chain,"/authz?authorizationId=").concat(c),{method:"GET",headers:{"Content-Type":"application/json"}}).then((function(e){return e.json()}));case 18:if(u=e.sent,l=u.status,h=u.transactionHash,"APPROVED"!==l){e.next=24;break}return s.parentNode.removeChild(s),e.abrupt("return",h);case 24:if("DECLINED"!==l){e.next=27;break}throw s.parentNode.removeChild(s),new Error("Transaction Canceled");case 27:return e.next=29,ye(1e3);case 29:e.next=15;break;case 31:case"end":return e.stop()}}),e,this)}))),function(e){return u.apply(this,arguments)})}])&&i(n.prototype,c),s&&i(n,s),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=[],m=!1,v=-1;function b(){m&&p&&(m=!1,p.length?d=p.concat(d):v=-1,d.length&&w())}function w(){if(!m){var e=f(b);m=!0;for(var t=d.length;t;){for(p=d,d=[];++v<t;)p&&p[v].run();v=-1,t=d.length}p=null,m=!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 E=y,k=y,T=y,j=y,O=y,x=y,I=y;var A=s.performance||{},P=A.now||A.mozNow||A.msNow||A.oNow||A.webkitNow||function(){return(new Date).getTime()};var C=new Date;var _={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||m||f(w)},title:"browser",browser:!0,env:{},argv:[],version:"",versions:{},on:E,addListener:k,once:T,off:j,removeListener:O,removeAllListeners:x,emit:I,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*P.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-C)/1e3}},N=function(e,t,n,r,o,i,a,s){if("production"!==_.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}},S=Object.freeze({__proto__:null,default:{}});function R(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 L=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,D=function(e){return L.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=R(F(e.split("/"),(function(e){return!!e})),!t).join("/"))||"."}function B(e){var t=q(e),n="/"===X(e,-1);return(e=R(F(e.split("/"),(function(e){return!!e})),!t).join("/"))||t||(e="."),e&&n&&(e+="/"),(t?"/":"")+e}function q(e){return"/"===e.charAt(0)}function V(){var e=Array.prototype.slice.call(arguments,0);return B(F(e,(function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e})).join("/"))}function M(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 G(e){return D(e)[3]}var J={extname:G,basename:$,dirname:U,sep:"/",delimiter:":",relative:M,join:V,isAbsolute:q,normalize:B,resolve:z};function F(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 H,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:B,isAbsolute:q,join:V,relative:M,sep:"/",delimiter:":",dirname:U,basename:$,extname:G,default:J});function Q(){if(void 0===H){var e=new ArrayBuffer(2),t=new Uint8Array(e),n=new Uint16Array(e);if(t[0]=1,t[1]=2,258===n[0])H="BE";else{if(513!==n[0])throw new Error("unable to figure out endianess");H="LE"}}return H}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(S),pe=le(K),de=le(he);function me(e){console.log(`[dotenv][DEBUG] ${e}`)}const ve=/^\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(ve);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&&me(`did not match key and value when parsing line ${t+1}: ${e}`)})),r}function ye(e){return new Promise((function(t){return setTimeout(t,e)}))}({config:function(e){let t=pe.resolve(_.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(_.env,t)?r&&me(`"${t}" is already defined in \`process.env\` and will not be overwritten`):_.env[t]=e[t]})),{parsed:e}}catch(e){return{error:e}}},parse:ge}).config();var Ee="width:100vw;height:100vh;position:fixed;top:0;left:0;z-index:1000;border:none;",ke={56:"https://bsc-dataseed1.binance.org",97:"https://data-seed-prebsc-1-s1.binance.org:8545"},Te={1:"ethereum",4:"ethereum",56:"bsc",97:"bsc"},je={1:"mainnet",4:"rinkeby",56:"mainnet",97:"testnet"},Oe={1:"https://wallet.blocto.app",4:"https://wallet-testnet.blocto.app",56:"https://wallet.blocto.app",97:"https://wallet-testnet.blocto.app"};return function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=t.chainId,r=t.rpc,i=t.server,s=t.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),N(n,"'chainId' is required"),"number"==typeof n?this.chainId=n:n.includes("0x")?this.chainId=parseInt(n,16):this.chainId=parseInt(n,10),this.networkId=this.chainId,this.chain=Te[this.chainId],this.net=je[this.chainId],N(this.chain,"unsupported 'chainId': ".concat(this.chainId)),this.rpc=process.env.RPC||r||ke[this.chainId],N(this.rpc,"'rpc' is required for Ethereum"),this.server=process.env.SERVER||i||Oe[this.chainId],this.appId=process.env.APP_ID||s}var n,s,c,u,h,l,f,p;return n=e,(s=[{key:"request",value:(p=r(regeneratorRuntime.mark((function e(n){var r,o,i;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:e.prev=2,r=null,o=null,e.t0=n.method,e.next="eth_accounts"===e.t0?8:"eth_coinbase"===e.t0?12:"eth_chainId"===e.t0?17:"net_version"===e.t0?20:"eth_sign"===e.t0?23:"eth_sendTransaction"===e.t0?28:"eth_signTransaction"===e.t0||"eth_sendRawTransaction"===e.t0?32:34;break;case 8:return e.next=10,this.fetchAccounts();case 10:return o=e.sent,e.abrupt("break",37);case 12:return e.next=14,this.fetchAccounts();case 14:return i=e.sent,o=i[0],e.abrupt("break",37);case 17:return o="0x"+(o=this.chainId).toString(16),e.abrupt("break",37);case 20:return o="0x"+(o=this.networkId||this.chainId).toString(16),e.abrupt("break",37);case 23:return e.next=25,this.handleSign(n);case 25:return o=(o=e.sent).signature,e.abrupt("break",37);case 28:return e.next=30,this.handleSendTransaction(n);case 30:return o=e.sent,e.abrupt("break",37);case 32:return o=null,e.abrupt("break",37);case 34:return e.next=36,this.handleReadRequests(t({id:1,jsonrpc:"2.0"},n));case 36:r=e.sent;case 37:if(!r){e.next=39;break}return e.abrupt("return",r.result);case 39:return e.abrupt("return",o);case 42:throw e.prev=42,e.t1=e.catch(2),console.error(e.t1),e.t1;case 46:case"end":return e.stop()}}),e,this,[[2,42]])}))),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=document.createElement("iframe");r.setAttribute("src","".concat(e.server,"/authn?l6n=").concat(encodeURIComponent(window.location.origin),"&chain=").concat(e.chain)),r.setAttribute("style",Ee),document.body.appendChild(r);var o=null;o=window.addEventListener("message",(function(i){i.origin===e.server&&("FCL::CHALLENGE::RESPONSE"===i.data.type&&(window.removeEventListener("message",o),r.parentNode.removeChild(r),e.code=i.data.code,e.connected=!0,t([i.data.addr])),"FCL::CHALLENGE::CANCEL"===i.data.type&&(window.removeEventListener("message",o),r.parentNode.removeChild(r),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.next=2,fetch(this.rpc,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)}).then((function(e){return e.json()}));case 2:return e.abrupt("return",e.sent);case 3: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.next=3,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 3:return e.abrupt("return",e.sent);case 4:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"handleSendTransaction",value:(u=r(regeneratorRuntime.mark((function e(n){var r,o,i,a,s,c,u,h,l;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=n.params,e.next=3,fetch("".concat(this.server,"/api/").concat(this.chain,"/estimatePoint?code=").concat(this.code),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r[0])}).then((function(e){return e.json()}));case 3:return o=e.sent,i=o.cost,e.next=7,fetch("".concat(this.server,"/api/").concat(this.chain,"/authz?code=").concat(this.code),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t(t({},r[0]),{},{point:i}))}).then((function(e){return e.json()}));case 7:if(a=e.sent,s=a.authorizationId,"undefined"!=typeof window){e.next=11;break}throw new Error("Currently only supported in browser");case 11:(c=document.createElement("iframe")).setAttribute("src","".concat(this.server,"/authz/").concat(this.chain,"/").concat(s)),c.setAttribute("style",Ee),document.body.appendChild(c);case 15:return e.next=18,fetch("".concat(this.server,"/api/").concat(this.chain,"/authz?authorizationId=").concat(s),{method:"GET",headers:{"Content-Type":"application/json"}}).then((function(e){return e.json()}));case 18:if(u=e.sent,h=u.status,l=u.transactionHash,"APPROVED"!==h){e.next=24;break}return c.parentNode.removeChild(c),e.abrupt("return",l);case 24:if("DECLINED"!==h){e.next=27;break}throw c.parentNode.removeChild(c),new Error("Transaction Canceled");case 27:return e.next=29,ye(1e3);case 29:e.next=15;break;case 31:case"end":return e.stop()}}),e,this)}))),function(e){return u.apply(this,arguments)})}])&&i(n.prototype,s),c&&i(n,c),e}()})); |
{ | ||
"name": "@blocto/sdk", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"author": "Yang Lin", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -58,4 +58,5 @@ import invariant from 'invariant' | ||
server = null; | ||
appId = null; | ||
constructor({ chainId, rpc, server } = {}) { | ||
constructor({ chainId, rpc, server, appId } = {}) { | ||
invariant(chainId, "'chainId' is required"); | ||
@@ -82,5 +83,10 @@ | ||
this.server = process.env.SERVER || server || CHAIN_ID_SERVER_MAPPING[this.chainId]; | ||
this.appId = process.env.APP_ID || appId; | ||
} | ||
async request(payload) { | ||
if (window.ethereum && window.ethereum.isBlocto) { | ||
return window.ethereum.request(payload); | ||
} | ||
try { | ||
@@ -129,2 +135,6 @@ let response = null; | ||
enable() { | ||
if (window.ethereum && window.ethereum.isBlocto) { | ||
return window.ethereum.enable(); | ||
} | ||
return new Promise((resolve, reject) => { | ||
@@ -131,0 +141,0 @@ if (typeof window === "undefined") |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
85805
2029
4