1import{dD as e,iW as t,g5 as s,iX as n,en as o,g4 as r,dO as i,g7 as a}from"./index-Dmu6oiBK.js";class c{constructor(e){this.getNonce=e.getNonce}async createMessage(e){const t={accountAddress:e.accountAddress,chainId:e.chainId,version:"1",domain:"undefined"==typeof document?"Unknown Domain":document.location.host,uri:"undefined"==typeof document?"Unknown URI":document.location.href,resources:this.resources,nonce:await this.getNonce(e),issuedAt:this.stringifyDate(new Date),statement:void 0,expirationTime:void 0,notBefore:void 0},s={toString:()=>this.stringify(t)};return Object.assign(t,s)}stringify(e){var t;const s=this.getNetworkName(e.chainId);return[`${e.domain} wants you to sign in with your ${s} account:`,e.accountAddress,e.statement?`\n${e.statement}\n`:"",`URI: ${e.uri}`,`Version: ${e.version}`,`Chain ID: ${e.chainId}`,`Nonce: ${e.nonce}`,e.issuedAt&&`Issued At: ${e.issuedAt}`,e.expirationTime&&`Expiration Time: ${e.expirationTime}`,e.notBefore&&`Not Before: ${e.notBefore}`,e.requestId&&`Request ID: ${e.requestId}`,(null==(t=e.resources)?void 0:t.length)&&e.resources.reduce((e,t)=>`${e}\n- ${t}`,"Resources:")].filter(e=>"string"==typeof e).join("\n").trim()}getNetworkName(s){const n=e.getAllRequestedCaipNetworks();return t.getNetworkNameByCaipNetworkId(n,s)}stringifyDate(e){return e.toISOString()}}class u{constructor(e={}){this.otpUuid=null,this.listeners={sessionChanged:[]},this.localAuthStorageKey=e.localAuthStorageKey||s.SIWX_AUTH_TOKEN,this.localNonceStorageKey=e.localNonceStorageKey||s.SIWX_NONCE_TOKEN,this.required=e.required??!0,this.messenger=new c({getNonce:this.getNonce.bind(this)})}async createMessage(e){return this.messenger.createMessage(e)}async addSession(e){const t=await this.request({method:"POST",key:"authenticate",body:{data:e.data,message:e.message,signature:e.signature,clientId:this.getClientId(),walletInfo:this.getWalletInfo()},headers:["nonce","otp"]});this.setStorageToken(t.token,this.localAuthStorageKey),this.emit("sessionChanged",e),this.setAppKitAccountUser(function(e){const t=e.split(".");if(3!==t.length)throw new Error("Invalid token");const s=t[1];if("string"!=typeof s)throw new Error("Invalid token");const n=s.replace(/-/gu,"+").replace(/_/gu,"/"),o=n.padEnd(n.length+(4-n.length%4)%4,"=");return JSON.parse(atob(o))}(t.token)),this.otpUuid=null}async getSessions(e,t){try{if(!this.getStorageToken(this.localAuthStorageKey))return[];const s=await this.request({method:"GET",key:"me",query:{},headers:["auth"]});if(!s)return[];const n=s.address.toLowerCase()===t.toLowerCase(),o=s.caip2Network===e;if(!n||!o)return[];const r={data:{accountAddress:s.address,chainId:s.caip2Network},message:"",signature:""};return this.emit("sessionChanged",r),this.setAppKitAccountUser(s),[r]}catch{return[]}}async revokeSession(e,t){return Promise.resolve(this.clearStorageTokens())}async setSessions(e){if(0===e.length)this.clearStorageTokens();else{const t=e.find(e=>{var t;return e.data.chainId===(null==(t=n())?void 0:t.caipNetworkId)})||e[0];await this.addSession(t)}}getRequired(){return this.required}async getSessionAccount(){if(!this.getStorageToken(this.localAuthStorageKey))throw new Error("Not authenticated");return this.request({method:"GET",key:"me",body:void 0,query:{includeAppKitAccount:!0},headers:["auth"]})}async setSessionAccountMetadata(e=null){if(!this.getStorageToken(this.localAuthStorageKey))throw new Error("Not authenticated");return this.request({method:"PUT",key:"account-metadata",body:{metadata:e},headers:["auth"]})}on(e,t){return this.listeners[e].push(t),()=>{this.listeners[e]=this.listeners[e].filter(e=>e!==t)}}removeAllListeners(){Object.keys(this.listeners).forEach(e=>{this.listeners[e]=[]})}async requestEmailOtp({email:e,account:t}){const s=await this.request({method:"POST",key:"otp",body:{email:e,account:t}});return this.otpUuid=s.uuid,this.messenger.resources=[`email:${e}`],s}confirmEmailOtp({code:e}){return this.request({method:"PUT",key:"otp",body:{code:e},headers:["otp"]})}async request({method:e,key:t,query:s,body:n,headers:r}){var i;const{projectId:a,st:c,sv:u}=this.getSDKProperties(),h=new URL(`${o.W3M_API_URL}/auth/v1/${String(t)}`);h.searchParams.set("projectId",a),h.searchParams.set("st",c),h.searchParams.set("sv",u),s&&Object.entries(s).forEac
1h(([e,t])=>h.searchParams.set(e,String(t)));const d=await fetch(h,{method:e,body:n?JSON.stringify(n):void 0,headers:Array.isArray(r)?r.reduce((e,t)=>{switch(t){case"nonce":e["x-nonce-jwt"]=`Bearer ${this.getStorageToken(this.localNonceStorageKey)}`;break;case"auth":e.Authorization=`Bearer ${this.getStorageToken(this.localAuthStorageKey)}`;break;case"otp":this.otpUuid&&(e["x-otp"]=this.otpUuid)}return e},{}):void 0});if(!d.ok)throw new Error(await d.text());return(null==(i=d.headers.get("content-type"))?void 0:i.includes("application/json"))?d.json():null}getStorageToken(e){return r.getItem(e)}setStorageToken(e,t){r.setItem(t,e)}clearStorageTokens(){this.otpUuid=null,r.removeItem(this.localAuthStorageKey),r.removeItem(this.localNonceStorageKey),this.emit("sessionChanged",void 0)}async getNonce(){const{nonce:e,token:t}=await this.request({method:"GET",key:"nonce"});return this.setStorageToken(t,this.localNonceStorageKey),e}getClientId(){return i.state.clientId}getWalletInfo(){var t;const s=null==(t=e.getAccountData())?void 0:t.connectedWalletInfo;if(!s)return;if("social"in s&&"identifier"in s){return{type:"social",social:s.social,identifier:s.identifier}}const{name:n,icon:o}=s;let r="unknown";switch(s.type){case"EXTERNAL":case"INJECTED":case"ANNOUNCED":r="extension";break;case"WALLET_CONNECT":r="walletconnect";break;default:r="unknown"}return{type:r,name:n,icon:o}}getSDKProperties(){return a._getSdkProperties()}emit(e,t){this.listeners[e].forEach(e=>e(t))}setAppKitAccountUser(t){const{email:s}=t;
1s&&Object.values(o.CHAIN).forEach(t=>{e.setAccountProp("user",{email:s},t)})}}export{u as ReownAuthentication,c as ReownAuthenticationMessenger};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.