UNPKG

@sixnetwork/klip-connector

Version:

A simple, maximally extensible, dependency minimized framework for building modern Klaytn dApps

3 lines (2 loc) 9.23 kB
"use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var e=require("@sixnetwork/caverjs-react-abstract-connector"),n=t(require("qrcode")),r=t(require("axios")),o=require("react-device-detect");function i(){return(i=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 a(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,u(t,e)}function c(t){return(c=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function u(t,e){return(u=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function s(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}function l(t,e,n){return(l=s()?Reflect.construct:function(t,e,n){var r=[null];r.push.apply(r,e);var o=new(Function.bind.apply(t,r));return n&&u(o,n.prototype),o}).apply(null,arguments)}function h(t){var e="function"==typeof Map?new Map:void 0;return(h=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 l(t,arguments,c(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),u(n,t)})(t)}function f(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}var p=function(){function t(){var t=this,e=this,i=this;this.account="",this.initData=function(){i.requestKey="",i.responseData=void 0},this.login=function(){i.isAuth=!0},this.logout=function(){i.isAuth=!1},this.getAuth=function(){return i.isAuth},this.genQRcode=function(t){i.initData(),r.post("https://a2a-api.klipwallet.com/v2/a2a/prepare",{bapp:{name:"definix"},type:"auth"}).then((function(e){try{i.requestKey=e.data.request_key;var r=function(){if(!o.isMobile)return Promise.resolve(t()).then((function(){return Promise.resolve(n.toCanvas(document.getElementById("qrcode"),"https://klipwallet.com/?target=/a2a?request_key="+e.data.request_key,(function(){i.intervalCheckResult=setInterval(i.getResult,1e3)}))).then((function(){}))}));i.intervalCheckResult=setInterval(i.getResult,1e3),i.openDeeplink("https://klipwallet.com/?target=/a2a?request_key="+e.data.request_key)}();return Promise.resolve(r&&r.then?r.then((function(){})):void 0)}catch(t){return Promise.reject(t)}}))},this.getResult=function(){try{return Promise.resolve(r.get("https://a2a-api.klipwallet.com/v2/a2a/result?request_key="+t.requestKey)).then((function(e){"completed"==e.data.status&&(t.account=e.data.result.klaytn_address,t.responseData=e.data.result.klaytn_address,t.intervalCheckResult&&clearInterval(t.intervalCheckResult))}))}catch(t){return Promise.reject(t)}},this.getAccount=function(){return i.account},this.getRequestKey=function(){return i.requestKey},this.checkResponse=function(){try{return Promise.resolve(new Promise((function(t){var n=setInterval((function(){null!=e.responseData&&(clearInterval(n),t(e.responseData))}),1e3)})))}catch(t){return Promise.reject(t)}},this.genQRcodeContactInteract=function(t,e,o){i.initData(),r.post("https://a2a-api.klipwallet.com/v2/a2a/prepare",{bapp:{name:"definix"},type:"execute_contract",transaction:{to:t,value:"0",abi:e,params:o}}).then((function(t){i.requestKey=t.data.request_key,n.toCanvas(document.getElementById("qrcode"),"https://klipwallet.com/?target=/a2a?request_key="+t.data.request_key,(function(){i.intervalCheckResult=setInterval(i.getResult,1e3)}))}))},this.openDeeplink=function(t){null===window.open(t,"_blank")&&(window.location.href="kakaotalk://klipwallet/open?url="+t,setTimeout((function(){document.hasFocus()&&window.location.replace("https://apps.apple.com/kr/app/%EC%B9%B4%EC%B9%B4%EC%98%A4%ED%86%A1-kakaotalk/id362057947")}),4500))},this.isAuth=!1,this.requestKey="",this.responseData=void 0}return t.prototype.Popup=function(){var t=document.createElement("dialog");document.body.appendChild(t);var e=document.createTextNode("This is a dialog window");t.appendChild(e),t.showModal()},t}(),d=function(){function t(){}return t.prototype.then=function(e,n){var r=new t,o=this.s;if(o){var i=1&o?e:n;if(i){try{v(r,1,i(this.v))}catch(t){v(r,2,t)}return r}return this}return this.o=function(t){try{var o=t.v;1&t.s?v(r,1,e?e(o):o):n?v(r,1,n(o)):v(r,2,o)}catch(t){v(r,2,t)}},r},t}();function v(t,e,n){if(!t.s){if(n instanceof d){if(!n.s)return void(n.o=v.bind(null,t,e));1&e&&(e=n.s),n=n.v}if(n&&n.then)return void n.then(v.bind(null,t,e),v.bind(null,t,2));t.s=e,t.v=n;var r=t.o;r&&r(t)}}function y(t){return t instanceof d&&1&t.s}var m="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var g=["https://klaytn-en.sixnetwork.io:8651/","https://kaikas.cypress.klaytn.net:8651/","https://kaikas.cypress.klaytn.net:8651/"],k=function(){try{var t=!1,e=function(t,e,n){if("function"==typeof t[m]){var r,o,i,a=t[m]();if(function t(c){try{for(;!((r=a.next()).done||n&&n());)if((c=e(r.value))&&c.then){if(!y(c))return void c.then(t,i||(i=v.bind(null,o=new d,2)));c=c.v}o?v(o,1,c):o=c}catch(t){v(o||(o=new d),2,t)}}(),a.return){var c=function(t){try{r.done||a.return()}catch(t){}return t};if(o&&o.then)return o.then(c,(function(t){throw c(t)}));c()}return o}if(!("length"in t))throw new TypeError("Object is not iterable");for(var u=[],s=0;s<t.length;s++)u.push(t[s]);return function(t,e,n){var r,o,i=-1;return function a(c){try{for(;++i<t.length&&(!n||!n());)if((c=e(i))&&c.then){if(!y(c))return void c.then(a,o||(o=v.bind(null,r=new d,2)));c=c.v}r?v(r,1,c):r=c}catch(t){v(r||(r=new d),2,t)}}(),r}(u,(function(t){return e(u[t])}),n)}(g,(function(e){return Promise.resolve(function(t){try{return Promise.resolve(new Promise((function(e){r.get(t).then((function(t){e(t.status)})).catch((function(t){t.response&&e(t.response.status)}))})))}catch(t){return Promise.reject(t)}}(e)).then((function(n){if(function(t){return 200===t}(n))return t=!0,e}))}),(function(){return t}));return Promise.resolve(e&&e.then?e.then((function(e){return t?e:"all node rpc not work"})):t?e:"all node rpc not work")}catch(t){return Promise.reject(t)}},w=require("caver-js"),b=function(t){function e(){var e;return(e=t.call(this)||this).name=e.constructor.name,e.message="No Klaytn provider was found on window.klaytn.",e}return a(e,t),e}(h(Error)),C=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 a(e,t),e}(h(Error));exports.KlipConnector=function(t){function e(e){var n;return(n=t.call(this,e)||this).showModal=e.showModal,n.closeModal=e.closeModal,n.handleNetworkChanged=n.handleNetworkChanged.bind(f(n)),n.handleChainChanged=n.handleChainChanged.bind(f(n)),n.handleAccountsChanged=n.handleAccountsChanged.bind(f(n)),n.handleClose=n.handleClose.bind(f(n)),n}a(e,t);var n=e.prototype;return n.handleChainChanged=function(t){this.emitUpdate({chainId:t,provider:this.providerCaver})},n.handleAccountsChanged=function(t){0===t.length?this.emitDeactivate():this.emitUpdate({account:t[0]})},n.handleClose=function(t,e){this.emitDeactivate()},n.handleNetworkChanged=function(t){this.emitUpdate({chainId:t,provider:this.providerCaver})},n.activate=function(){try{var t=this;return Promise.resolve(Promise.resolve(function(t,e){try{var n=t()}catch(t){return""}return n&&n.then?n.then(void 0,(function(){return""})):n}(k))).then((function(e){function n(){return i({provider:t.providerCaver},r?{account:r}:{})}var r;t.providerCaver=new w(e).currentProvider,t.KlipConnectorProvider=new p,"klip"==localStorage.getItem("connector")&&localStorage.getItem("userAccount")&&(r=localStorage.getItem("userAccount"),t.KlipConnectorProvider.login());var o=function(){if(localStorage.getItem("connector")){var e=function(){if(!r)return t.KlipConnectorProvider.genQRcode(t.showModal),Promise.resolve(t.KlipConnectorProvider.checkResponse()).then((function(e){r=e,localStorage.setItem("userAccount",r),t.closeModal(),t.KlipConnectorProvider.login()}))}();if(e&&e.then)return e.then((function(){}))}}();return o&&o.then?o.then(n):n()}))}catch(t){return Promise.reject(t)}},n.getProvider=function(){try{return Promise.resolve(this.providerCaver)}catch(t){return Promise.reject(t)}},n.getChainId=function(){return Promise.resolve(8217)},n.getAccount=function(){try{return Promise.resolve(this.KlipConnectorProvider.getAccount())}catch(t){return Promise.reject(t)}},n.deactivate=function(){this.KlipConnectorProvider.logout()},n.isAuthorized=function(){try{return Promise.resolve(this.KlipConnectorProvider.getAuth())}catch(t){return Promise.reject(t)}},e}(e.AbstractConnector),exports.NoKlaytnProviderError=b,exports.UserRejectedRequestError=C; //# sourceMappingURL=klip-connector.cjs.production.min.js.map