@monerium/sdk-react-provider 0.0.8 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +394 -374
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.10](https://github.com/monerium/public-monorepo/compare/sdk-react-provider-v0.0.9...sdk-react-provider-v0.0.10) (2023-11-17)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* broken build 0.0.9 ([b1a4307](https://github.com/monerium/public-monorepo/commit/b1a43075435d900cbfc7b3abf44977e05337e540))
|
|
9
|
+
|
|
10
|
+
## [0.0.9](https://github.com/monerium/public-monorepo/compare/sdk-react-provider-v0.0.8...sdk-react-provider-v0.0.9) (2023-11-17)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* **react-provider:** force version update ([c2895de](https://github.com/monerium/public-monorepo/commit/c2895de0a0a019bdfb31ca3b0e52ebd7fa7348ca))
|
|
16
|
+
|
|
3
17
|
## [0.0.8](https://github.com/monerium/public-monorepo/compare/sdk-react-provider-v0.0.8...sdk-react-provider-v0.0.8) (2023-11-17)
|
|
4
18
|
|
|
5
19
|
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const fe=require("react/jsx-runtime"),S=require("react"),ne=S.createContext(null);var ie=(i,e,t)=>{if(!e.has(i))throw TypeError("Cannot "+t)},m=(i,e,t)=>(ie(i,e,"read from private field"),t?t.call(i):e.get(i)),R=(i,e,t)=>{if(e.has(i))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(i):e.set(i,t)},C=(i,e,t,n)=>(ie(i,e,"write to private field"),n?n.call(i,t):e.set(i,t),t),T=(i,e,t)=>(ie(i,e,"access private method"),t);const Y=i=>{var e;return i&&((e=Object.entries(i))==null?void 0:e.length)>0?Object.entries(i).map(([t,n])=>`${encodeURIComponent(t)}=${encodeURIComponent(n)}`).join("&"):""},pe=i=>{switch(i){case 1:case 5:return"ethereum";case 100:case 10200:return"gnosis";case 137:case 80001:return"polygon";default:throw new Error(`Chain not supported: ${i}`)}},ge=i=>{switch(i){case 1:case 100:case 137:return"mainnet";case 5:return"goerli";case 10200:return"chiado";case 80001:return"mumbai";default:throw new Error(`Network not supported: ${i}`)}},ee={environments:{production:{api:"https://api.monerium.app",web:"https://monerium.app",wss:"wss://api.monerium.app"},sandbox:{api:"https://api.monerium.dev",web:"https://sandbox.monerium.dev",wss:"wss://api.monerium.dev"}}},N="monerium.sdk.code_verifier",te="monerium.sdk.refresh_token";var q=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{};function le(i){return i&&i.__esModule&&Object.prototype.hasOwnProperty.call(i,"default")?i.default:i}function ve(i){if(i.__esModule)return i;var e=i.default;if(typeof e=="function"){var t=function n(){if(this instanceof n){var o=[null];o.push.apply(o,arguments);var a=Function.bind.apply(e,o);return new a}return e.apply(this,arguments)};t.prototype=e.prototype}else t={};return Object.defineProperty(t,"__esModule",{value:!0}),Object.keys(i).forEach(function(n){var o=Object.getOwnPropertyDescriptor(i,n);Object.defineProperty(t,n,o.get?o:{enumerable:!0,get:function(){return i[n]}})}),t}var ue={exports:{}};function we(i){throw new Error('Could not dynamically require "'+i+'". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.')}var oe={exports:{}};const ye={},me=Object.freeze(Object.defineProperty({__proto__:null,default:ye},Symbol.toStringTag,{value:"Module"})),_e=ve(me);var se;function de(){return se||(se=1,function(i,e){(function(t,n){i.exports=n()})(q,function(){var t=t||function(n,o){var a;if(typeof window<"u"&&window.crypto&&(a=window.crypto),typeof self<"u"&&self.crypto&&(a=self.crypto),typeof globalThis<"u"&&globalThis.crypto&&(a=globalThis.crypto),!a&&typeof window<"u"&&window.msCrypto&&(a=window.msCrypto),!a&&typeof q<"u"&&q.crypto&&(a=q.crypto),!a&&typeof we=="function")try{a=_e}catch{}var d=function(){if(a){if(typeof a.getRandomValues=="function")try{return a.getRandomValues(new Uint32Array(1))[0]}catch{}if(typeof a.randomBytes=="function")try{return a.randomBytes(4).readInt32LE()}catch{}}throw new Error("Native crypto module could not be used to get secure random number.")},O=Object.create||function(){function r(){}return function(s){var l;return r.prototype=s,l=new r,r.prototype=null,l}}(),f={},_=f.lib={},w=_.Base=function(){return{extend:function(r){var s=O(this);return r&&s.mixIn(r),(!s.hasOwnProperty("init")||this.init===s.init)&&(s.init=function(){s.$super.init.apply(this,arguments)}),s.init.prototype=s,s.$super=this,s},create:function(){var r=this.extend();return r.init.apply(r,arguments),r},init:function(){},mixIn:function(r){for(var s in r)r.hasOwnProperty(s)&&(this[s]=r[s]);r.hasOwnProperty("toString")&&(this.toString=r.toString)},clone:function(){return this.init.prototype.extend(this)}}}(),v=_.WordArray=w.extend({init:function(r,s){r=this.words=r||[],s!=o?this.sigBytes=s:this.sigBytes=r.length*4},toString:function(r){return(r||h).stringify(this)},concat:function(r){var s=this.words,l=r.words,u=this.sigBytes,y=r.sigBytes;if(this.clamp(),u%4)for(var b=0;b<y;b++){var x=l[b>>>2]>>>24-b%4*8&255;s[u+b>>>2]|=x<<24-(u+b)%4*8}else for(var A=0;A<y;A+=4)s[u+A>>>2]=l[A>>>2];return this.sigBytes+=y,this},clamp:function(){var r=this.words,s=this.sigBytes;r[s>>>2]&=4294967295<<32-s%4*8,r.length=n.ceil(s/4)},clone:function(){var r=w.clone.call(this);return r.words=this.words.slice(0),r},random:function(r){for(var s=[],l=0;l<r;l+=4)s.push(d());return new v.init(s,r)}}),k=f.enc={},h=k.Hex={stringify:function(r){for(var s=r.words,l=r.sigBytes,u=[],y=0;y<l;y++){var b=s[y>>>2]>>>24-y%4*8&255;u.push((b>>>4).toString(16)),u.push((b&15).toString(16))}return u.join("")},parse:function(r){for(var s=r.length,l=[],u=0;u<s;u+=2)l[u>>>3]|=parseInt(r.substr(u,2),16)<<24-u%8*4;return new v.init(l,s/2)}},g=k.Latin1={stringify:function(r){for(var s=r.words,l=r.sigBytes,u=[],y=0;y<l;y++){var b=s[y>>>2]>>>24-y%4*8&255;u.push(String.fromCharCode(b))}return u.join("")},parse:function(r){for(var s=r.length,l=[],u=0;u<s;u++)l[u>>>2]|=(r.charCodeAt(u)&255)<<24-u%4*8;return new v.init(l,s)}},c=k.Utf8={stringify:function(r){try{return decodeURIComponent(escape(g.stringify(r)))}catch{throw new Error("Malformed UTF-8 data")}},parse:function(r){return g.parse(unescape(encodeURIComponent(r)))}},p=_.BufferedBlockAlgorithm=w.extend({reset:function(){this._data=new v.init,this._nDataBytes=0},_append:function(r){typeof r=="string"&&(r=c.parse(r)),this._data.concat(r),this._nDataBytes+=r.sigBytes},_process:function(r){var s,l=this._data,u=l.words,y=l.sigBytes,b=this.blockSize,x=b*4,A=y/x;r?A=n.ceil(A):A=n.max((A|0)-this._minBufferSize,0);var W=A*b,z=n.min(W*4,y);if(W){for(var L=0;L<W;L+=b)this._doProcessBlock(u,L);s=u.splice(0,W),l.sigBytes-=z}return new v.init(s,z)},clone:function(){var r=w.clone.call(this);return r._data=this._data.clone(),r},_minBufferSize:0});_.Hasher=p.extend({cfg:w.extend(),init:function(r){this.cfg=this.cfg.extend(r),this.reset()},reset:function(){p.reset.call(this),this._doReset()},update:function(r){return this._append(r),this._process(),this},finalize:function(r){r&&this._append(r);var s=this._doFinalize();return s},blockSize:16,_createHelper:function(r){return function(s,l){return new r.init(l).finalize(s)}},_createHmacHelper:function(r){return function(s,l){return new I.HMAC.init(r,l).finalize(s)}}});var I=f.algo={};return f}(Math);return t})}(oe)),oe.exports}(function(i,e){(function(t,n){i.exports=n(de())})(q,function(t){return function(){var n=t,o=n.lib,a=o.WordArray,d=n.enc;d.Base64url={stringify:function(f,_){_===void 0&&(_=!0);var w=f.words,v=f.sigBytes,k=_?this._safe_map:this._map;f.clamp();for(var h=[],g=0;g<v;g+=3)for(var c=w[g>>>2]>>>24-g%4*8&255,p=w[g+1>>>2]>>>24-(g+1)%4*8&255,I=w[g+2>>>2]>>>24-(g+2)%4*8&255,r=c<<16|p<<8|I,s=0;s<4&&g+s*.75<v;s++)h.push(k.charAt(r>>>6*(3-s)&63));var l=k.charAt(64);if(l)for(;h.length%4;)h.push(l);return h.join("")},parse:function(f,_){_===void 0&&(_=!0);var w=f.length,v=_?this._safe_map:this._map,k=this._reverseMap;if(!k){k=this._reverseMap=[];for(var h=0;h<v.length;h++)k[v.charCodeAt(h)]=h}var g=v.charAt(64);if(g){var c=f.indexOf(g);c!==-1&&(w=c)}return O(f,w,k)},_map:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",_safe_map:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"};function O(f,_,w){for(var v=[],k=0,h=0;h<_;h++)if(h%4){var g=w[f.charCodeAt(h-1)]<<h%4*2,c=w[f.charCodeAt(h)]>>>6-h%4*2,p=g|c;v[k>>>2]|=p<<24-k%4*8,k++}return a.create(v,k)}}(),t.enc.Base64url})})(ue);var be=ue.exports;const ke=le(be);var he={exports:{}};(function(i,e){(function(t,n){i.exports=n(de())})(q,function(t){return function(n){var o=t,a=o.lib,d=a.WordArray,O=a.Hasher,f=o.algo,_=[],w=[];(function(){function h(I){for(var r=n.sqrt(I),s=2;s<=r;s++)if(!(I%s))return!1;return!0}function g(I){return(I-(I|0))*4294967296|0}for(var c=2,p=0;p<64;)h(c)&&(p<8&&(_[p]=g(n.pow(c,1/2))),w[p]=g(n.pow(c,1/3)),p++),c++})();var v=[],k=f.SHA256=O.extend({_doReset:function(){this._hash=new d.init(_.slice(0))},_doProcessBlock:function(h,g){for(var c=this._hash.words,p=c[0],I=c[1],r=c[2],s=c[3],l=c[4],u=c[5],y=c[6],b=c[7],x=0;x<64;x++){if(x<16)v[x]=h[g+x]|0;else{var A=v[x-15],W=(A<<25|A>>>7)^(A<<14|A>>>18)^A>>>3,z=v[x-2],L=(z<<15|z>>>17)^(z<<13|z>>>19)^z>>>10;v[x]=W+v[x-7]+L+v[x-16]}var X=l&u^~l&y,M=p&I^p&r^I&r,P=(p<<30|p>>>2)^(p<<19|p>>>13)^(p<<10|p>>>22),U=(l<<26|l>>>6)^(l<<21|l>>>11)^(l<<7|l>>>25),H=b+U+X+w[x]+v[x],J=P+M;b=y,y=u,u=l,l=s+H|0,s=r,r=I,I=p,p=H+J|0}c[0]=c[0]+p|0,c[1]=c[1]+I|0,c[2]=c[2]+r|0,c[3]=c[3]+s|0,c[4]=c[4]+l|0,c[5]=c[5]+u|0,c[6]=c[6]+y|0,c[7]=c[7]+b|0},_doFinalize:function(){var h=this._data,g=h.words,c=this._nDataBytes*8,p=h.sigBytes*8;return g[p>>>5]|=128<<24-p%32,g[(p+64>>>9<<4)+14]=n.floor(c/4294967296),g[(p+64>>>9<<4)+15]=c,h.sigBytes=g.length*4,this._process(),this._hash},clone:function(){var h=O.clone.call(this);return h._hash=this._hash.clone(),h}});o.SHA256=O._createHelper(k),o.HmacSHA256=O._createHmacHelper(k)}(Math),t.SHA256})})(he);var Se=he.exports;const Oe=le(Se),Ie=(i,e)=>{const{client_id:t,redirect_uri:n,scope:o,state:a,chainId:d,chain:O,network:f,address:_,signature:w}=i,v=_?{address:_,...w!==void 0?{signature:w}:{},...d!==void 0||O!==void 0?{chain:d?pe(d):O}:{},...d!==void 0||f!==void 0?{network:d?ge(d):f}:{}}:{};return Y({client_id:t,redirect_uri:n,...o!==void 0?{scope:o}:{},...a!==void 0?{state:a}:{},code_challenge:e,code_challenge_method:"S256",response_type:"code",...v})},xe=()=>{let i="";const e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",t=e.length;let n=0;for(;n<128;)i+=e.charAt(Math.floor(Math.random()*t)),n+=1;return i},Ae=i=>ke.stringify(Oe(i)),ae=(i,e)=>{const t=xe(),n=Ae(t);return sessionStorage.setItem(N,t||""),`${i}/auth?${Ie(e,n)}`},Me=()=>{const i=window.location.href,[e,t]=i.split("?");t&&window.history.replaceState(null,"",e)},ce=i=>i.code!=null,Ee=i=>i.refresh_token!=null,Be=i=>i.client_secret!=null,Pe=async(i,e,t,n)=>{const o=await fetch(`${i}`,{method:e,headers:n,body:t});let a;const d=await o.text();try{a=JSON.parse(d)}catch{throw d}if(!o.ok)throw a;return a},re=typeof window>"u";var j,Z,F,D,$,E,B,G,K,Q;class Ce{constructor(e){if(R(this,E),R(this,j,void 0),R(this,Z,void 0),R(this,F,void 0),R(this,D,void 0),R(this,$,void 0),R(this,G,void 0),R(this,K,void 0),R(this,Q,void 0),C(this,D,new Map),this.isAuthorized=!!this.bearerProfile,C(this,G,async(t,n,o)=>{const a=sessionStorage.getItem(N)||"";if(!a)throw new Error("Code verifier not found");return this.codeVerifier=a,sessionStorage.removeItem(N),await this.getBearerToken({code:o,redirect_uri:n,client_id:t,code_verifier:a})}),C(this,K,async({clientId:t,clientSecret:n})=>await this.getBearerToken({client_id:t,client_secret:n})),C(this,Q,async(t,n)=>await this.getBearerToken({refresh_token:n,client_id:t})),this.subscribeToOrderNotifications=()=>{var t,n;const o=`${m(this,j).wss}/profiles/${(t=this.bearerProfile)==null?void 0:t.profile}/orders?access_token=${(n=this.bearerProfile)==null?void 0:n.access_token}`,a=new WebSocket(o);return a.addEventListener("open",()=>{console.info(`Socket connected: ${o}`)}),a.addEventListener("error",d=>{throw console.error(d),new Error(`Socket error: ${o}`)}),a.addEventListener("message",d=>{var O;const f=JSON.parse(d.data);(O=m(this,D).get(f.meta.state))==null||O(f)}),a.addEventListener("close",()=>{console.info(`Socket connection closed: ${o}`)}),a},this.auth=async t=>await this.getBearerToken(t),this.getAuthFlowURI=t=>{const n=ae(m(this,j).api,t);return this.codeVerifier=sessionStorage.getItem(N),n},this.pkceRequest=t=>this.getAuthFlowURI(t),this.getEnvironment=()=>m(this,j),!e){C(this,j,ee.environments.sandbox);return}if(typeof e=="string")C(this,j,ee.environments[e]);else if(C(this,j,ee.environments[e.environment||"sandbox"]),re){const{clientId:t,clientSecret:n}=e;C(this,$,{clientId:t,clientSecret:n})}else{const{clientId:t,redirectUrl:n}=e;C(this,$,{clientId:t,redirectUrl:n})}}async authorize(e){var t,n;const o=(e==null?void 0:e.clientId)||((t=m(this,$))==null?void 0:t.clientId),a=(e==null?void 0:e.redirectUrl)||((n=m(this,$))==null?void 0:n.redirectUrl);if(!o)throw new Error("Missing ClientId");if(!a)throw new Error("Missing RedirectUrl");const d=ae(m(this,j).api,{client_id:o,redirect_uri:a,address:e==null?void 0:e.address,signature:e==null?void 0:e.signature,chainId:e==null?void 0:e.chainId});window.location.replace(d)}async connect(e){var t,n,o;const a=(e==null?void 0:e.clientId)||((t=m(this,$))==null?void 0:t.clientId);if(e!=null&&e.clientSecret||(n=m(this,$))!=null&&n.clientSecret){if(!re)throw new Error("Only use client credentials on server side");return await m(this,K).call(this,m(this,$)),!!this.bearerProfile}const d=(e==null?void 0:e.redirectUrl)||((o=m(this,$))==null?void 0:o.redirectUrl);if(!a)throw new Error("Missing ClientId");if(re)throw new Error("This only works on client side");const O=new URLSearchParams(window.location.search).get("code")||void 0,f=sessionStorage.getItem(te)||void 0;return O?await m(this,G).call(this,a,d,O):f&&await m(this,Q).call(this,a,f),!!this.bearerProfile}async getBearerToken(e){let t;if(ce(e))t={...e,grant_type:"authorization_code"};else if(Ee(e))t={...e,grant_type:"refresh_token"};else if(Be(e))t={...e,grant_type:"client_credentials"};else throw new Error("Authentication method could not be detected.");return await T(this,E,B).call(this,"post","auth/token",t,!0).then(n=>{var o;this.bearerProfile=n,this.isAuthorized=!!n,C(this,Z,`Bearer ${n==null?void 0:n.access_token}`),window.sessionStorage.setItem(te,((o=this.bearerProfile)==null?void 0:o.refresh_token)||"")}).catch(n=>{throw sessionStorage.removeItem(N),sessionStorage.removeItem(te),new Error(n==null?void 0:n.message)}),ce(e)&&Me(),this.bearerProfile}getAuthContext(){return T(this,E,B).call(this,"get","auth/context")}getProfile(e){return T(this,E,B).call(this,"get",`profiles/${e}`)}getBalances(e){return e?T(this,E,B).call(this,"get",`profiles/${e}/balances`):T(this,E,B).call(this,"get","balances")}getOrders(e){const t=Y(e);return T(this,E,B).call(this,"get",`orders?${t}`)}getOrder(e){return T(this,E,B).call(this,"get",`orders/${e}`)}getTokens(){return T(this,E,B).call(this,"get","tokens")}linkAddress(e,t){return T(this,E,B).call(this,"post",`profiles/${e}/addresses`,JSON.stringify(t))}placeOrder(e,t){const n={...e,kind:"redeem",currency:"eur"};return t?T(this,E,B).call(this,"post",`profiles/${t}/orders`,JSON.stringify(n)):T(this,E,B).call(this,"post","orders",JSON.stringify(n))}uploadSupportingDocument(e){const t=Y(e);return T(this,E,B).call(this,"post","files/supporting-document",t,!0)}async connectOrderSocket(){var e;(e=this.bearerProfile)!=null&&e.access_token&&m(this,D).size>0&&C(this,F,this.subscribeToOrderNotifications())}async disconnect(){var e;sessionStorage.removeItem(N),m(this,D).clear(),(e=m(this,F))==null||e.close()}subscribeOrders(e,t){m(this,D).set(e,t)}unsubscribeOrders(e){var t;m(this,D).delete(e),m(this,D).size===0&&((t=m(this,F))==null||t.close(),C(this,F,void 0))}}j=new WeakMap,Z=new WeakMap,F=new WeakMap,D=new WeakMap,$=new WeakMap,E=new WeakSet,B=async function(i,e,t,n){return Pe(`${m(this,j).api}/${e}`,i,n?Y(t):t,{Authorization:m(this,Z)||"","Content-Type":`application/${n?"x-www-form-urlencoded":"json"}`})},G=new WeakMap,K=new WeakMap,Q=new WeakMap;const Te=({children:i,clientId:e="f99e629b-6dca-11ee-8aa6-5273f65ed05b",redirectUrl:t="http://localhost:5173",environment:n="sandbox"})=>{const[o,a]=S.useState(),[d,O]=S.useState(!1),[f,_]=S.useState(null),[w,v]=S.useState(null),[k,h]=S.useState(!1),[g,c]=S.useState(!1),[p,I]=S.useState(!1),[r,s]=S.useState(!1),[l,u]=S.useState(null),[y,b]=S.useState([]),[x,A]=S.useState([]);S.useEffect(()=>{const M=new Ce({environment:n,clientId:e,redirectUrl:t});a(M)},[]),S.useEffect(()=>((async()=>{o&&O(await o.getAccess())})(),()=>{o&&o.disconnect()}),[o]),S.useEffect(()=>{(async()=>{if(o&&d)try{h(!0);const P=await o.getAuthContext(),U=await o.getProfile(P.defaultProfile),H=await o.getBalances(),J=await o.getOrders(),V=await o.getTokens();_(U),v(H),b(J),A(V)}catch(P){console.error("Error fetching data:",P),u(P)}finally{h(!1)}})()},[o,d]);const W=S.useCallback(async()=>{try{o&&await o.authorize()}catch(M){console.error("Error during authorization:",M),u(M)}},[o]),z=S.useCallback(async()=>{if(o&&d)try{s(!0);const M=await o.getBalances();v(M)}catch(M){console.error("Error getting balances:",M),u(M)}finally{s(!1)}},[o,d]),L=S.useCallback(async(M,P)=>{if(o&&d)try{c(!0);let U;parseInt(M.amount)>15e3&&P&&(U=(await o.uploadSupportingDocument(P)).id);const H={...M,documentId:U},J=await o.placeOrder(H);b(V=>[...V,J])}catch(U){console.error("Error placing order:",U),u(U)}finally{c(!1)}},[o,d]),X=S.useCallback(async M=>{if(o&&d&&f)try{return I(!0),await o.linkAddress(f.id,M)}catch(P){console.error("Error linking address:",P),u(P)}finally{I(!1)}},[o,d,f]);return fe.jsx(ne.Provider,{value:{authorize:W,isAuthorized:d,profile:f,balances:w,loading:k,loadingPlaceOrder:g,loadingLinkAddress:p,loadingBalances:r,getBalances:z,linkAddress:X,placeOrder:L,orders:y,tokens:x,error:l},children:i})};function ze(){const i=S.useContext(ne);if(i===null)throw new Error("useMonerium must be used within a MoneriumProvider");return i}exports.MoneriumContext=ne;exports.MoneriumProvider=Te;exports.useMonerium=ze;
|
|
1
|
+
"use strict";var se=(s,e,t)=>{if(!e.has(s))throw TypeError("Cannot "+t)};var y=(s,e,t)=>(se(s,e,"read from private field"),t?t.call(s):e.get(s)),$=(s,e,t)=>{if(e.has(s))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(s):e.set(s,t)},O=(s,e,t,n)=>(se(s,e,"write to private field"),n?n.call(s,t):e.set(s,t),t);var P=(s,e,t)=>(se(s,e,"access private method"),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const ve=require("react/jsx-runtime"),k=require("react"),ae=k.createContext(null),re=s=>{var e;return s&&((e=Object.entries(s))==null?void 0:e.length)>0?Object.entries(s).map(([t,n])=>`${encodeURIComponent(t)}=${encodeURIComponent(n)}`).join("&"):""},ge=s=>{switch(s){case 1:case 5:return"ethereum";case 100:case 10200:return"gnosis";case 137:case 80001:return"polygon";default:throw new Error(`Chain not supported: ${s}`)}},we=s=>{switch(s){case 1:case 100:case 137:return"mainnet";case 5:return"goerli";case 10200:return"chiado";case 80001:return"mumbai";default:throw new Error(`Network not supported: ${s}`)}},oe={environments:{production:{api:"https://api.monerium.app",web:"https://monerium.app",wss:"wss://api.monerium.app"},sandbox:{api:"https://api.monerium.dev",web:"https://sandbox.monerium.dev",wss:"wss://api.monerium.dev"}}},V="monerium.sdk.code_verifier",te="monerium.sdk.refresh_token";var J=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{};function de(s){return s&&s.__esModule&&Object.prototype.hasOwnProperty.call(s,"default")?s.default:s}function ye(s){if(s.__esModule)return s;var e=s.default;if(typeof e=="function"){var t=function n(){return this instanceof n?Reflect.construct(e,arguments,this.constructor):e.apply(this,arguments)};t.prototype=e.prototype}else t={};return Object.defineProperty(t,"__esModule",{value:!0}),Object.keys(s).forEach(function(n){var i=Object.getOwnPropertyDescriptor(s,n);Object.defineProperty(t,n,i.get?i:{enumerable:!0,get:function(){return s[n]}})}),t}var he={exports:{}};function me(s){throw new Error('Could not dynamically require "'+s+'". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.')}var ie={exports:{}};const _e={},be=Object.freeze(Object.defineProperty({__proto__:null,default:_e},Symbol.toStringTag,{value:"Module"})),Se=ye(be);var ce;function le(){return ce||(ce=1,function(s,e){(function(t,n){s.exports=n()})(J,function(){var t=t||function(n,i){var u;if(typeof window<"u"&&window.crypto&&(u=window.crypto),typeof self<"u"&&self.crypto&&(u=self.crypto),typeof globalThis<"u"&&globalThis.crypto&&(u=globalThis.crypto),!u&&typeof window<"u"&&window.msCrypto&&(u=window.msCrypto),!u&&typeof J<"u"&&J.crypto&&(u=J.crypto),!u&&typeof me=="function")try{u=Se}catch{}var d=function(){if(u){if(typeof u.getRandomValues=="function")try{return u.getRandomValues(new Uint32Array(1))[0]}catch{}if(typeof u.randomBytes=="function")try{return u.randomBytes(4).readInt32LE()}catch{}}throw new Error("Native crypto module could not be used to get secure random number.")},C=Object.create||function(){function r(){}return function(o){var c;return r.prototype=o,c=new r,r.prototype=null,c}}(),l={},w=l.lib={},m=w.Base=function(){return{extend:function(r){var o=C(this);return r&&o.mixIn(r),(!o.hasOwnProperty("init")||this.init===o.init)&&(o.init=function(){o.$super.init.apply(this,arguments)}),o.init.prototype=o,o.$super=this,o},create:function(){var r=this.extend();return r.init.apply(r,arguments),r},init:function(){},mixIn:function(r){for(var o in r)r.hasOwnProperty(o)&&(this[o]=r[o]);r.hasOwnProperty("toString")&&(this.toString=r.toString)},clone:function(){return this.init.prototype.extend(this)}}}(),g=w.WordArray=m.extend({init:function(r,o){r=this.words=r||[],o!=i?this.sigBytes=o:this.sigBytes=r.length*4},toString:function(r){return(r||h).stringify(this)},concat:function(r){var o=this.words,c=r.words,f=this.sigBytes,_=r.sigBytes;if(this.clamp(),f%4)for(var b=0;b<_;b++){var x=c[b>>>2]>>>24-b%4*8&255;o[f+b>>>2]|=x<<24-(f+b)%4*8}else for(var A=0;A<_;A+=4)o[f+A>>>2]=c[A>>>2];return this.sigBytes+=_,this},clamp:function(){var r=this.words,o=this.sigBytes;r[o>>>2]&=4294967295<<32-o%4*8,r.length=n.ceil(o/4)},clone:function(){var r=m.clone.call(this);return r.words=this.words.slice(0),r},random:function(r){for(var o=[],c=0;c<r;c+=4)o.push(d());return new g.init(o,r)}}),S=l.enc={},h=S.Hex={stringify:function(r){for(var o=r.words,c=r.sigBytes,f=[],_=0;_<c;_++){var b=o[_>>>2]>>>24-_%4*8&255;f.push((b>>>4).toString(16)),f.push((b&15).toString(16))}return f.join("")},parse:function(r){for(var o=r.length,c=[],f=0;f<o;f+=2)c[f>>>3]|=parseInt(r.substr(f,2),16)<<24-f%8*4;return new g.init(c,o/2)}},v=S.Latin1={stringify:function(r){for(var o=r.words,c=r.sigBytes,f=[],_=0;_<c;_++){var b=o[_>>>2]>>>24-_%4*8&255;f.push(String.fromCharCode(b))}return f.join("")},parse:function(r){for(var o=r.length,c=[],f=0;f<o;f++)c[f>>>2]|=(r.charCodeAt(f)&255)<<24-f%4*8;return new g.init(c,o)}},a=S.Utf8={stringify:function(r){try{return decodeURIComponent(escape(v.stringify(r)))}catch{throw new Error("Malformed UTF-8 data")}},parse:function(r){return v.parse(unescape(encodeURIComponent(r)))}},p=w.BufferedBlockAlgorithm=m.extend({reset:function(){this._data=new g.init,this._nDataBytes=0},_append:function(r){typeof r=="string"&&(r=a.parse(r)),this._data.concat(r),this._nDataBytes+=r.sigBytes},_process:function(r){var o,c=this._data,f=c.words,_=c.sigBytes,b=this.blockSize,x=b*4,A=_/x;r?A=n.ceil(A):A=n.max((A|0)-this._minBufferSize,0);var D=A*b,M=n.min(D*4,_);if(D){for(var W=0;W<D;W+=b)this._doProcessBlock(f,W);o=f.splice(0,D),c.sigBytes-=M}return new g.init(o,M)},clone:function(){var r=m.clone.call(this);return r._data=this._data.clone(),r},_minBufferSize:0});w.Hasher=p.extend({cfg:m.extend(),init:function(r){this.cfg=this.cfg.extend(r),this.reset()},reset:function(){p.reset.call(this),this._doReset()},update:function(r){return this._append(r),this._process(),this},finalize:function(r){r&&this._append(r);var o=this._doFinalize();return o},blockSize:16,_createHelper:function(r){return function(o,c){return new r.init(c).finalize(o)}},_createHmacHelper:function(r){return function(o,c){return new B.HMAC.init(r,c).finalize(o)}}});var B=l.algo={};return l}(Math);return t})}(ie)),ie.exports}(function(s,e){(function(t,n){s.exports=n(le())})(J,function(t){return function(){var n=t,i=n.lib,u=i.WordArray,d=n.enc;d.Base64url={stringify:function(l,w){w===void 0&&(w=!0);var m=l.words,g=l.sigBytes,S=w?this._safe_map:this._map;l.clamp();for(var h=[],v=0;v<g;v+=3)for(var a=m[v>>>2]>>>24-v%4*8&255,p=m[v+1>>>2]>>>24-(v+1)%4*8&255,B=m[v+2>>>2]>>>24-(v+2)%4*8&255,r=a<<16|p<<8|B,o=0;o<4&&v+o*.75<g;o++)h.push(S.charAt(r>>>6*(3-o)&63));var c=S.charAt(64);if(c)for(;h.length%4;)h.push(c);return h.join("")},parse:function(l,w){w===void 0&&(w=!0);var m=l.length,g=w?this._safe_map:this._map,S=this._reverseMap;if(!S){S=this._reverseMap=[];for(var h=0;h<g.length;h++)S[g.charCodeAt(h)]=h}var v=g.charAt(64);if(v){var a=l.indexOf(v);a!==-1&&(m=a)}return C(l,m,S)},_map:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",_safe_map:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"};function C(l,w,m){for(var g=[],S=0,h=0;h<w;h++)if(h%4){var v=m[l.charCodeAt(h-1)]<<h%4*2,a=m[l.charCodeAt(h)]>>>6-h%4*2,p=v|a;g[S>>>2]|=p<<24-S%4*8,S++}return u.create(g,S)}}(),t.enc.Base64url})})(he);var Ce=he.exports;const ke=de(Ce);var pe={exports:{}};(function(s,e){(function(t,n){s.exports=n(le())})(J,function(t){return function(n){var i=t,u=i.lib,d=u.WordArray,C=u.Hasher,l=i.algo,w=[],m=[];(function(){function h(B){for(var r=n.sqrt(B),o=2;o<=r;o++)if(!(B%o))return!1;return!0}function v(B){return(B-(B|0))*4294967296|0}for(var a=2,p=0;p<64;)h(a)&&(p<8&&(w[p]=v(n.pow(a,1/2))),m[p]=v(n.pow(a,1/3)),p++),a++})();var g=[],S=l.SHA256=C.extend({_doReset:function(){this._hash=new d.init(w.slice(0))},_doProcessBlock:function(h,v){for(var a=this._hash.words,p=a[0],B=a[1],r=a[2],o=a[3],c=a[4],f=a[5],_=a[6],b=a[7],x=0;x<64;x++){if(x<16)g[x]=h[v+x]|0;else{var A=g[x-15],D=(A<<25|A>>>7)^(A<<14|A>>>18)^A>>>3,M=g[x-2],W=(M<<15|M>>>17)^(M<<13|M>>>19)^M>>>10;g[x]=D+g[x-7]+W+g[x-16]}var ne=c&f^~c&_,E=p&B^p&r^B&r,z=(p<<30|p>>>2)^(p<<19|p>>>13)^(p<<10|p>>>22),L=(c<<26|c>>>6)^(c<<21|c>>>11)^(c<<7|c>>>25),N=b+L+ne+m[x]+g[x],K=z+E;b=_,_=f,f=c,c=o+N|0,o=r,r=B,B=p,p=N+K|0}a[0]=a[0]+p|0,a[1]=a[1]+B|0,a[2]=a[2]+r|0,a[3]=a[3]+o|0,a[4]=a[4]+c|0,a[5]=a[5]+f|0,a[6]=a[6]+_|0,a[7]=a[7]+b|0},_doFinalize:function(){var h=this._data,v=h.words,a=this._nDataBytes*8,p=h.sigBytes*8;return v[p>>>5]|=128<<24-p%32,v[(p+64>>>9<<4)+14]=n.floor(a/4294967296),v[(p+64>>>9<<4)+15]=a,h.sigBytes=v.length*4,this._process(),this._hash},clone:function(){var h=C.clone.call(this);return h._hash=this._hash.clone(),h}});i.SHA256=C._createHelper(S),i.HmacSHA256=C._createHmacHelper(S)}(Math),t.SHA256})})(pe);var Be=pe.exports;const xe=de(Be),Ae=(s,e)=>{const{client_id:t,redirect_uri:n,scope:i,state:u,chainId:d,chain:C,network:l,address:w,signature:m}=s,g=w?{address:w,...m!==void 0?{signature:m}:{},...d!==void 0||C!==void 0?{chain:d?ge(d):C}:{},...d!==void 0||l!==void 0?{network:d?we(d):l}:{}}:{};return re({client_id:t,redirect_uri:n,...i!==void 0?{scope:i}:{},...u!==void 0?{state:u}:{},code_challenge:e,code_challenge_method:"S256",response_type:"code",...g})},Ee=()=>{let s="";const e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",t=e.length;let n=0;for(;n<128;)s+=e.charAt(Math.floor(Math.random()*t)),n+=1;return s},Pe=s=>ke.stringify(xe(s)),ue=(s,e)=>{const t=Ee(),n=Pe(t);return sessionStorage.setItem(V,t||""),`${s}/auth?${Ae(e,n)}`},Ie=()=>{const s=window.location.href,[e,t]=s.split("?");t&&window.history.replaceState(null,"",e)},fe=s=>s.code!=null,Oe=s=>s.refresh_token!=null,Re=s=>s.client_secret!=null,ze=async(s,e,t,n)=>{const i=await fetch(`${s}`,{method:e,headers:n,body:t});let u;const d=await i.text();try{u=JSON.parse(d)}catch{throw d}if(!i.ok)throw u;return u},j=typeof window>"u";var U,Q,F,T,H,q,G,I,R,X,Y,Z;class $e{constructor(e){$(this,q);$(this,I);$(this,U,void 0);$(this,Q,void 0);$(this,F,void 0);$(this,T,void 0);$(this,H,void 0);$(this,X,void 0);$(this,Y,void 0);$(this,Z,void 0);if(O(this,T,new Map),this.isAuthorized=!!this.bearerProfile,O(this,X,async(t,n,i)=>{const u=sessionStorage.getItem(V)||"";if(!u)throw new Error("Code verifier not found");return this.codeVerifier=u,sessionStorage.removeItem(V),await P(this,q,G).call(this,{code:i,redirect_uri:n,client_id:t,code_verifier:u})}),O(this,Y,async({clientId:t,clientSecret:n})=>await P(this,q,G).call(this,{client_id:t,client_secret:n})),O(this,Z,async(t,n)=>await P(this,q,G).call(this,{refresh_token:n,client_id:t})),this.subscribeToOrderNotifications=()=>{var i,u;const t=`${y(this,U).wss}/profiles/${(i=this.bearerProfile)==null?void 0:i.profile}/orders?access_token=${(u=this.bearerProfile)==null?void 0:u.access_token}`,n=new WebSocket(t);return n.addEventListener("open",()=>{console.info(`Socket connected: ${t}`)}),n.addEventListener("error",d=>{throw console.error(d),new Error(`Socket error: ${t}`)}),n.addEventListener("message",d=>{var l;const C=JSON.parse(d.data);(l=y(this,T).get(C.meta.state))==null||l(C)}),n.addEventListener("close",()=>{console.info(`Socket connection closed: ${t}`)}),n},this.auth=async t=>await P(this,q,G).call(this,t),this.connect=async t=>await P(this,q,G).call(this,t),this.getAuthFlowURI=t=>{const n=ue(y(this,U).api,t);return this.codeVerifier=sessionStorage.getItem(V),n},this.pkceRequest=t=>this.getAuthFlowURI(t),this.getEnvironment=()=>y(this,U),!e){O(this,U,oe.environments.sandbox);return}if(typeof e=="string")O(this,U,oe.environments[e]);else if(O(this,U,oe.environments[e.environment||"sandbox"]),j){const{clientId:t,clientSecret:n}=e;O(this,H,{clientId:t,clientSecret:n})}else{const{clientId:t,redirectUrl:n}=e;O(this,H,{clientId:t,redirectUrl:n})}}async authorize(e){var u,d;const t=(e==null?void 0:e.clientId)||((u=y(this,H))==null?void 0:u.clientId),n=(e==null?void 0:e.redirectUrl)||((d=y(this,H))==null?void 0:d.redirectUrl);if(!t)throw new Error("Missing ClientId");if(!n)throw new Error("Missing RedirectUrl");const i=ue(y(this,U).api,{client_id:t,redirect_uri:n,address:e==null?void 0:e.address,signature:e==null?void 0:e.signature,chainId:e==null?void 0:e.chainId});window.location.replace(i)}async getAccess(e){var C,l,w;const t=(e==null?void 0:e.clientId)||((C=y(this,H))==null?void 0:C.clientId);if((e==null?void 0:e.clientSecret)||((l=y(this,H))==null?void 0:l.clientSecret)){if(!j)throw new Error("Only use client credentials on server side");return await y(this,Y).call(this,y(this,H)),!!this.bearerProfile}const i=(e==null?void 0:e.redirectUrl)||((w=y(this,H))==null?void 0:w.redirectUrl);if(!t)throw new Error("Missing ClientId");if(j)throw new Error("This only works client side");const u=new URLSearchParams(window.location.search).get("code")||void 0,d=sessionStorage.getItem(te)||void 0;return d?await y(this,Z).call(this,t,d):u&&await y(this,X).call(this,t,i,u),!!this.bearerProfile}getAuthContext(){return P(this,I,R).call(this,"get","auth/context")}getProfile(e){return P(this,I,R).call(this,"get",`profiles/${e}`)}getBalances(e){return e?P(this,I,R).call(this,"get",`profiles/${e}/balances`):P(this,I,R).call(this,"get","balances")}getOrders(e){const t=re(e);return P(this,I,R).call(this,"get",`orders?${t}`)}getOrder(e){return P(this,I,R).call(this,"get",`orders/${e}`)}getTokens(){return P(this,I,R).call(this,"get","tokens")}linkAddress(e,t){return P(this,I,R).call(this,"post",`profiles/${e}/addresses`,JSON.stringify(t))}placeOrder(e,t){const n={...e,kind:"redeem",currency:"eur"};return t?P(this,I,R).call(this,"post",`profiles/${t}/orders`,JSON.stringify(n)):P(this,I,R).call(this,"post","orders",JSON.stringify(n))}uploadSupportingDocument(e){const t=re(e);return P(this,I,R).call(this,"post","files/supporting-document",t,!0)}async connectOrderSocket(){var e;(e=this.bearerProfile)!=null&&e.access_token&&y(this,T).size>0&&O(this,F,this.subscribeToOrderNotifications())}async disconnect(){var e;j||sessionStorage.removeItem(V),y(this,T).clear(),(e=y(this,F))==null||e.close()}async revokeAccess(){j||sessionStorage.removeItem(te),this.disconnect()}subscribeOrders(e,t){y(this,T).set(e,t)}unsubscribeOrders(e){var t;y(this,T).delete(e),y(this,T).size===0&&((t=y(this,F))==null||t.close(),O(this,F,void 0))}}U=new WeakMap,Q=new WeakMap,F=new WeakMap,T=new WeakMap,H=new WeakMap,q=new WeakSet,G=async function(e){let t;if(fe(e))t={...e,grant_type:"authorization_code"};else if(Oe(e))t={...e,grant_type:"refresh_token"};else if(Re(e))t={...e,grant_type:"client_credentials"};else throw new Error("Authorization grant type could not be detected.");return await P(this,I,R).call(this,"post","auth/token",t,!0).then(n=>{var i;this.bearerProfile=n,this.isAuthorized=!!n,O(this,Q,`Bearer ${n==null?void 0:n.access_token}`),j||window.sessionStorage.setItem(te,((i=this.bearerProfile)==null?void 0:i.refresh_token)||"")}).catch(n=>{throw j||(sessionStorage.removeItem(V),sessionStorage.removeItem(te)),new Error(n==null?void 0:n.message)}),fe(e)&&Ie(),this.bearerProfile},I=new WeakSet,R=async function(e,t,n,i){return ze(`${y(this,U).api}/${t}`,e,i?re(n):n,{Authorization:y(this,Q)||"","Content-Type":`application/${i?"x-www-form-urlencoded":"json"}`})},X=new WeakMap,Y=new WeakMap,Z=new WeakMap;const Ue=({children:s,clientId:e="f99e629b-6dca-11ee-8aa6-5273f65ed05b",redirectUrl:t="http://localhost:5173",environment:n="sandbox"})=>{const[i,u]=k.useState(),[d,C]=k.useState(!1),[l,w]=k.useState(null),[m,g]=k.useState(null),[S,h]=k.useState(!1),[v,a]=k.useState(!1),[p,B]=k.useState(!1),[r,o]=k.useState(!1),[c,f]=k.useState(null),[_,b]=k.useState([]),[x,A]=k.useState([]);k.useEffect(()=>{const E=new $e({environment:n,clientId:e,redirectUrl:t});u(E)},[]),k.useEffect(()=>((async()=>{i&&C(await i.getAccess())})(),()=>{i&&i.disconnect()}),[i]),k.useEffect(()=>{(async()=>{if(i&&d)try{h(!0);const z=await i.getAuthContext(),L=await i.getProfile(z.defaultProfile),N=await i.getBalances(),K=await i.getOrders(),ee=await i.getTokens();w(L),g(N),b(K),A(ee)}catch(z){console.error("Error fetching data:",z),f(z)}finally{h(!1)}})()},[i,d]);const D=k.useCallback(async()=>{try{i&&await i.authorize()}catch(E){console.error("Error during authorization:",E),f(E)}},[i]),M=k.useCallback(async()=>{if(i&&d)try{o(!0);const E=await i.getBalances();g(E)}catch(E){console.error("Error getting balances:",E),f(E)}finally{o(!1)}},[i,d]),W=k.useCallback(async(E,z)=>{if(i&&d)try{a(!0);let L;parseInt(E.amount)>15e3&&z&&(L=(await i.uploadSupportingDocument(z)).id);const N={...E,documentId:L},K=await i.placeOrder(N);b(ee=>[...ee,K])}catch(L){console.error("Error placing order:",L),f(L)}finally{a(!1)}},[i,d]),ne=k.useCallback(async E=>{if(i&&d&&l)try{return B(!0),await i.linkAddress(l.id,E)}catch(z){console.error("Error linking address:",z),f(z)}finally{B(!1)}},[i,d,l]);return ve.jsx(ae.Provider,{value:{authorize:D,isAuthorized:d,profile:l,balances:m,loading:S,loadingPlaceOrder:v,loadingLinkAddress:p,loadingBalances:r,getBalances:M,linkAddress:ne,placeOrder:W,orders:_,tokens:x,error:c},children:s})};function He(){const s=k.useContext(ae);if(s===null)throw new Error("useMonerium must be used within a MoneriumProvider");return s}exports.MoneriumContext=ae;exports.MoneriumProvider=Ue;exports.useMonerium=He;
|