klip-sdk
Version:
JavaScript SDK to interact with klip App2App requests
2 lines (1 loc) • 17.9 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).klipSDK={})}(this,(function(e){"use strict";!function(e){function t(e){var t={},r=/(dolfin)[ \/]([\w.]+)/.exec(e)||/(edge)[ \/]([\w.]+)/.exec(e)||/(chrome)[ \/]([\w.]+)/.exec(e)||/(tizen)[ \/]([\w.]+)/.exec(e)||/(opera)(?:.*version)?[ \/]([\w.]+)/.exec(e)||/(webkit)(?:.*version)?[ \/]([\w.]+)/.exec(e)||/(msie) ([\w.]+)/.exec(e)||e.indexOf("compatible")<0&&/(mozilla)(?:.*? rv:([\w.]+))?/.exec(e)||["","unknown"];return"webkit"===r[1]?r=/(iphone|ipad|ipod)[\S\s]*os ([\w._\-]+) like/.exec(e)||/(android)[ \/]([\w._\-]+);/.exec(e)||[r[0],"safari",r[2]]:"mozilla"===r[1]?/trident/.test(e)?r[1]="msie":r[1]="firefox":/polaris|natebrowser|([010|011|016|017|018|019]{3}\d{3,4}\d{4}$)/.test(e)&&(r[1]="polaris"),t[r[1]]=!0,t.name=r[1],t.version=o(r[2]),t}function o(e){var t={},o=e?e.split(/\.|-|_/):["0","0","0"];return t.info=o.join("."),t.major=o[0]||"0",t.minor=o[1]||"0",t.patch=o[2]||"0",t}function r(e){return function(e){if(e.match(/linux|windows (nt|98)|macintosh|cros/)&&!e.match(/android|mobile|polaris|lgtelecom|uzard|natebrowser|ktf;|skt;/))return!0;return!1}(e)?"pc":function(e){if(e.match(/ipad/)||e.match(/android/)&&!e.match(/mobi|mini|fennec/))return!0;return!1}(e)?"tablet":function(e){return!!e.match(/ip(hone|od)|android.+mobile|windows (ce|phone)|blackberry|bb10|symbian|webos|firefox.+fennec|opera m(ob|in)i|tizen.+mobile|polaris|iemobile|lgtelecom|nokia|sonyericsson|dolfin|uzard|natebrowser|ktf;|skt;/)}(e)?"mobile":""}function n(e){var t={},r=/(iphone|ipad|ipod)[\S\s]*os ([\w._\-]+) like/.exec(e)||!!/polaris|natebrowser|([010|011|016|017|018|019]{3}\d{3,4}\d{4}$)/.test(e)&&["","polaris","0.0.0"]||/(windows)(?: nt | phone(?: os){0,1} | )([\w._\-]+)/.exec(e)||/(android)[ \/]([\w._\-]+);/.exec(e)||!!/android/.test(e)&&["","android","0.0.0"]||!!/(windows)/.test(e)&&["","windows","0.0.0"]||/(mac) os x ([\w._\-]+)/.exec(e)||/(tizen)[ \/]([\w._\-]+);/.exec(e)||!!/(linux)/.test(e)&&["","linux","0.0.0"]||!!/webos/.test(e)&&["","webos","0.0.0"]||/(cros)(?:\s[\w]+\s)([\d._\-]+)/.exec(e)||/(bada)[ \/]([\w._\-]+)/.exec(e)||!!/bada/.test(e)&&["","bada","0.0.0"]||!!/(rim|blackberry|bb10)/.test(e)&&["","blackberry","0.0.0"]||["","unknown","0.0.0"];return"iphone"===r[1]||"ipad"===r[1]||"ipod"===r[1]?r[1]="ios":"windows"===r[1]&&"98"===r[2]&&(r[2]="0.98.0"),"cros"===r[1]&&(r[1]="chrome"),t[r[1]]=!0,t.name=r[1],t.version=o(r[2]),t}Array.isArray||(Array.isArray=function(e){return"[object Array]"===Object.prototype.toString.call(e)});var i=["crios","fxios","daumapps"];function s(e,t){var r={},n=null,s=i;Array.isArray(t)?s=i.concat(t):"string"==typeof t&&(s=i.concat([t]));for(var a=0,c=s.length;a<c;a+=1){var u=s[a];if(n=new RegExp("("+u+")[ \\/]([\\w._\\-]+)").exec(e))break}return n||(n=["",""]),n[1]?(r.isApp=!0,r.name=n[1],r.version=o(n[2])):r.isApp=!1,r}var a=e.userAgent=function(e,o){var i=function(e){return e?e.toLowerCase():"undefined"!=typeof window&&window.navigator&&"string"==typeof window.navigator.userAgent?window.navigator.userAgent.toLowerCase():""}(e);return{ua:i,browser:t(i),platform:r(i),os:n(i),app:s(i,o)}};"object"==typeof window&&window.navigator.userAgent&&(window.ua_result=a(window.navigator.userAgent)||null)}("object"==typeof e?(e.daumtools=e,e.util=e,e):"object"==typeof window?(window.daumtools=void 0===window.daumtools?{}:window.daumtools,window.util=void 0===window.util?window.daumtools:window.util,window.daumtools):"object"==typeof self?(self.daumtools=void 0===self.daumtools?{}:self.daumtools,self.util=void 0===self.util?self.daumtools:self.util,self):void 0),function(e){e.web2app=function(){var t=e.userAgent(),o=t.os,r=["firefox","opr/"];function n(e,t,o){var r=(new Date).getTime();return setTimeout((function(){var n=(new Date).getTime();s()&&n-r<e+100&&(o?o():function(e){top.window.location.href=e}(t))}),e)}function i(e){document.addEventListener("visibilitychange",(function t(){"hidden"===document.visibilityState&&(clearTimeout(e),document.removeEventListener("visibilitychange",t))}))}function s(){for(var e=["hidden","webkitHidden"],t=0,o=e.length;t<o;t++)if(void 0!==document[e[t]])return!document[e[t]];return!0}function a(e){setTimeout((function(){(function(e){var t=document.createElement("iframe");return t.id=e,t.style.border="none",t.style.width="0",t.style.height="0",t.style.display="none",t.style.overflow="hidden",document.body.appendChild(t),t}("appLauncher")).src=e}),100)}return function(e){var c,u,d,f="function"==typeof e.willInvokeApp?e.willInvokeApp:function(){},p="function"==typeof e.onAppMissing?e.onAppMissing:null,l="function"==typeof e.onUnsupportedEnvironment?e.onUnsupportedEnvironment:function(){};if(f(),o.android)u=t.browser.chrome&&+t.browser.version.major>=25,d=new RegExp(r.join("|"),"i"),u&&!d.test(t.ua)&&e.intentURI&&!e.useUrlScheme?function(e,r,s){if(t.browser.chrome){i(n(o.android?2e3:2500,r,s)),a()}else setTimeout(a,100);function a(){top.window.location.href=e}}(e.intentURI,e.storeURL,p):e.storeURL&&(c=e.urlScheme,i(n(2e3,e.storeURL,p)),a(c));else if(o.ios&&e.storeURL){!/(crios|CriOS)/.test(t.ua)&&(t.browser.iphone||t.browser.ipad||t.browser.ipod)?setTimeout(()=>{window.open(e.urlScheme)}):function(e,o,r,c){var u=n(2500,o,r);parseInt(t.os.version.major,10)<8?function(e){window.addEventListener("pagehide",(function t(){s()&&(clearTimeout(e),window.removeEventListener("pagehide",t))}))}(u):i(u);parseInt(t.os.version.major,10)>8&&t.os.ios?(void 0===c?c=e:clearTimeout(u),function(e){top.window.location.href=e}(c)):a(e)}(e.urlScheme,e.storeURL,p,e.universalLink)}else setTimeout((function(){l()}),100)}}()}("object"==typeof e?(e.daumtools=e,e):"object"==typeof window?(window.daumtools=void 0===window.daumtools?{}:window.daumtools,window.daumtools):void 0),function(e){e.daumtools=void 0===e.daumtools?{}:e.daumtools,void 0!==e.daumtools.web2app&&(e.daumtools.web2app.version="1.1.1")}(window);var t="undefined"!=typeof globalThis&&globalThis||"undefined"!=typeof self&&self||void 0!==t&&t,o="URLSearchParams"in t,r="Symbol"in t&&"iterator"in Symbol,n="FileReader"in t&&"Blob"in t&&function(){try{return new Blob,!0}catch(e){return!1}}(),i="FormData"in t,s="ArrayBuffer"in t;if(s)var a=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],c=ArrayBuffer.isView||function(e){return e&&a.indexOf(Object.prototype.toString.call(e))>-1};function u(e){if("string"!=typeof e&&(e=String(e)),/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(e)||""===e)throw new TypeError("Invalid character in header field name");return e.toLowerCase()}function d(e){return"string"!=typeof e&&(e=String(e)),e}function f(e){var t={next:function(){var t=e.shift();return{done:void 0===t,value:t}}};return r&&(t[Symbol.iterator]=function(){return t}),t}function p(e){this.map={},e instanceof p?e.forEach((function(e,t){this.append(t,e)}),this):Array.isArray(e)?e.forEach((function(e){this.append(e[0],e[1])}),this):e&&Object.getOwnPropertyNames(e).forEach((function(t){this.append(t,e[t])}),this)}function l(e){if(e.bodyUsed)return Promise.reject(new TypeError("Already read"));e.bodyUsed=!0}function h(e){return new Promise((function(t,o){e.onload=function(){t(e.result)},e.onerror=function(){o(e.error)}}))}function y(e){var t=new FileReader,o=h(t);return t.readAsArrayBuffer(e),o}function m(e){if(e.slice)return e.slice(0);var t=new Uint8Array(e.byteLength);return t.set(new Uint8Array(e)),t.buffer}function b(){return this.bodyUsed=!1,this._initBody=function(e){var t;this.bodyUsed=this.bodyUsed,this._bodyInit=e,e?"string"==typeof e?this._bodyText=e:n&&Blob.prototype.isPrototypeOf(e)?this._bodyBlob=e:i&&FormData.prototype.isPrototypeOf(e)?this._bodyFormData=e:o&&URLSearchParams.prototype.isPrototypeOf(e)?this._bodyText=e.toString():s&&n&&((t=e)&&DataView.prototype.isPrototypeOf(t))?(this._bodyArrayBuffer=m(e.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer])):s&&(ArrayBuffer.prototype.isPrototypeOf(e)||c(e))?this._bodyArrayBuffer=m(e):this._bodyText=e=Object.prototype.toString.call(e):this._bodyText="",this.headers.get("content-type")||("string"==typeof e?this.headers.set("content-type","text/plain;charset=UTF-8"):this._bodyBlob&&this._bodyBlob.type?this.headers.set("content-type",this._bodyBlob.type):o&&URLSearchParams.prototype.isPrototypeOf(e)&&this.headers.set("content-type","application/x-www-form-urlencoded;charset=UTF-8"))},n&&(this.blob=function(){var e=l(this);if(e)return e;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(new Blob([this._bodyArrayBuffer]));if(this._bodyFormData)throw new Error("could not read FormData body as blob");return Promise.resolve(new Blob([this._bodyText]))},this.arrayBuffer=function(){if(this._bodyArrayBuffer){var e=l(this);return e||(ArrayBuffer.isView(this._bodyArrayBuffer)?Promise.resolve(this._bodyArrayBuffer.buffer.slice(this._bodyArrayBuffer.byteOffset,this._bodyArrayBuffer.byteOffset+this._bodyArrayBuffer.byteLength)):Promise.resolve(this._bodyArrayBuffer))}return this.blob().then(y)}),this.text=function(){var e,t,o,r=l(this);if(r)return r;if(this._bodyBlob)return e=this._bodyBlob,t=new FileReader,o=h(t),t.readAsText(e),o;if(this._bodyArrayBuffer)return Promise.resolve(function(e){for(var t=new Uint8Array(e),o=new Array(t.length),r=0;r<t.length;r++)o[r]=String.fromCharCode(t[r]);return o.join("")}(this._bodyArrayBuffer));if(this._bodyFormData)throw new Error("could not read FormData body as text");return Promise.resolve(this._bodyText)},i&&(this.formData=function(){return this.text().then(_)}),this.json=function(){return this.text().then(JSON.parse)},this}p.prototype.append=function(e,t){e=u(e),t=d(t);var o=this.map[e];this.map[e]=o?o+", "+t:t},p.prototype.delete=function(e){delete this.map[u(e)]},p.prototype.get=function(e){return e=u(e),this.has(e)?this.map[e]:null},p.prototype.has=function(e){return this.map.hasOwnProperty(u(e))},p.prototype.set=function(e,t){this.map[u(e)]=d(t)},p.prototype.forEach=function(e,t){for(var o in this.map)this.map.hasOwnProperty(o)&&e.call(t,this.map[o],o,this)},p.prototype.keys=function(){var e=[];return this.forEach((function(t,o){e.push(o)})),f(e)},p.prototype.values=function(){var e=[];return this.forEach((function(t){e.push(t)})),f(e)},p.prototype.entries=function(){var e=[];return this.forEach((function(t,o){e.push([o,t])})),f(e)},r&&(p.prototype[Symbol.iterator]=p.prototype.entries);var w=["DELETE","GET","HEAD","OPTIONS","POST","PUT"];function v(e,t){if(!(this instanceof v))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');var o,r,n=(t=t||{}).body;if(e instanceof v){if(e.bodyUsed)throw new TypeError("Already read");this.url=e.url,this.credentials=e.credentials,t.headers||(this.headers=new p(e.headers)),this.method=e.method,this.mode=e.mode,this.signal=e.signal,n||null==e._bodyInit||(n=e._bodyInit,e.bodyUsed=!0)}else this.url=String(e);if(this.credentials=t.credentials||this.credentials||"same-origin",!t.headers&&this.headers||(this.headers=new p(t.headers)),this.method=(o=t.method||this.method||"GET",r=o.toUpperCase(),w.indexOf(r)>-1?r:o),this.mode=t.mode||this.mode||null,this.signal=t.signal||this.signal,this.referrer=null,("GET"===this.method||"HEAD"===this.method)&&n)throw new TypeError("Body not allowed for GET or HEAD requests");if(this._initBody(n),!("GET"!==this.method&&"HEAD"!==this.method||"no-store"!==t.cache&&"no-cache"!==t.cache)){var i=/([?&])_=[^&]*/;if(i.test(this.url))this.url=this.url.replace(i,"$1_="+(new Date).getTime());else{this.url+=(/\?/.test(this.url)?"&":"?")+"_="+(new Date).getTime()}}}function _(e){var t=new FormData;return e.trim().split("&").forEach((function(e){if(e){var o=e.split("="),r=o.shift().replace(/\+/g," "),n=o.join("=").replace(/\+/g," ");t.append(decodeURIComponent(r),decodeURIComponent(n))}})),t}function g(e,t){if(!(this instanceof g))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');t||(t={}),this.type="default",this.status=void 0===t.status?200:t.status,this.ok=this.status>=200&&this.status<300,this.statusText="statusText"in t?t.statusText:"",this.headers=new p(t.headers),this.url=t.url||"",this._initBody(e)}v.prototype.clone=function(){return new v(this,{body:this._bodyInit})},b.call(v.prototype),b.call(g.prototype),g.prototype.clone=function(){return new g(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new p(this.headers),url:this.url})},g.error=function(){var e=new g(null,{status:0,statusText:""});return e.type="error",e};var A=[301,302,303,307,308];g.redirect=function(e,t){if(-1===A.indexOf(t))throw new RangeError("Invalid status code");return new g(null,{status:t,headers:{location:e}})};var T=t.DOMException;try{new T}catch(e){(T=function(e,t){this.message=e,this.name=t;var o=Error(e);this.stack=o.stack}).prototype=Object.create(Error.prototype),T.prototype.constructor=T}function E(e,o){return new Promise((function(r,i){var a=new v(e,o);if(a.signal&&a.signal.aborted)return i(new T("Aborted","AbortError"));var c=new XMLHttpRequest;function u(){c.abort()}c.onload=function(){var e,t,o={status:c.status,statusText:c.statusText,headers:(e=c.getAllResponseHeaders()||"",t=new p,e.replace(/\r?\n[\t ]+/g," ").split(/\r?\n/).forEach((function(e){var o=e.split(":"),r=o.shift().trim();if(r){var n=o.join(":").trim();t.append(r,n)}})),t)};o.url="responseURL"in c?c.responseURL:o.headers.get("X-Request-URL");var n="response"in c?c.response:c.responseText;setTimeout((function(){r(new g(n,o))}),0)},c.onerror=function(){setTimeout((function(){i(new TypeError("Network request failed"))}),0)},c.ontimeout=function(){setTimeout((function(){i(new TypeError("Network request failed"))}),0)},c.onabort=function(){setTimeout((function(){i(new T("Aborted","AbortError"))}),0)},c.open(a.method,function(e){try{return""===e&&t.location.href?t.location.href:e}catch(t){return e}}(a.url),!0),"include"===a.credentials?c.withCredentials=!0:"omit"===a.credentials&&(c.withCredentials=!1),"responseType"in c&&(n?c.responseType="blob":s&&a.headers.get("Content-Type")&&-1!==a.headers.get("Content-Type").indexOf("application/octet-stream")&&(c.responseType="arraybuffer")),!o||"object"!=typeof o.headers||o.headers instanceof p?a.headers.forEach((function(e,t){c.setRequestHeader(t,e)})):Object.getOwnPropertyNames(o.headers).forEach((function(e){c.setRequestHeader(e,d(o.headers[e]))})),a.signal&&(a.signal.addEventListener("abort",u),c.onreadystatechange=function(){4===c.readyState&&a.signal.removeEventListener("abort",u)}),c.send(void 0===a._bodyInit?null:a._bodyInit)}))}E.polyfill=!0,t.fetch||(t.fetch=E,t.Headers=p,t.Request=v,t.Response=g);const{web2app:L,userAgent:x}=e,{SERVER_URL:P,A2A_URL_SCHEME:k,KLIP_APP_URL_SCHEME:S,IOS_KLIP_APP_STORE_LINK:O,AOS_KLIP_APP_STORE_LINK:j,KLIP_URL:U}={SERVER_URL:"https://a2a-api.klipwallet.com/v2/a2a",KLIP_URL:"https://klipwallet.com",A2A_URL_SCHEME:"klipwallet/open?url=https://klipwallet.com/?target=/a2a",KLIP_APP_URL_SCHEME:"klip",IOS_KLIP_APP_STORE_LINK:"itms-apps://itunes.apple.com/app/id1627665524",AOS_KLIP_APP_STORE_LINK:"market://details?id=com.klipwallet.app",KLIP_APP_PACKAGE_NAME:"com.klipwallet.app",KLIP_APP_NAME:"클립"},R={auth:({bappName:e,successLink:t,failLink:o})=>fetch(P+"/prepare",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({bapp:{name:e,callback:{success:t,fail:o}},type:"auth"})}).then(e=>e.json()),sendKLAY:({bappName:e,from:t,to:o,amount:r,successLink:n,failLink:i})=>fetch(P+"/prepare",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({bapp:{name:e,callback:{success:n,fail:i}},type:"send_klay",transaction:{from:t,to:o,amount:r}})}).then(e=>e.json()),sendToken:({bappName:e,from:t,to:o,amount:r,contract:n,successLink:i,failLink:s})=>fetch(P+"/prepare",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({bapp:{name:e,callback:{success:i,fail:s}},type:"send_token",transaction:{from:t,to:o,amount:r,contract:n}})}).then(e=>e.json()),sendCard:({bappName:e,from:t,to:o,id:r,contract:n,successLink:i,failLink:s})=>fetch(P+"/prepare",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({bapp:{name:e,callback:{success:i,fail:s}},type:"send_card",transaction:{from:t,to:o,card_id:r,contract:n}})}).then(e=>e.json()),executeContract:({bappName:e,from:t,to:o,value:r,abi:n,params:i,successLink:s,failLink:a})=>fetch(P+"/prepare",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({bapp:{name:e,callback:{success:s,fail:a}},type:"execute_contract",transaction:{from:t,to:o,value:r,abi:n,params:i}})}).then(e=>e.json()),signMessage:({bappName:e,value:t,from:o,successLink:r,failLink:n})=>fetch(P+"/prepare",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({bapp:{name:e,callback:{success:r,fail:n}},type:"sign_message",message:{value:t,from:o}})}).then(e=>e.json())},B=e=>{e?e():alert("모바일 기기에서만 이용 가능한 기능입니다.")};e.getCardList=({contract:e,eoa:t,cursor:o=""})=>fetch(`${P}/cards?sca=${e}&eoa=${t}&cursor=${o}`,{method:"GET"}).then(e=>e.json()),e.getResult=e=>fetch(`${P}/result?request_key=${e}`,{method:"GET"}).then(e=>e.json()),e.prepare=R,e.request=(e,t,o)=>{const r=x(),n=r.browser.chrome||(()=>{const e=x();return/(crios|CriOS)/.test(e.ua)&&e.os.ios})()?`${S}://${k}?request_key=${e}`:`${U}/?target=/a2a?request_key=${e}`,i=r.os.android?j:O;L({urlScheme:n,useUrlScheme:!0,storeURL:i,onUnsupportedEnvironment:()=>{B(o)},onAppMissing:()=>{window.open(`${U}/?target=/a2a?request_key=${e}`,"_blank","noopener,noreferrer")}})},Object.defineProperty(e,"__esModule",{value:!0})}));