@magic-sdk/provider
Advanced tools
Comparing version 2.1.2 to 2.2.0
@@ -15,2 +15,15 @@ ## Upcoming Changes | ||
## `2.2.0` - 06/25/2020 | ||
#### Changed | ||
- Update dependencies. | ||
- Marked `encodeQueryParameters` and `decodeQueryParameters` utility methods for deprecation in `v3.0.0`. | ||
#### Added | ||
- Add `MagicExtensionWarning` class. | ||
- Add `createWarning` and `createDeprecationWarning` helper methods to `BaseExtension`. | ||
- Add `encodeJSON` and `decodeJSON` helper methods to `BaseExtension.utils`. These are direct aliases for `encodeQueryParameters` and `decodeQueryParameters` (which will be deprecated in the next major version). | ||
## `2.1.2` - 06/23/2020 | ||
@@ -17,0 +30,0 @@ |
@@ -27,2 +27,14 @@ import { SDKErrorCode, SDKWarningCode, RPCErrorCode, JsonRpcError } from '@magic-sdk/types'; | ||
/** | ||
* This error type is reserved for communicating errors that arise during | ||
* execution of Magic SDK Extension methods. Compare this to the `SDKError` | ||
* type, specifically in context of Extensions. | ||
*/ | ||
export declare class MagicExtensionWarning { | ||
code: string | number; | ||
rawMessage: string; | ||
message: string; | ||
constructor(ext: Extension<string>, code: string | number, rawMessage: string); | ||
log(): void; | ||
} | ||
/** | ||
* This error type communicates exceptions that occur during execution in the | ||
@@ -61,1 +73,8 @@ * Magic `<iframe>` context. | ||
export declare function createReactNativeEndpointConfigurationWarning(): MagicSDKWarning; | ||
export declare function createDeprecationWarning(options: { | ||
method: string; | ||
removalVersions: { | ||
[P in 'magic-sdk' | '@magic-sdk/react-native']: string; | ||
}; | ||
useInstead?: string; | ||
}): MagicSDKWarning; |
@@ -1,2 +0,2 @@ | ||
var e,r=require("@magic-sdk/types"),t=(e=require("eventemitter3"))&&"object"==typeof e&&"default"in e?e.default:e;function n(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function o(e,r,t){return r&&n(e.prototype,r),t&&n(e,t),e}function i(){return(i=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}function a(e,r){e.prototype=Object.create(r.prototype),e.prototype.constructor=e,e.__proto__=r}function s(e){return(s=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function u(e,r){return(u=Object.setPrototypeOf||function(e,r){return e.__proto__=r,e})(e,r)}function c(){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(e){return!1}}function d(e,r,t){return(d=c()?Reflect.construct:function(e,r,t){var n=[null];n.push.apply(n,r);var o=new(Function.bind.apply(e,n));return t&&u(o,t.prototype),o}).apply(null,arguments)}function l(e){var r="function"==typeof Map?new Map:void 0;return(l=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(e))return r.get(e);r.set(e,t)}function t(){return d(e,arguments,s(this).constructor)}return t.prototype=Object.create(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),u(t,e)})(e)}function p(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function f(e){return btoa(JSON.stringify(e))}function h(e){return JSON.parse(atob(e))}function y(e){return void 0===e}function v(e){return function(e){return null===e}(e)||y(e)}function g(e){return!v(e)&&!(y(e.jsonrpc)||y(e.id)||y(e.result)&&y(e.error))}function m(e){return!v(e)&&"number"==typeof e&&Object.values(r.RPCErrorCode).includes(e)}function _(e){if(!e)return!0;for(var r in e)if(Object.hasOwnProperty.call(e,r))return!1;return!0}var E={},M=function(e){function r(t,n){var o;return(o=e.call(this,"Magic SDK Error: ["+t+"] "+n)||this).code=t,o.rawMessage=n,o.__proto__=Error,Object.setPrototypeOf(p(o),r.prototype),o}return a(r,e),r}(l(Error)),w=function(e){function r(t,n,o,i){var a;return(a=e.call(this,"Magic Extension Error ("+t.name+"): ["+n+"] "+o)||this).code=n,a.rawMessage=o,a.data=i,a.__proto__=Error,Object.setPrototypeOf(p(a),r.prototype),a}return a(r,e),r}(l(Error)),P=function(e){function t(n){var o;(o=e.call(this)||this).__proto__=Error;var i=Number(null==n?void 0:n.code);return o.rawMessage=(null==n?void 0:n.message)||"Internal error",o.code=m(i)?i:r.RPCErrorCode.InternalError,o.message="Magic RPC Error: ["+o.code+"] "+o.rawMessage,Object.setPrototypeOf(p(o),t.prototype),o}return a(t,e),t}(l(Error)),b=function(){function e(e,r){this.code=e,this.rawMessage=r,this.message="Magic SDK Warning: ["+e+"] "+r}return e.prototype.log=function(){console.warn(this.message)},e}();function A(){return new M(r.SDKErrorCode.MissingApiKey,"Please provide an API key that you acquired from the Magic developer dashboard.")}function x(){return new M(r.SDKErrorCode.MalformedResponse,"Response from the Magic iframe is malformed.")}function I(e){return new M(r.SDKErrorCode.ExtensionNotInitialized,"Extensions must be initialized with a Magic SDK instance before `Extension."+e+"` can be accessed. Do not invoke `Extension."+e+"` inside an extension constructor.")}function R(e){var t,n,o;return new M(r.SDKErrorCode.InvalidArgument,"Invalid "+(o=(t=e.argument+1)%100,(1==(n=t%10)&&11!==o?t+"st":2===n&&12!==o?t+"nd":3===n&&13!==o?t+"rd":t+"th")+" argument given to `")+e.procedure+"`.\n Expected: `"+e.expected+"`\n Received: `"+e.received+"`")}function O(){return new b(r.SDKWarningCode.SyncWeb3Method,"Non-async web3 methods are deprecated in web3 > 1.0 and are not supported by the Magic web3 provider. Please use an async method instead.")}function S(){return new b(r.SDKWarningCode.ReactNativeEndpointConfiguration,"CUSTOM DOMAINS ARE NOT SUPPORTED WHEN USING MAGIC SDK WITH REACT NATIVE! The `endpoint` parameter SHOULD NOT be provided. The Magic `<iframe>` is automatically wrapped by a WebView pointed at `"+E.defaultEndpoint+"`. Changing this default behavior will lead to unexpected results and potentially security-threatening bugs.")}var C=regeneratorRuntime.mark(j);function j(){var e;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:e=0;case 1:if(!(e<Number.MAX_SAFE_INTEGER)){r.next=7;break}return r.next=5,++e;case 5:r.next=8;break;case 7:e=0;case 8:r.next=1;break;case 10:case"end":return r.stop()}},C)}var D=j();function N(){return D.next().value}var k=Symbol("Payload pre-processed by Magic SDK");function L(e){return Object.defineProperty(e,k,{value:!0,enumerable:!1}),e}function T(e){var r,t,n;return function(e){return!!e[k]}(e)||(e.jsonrpc=null!=(r=e.jsonrpc)?r:"2.0",e.id=N(),e.method=null!=(t=e.method)?t:"noop",e.params=null!=(n=e.params)?n:[],L(e)),e}function W(e,r){return void 0===r&&(r=[]),L({params:r,method:e,jsonrpc:"2.0",id:N()})}var K=function(){function e(r){r instanceof e?(this._jsonrpc=r.payload.jsonrpc,this._id=r.payload.id,this._result=r.payload.result,this._error=r.payload.error):g(r)?(this._jsonrpc=r.jsonrpc,this._id=r.id,this._result=r.result,this._error=r.error):(this._jsonrpc=r.jsonrpc,this._id=r.id,this._result=void 0,this._error=void 0)}var r=e.prototype;return r.applyError=function(e){return this._error=e,this},r.applyResult=function(e){return this._result=e,this},o(e,[{key:"hasError",get:function(){return null!=this._error}},{key:"hasResult",get:function(){return void 0!==this._result}},{key:"payload",get:function(){return{jsonrpc:this._jsonrpc,id:this._id,result:this._result,error:this._error}}}]),e}(),Q=function(e){function r(){return e.apply(this,arguments)||this}return a(r,e),r}(t);function q(){var e=new Q;return{emitter:e,createChainingEmitterMethod:function(r,t){return function(){return e[r].apply(e,[].slice.call(arguments)),t}},createBoundEmitterMethod:function(r){return function(){return e[r].apply(e,[].slice.call(arguments))}}}}var G=Symbol("isPromiEvent");function U(e){var r=H(e),t=q(),n=t.createBoundEmitterMethod,o=t.createChainingEmitterMethod,i=Symbol("Promise.then"),a=Symbol("Promise.catch"),s=Symbol("Promise.finally"),u=function(e,r){return function(){var t=r[e].apply(r,[].slice.call(arguments));return c(t)}},c=function(e){var r;return Object.assign(e,((r={})[G]=!0,r[i]=e[i]||e.then,r[a]=e[a]||e.catch,r[s]=e[s]||e.finally,r.then=u(i,e),r.catch=u(a,e),r.finally=u(s,e),r.on=o("on",e),r.once=o("once",e),r.addListener=o("addListener",e),r.off=o("off",e),r.removeListener=o("removeListener",e),r.removeAllListeners=o("removeAllListeners",e),r.emit=n("emit"),r.eventNames=n("eventNames"),r.listeners=n("listeners"),r.listenerCount=n("listenerCount"),r))},d=c(r.then(function(e){return d.emit("done",e),d.emit("settled"),e},function(e){throw d.emit("error",e),d.emit("settled"),e}));return d}function H(e){return new Promise(function(r,t){var n=e(r,t);Promise.resolve(n).catch(t)})}var z=function(){function e(e){this.sdk=e}return e.prototype.request=function(e){var t=this.transport.post(this.overlay,r.MagicOutgoingWindowMessage.MAGIC_HANDLE_REQUEST,T(e)),n=U(function(e,r){t.then(function(t){if(o(),t.hasError)r(new P(t.payload.error));else{if(!t.hasResult)throw x();e(t.payload.result)}}).catch(function(e){o(),r(e)})}),o=this.transport.on(r.MagicIncomingWindowMessage.MAGIC_HANDLE_EVENT,function(r){var t,o=r.data.response;if(o.id===e.id&&(null===(t=o.result)||void 0===t?void 0:t.event)){var i=o.result,a=i.params;n.emit.apply(n,[i.event].concat(void 0===a?[]:a))}});return n},o(e,[{key:"transport",get:function(){return this.sdk.transport}},{key:"overlay",get:function(){return this.sdk.overlay}}]),e}(),V=function(e){function t(){return e.apply(this,arguments)||this}return a(t,e),t.prototype.loginWithMagicLink=function(e){var t=e.showUI,n=W(r.MagicPayloadMethod.LoginWithMagicLink,[{email:e.email,showUI:void 0===t||t}]);return this.request(n)},t}(z),J=function(e){function t(){return e.apply(this,arguments)||this}a(t,e);var n=t.prototype;return n.getIdToken=function(e){var t=W(r.MagicPayloadMethod.GetIdToken,[e]);return this.request(t)},n.generateIdToken=function(e){var t=W(r.MagicPayloadMethod.GenerateIdToken,[e]);return this.request(t)},n.getMetadata=function(){var e=W(r.MagicPayloadMethod.GetMetadata);return this.request(e)},n.updateEmail=function(e){var t=e.showUI,n=W(r.MagicPayloadMethod.UpdateEmail,[{email:e.email,showUI:void 0===t||t}]);return this.request(n)},n.isLoggedIn=function(){var e=W(r.MagicPayloadMethod.IsLoggedIn);return this.request(e)},n.logout=function(){var e=W(r.MagicPayloadMethod.Logout);return this.request(e)},t}(z),F=q(),Y=F.createBoundEmitterMethod,B=F.createChainingEmitterMethod,X=function(e){function t(){var r;return(r=e.apply(this,arguments)||this).isMagic=!0,r.on=B("on",p(r)),r.once=B("once",p(r)),r.addListener=B("addListener",p(r)),r.off=B("off",p(r)),r.removeListener=B("removeListener",p(r)),r.removeAllListeners=B("removeAllListeners",p(r)),r.emit=Y("emit"),r.eventNames=Y("eventNames"),r.listeners=Y("listeners"),r.listenerCount=Y("listenerCount"),r}a(t,e);var n=t.prototype;return n.sendAsync=function(e,t){if(!t)throw R({procedure:"Magic.rpcProvider.sendAsync",argument:1,expected:"function",received:null===t?"null":typeof t});if(Array.isArray(e))this.transport.post(this.overlay,r.MagicOutgoingWindowMessage.MAGIC_HANDLE_REQUEST,e.map(function(e){return T(e)})).then(function(e){t(null,e.map(function(e){return i({},e.payload,{error:e.hasError?new P(e.payload.error):null})}))});else{var n=T(e);this.transport.post(this.overlay,r.MagicOutgoingWindowMessage.MAGIC_HANDLE_REQUEST,n).then(function(e){t(e.hasError?new P(e.payload.error):null,e.payload)})}},n.send=function(e,r){if("string"==typeof e){var t=W(e,Array.isArray(r)?r:[]);return this.request(t)}if(!Array.isArray(e)&&!r){var n=O();return n.log(),new K(e).applyError({code:-32603,message:n.rawMessage}).payload}this.sendAsync(e,r)},n.enable=function(){var e=W("eth_accounts");return this.request(e)},t}(z);function Z(e,r){return r?new URL(e,r):new URL(e)}var $=function(e){function r(){var r;(r=e.call(this,void 0)||this).isInitialized=!1,r.utils={createPromiEvent:U,decodeQueryParameters:h,encodeQueryParameters:f,createJsonRpcRequestPayload:W,standardizeJsonRpcRequestPayload:T};var t=["request","transport","overlay","sdk"];return new Proxy(p(r),{get:function(e,n,o){if(t.includes(n)&&!r.isInitialized)throw I(n);return Reflect.get(e,n,o)}})||p(r)}a(r,e);var t=r.prototype;return t.init=function(e){this.isInitialized||(this.sdk=e,this.isInitialized=!0)},t.createError=function(e,r,t){return new w(this,e,r,t)},t.raiseError=function(e,r,t){throw new w(this,e,r,t)},r}(z),ee=function(e){function r(){return e.apply(this,arguments)||this}return a(r,e),r}($),re=function(e){function r(){return e.apply(this,arguments)||this}return a(r,e),r}($);re.Internal=ee;var te=function(){function e(e,r){var t,n,o=this;if(this.apiKey=e,!e)throw A();"react-native"===E.target&&(null==r?void 0:r.endpoint)&&S().log();var i=E.version;this.endpoint=Z(null!=(t=null==r?void 0:r.endpoint)?t:E.defaultEndpoint).origin,this.auth=new V(this),this.user=new J(this),this.rpcProvider=new X(this);var a=null!=(n=null==r?void 0:r.extensions)?n:[],s={};Array.isArray(a)?a.forEach(function(e){e.init(o),o[e.name]=e,e instanceof re.Internal&&(_(e.config)||(s[e.name]=e.config))}):Object.keys(a).forEach(function(e){a[e].init(o);var r=a[e];o[e]=r,r instanceof re.Internal&&(_(r.config)||(s[a[e].name]=r.config))}),this.encodedQueryParams=f({API_KEY:this.apiKey,DOMAIN_ORIGIN:window.location?window.location.origin:"",ETH_NETWORK:null==r?void 0:r.network,host:Z(this.endpoint).host,sdk:E.sdkName,version:i,ext:_(s)?void 0:s})}return e.prototype.preload=function(){try{return Promise.resolve(this.overlay.ready).then(function(){})}catch(e){return Promise.reject(e)}},o(e,[{key:"transport",get:function(){return e.__transports__.has(this.encodedQueryParams)||e.__transports__.set(this.encodedQueryParams,new E.PayloadTransport(this.endpoint,this.encodedQueryParams)),e.__transports__.get(this.encodedQueryParams)}},{key:"overlay",get:function(){if(!e.__overlays__.has(this.encodedQueryParams)){var r=new E.ViewController(this.transport,this.endpoint,this.encodedQueryParams);e.__overlays__.set(this.encodedQueryParams,r)}return e.__overlays__.get(this.encodedQueryParams)}}]),e}();te.__transports__=new Map,te.__overlays__=new Map;var ne=function(){function e(e,r){this.endpoint=e,this.encodedQueryParams=r,this.messageHandlers=new Set,this.init()}var t=e.prototype;return t.post=function(e,t,n){try{var o=this;return Promise.resolve(H(function(i){try{return Promise.resolve(e.ready).then(function(){var a=[],s=Array.isArray(n)?n.map(function(e){return e.id}):[];return Promise.resolve(e.postMessage({msgType:t+"-"+o.encodedQueryParams,payload:n})).then(function(){var e,t=o.on(r.MagicIncomingWindowMessage.MAGIC_HANDLE_RESPONSE,(e=function(){return t()},function(r){var t=function(e,r){var t,n=null===(t=r.data.response)||void 0===t?void 0:t.id,o=function(e,r){return r&&Array.isArray(e)?e.find(function(e){return e.id===r}):e}(e,n);return n&&o?{id:n,response:new K(o).applyResult(r.data.response.result).applyError(r.data.response.error)}:{}}(n,r),o=t.id,u=t.response;o&&u&&Array.isArray(n)&&s.includes(o)?(a.push(u),a.length===n.length&&(e(),i(a))):o&&u&&!Array.isArray(n)&&o===n.id&&(e(),i(u))}))})})}catch(e){return Promise.reject(e)}}))}catch(e){return Promise.reject(e)}},t.on=function(e,r){var t=this,n=r.bind(window),o=function(r){r.data.msgType===e+"-"+t.encodedQueryParams&&n(r)};return this.messageHandlers.add(o),function(){return t.messageHandlers.delete(o)}},e}(),oe=function(){function e(e,r,t){this.transport=e,this.endpoint=r,this.encodedQueryParams=t,this.ready=this.waitForReady(),this.init&&this.init(),this.listen()}var t=e.prototype;return t.waitForReady=function(){var e=this;return new Promise(function(t){e.transport.on(r.MagicIncomingWindowMessage.MAGIC_OVERLAY_READY,function(){return t()})})},t.listen=function(){var e=this;this.transport.on(r.MagicIncomingWindowMessage.MAGIC_HIDE_OVERLAY,function(){e.hideOverlay()}),this.transport.on(r.MagicIncomingWindowMessage.MAGIC_SHOW_OVERLAY,function(){e.showOverlay()})},e}();exports.Extension=re,exports.MagicExtensionError=w,exports.MagicRPCError=P,exports.MagicSDKError=M,exports.MagicSDKWarning=b,exports.PayloadTransport=ne,exports.SDKBase=te,exports.ViewController=oe,exports.createAutoCatchingPromise=H,exports.createDuplicateIframeWarning=function(){return new b(r.SDKWarningCode.DuplicateIframe,"Duplicate iframes found.")},exports.createExtensionNotInitializedError=I,exports.createInvalidArgumentError=R,exports.createMalformedResponseError=x,exports.createMissingApiKeyError=A,exports.createModalNotReadyError=function(){return new M(r.SDKErrorCode.ModalNotReady,"Modal is not ready.")},exports.createPromiEvent=U,exports.createReactNativeEndpointConfigurationWarning=S,exports.createSDK=function(e,r){return Object.assign(E,r),e},exports.createSynchronousWeb3MethodWarning=O,exports.createURL=Z,exports.decodeQueryParameters=h,exports.encodeQueryParameters=f,exports.getPayloadId=N,exports.isEmpty=_,exports.isJsonRpcErrorCode=m,exports.isJsonRpcRequestPayload=function(e){return!v(e)&&!(y(e.jsonrpc)||y(e.id)||y(e.method)||y(e.params))},exports.isJsonRpcResponsePayload=g,exports.isMagicPayloadMethod=function(e){return!v(e)&&"string"==typeof e&&Object.values(r.MagicPayloadMethod).includes(e)},exports.isPromiEvent=function(e){return!!e[G]}; | ||
var e,r=require("@magic-sdk/types"),t=(e=require("eventemitter3"))&&"object"==typeof e&&"default"in e?e.default:e;function n(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function o(e,r,t){return r&&n(e.prototype,r),t&&n(e,t),e}function i(){return(i=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}function a(e,r){e.prototype=Object.create(r.prototype),e.prototype.constructor=e,e.__proto__=r}function s(e){return(s=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function u(e,r){return(u=Object.setPrototypeOf||function(e,r){return e.__proto__=r,e})(e,r)}function c(){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(e){return!1}}function d(e,r,t){return(d=c()?Reflect.construct:function(e,r,t){var n=[null];n.push.apply(n,r);var o=new(Function.bind.apply(e,n));return t&&u(o,t.prototype),o}).apply(null,arguments)}function l(e){var r="function"==typeof Map?new Map:void 0;return(l=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(e))return r.get(e);r.set(e,t)}function t(){return d(e,arguments,s(this).constructor)}return t.prototype=Object.create(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),u(t,e)})(e)}function p(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function f(e){return void 0===e}function h(e){return function(e){return null===e}(e)||f(e)}function y(e){return!h(e)&&!(f(e.jsonrpc)||f(e.id)||f(e.result)&&f(e.error))}function v(e){return!h(e)&&"number"==typeof e&&Object.values(r.RPCErrorCode).includes(e)}function g(e){if(!e)return!0;for(var r in e)if(Object.hasOwnProperty.call(e,r))return!1;return!0}var m={},E=function(e){function r(t,n){var o;return(o=e.call(this,"Magic SDK Error: ["+t+"] "+n)||this).code=t,o.rawMessage=n,o.__proto__=Error,Object.setPrototypeOf(p(o),r.prototype),o}return a(r,e),r}(l(Error)),_=function(e){function r(t,n,o,i){var a;return(a=e.call(this,"Magic Extension Error ("+t.name+"): ["+n+"] "+o)||this).code=n,a.rawMessage=o,a.data=i,a.__proto__=Error,Object.setPrototypeOf(p(a),r.prototype),a}return a(r,e),r}(l(Error)),M=function(){function e(e,r,t){this.code=r,this.rawMessage=t,this.message="Magic Extension Error ("+e.name+"): ["+r+"] "+t}return e.prototype.log=function(){console.warn(this.message)},e}(),w=function(e){function t(n){var o;(o=e.call(this)||this).__proto__=Error;var i=Number(null==n?void 0:n.code);return o.rawMessage=(null==n?void 0:n.message)||"Internal error",o.code=v(i)?i:r.RPCErrorCode.InternalError,o.message="Magic RPC Error: ["+o.code+"] "+o.rawMessage,Object.setPrototypeOf(p(o),t.prototype),o}return a(t,e),t}(l(Error)),P=function(){function e(e,r){this.code=e,this.rawMessage=r,this.message="Magic SDK Warning: ["+e+"] "+r}return e.prototype.log=function(){console.warn(this.message)},e}();function b(){return new E(r.SDKErrorCode.MissingApiKey,"Please provide an API key that you acquired from the Magic developer dashboard.")}function x(){return new E(r.SDKErrorCode.MalformedResponse,"Response from the Magic iframe is malformed.")}function O(e){return new E(r.SDKErrorCode.ExtensionNotInitialized,"Extensions must be initialized with a Magic SDK instance before `Extension."+e+"` can be accessed. Do not invoke `Extension."+e+"` inside an extension constructor.")}function I(e){var t,n,o;return new E(r.SDKErrorCode.InvalidArgument,"Invalid "+(o=(t=e.argument+1)%100,(1==(n=t%10)&&11!==o?t+"st":2===n&&12!==o?t+"nd":3===n&&13!==o?t+"rd":t+"th")+" argument given to `")+e.procedure+"`.\n Expected: `"+e.expected+"`\n Received: `"+e.received+"`")}function A(){return new P(r.SDKWarningCode.SyncWeb3Method,"Non-async web3 methods are deprecated in web3 > 1.0 and are not supported by the Magic web3 provider. Please use an async method instead.")}function R(){return new P(r.SDKWarningCode.ReactNativeEndpointConfiguration,"CUSTOM DOMAINS ARE NOT SUPPORTED WHEN USING MAGIC SDK WITH REACT NATIVE! The `endpoint` parameter SHOULD NOT be provided. The Magic `<iframe>` is automatically wrapped by a WebView pointed at `"+m.defaultEndpoint+"`. Changing this default behavior will lead to unexpected results and potentially security-threatening bugs.")}function S(e){var t=e.useInstead,n={"magic-sdk":"magic-sdk","magic-sdk-rn":"@magic-sdk/react-native"}[m.sdkName];return new P(r.SDKWarningCode.DeprecationNotice,"`"+e.method+"` will be removed from `"+n+"` in version `"+e.removalVersions[n]+"`."+(t?" Use `"+t+"` instead.":""))}function N(e){return btoa(JSON.stringify(e))}function C(e){return JSON.parse(atob(e))}function k(e){return S({method:"encodeQueryParameters()",removalVersions:{"magic-sdk":"v3.0.0","@magic-sdk/react-native":"v3.0.0"},useInstead:"encodeJSON()"}).log(),btoa(JSON.stringify(e))}function D(e){return S({method:"decodeQueryParameters()",removalVersions:{"magic-sdk":"v3.0.0","@magic-sdk/react-native":"v3.0.0"},useInstead:"decodeJSON()"}).log(),JSON.parse(atob(e))}var j=regeneratorRuntime.mark(L);function L(){var e;return regeneratorRuntime.wrap(function(r){for(;;)switch(r.prev=r.next){case 0:e=0;case 1:if(!(e<Number.MAX_SAFE_INTEGER)){r.next=7;break}return r.next=5,++e;case 5:r.next=8;break;case 7:e=0;case 8:r.next=1;break;case 10:case"end":return r.stop()}},j)}var W=L();function T(){return W.next().value}var K=Symbol("Payload pre-processed by Magic SDK");function Q(e){return Object.defineProperty(e,K,{value:!0,enumerable:!1}),e}function q(e){var r,t,n;return function(e){return!!e[K]}(e)||(e.jsonrpc=null!=(r=e.jsonrpc)?r:"2.0",e.id=T(),e.method=null!=(t=e.method)?t:"noop",e.params=null!=(n=e.params)?n:[],Q(e)),e}function U(e,r){return void 0===r&&(r=[]),Q({params:r,method:e,jsonrpc:"2.0",id:T()})}var G=function(){function e(r){r instanceof e?(this._jsonrpc=r.payload.jsonrpc,this._id=r.payload.id,this._result=r.payload.result,this._error=r.payload.error):y(r)?(this._jsonrpc=r.jsonrpc,this._id=r.id,this._result=r.result,this._error=r.error):(this._jsonrpc=r.jsonrpc,this._id=r.id,this._result=void 0,this._error=void 0)}var r=e.prototype;return r.applyError=function(e){return this._error=e,this},r.applyResult=function(e){return this._result=e,this},o(e,[{key:"hasError",get:function(){return null!=this._error}},{key:"hasResult",get:function(){return void 0!==this._result}},{key:"payload",get:function(){return{jsonrpc:this._jsonrpc,id:this._id,result:this._result,error:this._error}}}]),e}(),J=function(e){function r(){return e.apply(this,arguments)||this}return a(r,e),r}(t);function H(){var e=new J;return{emitter:e,createChainingEmitterMethod:function(r,t){return function(){return e[r].apply(e,[].slice.call(arguments)),t}},createBoundEmitterMethod:function(r){return function(){return e[r].apply(e,[].slice.call(arguments))}}}}var V=Symbol("isPromiEvent");function z(e){var r=F(e),t=H(),n=t.createBoundEmitterMethod,o=t.createChainingEmitterMethod,i=Symbol("Promise.then"),a=Symbol("Promise.catch"),s=Symbol("Promise.finally"),u=function(e,r){return function(){var t=r[e].apply(r,[].slice.call(arguments));return c(t)}},c=function(e){var r;return Object.assign(e,((r={})[V]=!0,r[i]=e[i]||e.then,r[a]=e[a]||e.catch,r[s]=e[s]||e.finally,r.then=u(i,e),r.catch=u(a,e),r.finally=u(s,e),r.on=o("on",e),r.once=o("once",e),r.addListener=o("addListener",e),r.off=o("off",e),r.removeListener=o("removeListener",e),r.removeAllListeners=o("removeAllListeners",e),r.emit=n("emit"),r.eventNames=n("eventNames"),r.listeners=n("listeners"),r.listenerCount=n("listenerCount"),r))},d=c(r.then(function(e){return d.emit("done",e),d.emit("settled"),e},function(e){throw d.emit("error",e),d.emit("settled"),e}));return d}function F(e){return new Promise(function(r,t){var n=e(r,t);Promise.resolve(n).catch(t)})}var Y=function(){function e(e){this.sdk=e}return e.prototype.request=function(e){var t=this.transport.post(this.overlay,r.MagicOutgoingWindowMessage.MAGIC_HANDLE_REQUEST,q(e)),n=z(function(e,r){t.then(function(t){if(o(),t.hasError)r(new w(t.payload.error));else{if(!t.hasResult)throw x();e(t.payload.result)}}).catch(function(e){o(),r(e)})}),o=this.transport.on(r.MagicIncomingWindowMessage.MAGIC_HANDLE_EVENT,function(r){var t,o=r.data.response;if(o.id===e.id&&(null===(t=o.result)||void 0===t?void 0:t.event)){var i=o.result,a=i.params;n.emit.apply(n,[i.event].concat(void 0===a?[]:a))}});return n},o(e,[{key:"transport",get:function(){return this.sdk.transport}},{key:"overlay",get:function(){return this.sdk.overlay}}]),e}(),B=function(e){function t(){return e.apply(this,arguments)||this}return a(t,e),t.prototype.loginWithMagicLink=function(e){var t=e.showUI,n=U(r.MagicPayloadMethod.LoginWithMagicLink,[{email:e.email,showUI:void 0===t||t}]);return this.request(n)},t}(Y),X=function(e){function t(){return e.apply(this,arguments)||this}a(t,e);var n=t.prototype;return n.getIdToken=function(e){var t=U(r.MagicPayloadMethod.GetIdToken,[e]);return this.request(t)},n.generateIdToken=function(e){var t=U(r.MagicPayloadMethod.GenerateIdToken,[e]);return this.request(t)},n.getMetadata=function(){var e=U(r.MagicPayloadMethod.GetMetadata);return this.request(e)},n.updateEmail=function(e){var t=e.showUI,n=U(r.MagicPayloadMethod.UpdateEmail,[{email:e.email,showUI:void 0===t||t}]);return this.request(n)},n.isLoggedIn=function(){var e=U(r.MagicPayloadMethod.IsLoggedIn);return this.request(e)},n.logout=function(){var e=U(r.MagicPayloadMethod.Logout);return this.request(e)},t}(Y),Z=H(),$=Z.createBoundEmitterMethod,ee=Z.createChainingEmitterMethod,re=function(e){function t(){var r;return(r=e.apply(this,arguments)||this).isMagic=!0,r.on=ee("on",p(r)),r.once=ee("once",p(r)),r.addListener=ee("addListener",p(r)),r.off=ee("off",p(r)),r.removeListener=ee("removeListener",p(r)),r.removeAllListeners=ee("removeAllListeners",p(r)),r.emit=$("emit"),r.eventNames=$("eventNames"),r.listeners=$("listeners"),r.listenerCount=$("listenerCount"),r}a(t,e);var n=t.prototype;return n.sendAsync=function(e,t){if(!t)throw I({procedure:"Magic.rpcProvider.sendAsync",argument:1,expected:"function",received:null===t?"null":typeof t});if(Array.isArray(e))this.transport.post(this.overlay,r.MagicOutgoingWindowMessage.MAGIC_HANDLE_REQUEST,e.map(function(e){return q(e)})).then(function(e){t(null,e.map(function(e){return i({},e.payload,{error:e.hasError?new w(e.payload.error):null})}))});else{var n=q(e);this.transport.post(this.overlay,r.MagicOutgoingWindowMessage.MAGIC_HANDLE_REQUEST,n).then(function(e){t(e.hasError?new w(e.payload.error):null,e.payload)})}},n.send=function(e,r){if("string"==typeof e){var t=U(e,Array.isArray(r)?r:[]);return this.request(t)}if(!Array.isArray(e)&&!r){var n=A();return n.log(),new G(e).applyError({code:-32603,message:n.rawMessage}).payload}this.sendAsync(e,r)},n.enable=function(){var e=U("eth_accounts");return this.request(e)},t}(Y);function te(e,r){return r?new URL(e,r):new URL(e)}var ne=function(e){function r(){var r;(r=e.call(this,void 0)||this).isInitialized=!1,r.utils={createPromiEvent:z,encodeJSON:N,decodeJSON:C,encodeQueryParameters:k,decodeQueryParameters:D,createJsonRpcRequestPayload:U,standardizeJsonRpcRequestPayload:q};var t=["request","transport","overlay","sdk"];return new Proxy(p(r),{get:function(e,n,o){if(t.includes(n)&&!r.isInitialized)throw O(n);return Reflect.get(e,n,o)}})||p(r)}a(r,e);var t=r.prototype;return t.init=function(e){this.isInitialized||(this.sdk=e,this.isInitialized=!0)},t.createDeprecationWarning=function(e){var r=e.useInstead;return new M(this,"DEPRECATION_NOTICE","`"+e.method+"` will be removed from this Extension in version `"+e.removalVersion+"`."+(r?" Use `"+r+"` instead.":""))},t.createWarning=function(e,r){return new M(this,e,r)},t.createError=function(e,r,t){return new _(this,e,r,t)},t.raiseError=function(e,r,t){throw new _(this,e,r,t)},r}(Y),oe=function(e){function r(){return e.apply(this,arguments)||this}return a(r,e),r}(ne),ie=function(e){function r(){return e.apply(this,arguments)||this}return a(r,e),r}(ne);ie.Internal=oe;var ae=function(){function e(e,r){var t,n,o=this;if(this.apiKey=e,!e)throw b();"react-native"===m.target&&(null==r?void 0:r.endpoint)&&R().log();var i=m.version;this.endpoint=te(null!=(t=null==r?void 0:r.endpoint)?t:m.defaultEndpoint).origin,this.auth=new B(this),this.user=new X(this),this.rpcProvider=new re(this);var a=null!=(n=null==r?void 0:r.extensions)?n:[],s={};Array.isArray(a)?a.forEach(function(e){e.init(o),o[e.name]=e,e instanceof ie.Internal&&(g(e.config)||(s[e.name]=e.config))}):Object.keys(a).forEach(function(e){a[e].init(o);var r=a[e];o[e]=r,r instanceof ie.Internal&&(g(r.config)||(s[a[e].name]=r.config))}),this.encodedQueryParams=N({API_KEY:this.apiKey,DOMAIN_ORIGIN:window.location?window.location.origin:"",ETH_NETWORK:null==r?void 0:r.network,host:te(this.endpoint).host,sdk:m.sdkName,version:i,ext:g(s)?void 0:s})}return e.prototype.preload=function(){try{return Promise.resolve(this.overlay.ready).then(function(){})}catch(e){return Promise.reject(e)}},o(e,[{key:"transport",get:function(){return e.__transports__.has(this.encodedQueryParams)||e.__transports__.set(this.encodedQueryParams,new m.PayloadTransport(this.endpoint,this.encodedQueryParams)),e.__transports__.get(this.encodedQueryParams)}},{key:"overlay",get:function(){if(!e.__overlays__.has(this.encodedQueryParams)){var r=new m.ViewController(this.transport,this.endpoint,this.encodedQueryParams);e.__overlays__.set(this.encodedQueryParams,r)}return e.__overlays__.get(this.encodedQueryParams)}}]),e}();ae.__transports__=new Map,ae.__overlays__=new Map;var se=function(){function e(e,r){this.endpoint=e,this.encodedQueryParams=r,this.messageHandlers=new Set,this.init()}var t=e.prototype;return t.post=function(e,t,n){try{var o=this;return Promise.resolve(F(function(i){try{return Promise.resolve(e.ready).then(function(){var a=[],s=Array.isArray(n)?n.map(function(e){return e.id}):[];return Promise.resolve(e.postMessage({msgType:t+"-"+o.encodedQueryParams,payload:n})).then(function(){var e,t=o.on(r.MagicIncomingWindowMessage.MAGIC_HANDLE_RESPONSE,(e=function(){return t()},function(r){var t=function(e,r){var t,n=null===(t=r.data.response)||void 0===t?void 0:t.id,o=function(e,r){return r&&Array.isArray(e)?e.find(function(e){return e.id===r}):e}(e,n);return n&&o?{id:n,response:new G(o).applyResult(r.data.response.result).applyError(r.data.response.error)}:{}}(n,r),o=t.id,u=t.response;o&&u&&Array.isArray(n)&&s.includes(o)?(a.push(u),a.length===n.length&&(e(),i(a))):o&&u&&!Array.isArray(n)&&o===n.id&&(e(),i(u))}))})})}catch(e){return Promise.reject(e)}}))}catch(e){return Promise.reject(e)}},t.on=function(e,r){var t=this,n=r.bind(window),o=function(r){r.data.msgType===e+"-"+t.encodedQueryParams&&n(r)};return this.messageHandlers.add(o),function(){return t.messageHandlers.delete(o)}},e}(),ue=function(){function e(e,r,t){this.transport=e,this.endpoint=r,this.encodedQueryParams=t,this.ready=this.waitForReady(),this.init&&this.init(),this.listen()}var t=e.prototype;return t.waitForReady=function(){var e=this;return new Promise(function(t){e.transport.on(r.MagicIncomingWindowMessage.MAGIC_OVERLAY_READY,function(){return t()})})},t.listen=function(){var e=this;this.transport.on(r.MagicIncomingWindowMessage.MAGIC_HIDE_OVERLAY,function(){e.hideOverlay()}),this.transport.on(r.MagicIncomingWindowMessage.MAGIC_SHOW_OVERLAY,function(){e.showOverlay()})},e}();exports.Extension=ie,exports.MagicExtensionError=_,exports.MagicExtensionWarning=M,exports.MagicRPCError=w,exports.MagicSDKError=E,exports.MagicSDKWarning=P,exports.PayloadTransport=se,exports.SDKBase=ae,exports.ViewController=ue,exports.createAutoCatchingPromise=F,exports.createDeprecationWarning=S,exports.createDuplicateIframeWarning=function(){return new P(r.SDKWarningCode.DuplicateIframe,"Duplicate iframes found.")},exports.createExtensionNotInitializedError=O,exports.createInvalidArgumentError=I,exports.createMalformedResponseError=x,exports.createMissingApiKeyError=b,exports.createModalNotReadyError=function(){return new E(r.SDKErrorCode.ModalNotReady,"Modal is not ready.")},exports.createPromiEvent=z,exports.createReactNativeEndpointConfigurationWarning=R,exports.createSDK=function(e,r){return Object.assign(m,r),e},exports.createSynchronousWeb3MethodWarning=A,exports.createURL=te,exports.decodeJSON=C,exports.decodeQueryParameters=D,exports.encodeJSON=N,exports.encodeQueryParameters=k,exports.getPayloadId=T,exports.isEmpty=g,exports.isJsonRpcErrorCode=v,exports.isJsonRpcRequestPayload=function(e){return!h(e)&&!(f(e.jsonrpc)||f(e.id)||f(e.method)||f(e.params))},exports.isJsonRpcResponsePayload=y,exports.isMagicPayloadMethod=function(e){return!h(e)&&"string"==typeof e&&Object.values(r.MagicPayloadMethod).includes(e)},exports.isPromiEvent=function(e){return!!e[V]}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import{MagicPayloadMethod as e,RPCErrorCode as t,SDKErrorCode as r,SDKWarningCode as n,MagicOutgoingWindowMessage as o,MagicIncomingWindowMessage as i}from"@magic-sdk/types";import a from"eventemitter3";function s(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function u(e,t,r){return t&&s(e.prototype,t),r&&s(e,r),e}function c(){return(c=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function l(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}function d(e){return(d=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function f(e,t){return(f=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function p(){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(e){return!1}}function h(e,t,r){return(h=p()?Reflect.construct:function(e,t,r){var n=[null];n.push.apply(n,t);var o=new(Function.bind.apply(e,n));return r&&f(o,r.prototype),o}).apply(null,arguments)}function y(e){var t="function"==typeof Map?new Map:void 0;return(y=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return h(e,arguments,d(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),f(r,e)})(e)}function v(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function m(e){return btoa(JSON.stringify(e))}function g(e){return JSON.parse(atob(e))}function _(e){return void 0===e}function w(e){return function(e){return null===e}(e)||_(e)}function E(e){return!w(e)&&!(_(e.jsonrpc)||_(e.id)||_(e.method)||_(e.params))}function b(e){return!w(e)&&!(_(e.jsonrpc)||_(e.id)||_(e.result)&&_(e.error))}function P(t){return!w(t)&&"string"==typeof t&&Object.values(e).includes(t)}function A(e){return!w(e)&&"number"==typeof e&&Object.values(t).includes(e)}function M(e){if(!e)return!0;for(var t in e)if(Object.hasOwnProperty.call(e,t))return!1;return!0}var O={};function I(e,t){return Object.assign(O,t),e}var R=function(e){function t(r,n){var o;return(o=e.call(this,"Magic SDK Error: ["+r+"] "+n)||this).code=r,o.rawMessage=n,o.__proto__=Error,Object.setPrototypeOf(v(o),t.prototype),o}return l(t,e),t}(y(Error)),j=function(e){function t(r,n,o,i){var a;return(a=e.call(this,"Magic Extension Error ("+r.name+"): ["+n+"] "+o)||this).code=n,a.rawMessage=o,a.data=i,a.__proto__=Error,Object.setPrototypeOf(v(a),t.prototype),a}return l(t,e),t}(y(Error)),k=function(e){function r(n){var o;(o=e.call(this)||this).__proto__=Error;var i=Number(null==n?void 0:n.code);return o.rawMessage=(null==n?void 0:n.message)||"Internal error",o.code=A(i)?i:t.InternalError,o.message="Magic RPC Error: ["+o.code+"] "+o.rawMessage,Object.setPrototypeOf(v(o),r.prototype),o}return l(r,e),r}(y(Error)),N=function(){function e(e,t){this.code=e,this.rawMessage=t,this.message="Magic SDK Warning: ["+e+"] "+t}return e.prototype.log=function(){console.warn(this.message)},e}();function L(){return new R(r.MissingApiKey,"Please provide an API key that you acquired from the Magic developer dashboard.")}function S(){return new R(r.ModalNotReady,"Modal is not ready.")}function T(){return new R(r.MalformedResponse,"Response from the Magic iframe is malformed.")}function x(e){return new R(r.ExtensionNotInitialized,"Extensions must be initialized with a Magic SDK instance before `Extension."+e+"` can be accessed. Do not invoke `Extension."+e+"` inside an extension constructor.")}function C(e){var t,n,o;return new R(r.InvalidArgument,"Invalid "+(o=(t=e.argument+1)%100,(1==(n=t%10)&&11!==o?t+"st":2===n&&12!==o?t+"nd":3===n&&13!==o?t+"rd":t+"th")+" argument given to `")+e.procedure+"`.\n Expected: `"+e.expected+"`\n Received: `"+e.received+"`")}function D(){return new N(n.DuplicateIframe,"Duplicate iframes found.")}function Q(){return new N(n.SyncWeb3Method,"Non-async web3 methods are deprecated in web3 > 1.0 and are not supported by the Magic web3 provider. Please use an async method instead.")}function G(){return new N(n.ReactNativeEndpointConfiguration,"CUSTOM DOMAINS ARE NOT SUPPORTED WHEN USING MAGIC SDK WITH REACT NATIVE! The `endpoint` parameter SHOULD NOT be provided. The Magic `<iframe>` is automatically wrapped by a WebView pointed at `"+O.defaultEndpoint+"`. Changing this default behavior will lead to unexpected results and potentially security-threatening bugs.")}var q=regeneratorRuntime.mark(H);function H(){var e;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:e=0;case 1:if(!(e<Number.MAX_SAFE_INTEGER)){t.next=7;break}return t.next=5,++e;case 5:t.next=8;break;case 7:e=0;case 8:t.next=1;break;case 10:case"end":return t.stop()}},q)}var U=H();function K(){return U.next().value}var W=Symbol("Payload pre-processed by Magic SDK");function z(e){return Object.defineProperty(e,W,{value:!0,enumerable:!1}),e}function V(e){var t,r,n;return function(e){return!!e[W]}(e)||(e.jsonrpc=null!=(t=e.jsonrpc)?t:"2.0",e.id=K(),e.method=null!=(r=e.method)?r:"noop",e.params=null!=(n=e.params)?n:[],z(e)),e}function F(e,t){return void 0===t&&(t=[]),z({params:t,method:e,jsonrpc:"2.0",id:K()})}var Y=function(){function e(t){t instanceof e?(this._jsonrpc=t.payload.jsonrpc,this._id=t.payload.id,this._result=t.payload.result,this._error=t.payload.error):b(t)?(this._jsonrpc=t.jsonrpc,this._id=t.id,this._result=t.result,this._error=t.error):(this._jsonrpc=t.jsonrpc,this._id=t.id,this._result=void 0,this._error=void 0)}var t=e.prototype;return t.applyError=function(e){return this._error=e,this},t.applyResult=function(e){return this._result=e,this},u(e,[{key:"hasError",get:function(){return null!=this._error}},{key:"hasResult",get:function(){return void 0!==this._result}},{key:"payload",get:function(){return{jsonrpc:this._jsonrpc,id:this._id,result:this._result,error:this._error}}}]),e}(),J=function(e){function t(){return e.apply(this,arguments)||this}return l(t,e),t}(a);function B(){var e=new J;return{emitter:e,createChainingEmitterMethod:function(t,r){return function(){return e[t].apply(e,[].slice.call(arguments)),r}},createBoundEmitterMethod:function(t){return function(){return e[t].apply(e,[].slice.call(arguments))}}}}var X=Symbol("isPromiEvent");function Z(e){return!!e[X]}function $(e){var t=ee(e),r=B(),n=r.createBoundEmitterMethod,o=r.createChainingEmitterMethod,i=Symbol("Promise.then"),a=Symbol("Promise.catch"),s=Symbol("Promise.finally"),u=function(e,t){return function(){var r=t[e].apply(t,[].slice.call(arguments));return c(r)}},c=function(e){var t;return Object.assign(e,((t={})[X]=!0,t[i]=e[i]||e.then,t[a]=e[a]||e.catch,t[s]=e[s]||e.finally,t.then=u(i,e),t.catch=u(a,e),t.finally=u(s,e),t.on=o("on",e),t.once=o("once",e),t.addListener=o("addListener",e),t.off=o("off",e),t.removeListener=o("removeListener",e),t.removeAllListeners=o("removeAllListeners",e),t.emit=n("emit"),t.eventNames=n("eventNames"),t.listeners=n("listeners"),t.listenerCount=n("listenerCount"),t))},l=c(t.then(function(e){return l.emit("done",e),l.emit("settled"),e},function(e){throw l.emit("error",e),l.emit("settled"),e}));return l}function ee(e){return new Promise(function(t,r){var n=e(t,r);Promise.resolve(n).catch(r)})}var te=function(){function e(e){this.sdk=e}return e.prototype.request=function(e){var t=this.transport.post(this.overlay,o.MAGIC_HANDLE_REQUEST,V(e)),r=$(function(e,r){t.then(function(t){if(n(),t.hasError)r(new k(t.payload.error));else{if(!t.hasResult)throw T();e(t.payload.result)}}).catch(function(e){n(),r(e)})}),n=this.transport.on(i.MAGIC_HANDLE_EVENT,function(t){var n,o=t.data.response;if(o.id===e.id&&(null===(n=o.result)||void 0===n?void 0:n.event)){var i=o.result,a=i.params;r.emit.apply(r,[i.event].concat(void 0===a?[]:a))}});return r},u(e,[{key:"transport",get:function(){return this.sdk.transport}},{key:"overlay",get:function(){return this.sdk.overlay}}]),e}(),re=function(t){function r(){return t.apply(this,arguments)||this}return l(r,t),r.prototype.loginWithMagicLink=function(t){var r=t.showUI,n=F(e.LoginWithMagicLink,[{email:t.email,showUI:void 0===r||r}]);return this.request(n)},r}(te),ne=function(t){function r(){return t.apply(this,arguments)||this}l(r,t);var n=r.prototype;return n.getIdToken=function(t){var r=F(e.GetIdToken,[t]);return this.request(r)},n.generateIdToken=function(t){var r=F(e.GenerateIdToken,[t]);return this.request(r)},n.getMetadata=function(){var t=F(e.GetMetadata);return this.request(t)},n.updateEmail=function(t){var r=t.showUI,n=F(e.UpdateEmail,[{email:t.email,showUI:void 0===r||r}]);return this.request(n)},n.isLoggedIn=function(){var t=F(e.IsLoggedIn);return this.request(t)},n.logout=function(){var t=F(e.Logout);return this.request(t)},r}(te),oe=B(),ie=oe.createBoundEmitterMethod,ae=oe.createChainingEmitterMethod,se=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).isMagic=!0,t.on=ae("on",v(t)),t.once=ae("once",v(t)),t.addListener=ae("addListener",v(t)),t.off=ae("off",v(t)),t.removeListener=ae("removeListener",v(t)),t.removeAllListeners=ae("removeAllListeners",v(t)),t.emit=ie("emit"),t.eventNames=ie("eventNames"),t.listeners=ie("listeners"),t.listenerCount=ie("listenerCount"),t}l(t,e);var r=t.prototype;return r.sendAsync=function(e,t){if(!t)throw C({procedure:"Magic.rpcProvider.sendAsync",argument:1,expected:"function",received:null===t?"null":typeof t});if(Array.isArray(e))this.transport.post(this.overlay,o.MAGIC_HANDLE_REQUEST,e.map(function(e){return V(e)})).then(function(e){t(null,e.map(function(e){return c({},e.payload,{error:e.hasError?new k(e.payload.error):null})}))});else{var r=V(e);this.transport.post(this.overlay,o.MAGIC_HANDLE_REQUEST,r).then(function(e){t(e.hasError?new k(e.payload.error):null,e.payload)})}},r.send=function(e,t){if("string"==typeof e){var r=F(e,Array.isArray(t)?t:[]);return this.request(r)}if(!Array.isArray(e)&&!t){var n=Q();return n.log(),new Y(e).applyError({code:-32603,message:n.rawMessage}).payload}this.sendAsync(e,t)},r.enable=function(){var e=F("eth_accounts");return this.request(e)},t}(te);function ue(e,t){return t?new URL(e,t):new URL(e)}var ce=function(e){function t(){var t;(t=e.call(this,void 0)||this).isInitialized=!1,t.utils={createPromiEvent:$,decodeQueryParameters:g,encodeQueryParameters:m,createJsonRpcRequestPayload:F,standardizeJsonRpcRequestPayload:V};var r=["request","transport","overlay","sdk"];return new Proxy(v(t),{get:function(e,n,o){if(r.includes(n)&&!t.isInitialized)throw x(n);return Reflect.get(e,n,o)}})||v(t)}l(t,e);var r=t.prototype;return r.init=function(e){this.isInitialized||(this.sdk=e,this.isInitialized=!0)},r.createError=function(e,t,r){return new j(this,e,t,r)},r.raiseError=function(e,t,r){throw new j(this,e,t,r)},t}(te),le=function(e){function t(){return e.apply(this,arguments)||this}return l(t,e),t}(ce),de=function(e){function t(){return e.apply(this,arguments)||this}return l(t,e),t}(ce);de.Internal=le;var fe=function(){function e(e,t){var r,n,o=this;if(this.apiKey=e,!e)throw L();"react-native"===O.target&&(null==t?void 0:t.endpoint)&&G().log();var i=O.version;this.endpoint=ue(null!=(r=null==t?void 0:t.endpoint)?r:O.defaultEndpoint).origin,this.auth=new re(this),this.user=new ne(this),this.rpcProvider=new se(this);var a=null!=(n=null==t?void 0:t.extensions)?n:[],s={};Array.isArray(a)?a.forEach(function(e){e.init(o),o[e.name]=e,e instanceof de.Internal&&(M(e.config)||(s[e.name]=e.config))}):Object.keys(a).forEach(function(e){a[e].init(o);var t=a[e];o[e]=t,t instanceof de.Internal&&(M(t.config)||(s[a[e].name]=t.config))}),this.encodedQueryParams=m({API_KEY:this.apiKey,DOMAIN_ORIGIN:window.location?window.location.origin:"",ETH_NETWORK:null==t?void 0:t.network,host:ue(this.endpoint).host,sdk:O.sdkName,version:i,ext:M(s)?void 0:s})}return e.prototype.preload=function(){try{return Promise.resolve(this.overlay.ready).then(function(){})}catch(e){return Promise.reject(e)}},u(e,[{key:"transport",get:function(){return e.__transports__.has(this.encodedQueryParams)||e.__transports__.set(this.encodedQueryParams,new O.PayloadTransport(this.endpoint,this.encodedQueryParams)),e.__transports__.get(this.encodedQueryParams)}},{key:"overlay",get:function(){if(!e.__overlays__.has(this.encodedQueryParams)){var t=new O.ViewController(this.transport,this.endpoint,this.encodedQueryParams);e.__overlays__.set(this.encodedQueryParams,t)}return e.__overlays__.get(this.encodedQueryParams)}}]),e}();fe.__transports__=new Map,fe.__overlays__=new Map;var pe=function(){function e(e,t){this.endpoint=e,this.encodedQueryParams=t,this.messageHandlers=new Set,this.init()}var t=e.prototype;return t.post=function(e,t,r){try{var n=this;return Promise.resolve(ee(function(o){try{return Promise.resolve(e.ready).then(function(){var a=[],s=Array.isArray(r)?r.map(function(e){return e.id}):[];return Promise.resolve(e.postMessage({msgType:t+"-"+n.encodedQueryParams,payload:r})).then(function(){var e,t=n.on(i.MAGIC_HANDLE_RESPONSE,(e=function(){return t()},function(t){var n=function(e,t){var r,n=null===(r=t.data.response)||void 0===r?void 0:r.id,o=function(e,t){return t&&Array.isArray(e)?e.find(function(e){return e.id===t}):e}(e,n);return n&&o?{id:n,response:new Y(o).applyResult(t.data.response.result).applyError(t.data.response.error)}:{}}(r,t),i=n.id,u=n.response;i&&u&&Array.isArray(r)&&s.includes(i)?(a.push(u),a.length===r.length&&(e(),o(a))):i&&u&&!Array.isArray(r)&&i===r.id&&(e(),o(u))}))})})}catch(e){return Promise.reject(e)}}))}catch(e){return Promise.reject(e)}},t.on=function(e,t){var r=this,n=t.bind(window),o=function(t){t.data.msgType===e+"-"+r.encodedQueryParams&&n(t)};return this.messageHandlers.add(o),function(){return r.messageHandlers.delete(o)}},e}(),he=function(){function e(e,t,r){this.transport=e,this.endpoint=t,this.encodedQueryParams=r,this.ready=this.waitForReady(),this.init&&this.init(),this.listen()}var t=e.prototype;return t.waitForReady=function(){var e=this;return new Promise(function(t){e.transport.on(i.MAGIC_OVERLAY_READY,function(){return t()})})},t.listen=function(){var e=this;this.transport.on(i.MAGIC_HIDE_OVERLAY,function(){e.hideOverlay()}),this.transport.on(i.MAGIC_SHOW_OVERLAY,function(){e.showOverlay()})},e}();export{de as Extension,j as MagicExtensionError,k as MagicRPCError,R as MagicSDKError,N as MagicSDKWarning,pe as PayloadTransport,fe as SDKBase,he as ViewController,ee as createAutoCatchingPromise,D as createDuplicateIframeWarning,x as createExtensionNotInitializedError,C as createInvalidArgumentError,T as createMalformedResponseError,L as createMissingApiKeyError,S as createModalNotReadyError,$ as createPromiEvent,G as createReactNativeEndpointConfigurationWarning,I as createSDK,Q as createSynchronousWeb3MethodWarning,ue as createURL,g as decodeQueryParameters,m as encodeQueryParameters,K as getPayloadId,M as isEmpty,A as isJsonRpcErrorCode,E as isJsonRpcRequestPayload,b as isJsonRpcResponsePayload,P as isMagicPayloadMethod,Z as isPromiEvent}; | ||
import{MagicPayloadMethod as e,RPCErrorCode as t,SDKErrorCode as r,SDKWarningCode as n,MagicOutgoingWindowMessage as o,MagicIncomingWindowMessage as i}from"@magic-sdk/types";import a from"eventemitter3";function s(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function u(e,t,r){return t&&s(e.prototype,t),r&&s(e,r),e}function c(){return(c=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function d(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}function l(e){return(l=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function f(e,t){return(f=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function p(){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(e){return!1}}function h(e,t,r){return(h=p()?Reflect.construct:function(e,t,r){var n=[null];n.push.apply(n,t);var o=new(Function.bind.apply(e,n));return r&&f(o,r.prototype),o}).apply(null,arguments)}function y(e){var t="function"==typeof Map?new Map:void 0;return(y=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return h(e,arguments,l(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),f(r,e)})(e)}function v(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function m(e){return void 0===e}function g(e){return function(e){return null===e}(e)||m(e)}function _(e){return!g(e)&&!(m(e.jsonrpc)||m(e.id)||m(e.method)||m(e.params))}function w(e){return!g(e)&&!(m(e.jsonrpc)||m(e.id)||m(e.result)&&m(e.error))}function E(t){return!g(t)&&"string"==typeof t&&Object.values(e).includes(t)}function b(e){return!g(e)&&"number"==typeof e&&Object.values(t).includes(e)}function P(e){if(!e)return!0;for(var t in e)if(Object.hasOwnProperty.call(e,t))return!1;return!0}var O={};function A(e,t){return Object.assign(O,t),e}var M=function(e){function t(r,n){var o;return(o=e.call(this,"Magic SDK Error: ["+r+"] "+n)||this).code=r,o.rawMessage=n,o.__proto__=Error,Object.setPrototypeOf(v(o),t.prototype),o}return d(t,e),t}(y(Error)),I=function(e){function t(r,n,o,i){var a;return(a=e.call(this,"Magic Extension Error ("+r.name+"): ["+n+"] "+o)||this).code=n,a.rawMessage=o,a.data=i,a.__proto__=Error,Object.setPrototypeOf(v(a),t.prototype),a}return d(t,e),t}(y(Error)),R=function(){function e(e,t,r){this.code=t,this.rawMessage=r,this.message="Magic Extension Error ("+e.name+"): ["+t+"] "+r}return e.prototype.log=function(){console.warn(this.message)},e}(),N=function(e){function r(n){var o;(o=e.call(this)||this).__proto__=Error;var i=Number(null==n?void 0:n.code);return o.rawMessage=(null==n?void 0:n.message)||"Internal error",o.code=b(i)?i:t.InternalError,o.message="Magic RPC Error: ["+o.code+"] "+o.rawMessage,Object.setPrototypeOf(v(o),r.prototype),o}return d(r,e),r}(y(Error)),k=function(){function e(e,t){this.code=e,this.rawMessage=t,this.message="Magic SDK Warning: ["+e+"] "+t}return e.prototype.log=function(){console.warn(this.message)},e}();function j(){return new M(r.MissingApiKey,"Please provide an API key that you acquired from the Magic developer dashboard.")}function S(){return new M(r.ModalNotReady,"Modal is not ready.")}function L(){return new M(r.MalformedResponse,"Response from the Magic iframe is malformed.")}function T(e){return new M(r.ExtensionNotInitialized,"Extensions must be initialized with a Magic SDK instance before `Extension."+e+"` can be accessed. Do not invoke `Extension."+e+"` inside an extension constructor.")}function x(e){var t,n,o;return new M(r.InvalidArgument,"Invalid "+(o=(t=e.argument+1)%100,(1==(n=t%10)&&11!==o?t+"st":2===n&&12!==o?t+"nd":3===n&&13!==o?t+"rd":t+"th")+" argument given to `")+e.procedure+"`.\n Expected: `"+e.expected+"`\n Received: `"+e.received+"`")}function C(){return new k(n.DuplicateIframe,"Duplicate iframes found.")}function D(){return new k(n.SyncWeb3Method,"Non-async web3 methods are deprecated in web3 > 1.0 and are not supported by the Magic web3 provider. Please use an async method instead.")}function Q(){return new k(n.ReactNativeEndpointConfiguration,"CUSTOM DOMAINS ARE NOT SUPPORTED WHEN USING MAGIC SDK WITH REACT NATIVE! The `endpoint` parameter SHOULD NOT be provided. The Magic `<iframe>` is automatically wrapped by a WebView pointed at `"+O.defaultEndpoint+"`. Changing this default behavior will lead to unexpected results and potentially security-threatening bugs.")}function U(e){var t=e.useInstead,r={"magic-sdk":"magic-sdk","magic-sdk-rn":"@magic-sdk/react-native"}[O.sdkName];return new k(n.DeprecationNotice,"`"+e.method+"` will be removed from `"+r+"` in version `"+e.removalVersions[r]+"`."+(t?" Use `"+t+"` instead.":""))}function G(e){return btoa(JSON.stringify(e))}function q(e){return JSON.parse(atob(e))}function H(e){return U({method:"encodeQueryParameters()",removalVersions:{"magic-sdk":"v3.0.0","@magic-sdk/react-native":"v3.0.0"},useInstead:"encodeJSON()"}).log(),btoa(JSON.stringify(e))}function V(e){return U({method:"decodeQueryParameters()",removalVersions:{"magic-sdk":"v3.0.0","@magic-sdk/react-native":"v3.0.0"},useInstead:"decodeJSON()"}).log(),JSON.parse(atob(e))}var W=regeneratorRuntime.mark(J);function J(){var e;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:e=0;case 1:if(!(e<Number.MAX_SAFE_INTEGER)){t.next=7;break}return t.next=5,++e;case 5:t.next=8;break;case 7:e=0;case 8:t.next=1;break;case 10:case"end":return t.stop()}},W)}var K=J();function z(){return K.next().value}var F=Symbol("Payload pre-processed by Magic SDK");function Y(e){return Object.defineProperty(e,F,{value:!0,enumerable:!1}),e}function B(e){var t,r,n;return function(e){return!!e[F]}(e)||(e.jsonrpc=null!=(t=e.jsonrpc)?t:"2.0",e.id=z(),e.method=null!=(r=e.method)?r:"noop",e.params=null!=(n=e.params)?n:[],Y(e)),e}function X(e,t){return void 0===t&&(t=[]),Y({params:t,method:e,jsonrpc:"2.0",id:z()})}var Z=function(){function e(t){t instanceof e?(this._jsonrpc=t.payload.jsonrpc,this._id=t.payload.id,this._result=t.payload.result,this._error=t.payload.error):w(t)?(this._jsonrpc=t.jsonrpc,this._id=t.id,this._result=t.result,this._error=t.error):(this._jsonrpc=t.jsonrpc,this._id=t.id,this._result=void 0,this._error=void 0)}var t=e.prototype;return t.applyError=function(e){return this._error=e,this},t.applyResult=function(e){return this._result=e,this},u(e,[{key:"hasError",get:function(){return null!=this._error}},{key:"hasResult",get:function(){return void 0!==this._result}},{key:"payload",get:function(){return{jsonrpc:this._jsonrpc,id:this._id,result:this._result,error:this._error}}}]),e}(),$=function(e){function t(){return e.apply(this,arguments)||this}return d(t,e),t}(a);function ee(){var e=new $;return{emitter:e,createChainingEmitterMethod:function(t,r){return function(){return e[t].apply(e,[].slice.call(arguments)),r}},createBoundEmitterMethod:function(t){return function(){return e[t].apply(e,[].slice.call(arguments))}}}}var te=Symbol("isPromiEvent");function re(e){return!!e[te]}function ne(e){var t=oe(e),r=ee(),n=r.createBoundEmitterMethod,o=r.createChainingEmitterMethod,i=Symbol("Promise.then"),a=Symbol("Promise.catch"),s=Symbol("Promise.finally"),u=function(e,t){return function(){var r=t[e].apply(t,[].slice.call(arguments));return c(r)}},c=function(e){var t;return Object.assign(e,((t={})[te]=!0,t[i]=e[i]||e.then,t[a]=e[a]||e.catch,t[s]=e[s]||e.finally,t.then=u(i,e),t.catch=u(a,e),t.finally=u(s,e),t.on=o("on",e),t.once=o("once",e),t.addListener=o("addListener",e),t.off=o("off",e),t.removeListener=o("removeListener",e),t.removeAllListeners=o("removeAllListeners",e),t.emit=n("emit"),t.eventNames=n("eventNames"),t.listeners=n("listeners"),t.listenerCount=n("listenerCount"),t))},d=c(t.then(function(e){return d.emit("done",e),d.emit("settled"),e},function(e){throw d.emit("error",e),d.emit("settled"),e}));return d}function oe(e){return new Promise(function(t,r){var n=e(t,r);Promise.resolve(n).catch(r)})}var ie=function(){function e(e){this.sdk=e}return e.prototype.request=function(e){var t=this.transport.post(this.overlay,o.MAGIC_HANDLE_REQUEST,B(e)),r=ne(function(e,r){t.then(function(t){if(n(),t.hasError)r(new N(t.payload.error));else{if(!t.hasResult)throw L();e(t.payload.result)}}).catch(function(e){n(),r(e)})}),n=this.transport.on(i.MAGIC_HANDLE_EVENT,function(t){var n,o=t.data.response;if(o.id===e.id&&(null===(n=o.result)||void 0===n?void 0:n.event)){var i=o.result,a=i.params;r.emit.apply(r,[i.event].concat(void 0===a?[]:a))}});return r},u(e,[{key:"transport",get:function(){return this.sdk.transport}},{key:"overlay",get:function(){return this.sdk.overlay}}]),e}(),ae=function(t){function r(){return t.apply(this,arguments)||this}return d(r,t),r.prototype.loginWithMagicLink=function(t){var r=t.showUI,n=X(e.LoginWithMagicLink,[{email:t.email,showUI:void 0===r||r}]);return this.request(n)},r}(ie),se=function(t){function r(){return t.apply(this,arguments)||this}d(r,t);var n=r.prototype;return n.getIdToken=function(t){var r=X(e.GetIdToken,[t]);return this.request(r)},n.generateIdToken=function(t){var r=X(e.GenerateIdToken,[t]);return this.request(r)},n.getMetadata=function(){var t=X(e.GetMetadata);return this.request(t)},n.updateEmail=function(t){var r=t.showUI,n=X(e.UpdateEmail,[{email:t.email,showUI:void 0===r||r}]);return this.request(n)},n.isLoggedIn=function(){var t=X(e.IsLoggedIn);return this.request(t)},n.logout=function(){var t=X(e.Logout);return this.request(t)},r}(ie),ue=ee(),ce=ue.createBoundEmitterMethod,de=ue.createChainingEmitterMethod,le=function(e){function t(){var t;return(t=e.apply(this,arguments)||this).isMagic=!0,t.on=de("on",v(t)),t.once=de("once",v(t)),t.addListener=de("addListener",v(t)),t.off=de("off",v(t)),t.removeListener=de("removeListener",v(t)),t.removeAllListeners=de("removeAllListeners",v(t)),t.emit=ce("emit"),t.eventNames=ce("eventNames"),t.listeners=ce("listeners"),t.listenerCount=ce("listenerCount"),t}d(t,e);var r=t.prototype;return r.sendAsync=function(e,t){if(!t)throw x({procedure:"Magic.rpcProvider.sendAsync",argument:1,expected:"function",received:null===t?"null":typeof t});if(Array.isArray(e))this.transport.post(this.overlay,o.MAGIC_HANDLE_REQUEST,e.map(function(e){return B(e)})).then(function(e){t(null,e.map(function(e){return c({},e.payload,{error:e.hasError?new N(e.payload.error):null})}))});else{var r=B(e);this.transport.post(this.overlay,o.MAGIC_HANDLE_REQUEST,r).then(function(e){t(e.hasError?new N(e.payload.error):null,e.payload)})}},r.send=function(e,t){if("string"==typeof e){var r=X(e,Array.isArray(t)?t:[]);return this.request(r)}if(!Array.isArray(e)&&!t){var n=D();return n.log(),new Z(e).applyError({code:-32603,message:n.rawMessage}).payload}this.sendAsync(e,t)},r.enable=function(){var e=X("eth_accounts");return this.request(e)},t}(ie);function fe(e,t){return t?new URL(e,t):new URL(e)}var pe=function(e){function t(){var t;(t=e.call(this,void 0)||this).isInitialized=!1,t.utils={createPromiEvent:ne,encodeJSON:G,decodeJSON:q,encodeQueryParameters:H,decodeQueryParameters:V,createJsonRpcRequestPayload:X,standardizeJsonRpcRequestPayload:B};var r=["request","transport","overlay","sdk"];return new Proxy(v(t),{get:function(e,n,o){if(r.includes(n)&&!t.isInitialized)throw T(n);return Reflect.get(e,n,o)}})||v(t)}d(t,e);var r=t.prototype;return r.init=function(e){this.isInitialized||(this.sdk=e,this.isInitialized=!0)},r.createDeprecationWarning=function(e){var t=e.useInstead;return new R(this,"DEPRECATION_NOTICE","`"+e.method+"` will be removed from this Extension in version `"+e.removalVersion+"`."+(t?" Use `"+t+"` instead.":""))},r.createWarning=function(e,t){return new R(this,e,t)},r.createError=function(e,t,r){return new I(this,e,t,r)},r.raiseError=function(e,t,r){throw new I(this,e,t,r)},t}(ie),he=function(e){function t(){return e.apply(this,arguments)||this}return d(t,e),t}(pe),ye=function(e){function t(){return e.apply(this,arguments)||this}return d(t,e),t}(pe);ye.Internal=he;var ve=function(){function e(e,t){var r,n,o=this;if(this.apiKey=e,!e)throw j();"react-native"===O.target&&(null==t?void 0:t.endpoint)&&Q().log();var i=O.version;this.endpoint=fe(null!=(r=null==t?void 0:t.endpoint)?r:O.defaultEndpoint).origin,this.auth=new ae(this),this.user=new se(this),this.rpcProvider=new le(this);var a=null!=(n=null==t?void 0:t.extensions)?n:[],s={};Array.isArray(a)?a.forEach(function(e){e.init(o),o[e.name]=e,e instanceof ye.Internal&&(P(e.config)||(s[e.name]=e.config))}):Object.keys(a).forEach(function(e){a[e].init(o);var t=a[e];o[e]=t,t instanceof ye.Internal&&(P(t.config)||(s[a[e].name]=t.config))}),this.encodedQueryParams=G({API_KEY:this.apiKey,DOMAIN_ORIGIN:window.location?window.location.origin:"",ETH_NETWORK:null==t?void 0:t.network,host:fe(this.endpoint).host,sdk:O.sdkName,version:i,ext:P(s)?void 0:s})}return e.prototype.preload=function(){try{return Promise.resolve(this.overlay.ready).then(function(){})}catch(e){return Promise.reject(e)}},u(e,[{key:"transport",get:function(){return e.__transports__.has(this.encodedQueryParams)||e.__transports__.set(this.encodedQueryParams,new O.PayloadTransport(this.endpoint,this.encodedQueryParams)),e.__transports__.get(this.encodedQueryParams)}},{key:"overlay",get:function(){if(!e.__overlays__.has(this.encodedQueryParams)){var t=new O.ViewController(this.transport,this.endpoint,this.encodedQueryParams);e.__overlays__.set(this.encodedQueryParams,t)}return e.__overlays__.get(this.encodedQueryParams)}}]),e}();ve.__transports__=new Map,ve.__overlays__=new Map;var me=function(){function e(e,t){this.endpoint=e,this.encodedQueryParams=t,this.messageHandlers=new Set,this.init()}var t=e.prototype;return t.post=function(e,t,r){try{var n=this;return Promise.resolve(oe(function(o){try{return Promise.resolve(e.ready).then(function(){var a=[],s=Array.isArray(r)?r.map(function(e){return e.id}):[];return Promise.resolve(e.postMessage({msgType:t+"-"+n.encodedQueryParams,payload:r})).then(function(){var e,t=n.on(i.MAGIC_HANDLE_RESPONSE,(e=function(){return t()},function(t){var n=function(e,t){var r,n=null===(r=t.data.response)||void 0===r?void 0:r.id,o=function(e,t){return t&&Array.isArray(e)?e.find(function(e){return e.id===t}):e}(e,n);return n&&o?{id:n,response:new Z(o).applyResult(t.data.response.result).applyError(t.data.response.error)}:{}}(r,t),i=n.id,u=n.response;i&&u&&Array.isArray(r)&&s.includes(i)?(a.push(u),a.length===r.length&&(e(),o(a))):i&&u&&!Array.isArray(r)&&i===r.id&&(e(),o(u))}))})})}catch(e){return Promise.reject(e)}}))}catch(e){return Promise.reject(e)}},t.on=function(e,t){var r=this,n=t.bind(window),o=function(t){t.data.msgType===e+"-"+r.encodedQueryParams&&n(t)};return this.messageHandlers.add(o),function(){return r.messageHandlers.delete(o)}},e}(),ge=function(){function e(e,t,r){this.transport=e,this.endpoint=t,this.encodedQueryParams=r,this.ready=this.waitForReady(),this.init&&this.init(),this.listen()}var t=e.prototype;return t.waitForReady=function(){var e=this;return new Promise(function(t){e.transport.on(i.MAGIC_OVERLAY_READY,function(){return t()})})},t.listen=function(){var e=this;this.transport.on(i.MAGIC_HIDE_OVERLAY,function(){e.hideOverlay()}),this.transport.on(i.MAGIC_SHOW_OVERLAY,function(){e.showOverlay()})},e}();export{ye as Extension,I as MagicExtensionError,R as MagicExtensionWarning,N as MagicRPCError,M as MagicSDKError,k as MagicSDKWarning,me as PayloadTransport,ve as SDKBase,ge as ViewController,oe as createAutoCatchingPromise,U as createDeprecationWarning,C as createDuplicateIframeWarning,T as createExtensionNotInitializedError,x as createInvalidArgumentError,L as createMalformedResponseError,j as createMissingApiKeyError,S as createModalNotReadyError,ne as createPromiEvent,Q as createReactNativeEndpointConfigurationWarning,A as createSDK,D as createSynchronousWeb3MethodWarning,fe as createURL,q as decodeJSON,V as decodeQueryParameters,G as encodeJSON,H as encodeQueryParameters,z as getPayloadId,P as isEmpty,b as isJsonRpcErrorCode,_ as isJsonRpcRequestPayload,w as isJsonRpcResponsePayload,E as isMagicPayloadMethod,re as isPromiEvent}; | ||
//# sourceMappingURL=index.m.js.map |
@@ -1,2 +0,2 @@ | ||
import{MagicPayloadMethod as e,RPCErrorCode as t,SDKErrorCode as r,SDKWarningCode as s,MagicOutgoingWindowMessage as n,MagicIncomingWindowMessage as i}from"@magic-sdk/types";import o from"eventemitter3";function a(e){return btoa(JSON.stringify(e))}function c(e){return JSON.parse(atob(e))}function d(e){return void 0===e}function u(e){return function(e){return null===e}(e)||d(e)}function h(e){return!u(e)&&!(d(e.jsonrpc)||d(e.id)||d(e.method)||d(e.params))}function l(e){return!u(e)&&!(d(e.jsonrpc)||d(e.id)||d(e.result)&&d(e.error))}function p(t){return!u(t)&&"string"==typeof t&&Object.values(e).includes(t)}function y(e){return!u(e)&&"number"==typeof e&&Object.values(t).includes(e)}function m(e){if(!e)return!0;for(const t in e)if(Object.hasOwnProperty.call(e,t))return!1;return!0}const f={};function g(e,t){return Object.assign(f,t),e}class _ extends Error{constructor(e,t){super(`Magic SDK Error: [${e}] ${t}`),this.code=e,this.rawMessage=t,this.__proto__=Error,Object.setPrototypeOf(this,_.prototype)}}class v extends Error{constructor(e,t,r,s){super(`Magic Extension Error (${e.name}): [${t}] ${r}`),this.code=t,this.rawMessage=r,this.data=s,this.__proto__=Error,Object.setPrototypeOf(this,v.prototype)}}class E extends Error{constructor(e){super(),this.__proto__=Error;const r=Number(null==e?void 0:e.code);this.rawMessage=(null==e?void 0:e.message)||"Internal error",this.code=y(r)?r:t.InternalError,this.message=`Magic RPC Error: [${this.code}] ${this.rawMessage}`,Object.setPrototypeOf(this,E.prototype)}}class w{constructor(e,t){this.code=e,this.rawMessage=t,this.message=`Magic SDK Warning: [${e}] ${t}`}log(){console.warn(this.message)}}function A(){return new _(r.MissingApiKey,"Please provide an API key that you acquired from the Magic developer dashboard.")}function M(){return new _(r.ModalNotReady,"Modal is not ready.")}function I(){return new _(r.MalformedResponse,"Response from the Magic iframe is malformed.")}function P(e){return new _(r.ExtensionNotInitialized,`Extensions must be initialized with a Magic SDK instance before \`Extension.${e}\` can be accessed. Do not invoke \`Extension.${e}\` inside an extension constructor.`)}function b(e){return new _(r.InvalidArgument,`Invalid ${(e=>{const t=e+1,r=t%10,s=t%100;return 1===r&&11!==s?t+"st":2===r&&12!==s?t+"nd":3===r&&13!==s?t+"rd":t+"th"})(e.argument)} argument given to \`${e.procedure}\`.\n Expected: \`${e.expected}\`\n Received: \`${e.received}\``)}function R(){return new w(s.DuplicateIframe,"Duplicate iframes found.")}function O(){return new w(s.SyncWeb3Method,"Non-async web3 methods are deprecated in web3 > 1.0 and are not supported by the Magic web3 provider. Please use an async method instead.")}function N(){return new w(s.ReactNativeEndpointConfiguration,`CUSTOM DOMAINS ARE NOT SUPPORTED WHEN USING MAGIC SDK WITH REACT NATIVE! The \`endpoint\` parameter SHOULD NOT be provided. The Magic \`<iframe>\` is automatically wrapped by a WebView pointed at \`${f.defaultEndpoint}\`. Changing this default behavior will lead to unexpected results and potentially security-threatening bugs.`)}const L=function*(){let e=0;for(;;)e<Number.MAX_SAFE_INTEGER?yield++e:e=0}();function S(){return L.next().value}const x=Symbol("Payload pre-processed by Magic SDK");function j(e){return Object.defineProperty(e,x,{value:!0,enumerable:!1}),e}function T(e){return function(e){return!!e[x]}(e)||(e.jsonrpc=e.jsonrpc??"2.0",e.id=S(),e.method=e.method??"noop",e.params=e.params??[],j(e)),e}function C(e,t=[]){return j({params:t,method:e,jsonrpc:"2.0",id:S()})}class ${constructor(e){e instanceof $?(this._jsonrpc=e.payload.jsonrpc,this._id=e.payload.id,this._result=e.payload.result,this._error=e.payload.error):l(e)?(this._jsonrpc=e.jsonrpc,this._id=e.id,this._result=e.result,this._error=e.error):(this._jsonrpc=e.jsonrpc,this._id=e.id,this._result=void 0,this._error=void 0)}applyError(e){return this._error=e,this}applyResult(e){return this._result=e,this}get hasError(){return null!=this._error}get hasResult(){return void 0!==this._result}get payload(){return{jsonrpc:this._jsonrpc,id:this._id,result:this._result,error:this._error}}}class D extends o{}function k(){const e=new D;return{emitter:e,createChainingEmitterMethod:(t,r)=>(...s)=>(e[t].apply(e,s),r),createBoundEmitterMethod:t=>(...r)=>e[t].apply(e,r)}}const Q=Symbol("isPromiEvent");function G(e){return!!e[Q]}function q(e){const t=H(e),{createBoundEmitterMethod:r,createChainingEmitterMethod:s}=k(),n=Symbol("Promise.then"),i=Symbol("Promise.catch"),o=Symbol("Promise.finally"),a=(e,t)=>(...r)=>{const s=t[e].apply(t,r);return c(s)},c=e=>Object.assign(e,{[Q]:!0,[n]:e[n]||e.then,[i]:e[i]||e.catch,[o]:e[o]||e.finally,then:a(n,e),catch:a(i,e),finally:a(o,e),on:s("on",e),once:s("once",e),addListener:s("addListener",e),off:s("off",e),removeListener:s("removeListener",e),removeAllListeners:s("removeAllListeners",e),emit:r("emit"),eventNames:r("eventNames"),listeners:r("listeners"),listenerCount:r("listenerCount")}),d=c(t.then(e=>(d.emit("done",e),d.emit("settled"),e),e=>{throw d.emit("error",e),d.emit("settled"),e}));return d}function H(e){return new Promise((t,r)=>{const s=e(t,r);Promise.resolve(s).catch(r)})}class U{constructor(e){this.sdk=e}get transport(){return this.sdk.transport}get overlay(){return this.sdk.overlay}request(e){const t=this.transport.post(this.overlay,n.MAGIC_HANDLE_REQUEST,T(e)),r=q((e,r)=>{t.then(t=>{if(s(),t.hasError)r(new E(t.payload.error));else{if(!t.hasResult)throw I();e(t.payload.result)}}).catch(e=>{s(),r(e)})}),s=this.transport.on(i.MAGIC_HANDLE_EVENT,t=>{var s;const{response:n}=t.data;if(n.id===e.id&&(null===(s=n.result)||void 0===s?void 0:s.event)){const{event:e,params:t=[]}=n.result;r.emit(e,...t)}});return r}}class K extends U{loginWithMagicLink(t){const{email:r,showUI:s=!0}=t,n=C(e.LoginWithMagicLink,[{email:r,showUI:s}]);return this.request(n)}}class W extends U{getIdToken(t){const r=C(e.GetIdToken,[t]);return this.request(r)}generateIdToken(t){const r=C(e.GenerateIdToken,[t]);return this.request(r)}getMetadata(){const t=C(e.GetMetadata);return this.request(t)}updateEmail(t){const{email:r,showUI:s=!0}=t,n=C(e.UpdateEmail,[{email:r,showUI:s}]);return this.request(n)}isLoggedIn(){const t=C(e.IsLoggedIn);return this.request(t)}logout(){const t=C(e.Logout);return this.request(t)}}const{createBoundEmitterMethod:z,createChainingEmitterMethod:V}=k();class Y extends U{constructor(){super(...arguments),this.isMagic=!0,this.on=V("on",this),this.once=V("once",this),this.addListener=V("addListener",this),this.off=V("off",this),this.removeListener=V("removeListener",this),this.removeAllListeners=V("removeAllListeners",this),this.emit=z("emit"),this.eventNames=z("eventNames"),this.listeners=z("listeners"),this.listenerCount=z("listenerCount")}sendAsync(e,t){if(!t)throw b({procedure:"Magic.rpcProvider.sendAsync",argument:1,expected:"function",received:null===t?"null":typeof t});if(Array.isArray(e))this.transport.post(this.overlay,n.MAGIC_HANDLE_REQUEST,e.map(e=>T(e))).then(e=>{t(null,e.map(e=>({...e.payload,error:e.hasError?new E(e.payload.error):null})))});else{const r=T(e);this.transport.post(this.overlay,n.MAGIC_HANDLE_REQUEST,r).then(e=>{t(e.hasError?new E(e.payload.error):null,e.payload)})}}send(e,t){if("string"==typeof e){const r=C(e,Array.isArray(t)?t:[]);return this.request(r)}if(Array.isArray(e)||t)return void this.sendAsync(e,t);const r=O();return r.log(),new $(e).applyError({code:-32603,message:r.rawMessage}).payload}enable(){const e=C("eth_accounts");return this.request(e)}}function J(e,t){return t?new URL(e,t):new URL(e)}class B extends U{constructor(){super(void 0),this.isInitialized=!1,this.utils={createPromiEvent:q,decodeQueryParameters:c,encodeQueryParameters:a,createJsonRpcRequestPayload:C,standardizeJsonRpcRequestPayload:T};const e=["request","transport","overlay","sdk"];return new Proxy(this,{get:(t,r,s)=>{if(e.includes(r)&&!this.isInitialized)throw P(r);return Reflect.get(t,r,s)}})}init(e){this.isInitialized||(this.sdk=e,this.isInitialized=!0)}createError(e,t,r){return new v(this,e,t,r)}raiseError(e,t,r){throw new v(this,e,t,r)}}class F extends B{}F.Internal=class extends B{};class X{constructor(e,t){if(this.apiKey=e,!e)throw A();"react-native"===f.target&&(null==t?void 0:t.endpoint)&&N().log();const{defaultEndpoint:r,version:s}=f;this.endpoint=J((null==t?void 0:t.endpoint)??r).origin,this.auth=new K(this),this.user=new W(this),this.rpcProvider=new Y(this);const n=(null==t?void 0:t.extensions)??[],i={};Array.isArray(n)?n.forEach(e=>{e.init(this),this[e.name]=e,e instanceof F.Internal&&(m(e.config)||(i[e.name]=e.config))}):Object.keys(n).forEach(e=>{n[e].init(this);const t=n[e];this[e]=t,t instanceof F.Internal&&(m(t.config)||(i[n[e].name]=t.config))}),this.encodedQueryParams=a({API_KEY:this.apiKey,DOMAIN_ORIGIN:window.location?window.location.origin:"",ETH_NETWORK:null==t?void 0:t.network,host:J(this.endpoint).host,sdk:f.sdkName,version:s,ext:m(i)?void 0:i})}get transport(){return X.__transports__.has(this.encodedQueryParams)||X.__transports__.set(this.encodedQueryParams,new f.PayloadTransport(this.endpoint,this.encodedQueryParams)),X.__transports__.get(this.encodedQueryParams)}get overlay(){if(!X.__overlays__.has(this.encodedQueryParams)){const e=new f.ViewController(this.transport,this.endpoint,this.encodedQueryParams);X.__overlays__.set(this.encodedQueryParams,e)}return X.__overlays__.get(this.encodedQueryParams)}async preload(){await this.overlay.ready}}X.__transports__=new Map,X.__overlays__=new Map;class Z{constructor(e,t){this.endpoint=e,this.encodedQueryParams=t,this.messageHandlers=new Set,this.init()}async post(e,t,r){var s=this;return H(async function(n){await e.ready;const o=[],a=Array.isArray(r)?r.map(e=>e.id):[];await e.postMessage({msgType:`${t}-${s.encodedQueryParams}`,payload:r});const c=s.on(i.MAGIC_HANDLE_RESPONSE,(d=()=>c(),e=>{const{id:t,response:s}=function(e,t){var r;const s=null===(r=t.data.response)||void 0===r?void 0:r.id,n=function(e,t){return t&&Array.isArray(e)?e.find(e=>e.id===t):e}(e,s);return s&&n?{id:s,response:new $(n).applyResult(t.data.response.result).applyError(t.data.response.error)}:{}}(r,e);t&&s&&Array.isArray(r)&&a.includes(t)?(o.push(s),o.length===r.length&&(d(),n(o))):t&&s&&!Array.isArray(r)&&t===r.id&&(d(),n(s))}));var d})}on(e,t){const r=t.bind(window),s=t=>{t.data.msgType===`${e}-${this.encodedQueryParams}`&&r(t)};return this.messageHandlers.add(s),()=>this.messageHandlers.delete(s)}}class ee{constructor(e,t,r){this.transport=e,this.endpoint=t,this.encodedQueryParams=r,this.ready=this.waitForReady(),this.init&&this.init(),this.listen()}waitForReady(){return new Promise(e=>{this.transport.on(i.MAGIC_OVERLAY_READY,()=>e())})}listen(){this.transport.on(i.MAGIC_HIDE_OVERLAY,()=>{this.hideOverlay()}),this.transport.on(i.MAGIC_SHOW_OVERLAY,()=>{this.showOverlay()})}}export{F as Extension,v as MagicExtensionError,E as MagicRPCError,_ as MagicSDKError,w as MagicSDKWarning,Z as PayloadTransport,X as SDKBase,ee as ViewController,H as createAutoCatchingPromise,R as createDuplicateIframeWarning,P as createExtensionNotInitializedError,b as createInvalidArgumentError,I as createMalformedResponseError,A as createMissingApiKeyError,M as createModalNotReadyError,q as createPromiEvent,N as createReactNativeEndpointConfigurationWarning,g as createSDK,O as createSynchronousWeb3MethodWarning,J as createURL,c as decodeQueryParameters,a as encodeQueryParameters,S as getPayloadId,m as isEmpty,y as isJsonRpcErrorCode,h as isJsonRpcRequestPayload,l as isJsonRpcResponsePayload,p as isMagicPayloadMethod,G as isPromiEvent}; | ||
import{MagicPayloadMethod as e,RPCErrorCode as t,SDKErrorCode as r,SDKWarningCode as s,MagicOutgoingWindowMessage as n,MagicIncomingWindowMessage as i}from"@magic-sdk/types";import o from"eventemitter3";function a(e){return void 0===e}function c(e){return function(e){return null===e}(e)||a(e)}function d(e){return!c(e)&&!(a(e.jsonrpc)||a(e.id)||a(e.method)||a(e.params))}function u(e){return!c(e)&&!(a(e.jsonrpc)||a(e.id)||a(e.result)&&a(e.error))}function l(t){return!c(t)&&"string"==typeof t&&Object.values(e).includes(t)}function h(e){return!c(e)&&"number"==typeof e&&Object.values(t).includes(e)}function p(e){if(!e)return!0;for(const t in e)if(Object.hasOwnProperty.call(e,t))return!1;return!0}const m={};function y(e,t){return Object.assign(m,t),e}class g extends Error{constructor(e,t){super(`Magic SDK Error: [${e}] ${t}`),this.code=e,this.rawMessage=t,this.__proto__=Error,Object.setPrototypeOf(this,g.prototype)}}class f extends Error{constructor(e,t,r,s){super(`Magic Extension Error (${e.name}): [${t}] ${r}`),this.code=t,this.rawMessage=r,this.data=s,this.__proto__=Error,Object.setPrototypeOf(this,f.prototype)}}class v{constructor(e,t,r){this.code=t,this.rawMessage=r,this.message=`Magic Extension Error (${e.name}): [${t}] ${r}`}log(){console.warn(this.message)}}class _ extends Error{constructor(e){super(),this.__proto__=Error;const r=Number(null==e?void 0:e.code);this.rawMessage=(null==e?void 0:e.message)||"Internal error",this.code=h(r)?r:t.InternalError,this.message=`Magic RPC Error: [${this.code}] ${this.rawMessage}`,Object.setPrototypeOf(this,_.prototype)}}class E{constructor(e,t){this.code=e,this.rawMessage=t,this.message=`Magic SDK Warning: [${e}] ${t}`}log(){console.warn(this.message)}}function w(){return new g(r.MissingApiKey,"Please provide an API key that you acquired from the Magic developer dashboard.")}function A(){return new g(r.ModalNotReady,"Modal is not ready.")}function M(){return new g(r.MalformedResponse,"Response from the Magic iframe is malformed.")}function I(e){return new g(r.ExtensionNotInitialized,`Extensions must be initialized with a Magic SDK instance before \`Extension.${e}\` can be accessed. Do not invoke \`Extension.${e}\` inside an extension constructor.`)}function P(e){return new g(r.InvalidArgument,`Invalid ${(e=>{const t=e+1,r=t%10,s=t%100;return 1===r&&11!==s?t+"st":2===r&&12!==s?t+"nd":3===r&&13!==s?t+"rd":t+"th"})(e.argument)} argument given to \`${e.procedure}\`.\n Expected: \`${e.expected}\`\n Received: \`${e.received}\``)}function b(){return new E(s.DuplicateIframe,"Duplicate iframes found.")}function N(){return new E(s.SyncWeb3Method,"Non-async web3 methods are deprecated in web3 > 1.0 and are not supported by the Magic web3 provider. Please use an async method instead.")}function O(){return new E(s.ReactNativeEndpointConfiguration,`CUSTOM DOMAINS ARE NOT SUPPORTED WHEN USING MAGIC SDK WITH REACT NATIVE! The \`endpoint\` parameter SHOULD NOT be provided. The Magic \`<iframe>\` is automatically wrapped by a WebView pointed at \`${m.defaultEndpoint}\`. Changing this default behavior will lead to unexpected results and potentially security-threatening bugs.`)}function R(e){const{method:t,removalVersions:r,useInstead:n}=e,i={"magic-sdk":"magic-sdk","magic-sdk-rn":"@magic-sdk/react-native"}[m.sdkName];return new E(s.DeprecationNotice,`\`${t}\` will be removed from \`${i}\` in version \`${r[i]}\`.${n?` Use \`${n}\` instead.`:""}`)}function S(e){return btoa(JSON.stringify(e))}function $(e){return JSON.parse(atob(e))}function L(e){return R({method:"encodeQueryParameters()",removalVersions:{"magic-sdk":"v3.0.0","@magic-sdk/react-native":"v3.0.0"},useInstead:"encodeJSON()"}).log(),btoa(JSON.stringify(e))}function k(e){return R({method:"decodeQueryParameters()",removalVersions:{"magic-sdk":"v3.0.0","@magic-sdk/react-native":"v3.0.0"},useInstead:"decodeJSON()"}).log(),JSON.parse(atob(e))}const x=function*(){let e=0;for(;;)e<Number.MAX_SAFE_INTEGER?yield++e:e=0}();function T(){return x.next().value}const C=Symbol("Payload pre-processed by Magic SDK");function j(e){return Object.defineProperty(e,C,{value:!0,enumerable:!1}),e}function D(e){return function(e){return!!e[C]}(e)||(e.jsonrpc=e.jsonrpc??"2.0",e.id=T(),e.method=e.method??"noop",e.params=e.params??[],j(e)),e}function Q(e,t=[]){return j({params:t,method:e,jsonrpc:"2.0",id:T()})}class U{constructor(e){e instanceof U?(this._jsonrpc=e.payload.jsonrpc,this._id=e.payload.id,this._result=e.payload.result,this._error=e.payload.error):u(e)?(this._jsonrpc=e.jsonrpc,this._id=e.id,this._result=e.result,this._error=e.error):(this._jsonrpc=e.jsonrpc,this._id=e.id,this._result=void 0,this._error=void 0)}applyError(e){return this._error=e,this}applyResult(e){return this._result=e,this}get hasError(){return null!=this._error}get hasResult(){return void 0!==this._result}get payload(){return{jsonrpc:this._jsonrpc,id:this._id,result:this._result,error:this._error}}}class G extends o{}function q(){const e=new G;return{emitter:e,createChainingEmitterMethod:(t,r)=>(...s)=>(e[t].apply(e,s),r),createBoundEmitterMethod:t=>(...r)=>e[t].apply(e,r)}}const H=Symbol("isPromiEvent");function V(e){return!!e[H]}function W(e){const t=J(e),{createBoundEmitterMethod:r,createChainingEmitterMethod:s}=q(),n=Symbol("Promise.then"),i=Symbol("Promise.catch"),o=Symbol("Promise.finally"),a=(e,t)=>(...r)=>{const s=t[e].apply(t,r);return c(s)},c=e=>Object.assign(e,{[H]:!0,[n]:e[n]||e.then,[i]:e[i]||e.catch,[o]:e[o]||e.finally,then:a(n,e),catch:a(i,e),finally:a(o,e),on:s("on",e),once:s("once",e),addListener:s("addListener",e),off:s("off",e),removeListener:s("removeListener",e),removeAllListeners:s("removeAllListeners",e),emit:r("emit"),eventNames:r("eventNames"),listeners:r("listeners"),listenerCount:r("listenerCount")}),d=c(t.then(e=>(d.emit("done",e),d.emit("settled"),e),e=>{throw d.emit("error",e),d.emit("settled"),e}));return d}function J(e){return new Promise((t,r)=>{const s=e(t,r);Promise.resolve(s).catch(r)})}class K{constructor(e){this.sdk=e}get transport(){return this.sdk.transport}get overlay(){return this.sdk.overlay}request(e){const t=this.transport.post(this.overlay,n.MAGIC_HANDLE_REQUEST,D(e)),r=W((e,r)=>{t.then(t=>{if(s(),t.hasError)r(new _(t.payload.error));else{if(!t.hasResult)throw M();e(t.payload.result)}}).catch(e=>{s(),r(e)})}),s=this.transport.on(i.MAGIC_HANDLE_EVENT,t=>{var s;const{response:n}=t.data;if(n.id===e.id&&(null===(s=n.result)||void 0===s?void 0:s.event)){const{event:e,params:t=[]}=n.result;r.emit(e,...t)}});return r}}class z extends K{loginWithMagicLink(t){const{email:r,showUI:s=!0}=t,n=Q(e.LoginWithMagicLink,[{email:r,showUI:s}]);return this.request(n)}}class Y extends K{getIdToken(t){const r=Q(e.GetIdToken,[t]);return this.request(r)}generateIdToken(t){const r=Q(e.GenerateIdToken,[t]);return this.request(r)}getMetadata(){const t=Q(e.GetMetadata);return this.request(t)}updateEmail(t){const{email:r,showUI:s=!0}=t,n=Q(e.UpdateEmail,[{email:r,showUI:s}]);return this.request(n)}isLoggedIn(){const t=Q(e.IsLoggedIn);return this.request(t)}logout(){const t=Q(e.Logout);return this.request(t)}}const{createBoundEmitterMethod:B,createChainingEmitterMethod:F}=q();class X extends K{constructor(){super(...arguments),this.isMagic=!0,this.on=F("on",this),this.once=F("once",this),this.addListener=F("addListener",this),this.off=F("off",this),this.removeListener=F("removeListener",this),this.removeAllListeners=F("removeAllListeners",this),this.emit=B("emit"),this.eventNames=B("eventNames"),this.listeners=B("listeners"),this.listenerCount=B("listenerCount")}sendAsync(e,t){if(!t)throw P({procedure:"Magic.rpcProvider.sendAsync",argument:1,expected:"function",received:null===t?"null":typeof t});if(Array.isArray(e))this.transport.post(this.overlay,n.MAGIC_HANDLE_REQUEST,e.map(e=>D(e))).then(e=>{t(null,e.map(e=>({...e.payload,error:e.hasError?new _(e.payload.error):null})))});else{const r=D(e);this.transport.post(this.overlay,n.MAGIC_HANDLE_REQUEST,r).then(e=>{t(e.hasError?new _(e.payload.error):null,e.payload)})}}send(e,t){if("string"==typeof e){const r=Q(e,Array.isArray(t)?t:[]);return this.request(r)}if(Array.isArray(e)||t)return void this.sendAsync(e,t);const r=N();return r.log(),new U(e).applyError({code:-32603,message:r.rawMessage}).payload}enable(){const e=Q("eth_accounts");return this.request(e)}}function Z(e,t){return t?new URL(e,t):new URL(e)}class ee extends K{constructor(){super(void 0),this.isInitialized=!1,this.utils={createPromiEvent:W,encodeJSON:S,decodeJSON:$,encodeQueryParameters:L,decodeQueryParameters:k,createJsonRpcRequestPayload:Q,standardizeJsonRpcRequestPayload:D};const e=["request","transport","overlay","sdk"];return new Proxy(this,{get:(t,r,s)=>{if(e.includes(r)&&!this.isInitialized)throw I(r);return Reflect.get(t,r,s)}})}init(e){this.isInitialized||(this.sdk=e,this.isInitialized=!0)}createDeprecationWarning(e){const{method:t,removalVersion:r,useInstead:s}=e;return new v(this,"DEPRECATION_NOTICE",`\`${t}\` will be removed from this Extension in version \`${r}\`.${s?` Use \`${s}\` instead.`:""}`)}createWarning(e,t){return new v(this,e,t)}createError(e,t,r){return new f(this,e,t,r)}raiseError(e,t,r){throw new f(this,e,t,r)}}class te extends ee{}te.Internal=class extends ee{};class re{constructor(e,t){if(this.apiKey=e,!e)throw w();"react-native"===m.target&&(null==t?void 0:t.endpoint)&&O().log();const{defaultEndpoint:r,version:s}=m;this.endpoint=Z((null==t?void 0:t.endpoint)??r).origin,this.auth=new z(this),this.user=new Y(this),this.rpcProvider=new X(this);const n=(null==t?void 0:t.extensions)??[],i={};Array.isArray(n)?n.forEach(e=>{e.init(this),this[e.name]=e,e instanceof te.Internal&&(p(e.config)||(i[e.name]=e.config))}):Object.keys(n).forEach(e=>{n[e].init(this);const t=n[e];this[e]=t,t instanceof te.Internal&&(p(t.config)||(i[n[e].name]=t.config))}),this.encodedQueryParams=S({API_KEY:this.apiKey,DOMAIN_ORIGIN:window.location?window.location.origin:"",ETH_NETWORK:null==t?void 0:t.network,host:Z(this.endpoint).host,sdk:m.sdkName,version:s,ext:p(i)?void 0:i})}get transport(){return re.__transports__.has(this.encodedQueryParams)||re.__transports__.set(this.encodedQueryParams,new m.PayloadTransport(this.endpoint,this.encodedQueryParams)),re.__transports__.get(this.encodedQueryParams)}get overlay(){if(!re.__overlays__.has(this.encodedQueryParams)){const e=new m.ViewController(this.transport,this.endpoint,this.encodedQueryParams);re.__overlays__.set(this.encodedQueryParams,e)}return re.__overlays__.get(this.encodedQueryParams)}async preload(){await this.overlay.ready}}re.__transports__=new Map,re.__overlays__=new Map;class se{constructor(e,t){this.endpoint=e,this.encodedQueryParams=t,this.messageHandlers=new Set,this.init()}async post(e,t,r){var s=this;return J(async function(n){await e.ready;const o=[],a=Array.isArray(r)?r.map(e=>e.id):[];await e.postMessage({msgType:`${t}-${s.encodedQueryParams}`,payload:r});const c=s.on(i.MAGIC_HANDLE_RESPONSE,(d=()=>c(),e=>{const{id:t,response:s}=function(e,t){var r;const s=null===(r=t.data.response)||void 0===r?void 0:r.id,n=function(e,t){return t&&Array.isArray(e)?e.find(e=>e.id===t):e}(e,s);return s&&n?{id:s,response:new U(n).applyResult(t.data.response.result).applyError(t.data.response.error)}:{}}(r,e);t&&s&&Array.isArray(r)&&a.includes(t)?(o.push(s),o.length===r.length&&(d(),n(o))):t&&s&&!Array.isArray(r)&&t===r.id&&(d(),n(s))}));var d})}on(e,t){const r=t.bind(window),s=t=>{t.data.msgType===`${e}-${this.encodedQueryParams}`&&r(t)};return this.messageHandlers.add(s),()=>this.messageHandlers.delete(s)}}class ne{constructor(e,t,r){this.transport=e,this.endpoint=t,this.encodedQueryParams=r,this.ready=this.waitForReady(),this.init&&this.init(),this.listen()}waitForReady(){return new Promise(e=>{this.transport.on(i.MAGIC_OVERLAY_READY,()=>e())})}listen(){this.transport.on(i.MAGIC_HIDE_OVERLAY,()=>{this.hideOverlay()}),this.transport.on(i.MAGIC_SHOW_OVERLAY,()=>{this.showOverlay()})}}export{te as Extension,f as MagicExtensionError,v as MagicExtensionWarning,_ as MagicRPCError,g as MagicSDKError,E as MagicSDKWarning,se as PayloadTransport,re as SDKBase,ne as ViewController,J as createAutoCatchingPromise,R as createDeprecationWarning,b as createDuplicateIframeWarning,I as createExtensionNotInitializedError,P as createInvalidArgumentError,M as createMalformedResponseError,w as createMissingApiKeyError,A as createModalNotReadyError,W as createPromiEvent,O as createReactNativeEndpointConfigurationWarning,y as createSDK,N as createSynchronousWeb3MethodWarning,Z as createURL,$ as decodeJSON,k as decodeQueryParameters,S as encodeJSON,L as encodeQueryParameters,T as getPayloadId,p as isEmpty,h as isJsonRpcErrorCode,d as isJsonRpcRequestPayload,u as isJsonRpcResponsePayload,l as isMagicPayloadMethod,V as isPromiEvent}; | ||
//# sourceMappingURL=index.modern.js.map |
import { createJsonRpcRequestPayload, standardizeJsonRpcRequestPayload } from '../core/json-rpc'; | ||
import { BaseModule } from './base-module'; | ||
import { SDKBase, MagicSDKAdditionalConfiguration } from '../core/sdk'; | ||
import { MagicExtensionError } from '../core/sdk-exceptions'; | ||
import { createPromiEvent, encodeQueryParameters, decodeQueryParameters } from '../util'; | ||
import { MagicExtensionError, MagicExtensionWarning } from '../core/sdk-exceptions'; | ||
import { createPromiEvent, encodeJSON, decodeJSON, encodeQueryParameters, decodeQueryParameters } from '../util'; | ||
declare abstract class BaseExtension<TName extends string> extends BaseModule { | ||
@@ -11,4 +11,6 @@ abstract readonly name: TName; | ||
createPromiEvent: typeof createPromiEvent; | ||
encodeJSON: typeof encodeJSON; | ||
decodeJSON: typeof decodeJSON; | ||
encodeQueryParameters: typeof encodeQueryParameters; | ||
decodeQueryParameters: typeof decodeQueryParameters; | ||
encodeQueryParameters: typeof encodeQueryParameters; | ||
createJsonRpcRequestPayload: typeof createJsonRpcRequestPayload; | ||
@@ -23,3 +25,20 @@ standardizeJsonRpcRequestPayload: typeof standardizeJsonRpcRequestPayload; | ||
/** | ||
* Creates an error wrapped with a native Magic SDK error type. This enables | ||
* Creates a deprecation warning wrapped with a native Magic SDK warning type. | ||
* Best practice is to warn users of upcoming deprecations at least one major | ||
* version before the change is implemented. You can use this method to | ||
* communicate deprecations in a manner consistent with Magic SDK core code. | ||
*/ | ||
protected createDeprecationWarning(options: { | ||
method: string; | ||
removalVersion: string; | ||
useInstead?: string; | ||
}): MagicExtensionWarning; | ||
/** | ||
* Creates a warning wrapped with a native Magic SDK warning type. This | ||
* maintains consistency in warning messaging for consumers of Magic SDK and | ||
* this Extension. | ||
*/ | ||
protected createWarning(code: string | number, message: string): MagicExtensionWarning; | ||
/** | ||
* Creates an error wrapped with a native Magic SDK error type. This maintains | ||
* consistency in error handling for consumers of Magic SDK and this | ||
@@ -30,3 +49,3 @@ * Extension. | ||
/** | ||
* Throws an error wrapped with a native Magic SDK error type. This enables | ||
* Throws an error wrapped with a native Magic SDK error type. This maintains | ||
* consistency in error handling for consumers of Magic SDK and this | ||
@@ -33,0 +52,0 @@ * Extension. |
export * from './get-payload-id'; | ||
export * from './promise-tools'; | ||
export * from './query-params'; | ||
export * from './base64-json'; | ||
export * from './type-guards'; | ||
export * from './url'; |
{ | ||
"name": "@magic-sdk/provider", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "Core business logic for Magic SDK packages.", | ||
@@ -25,3 +25,3 @@ "author": "Fortmatic <team@fortmatic.com> (https://fortmatic.com/)", | ||
"dependencies": { | ||
"@magic-sdk/types": "^1.0.3", | ||
"@magic-sdk/types": "^1.1.0", | ||
"eventemitter3": "^4.0.4" | ||
@@ -59,3 +59,3 @@ }, | ||
}, | ||
"gitHead": "a640337209f264994a69a3b100065c68b6fc7e87" | ||
"gitHead": "1ef062ea699d48d5e9a9375a93b7c147632b05ca" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
280874
793
Updated@magic-sdk/types@^1.1.0