kaikas-connector
Version:
A simple, maximally extensible, dependency minimized framework for building modern Klaytn dApps
3 lines (2 loc) • 13.7 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("klaytn-abstract-connector");function e(t,e,n,r,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void n(t)}c.done?e(u):Promise.resolve(u).then(r,o)}function n(t){return function(){var n=this,r=arguments;return new Promise((function(o,i){var a=t.apply(n,r);function c(t){e(a,o,i,c,u,"next",t)}function u(t){e(a,o,i,c,u,"throw",t)}c(void 0)}))}}function r(){return(r=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}function o(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,a(t,e)}function i(t){return(i=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function a(t,e){return(a=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function c(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}function u(t,e,n){return(u=c()?Reflect.construct:function(t,e,n){var r=[null];r.push.apply(r,e);var o=new(Function.bind.apply(t,r));return n&&a(o,n.prototype),o}).apply(null,arguments)}function s(t){var e="function"==typeof Map?new Map:void 0;return(s=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,n)}function n(){return u(t,arguments,i(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),a(n,t)})(t)}function h(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function l(t,e){return t(e={exports:{}},e.exports),e.exports}require("tiny-warning");var f=l((function(t){!function(e){var n=Object.prototype,r=n.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",a=o.asyncIterator||"@@asyncIterator",c=o.toStringTag||"@@toStringTag",u=e.regeneratorRuntime;if(u)t.exports=u;else{(u=e.regeneratorRuntime=t.exports).wrap=p;var s={},h={};h[i]=function(){return this};var l=Object.getPrototypeOf,f=l&&l(l(L([])));f&&f!==n&&r.call(f,i)&&(h=f);var d=g.prototype=y.prototype=Object.create(h);v.prototype=d.constructor=g,g.constructor=v,g[c]=v.displayName="GeneratorFunction",u.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===v||"GeneratorFunction"===(e.displayName||e.name))},u.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,g):(t.__proto__=g,c in t||(t[c]="GeneratorFunction")),t.prototype=Object.create(d),t},u.awrap=function(t){return{__await:t}},m(k.prototype),k.prototype[a]=function(){return this},u.AsyncIterator=k,u.async=function(t,e,n,r){var o=new k(p(t,e,n,r));return u.isGeneratorFunction(e)?o:o.next().then((function(t){return t.done?t.value:o.next()}))},m(d),d[c]="Generator",d[i]=function(){return this},d.toString=function(){return"[object Generator]"},u.keys=function(t){var e=[];for(var n in t)e.push(n);return e.reverse(),function n(){for(;e.length;){var r=e.pop();if(r in t)return n.value=r,n.done=!1,n}return n.done=!0,n}},u.values=L,_.prototype={constructor:_,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(C),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=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 e=this;function n(n,r){return a.type="throw",a.arg=t,e.next=n,r&&(e.method="next",e.arg=void 0),!!r}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var c=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,s):this.complete(a)},complete:function(t,e){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&&e&&(this.next=e),s},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),C(n),s}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var o=r.arg;C(n)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:L(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),s}}}function p(t,e,n,r){var o=Object.create((e&&e.prototype instanceof y?e:y).prototype),i=new _(r||[]);return o._invoke=function(t,e,n){var r="suspendedStart";return function(o,i){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===o)throw i;return{value:void 0,done:!0}}for(n.method=o,n.arg=i;;){var a=n.delegate;if(a){var c=x(a,n);if(c){if(c===s)continue;return c}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var u=w(t,e,n);if("normal"===u.type){if(r=n.done?"completed":"suspendedYield",u.arg===s)continue;return{value:u.arg,done:n.done}}"throw"===u.type&&(r="completed",n.method="throw",n.arg=u.arg)}}}(t,n,i),o}function w(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}function y(){}function v(){}function g(){}function m(t){["next","throw","return"].forEach((function(e){t[e]=function(t){return this._invoke(e,t)}}))}function k(t){var e;this._invoke=function(n,o){function i(){return new Promise((function(e,i){!function e(n,o,i,a){var c=w(t[n],t,o);if("throw"!==c.type){var u=c.arg,s=u.value;return s&&"object"==typeof s&&r.call(s,"__await")?Promise.resolve(s.__await).then((function(t){e("next",t,i,a)}),(function(t){e("throw",t,i,a)})):Promise.resolve(s).then((function(t){u.value=t,i(u)}),(function(t){return e("throw",t,i,a)}))}a(c.arg)}(n,o,e,i)}))}return e=e?e.then(i,i):i()}}function x(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method))return s;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return s}var r=w(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,s;var o=r.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,s):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,s)}function b(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function C(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function _(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function L(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:O}}function O(){return{value:void 0,done:!0}}}(function(){return this||"object"==typeof self&&self}()||Function("return this")())})),d=function(){return this||"object"==typeof self&&self}()||Function("return this")(),p=d.regeneratorRuntime&&Object.getOwnPropertyNames(d).indexOf("regeneratorRuntime")>=0,w=p&&d.regeneratorRuntime;d.regeneratorRuntime=void 0;var y=f;if(p)d.regeneratorRuntime=w;else try{delete d.regeneratorRuntime}catch(t){d.regeneratorRuntime=void 0}function v(t){return t.hasOwnProperty("result")?t.result:t}var g=function(t){function e(){var e;return(e=t.call(this)||this).name=e.constructor.name,e.message="No Kaikas provider was found on window.klaytn.",e}return o(e,t),e}(s(Error)),m=function(t){function e(){var e;return(e=t.call(this)||this).name=e.constructor.name,e.message="The user rejected the request.",e}return o(e,t),e}(s(Error));exports.KaikasConnector=function(t){function e(e){var n;return(n=t.call(this,e)||this).handleNetworkChanged=n.handleNetworkChanged.bind(h(n)),n.handleChainChanged=n.handleChainChanged.bind(h(n)),n.handleAccountsChanged=n.handleAccountsChanged.bind(h(n)),n.handleClose=n.handleClose.bind(h(n)),n}o(e,t);var i=e.prototype;return i.handleChainChanged=function(t){this.emitUpdate({chainId:t,provider:window.klaytn})},i.handleAccountsChanged=function(t){0===t.length?this.emitDeactivate():this.emitUpdate({account:t[0]})},i.handleClose=function(t,e){this.emitDeactivate()},i.handleNetworkChanged=function(t){this.emitUpdate({chainId:t,provider:window.klaytn})},i.activate=function(){var t=n(y.mark((function t(){var e;return y.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(window.klaytn){t.next=2;break}throw new g;case 2:return window.klaytn.on&&(window.klaytn.on("chainChanged",this.handleChainChanged),window.klaytn.on("accountsChanged",this.handleAccountsChanged),window.klaytn.on("close",this.handleClose),window.klaytn.on("networkChanged",this.handleNetworkChanged)),window.klaytn.isKaikas&&(window.klaytn.autoRefreshOnNetworkChange=!1),t.prev=4,t.next=7,window.klaytn.send("eth_requestAccounts").then((function(t){return v(t)[0]}));case 7:e=t.sent,t.next=15;break;case 10:if(t.prev=10,t.t0=t.catch(4),4001!==t.t0.code){t.next=14;break}throw new m;case 14:case 15:if(e){t.next=19;break}return t.next=18,window.klaytn.enable().then((function(t){return t&&v(t)[0]}));case 18:e=t.sent;case 19:return t.abrupt("return",r({provider:window.klaytn},e?{account:e}:{}));case 20:case"end":return t.stop()}}),t,this,[[4,10]])})));return function(){return t.apply(this,arguments)}}(),i.getProvider=function(){var t=n(y.mark((function t(){return y.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",window.klaytn);case 1:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}(),i.getChainId=function(){var t=n(y.mark((function t(){var e;return y.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(window.klaytn){t.next=2;break}throw new g;case 2:return t.prev=2,t.next=5,window.klaytn.send("eth_chainId").then(v);case 5:e=t.sent,t.next=11;break;case 8:t.prev=8,t.t0=t.catch(2);case 11:if(e){t.next=21;break}return t.prev=12,t.next=15,window.klaytn.send("net_version").then(v);case 15:e=t.sent,t.next=21;break;case 18:t.prev=18,t.t1=t.catch(12);case 21:if(!e)try{e=v(window.klaytn.send({method:"net_version"}))}catch(t){}return e||(e=window.klaytn.isDapper?v(window.klaytn.cachedResults.net_version):window.klaytn.chainId||window.klaytn.netVersion||window.klaytn.networkVersion||window.klaytn._chainId),t.abrupt("return",e);case 24:case"end":return t.stop()}}),t,null,[[2,8],[12,18]])})));return function(){return t.apply(this,arguments)}}(),i.getAccount=function(){var t=n(y.mark((function t(){var e;return y.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(window.klaytn){t.next=2;break}throw new g;case 2:return t.prev=2,t.next=5,window.klaytn.send("eth_accounts").then((function(t){return v(t)[0]}));case 5:e=t.sent,t.next=11;break;case 8:t.prev=8,t.t0=t.catch(2);case 11:if(e){t.next=21;break}return t.prev=12,t.next=15,window.klaytn.enable().then((function(t){return v(t)[0]}));case 15:e=t.sent,t.next=21;break;case 18:t.prev=18,t.t1=t.catch(12);case 21:return e||(e=v(window.klaytn.send({method:"eth_accounts"}))[0]),t.abrupt("return",e);case 23:case"end":return t.stop()}}),t,null,[[2,8],[12,18]])})));return function(){return t.apply(this,arguments)}}(),i.deactivate=function(){window.klaytn&&window.klaytn.removeListener&&(window.klaytn.removeListener("chainChanged",this.handleChainChanged),window.klaytn.removeListener("accountsChanged",this.handleAccountsChanged),window.klaytn.removeListener("close",this.handleClose),window.klaytn.removeListener("networkChanged",this.handleNetworkChanged))},i.isAuthorized=function(){var t=n(y.mark((function t(){return y.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(window.klaytn){t.next=2;break}return t.abrupt("return",!1);case 2:return t.prev=2,t.next=5,window.klaytn.send("eth_accounts").then((function(t){return v(t).length>0}));case 5:return t.abrupt("return",t.sent);case 8:return t.prev=8,t.t0=t.catch(2),t.abrupt("return",!1);case 11:case"end":return t.stop()}}),t,null,[[2,8]])})));return function(){return t.apply(this,arguments)}}(),e}(t.AbstractConnector),exports.NoEthereumProviderError=g,exports.UserRejectedRequestError=m;
//# sourceMappingURL=kaikas-connector.cjs.production.min.js.map