@privy-io/react-auth 1.12.0-beta.8 → 1.12.1-beta.1

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/dist/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import{useEffect as Ot,useState as re}from"react";import Vt from"axios";import gi from"axios";var z=class extends Error{constructor(o,r){super(o);r instanceof Error&&(this.cause=r)}toString(){return`${this.type}: ${this.message}${this.cause?` [cause: ${this.cause}]`:""}`}},et=class extends z{constructor(o,r,i,n){super(i,n);this.type=o;this.status=r}},u=class extends z{constructor(o,r){super(o,r);this.type="client_error"}};var V=class extends z{constructor(o,r){super(o,r);this.type="connector_error"}},I=t=>{if(t instanceof z)return t;if(!gi.isAxiosError(t))return H(t);if(!t.response)return new et("api_error",null,t.message,t);let{type:e,message:o,error:r}=t.response.data;return new et(e||"ApiError",t.response.status,o||r,t)},H=t=>t instanceof z?t:t instanceof Error?new u(t.message,t):new u(`Internal error: ${t}`);var wo="1.12.0-beta.8";var yo="https://api.privy.io/v0",bo=process.env.NEXT_PUBLIC_PRIVY_AUTH_URL||"https://auth.privy.io",tt="4df5e2316331463a9130964bd6078dfa",Co=1e4,Ee=1400,vo=365*24*60*60,Gt=wo,xo="ES256",Lo="privy.io",ke="privy:token",ot="privy:refresh_token",K="privy:active_wallet",rt="privy:wallet_type",Ao="walletconnect";var it=class{constructor(e,o,r){this.appId=e,this.sdkVersion=Gt,this.session=o,this.defaults=r}async get(e,o){try{return await Vt.get(e,this.buildConfig({headers:{"Cache-Control":"no-cache"},...o}))}catch(r){throw I(r)}}async post(e,o,r){try{return await Vt.post(e,o,this.buildConfig(r))}catch(i){throw I(i)}}async delete(e,o){try{return await Vt.delete(e,this.buildConfig(o))}catch(r){throw I(r)}}buildConfig(e){return e=e||{},e.headers=e.headers||{},e.headers["privy-app-id"]=this.appId,e.headers["privy-client"]=`react-auth:${this.sdkVersion}`,this.session.token!==null&&(e.headers.authorization=`Bearer ${this.session.token}`),{...this.defaults,...e}}};import*as _e from"jose";var ne=class{static parse(e){try{return new ne(e)}catch{return null}}constructor(e){this.value=e,this._decoded=_e.decodeJwt(e)}get subject(){return this._decoded.sub}get expiration(){return this._decoded.exp}get issuer(){return this._decoded.iss}get audience(){return this._decoded.aud}isExpired(e=0){let o=Date.now(),r=(this.expiration-e)*1e3;return o>=r}async isValid(e,o,r=0){let i=await _e.importSPKI(e,xo);try{return await _e.jwtVerify(this.value,i,{issuer:Lo,audience:o,clockTolerance:r}),!0}catch{return!1}}};var se=class{constructor(e){this.promise=null;this.fn=e}execute(e){return this.promise===null&&(this.promise=(async()=>{try{return await this.fn(e)}finally{this.promise=null}})()),this.promise}};var Ht=class{constructor(){this._cache={}}get(e){return this._cache[e]}put(e,o){o!==void 0?this._cache[e]=o:this.del(e)}del(e){delete this._cache[e]}},Bt=class{get(e){let o=localStorage.getItem(e);return o===null?void 0:JSON.parse(o)}put(e,o){o!==void 0?localStorage.setItem(e,JSON.stringify(o)):this.del(e)}del(e){localStorage.removeItem(e)}};function yi(){return typeof window<"u"&&window.localStorage?new Bt:new Ht}var E=yi();function ae(t){return new Date(t*1e3)}function bi(t){return t.map(e=>{switch(e.type){case"wallet":return{address:e.address,type:e.type,verifiedAt:ae(e.verified_at),chainType:e.chain_type};case"email":return{address:e.address,type:e.type,verifiedAt:ae(e.verified_at)};case"phone":return{number:e.phoneNumber,type:e.type,verifiedAt:ae(e.verified_at)};case"google_oauth":return{subject:e.subject,email:e.email,name:e.name,type:e.type,verifiedAt:ae(e.verified_at)};case"twitter_oauth":return{subject:e.subject,username:e.username,name:e.name,type:e.type,verifiedAt:ae(e.verified_at)};case"discord_oauth":return{subject:e.subject,username:e.username,email:e.email,type:e.type,verifiedAt:ae(e.verified_at)};case"github_oauth":return{subject:e.subject,username:e.username,name:e.name,email:e.email,type:e.type,verifiedAt:ae(e.verified_at)};default:throw new u("Unrecognized account type")}})}function Ci(t,e){return e.find(o=>o.type==="wallet"&&o.address===t)}function le(t,e){return t.sort((o,r)=>r.verifiedAt.getTime()-o.verifiedAt.getTime()),t.find(o=>o.type===e)}function Fe(t){let e=t.linkedAccounts.map(s=>s.type==="wallet"?s.address:null),r=E.get(K);if(!r||!e.includes(r)){E.del(K);let s=le(t.linkedAccounts,"wallet");s&&(r=s.address,E.put(K,r))}let i=Ci(r,t.linkedAccounts),n=Object.assign({},t);return n.wallet=i&&{address:i.address,chainType:i.chainType,chainId:i.chainId,walletType:i.walletType},n}function Y(t){if(!t)return null;let e=bi(t.linked_accounts),o=le(e,"wallet"),r=le(e,"email"),i=le(e,"phone"),n=le(e,"google_oauth"),s=le(e,"twitter_oauth"),c=le(e,"discord_oauth"),m=le(e,"github_oauth"),p={id:t.id,createdAt:ae(t.created_at),linkedAccounts:e,email:r&&{address:r==null?void 0:r.address},phone:i&&{number:i==null?void 0:i.number},wallet:o&&{address:o.address,chainType:o.chainType,chainId:o.chainId,walletType:o.walletType},google:n&&{subject:n.subject,email:n.email,name:n.name},twitter:s&&{subject:s.subject,username:s.username,name:s.name},discord:c&&{subject:c.subject,username:c.username,email:c.email},github:m&&{subject:m.subject,username:m.username,name:m.name,email:m.email}};return Fe(p)}var Eo=()=>"/api/v1/sessions",ko=()=>"/api/v1/sessions/logout",_o=()=>"/api/v1/sessions/fork",Po=()=>"/api/v1/siwe/init",So=()=>"/api/v1/siwe/authenticate",To=()=>"/api/v1/siwe/link",Io=()=>"/api/v1/passwordless/init",Mo=()=>"/api/v1/passwordless/authenticate",Ro=()=>"/api/v1/passwordless/link",Wo=()=>"/api/v1/passwordless_sms/init",Oo=()=>"/api/v1/passwordless_sms/authenticate",No=()=>"/api/v1/passwordless_sms/link",Uo=()=>"/api/v1/oauth/init",Do=()=>"/api/v1/oauth/authenticate",Fo=()=>"/api/v1/oauth/link",Go=()=>"/api/v1/siwe/unlink",Vo=()=>"/api/v1/passwordless/unlink",Ho=()=>"/api/v1/passwordless_sms/unlink",Bo=()=>"/api/v1/oauth/unlink";var vi=30,nt=class{constructor(){this.authenticateOnce=new se(async e=>this._authenticate(e)),this.linkOnce=new se(async e=>this._link(e)),this.refreshOnce=new se(this._refresh.bind(this)),this.destroyOnce=new se(this._destroy.bind(this)),this.forkSessionOnce=new se(this._forkSession.bind(this))}get token(){try{let e=E.get(ke);return typeof e=="string"?new ne(e).value:null}catch(e){return console.error(e),this.destroyLocalState(),null}}get refreshToken(){try{let e=E.get(ot);return typeof e=="string"?e:null}catch(e){return console.error(e),this.destroyLocalState(),null}}hasRefreshCredentials(){return typeof this.token=="string"&&typeof this.refreshToken=="string"}hasActiveToken(){let e=ne.parse(this.token);return e!==null&&!e.isExpired(vi)}async hasValidToken(e,o,r=0){let i=ne.parse(this.token);return i!==null&&await i.isValid(e,o,r)}authenticate(e){return this.authenticateOnce.execute(e)}link(e){return this.linkOnce.execute(e)}refresh(){return this.refreshOnce.execute()}forkSession(){return this.forkSessionOnce.execute()}destroy(){return this.destroyOnce.execute()}async _authenticate(e){try{let{token:o,refresh_token:r,user:i,is_new_user:n}=await e.authenticate();return this.storeToken(o),this.storeRefreshToken(r),{user:Y(i),isNewUser:n}}catch(o){throw console.warn("Error authenticating session"),H(o)}}async _link(e){try{let o=await e.link();return Y(o)}catch(o){throw console.warn("Error linking account"),H(o)}}async _refresh(){if(!this.api)throw new u("Session has no API instance");let e=this.token,o=this.refreshToken;if(!e||!o)return null;try{let r=await this.api.post(Eo(),{refresh_token:o});return this.storeToken(r.data.token),this.storeRefreshToken(r.data.refresh_token),Y(r.data.user)}catch(r){throw H(r)}}async _destroy(){var e;try{await((e=this.api)==null?void 0:e.post(ko(),{refresh_token:this.refreshToken}))}catch{console.warn("Error destroying session")}this.destroyLocalState()}async _forkSession(){if(!this.api)throw new u("Session has no API instance");let e=this.refreshToken;try{let o=await this.api.post(_o(),{refresh_token:e});return this.storeToken(o.data.token),this.storeRefreshToken(o.data.refresh_token),o.data.new_session_refresh_token}catch(o){throw H(o)}}destroyLocalState(){this.storeToken(null),this.storeRefreshToken(null),E.del(K)}storeToken(e){typeof e=="string"?E.put(ke,e):E.del(ke)}storeRefreshToken(e){typeof e=="string"?E.put(ot,e):E.del(ot)}};import{getAddress as _i}from"@ethersproject/address";import jo from"@walletconnect/web3-provider";import{ErrorCode as xi}from"@ethersproject/logger";var Pe=class extends V{constructor(){super("Wallet timeout");this.type="wallet_error"}},st=t=>{if(t instanceof V)return t;if((t==null?void 0:t.code)&&(t==null?void 0:t.reason)){let e=new Se(t);return t.code===xi.ACTION_REJECTED&&(e.details=me.E4001_USER_REJECTED_REQUEST),e}return t!=null&&t.code?new Se(t):new V("Unknown connector error",t)},zt=class extends z{constructor(o,r,i){super(o);this.type="provider_error";this.code=r,this.data=i}},Se=class extends zt{constructor(o){var n,s,c;let r=o;super(r.message,r.code,r.data);let i=Object.values(me).find(m=>m.eipCode===r.code);this.details=i||me.UNKNOWN_ERROR,r.code===-32002&&((n=r.message)!=null&&n.includes("already pending for origin")?(s=r.message)!=null&&s.includes("wallet_requestPermissions")?this.details=me.E32002_CONNECTION_ALREADY_PENDING:this.details=me.E32002_REQUEST_ALREADY_PENDING:(c=r.message)!=null&&c.includes("Already processing")&&r.message.includes("eth_requestAccounts")&&(this.details=me.E32002_WALLET_LOCKED))}},Li={ERROR_USER_EXISTS:{message:"User already exists for this address",detail:"Try another address!",retryable:!1},ERROR_TIMED_OUT:{message:"Wallet request timed out",detail:"Please try connecting again.",retryable:!0},ERROR_WALLET_CONNECTION:{message:"Could not log in with wallet",detail:"Please try connecting again.",retryable:!0},ERROR_WALLET_NOT_ALLOWED:{message:"You don\u2019t have access to this app",detail:"Try with a different address?",retryable:!0}},Ai={E32002_CONNECTION_ALREADY_PENDING:{eipCode:-32002,message:"Connection request already pending",detail:"Don\u2019t see your wallet modal? Check your other browser windows.",retryable:!1},E32002_REQUEST_ALREADY_PENDING:{eipCode:-32002,message:"Resource request already pending",detail:"Don\u2019t see your wallet modal? Check your other browser windows.",retryable:!1},E32002_WALLET_LOCKED:{eipCode:-32002,message:"Wallet might be locked",detail:"Don\u2019t see your wallet modal? Check your other browser windows.",retryable:!1},E4001_USER_REJECTED_REQUEST:{eipCode:4001,message:"Signature rejected",detail:"Please try signing again.",retryable:!0}},Ei={E4001_DEFAULT_USER_REJECTED_REQUEST:{eipCode:4001,message:"User Rejected Request",detail:"The user rejected the request.",default:!0,retryable:!0},E4100_DEFAULT_UNAUTHORIZED:{eipCode:4100,message:"Unauthorized",detail:"The requested method and/or account has not been authorized by the user.",default:!0,retryable:!1},E4200_DEFAULT_UNSUPPORTED_METHOD:{eipCode:4200,message:"Unsupported Method",detail:"The Provider does not support the requested method.",default:!0,retryable:!1},E4900_DEFAULT_DISCONNECTED:{eipCode:4900,message:"Disconnected",detail:"The Provider is disconnected from all chains.",default:!0,retryable:!0},E4901_DEFAULT_CHAIN_DISCONNECTED:{eipCode:4901,message:"Chain Disconnected",detail:"The Provider is not connected to the requested chain.",default:!0,retryable:!0},E32700_DEFAULT_PARSE_ERROR:{eipCode:-32700,message:"Parse error",detail:"Invalid JSON",default:!0,retryable:!1},E32600_DEFAULT_INVALID_REQUEST:{eipCode:-32600,message:"Invalid request",detail:"JSON is not a valid request object",default:!0,retryable:!1},E32601_DEFAULT_METHOD_NOT_FOUND:{eipCode:-32601,message:"Method not found",detail:"Method does not exist",default:!0,retryable:!1},E32602_DEFAULT_INVALID_PARAMS:{eipCode:-32602,message:"Invalid params",detail:"Invalid method parameters",default:!0,retryable:!1},E32603_DEFAULT_INTERNAL_ERROR:{eipCode:-32603,message:"Internal error",detail:"Internal JSON-RPC error",default:!0,retryable:!0},E32000_DEFAULT_INVALID_INPUT:{eipCode:-32e3,message:"Invalid input",detail:"Missing or invalid parameters",default:!0,retryable:!1},E32001_DEFAULT_RESOURCE_NOT_FOUND:{eipCode:-32001,message:"Resource not found",detail:"Requested resource not found",default:!0,retryable:!1},E32002_DEFAULT_RESOURCE_UNAVAILABLE:{eipCode:-32002,message:"Resource unavailable",detail:"Requested resource not available",default:!0,retryable:!0},E32003_DEFAULT_TRANSACTION_REJECTED:{eipCode:-32003,message:"Transaction rejected",detail:"Transaction creation failed",default:!0,retryable:!0},E32004_DEFAULT_METHOD_NOT_SUPPORTED:{eipCode:-32004,message:"Method not supported",detail:"Method is not implemented",default:!0,retryable:!1},E32005_DEFAULT_LIMIT_EXCEEDED:{eipCode:-32005,message:"Limit exceeded",detail:"Request exceeds defined limit",default:!0,retryable:!1},E32006_DEFAULT_JSON_RPC_VERSION_NOT_SUPPORTED:{eipCode:-32006,message:"JSON-RPC version not supported",detail:"Version of JSON-RPC protocol is not supported",default:!0,retryable:!1}},me={UNKNOWN_ERROR:{eipCode:0,message:"Unknown error",detail:"Unknown error",retryable:!0},...Ei,...Ai},Ge={...Li,...me};var ki=12e4,jt=(t=new Pe,e=ki)=>new Promise((o,r)=>setTimeout(()=>{r(t)},e)),fe=class{constructor(e){this.removeListener=(e,o)=>{if(this.walletProvider)return this.walletProvider.removeListener(e,o)};this.setProvider=e=>{this.walletProvider=e,this._subscriptions.forEach(o=>{var r;(r=this.walletProvider)==null||r.on(o.eventName,o.listener)})};this.walletProvider=e,this._subscriptions=[]}on(e,o){if(this.walletProvider)return this.walletProvider.on(e,o);this._subscriptions.push({eventName:e,listener:o})}request(e){if(!this.walletProvider)throw new V(`A wallet request of type ${e.method} was made before setting a wallet provider.`);return Promise.race([this.walletProvider.request(e),jt()]).catch(o=>{throw st(o)})}},ge=class extends fe{constructor(o){super(o)}},at=class extends fe{constructor(e){super(e)}sendAsync(e,o){throw new Error("sendAsync is no longer supported by EIP-1193. Use the request method instead.")}};import{Web3Provider as Pi}from"@ethersproject/providers";var zo=["metamask","coinbase_wallet","wallet_connect"];import Si from"@coinbase/wallet-sdk";var Ti=()=>{let t=`https://mainnet.infura.io/v3/${tt}`,e=1;return new Si({appName:"Privy",darkMode:!1,headlessMode:!1}).makeWeb3Provider(t,e)},Zt=()=>{if(!window.ethereum)return!1;if(window.ethereum.isMetaMask)return!0;if(window.ethereum.providers){for(let t of window.ethereum.providers)if(t.isMetaMask)return!0}return!1},lt=()=>{if(!window.ethereum)return!1;if(window.ethereum.isCoinbaseWallet)return!0;if(window.ethereum.providers){for(let t of window.ethereum.providers)if(t.isCoinbaseWallet)return!0}return!1},Ve=class{constructor(){this.walletType=null;this.getEthereumProvider=()=>this.ethProvider;this.createSiweMessage=(e,o,r,i,n,s,c)=>`${r} wants you to sign in with your Ethereum account:
1
+ import{useEffect as Ot,useState as re}from"react";import Vt from"axios";import gi from"axios";var z=class extends Error{constructor(o,r){super(o);r instanceof Error&&(this.cause=r)}toString(){return`${this.type}: ${this.message}${this.cause?` [cause: ${this.cause}]`:""}`}},et=class extends z{constructor(o,r,i,n){super(i,n);this.type=o;this.status=r}},u=class extends z{constructor(o,r){super(o,r);this.type="client_error"}};var V=class extends z{constructor(o,r){super(o,r);this.type="connector_error"}},I=t=>{if(t instanceof z)return t;if(!gi.isAxiosError(t))return H(t);if(!t.response)return new et("api_error",null,t.message,t);let{type:e,message:o,error:r}=t.response.data;return new et(e||"ApiError",t.response.status,o||r,t)},H=t=>t instanceof z?t:t instanceof Error?new u(t.message,t):new u(`Internal error: ${t}`);var wo="1.12.1-beta.1";var yo="https://api.privy.io/v0",bo=process.env.NEXT_PUBLIC_PRIVY_AUTH_URL||"https://auth.privy.io",tt="4df5e2316331463a9130964bd6078dfa",Co=1e4,Ee=1400,vo=365*24*60*60,Gt=wo,xo="ES256",Lo="privy.io",ke="privy:token",ot="privy:refresh_token",K="privy:active_wallet",rt="privy:wallet_type",Ao="walletconnect";var it=class{constructor(e,o,r){this.appId=e,this.sdkVersion=Gt,this.session=o,this.defaults=r}async get(e,o){try{return await Vt.get(e,this.buildConfig({headers:{"Cache-Control":"no-cache"},...o}))}catch(r){throw I(r)}}async post(e,o,r){try{return await Vt.post(e,o,this.buildConfig(r))}catch(i){throw I(i)}}async delete(e,o){try{return await Vt.delete(e,this.buildConfig(o))}catch(r){throw I(r)}}buildConfig(e){return e=e||{},e.headers=e.headers||{},e.headers["privy-app-id"]=this.appId,e.headers["privy-client"]=`react-auth:${this.sdkVersion}`,this.session.token!==null&&(e.headers.authorization=`Bearer ${this.session.token}`),{...this.defaults,...e}}};import*as _e from"jose";var ne=class{static parse(e){try{return new ne(e)}catch{return null}}constructor(e){this.value=e,this._decoded=_e.decodeJwt(e)}get subject(){return this._decoded.sub}get expiration(){return this._decoded.exp}get issuer(){return this._decoded.iss}get audience(){return this._decoded.aud}isExpired(e=0){let o=Date.now(),r=(this.expiration-e)*1e3;return o>=r}async isValid(e,o,r=0){let i=await _e.importSPKI(e,xo);try{return await _e.jwtVerify(this.value,i,{issuer:Lo,audience:o,clockTolerance:r}),!0}catch{return!1}}};var se=class{constructor(e){this.promise=null;this.fn=e}execute(e){return this.promise===null&&(this.promise=(async()=>{try{return await this.fn(e)}finally{this.promise=null}})()),this.promise}};var Ht=class{constructor(){this._cache={}}get(e){return this._cache[e]}put(e,o){o!==void 0?this._cache[e]=o:this.del(e)}del(e){delete this._cache[e]}},Bt=class{get(e){let o=localStorage.getItem(e);return o===null?void 0:JSON.parse(o)}put(e,o){o!==void 0?localStorage.setItem(e,JSON.stringify(o)):this.del(e)}del(e){localStorage.removeItem(e)}};function yi(){return typeof window<"u"&&window.localStorage?new Bt:new Ht}var E=yi();function ae(t){return new Date(t*1e3)}function bi(t){return t.map(e=>{switch(e.type){case"wallet":return{address:e.address,type:e.type,verifiedAt:ae(e.verified_at),chainType:e.chain_type};case"email":return{address:e.address,type:e.type,verifiedAt:ae(e.verified_at)};case"phone":return{number:e.phoneNumber,type:e.type,verifiedAt:ae(e.verified_at)};case"google_oauth":return{subject:e.subject,email:e.email,name:e.name,type:e.type,verifiedAt:ae(e.verified_at)};case"twitter_oauth":return{subject:e.subject,username:e.username,name:e.name,type:e.type,verifiedAt:ae(e.verified_at)};case"discord_oauth":return{subject:e.subject,username:e.username,email:e.email,type:e.type,verifiedAt:ae(e.verified_at)};case"github_oauth":return{subject:e.subject,username:e.username,name:e.name,email:e.email,type:e.type,verifiedAt:ae(e.verified_at)};default:throw new u("Unrecognized account type")}})}function Ci(t,e){return e.find(o=>o.type==="wallet"&&o.address===t)}function le(t,e){return t.sort((o,r)=>r.verifiedAt.getTime()-o.verifiedAt.getTime()),t.find(o=>o.type===e)}function Fe(t){let e=t.linkedAccounts.map(s=>s.type==="wallet"?s.address:null),r=E.get(K);if(!r||!e.includes(r)){E.del(K);let s=le(t.linkedAccounts,"wallet");s&&(r=s.address,E.put(K,r))}let i=Ci(r,t.linkedAccounts),n=Object.assign({},t);return n.wallet=i&&{address:i.address,chainType:i.chainType,chainId:i.chainId,walletType:i.walletType},n}function Y(t){if(!t)return null;let e=bi(t.linked_accounts),o=le(e,"wallet"),r=le(e,"email"),i=le(e,"phone"),n=le(e,"google_oauth"),s=le(e,"twitter_oauth"),c=le(e,"discord_oauth"),m=le(e,"github_oauth"),p={id:t.id,createdAt:ae(t.created_at),linkedAccounts:e,email:r&&{address:r==null?void 0:r.address},phone:i&&{number:i==null?void 0:i.number},wallet:o&&{address:o.address,chainType:o.chainType,chainId:o.chainId,walletType:o.walletType},google:n&&{subject:n.subject,email:n.email,name:n.name},twitter:s&&{subject:s.subject,username:s.username,name:s.name},discord:c&&{subject:c.subject,username:c.username,email:c.email},github:m&&{subject:m.subject,username:m.username,name:m.name,email:m.email}};return Fe(p)}var Eo=()=>"/api/v1/sessions",ko=()=>"/api/v1/sessions/logout",_o=()=>"/api/v1/sessions/fork",Po=()=>"/api/v1/siwe/init",So=()=>"/api/v1/siwe/authenticate",To=()=>"/api/v1/siwe/link",Io=()=>"/api/v1/passwordless/init",Mo=()=>"/api/v1/passwordless/authenticate",Ro=()=>"/api/v1/passwordless/link",Wo=()=>"/api/v1/passwordless_sms/init",Oo=()=>"/api/v1/passwordless_sms/authenticate",No=()=>"/api/v1/passwordless_sms/link",Uo=()=>"/api/v1/oauth/init",Do=()=>"/api/v1/oauth/authenticate",Fo=()=>"/api/v1/oauth/link",Go=()=>"/api/v1/siwe/unlink",Vo=()=>"/api/v1/passwordless/unlink",Ho=()=>"/api/v1/passwordless_sms/unlink",Bo=()=>"/api/v1/oauth/unlink";var vi=30,nt=class{constructor(){this.authenticateOnce=new se(async e=>this._authenticate(e)),this.linkOnce=new se(async e=>this._link(e)),this.refreshOnce=new se(this._refresh.bind(this)),this.destroyOnce=new se(this._destroy.bind(this)),this.forkSessionOnce=new se(this._forkSession.bind(this))}get token(){try{let e=E.get(ke);return typeof e=="string"?new ne(e).value:null}catch(e){return console.error(e),this.destroyLocalState(),null}}get refreshToken(){try{let e=E.get(ot);return typeof e=="string"?e:null}catch(e){return console.error(e),this.destroyLocalState(),null}}hasRefreshCredentials(){return typeof this.token=="string"&&typeof this.refreshToken=="string"}hasActiveToken(){let e=ne.parse(this.token);return e!==null&&!e.isExpired(vi)}async hasValidToken(e,o,r=0){let i=ne.parse(this.token);return i!==null&&await i.isValid(e,o,r)}authenticate(e){return this.authenticateOnce.execute(e)}link(e){return this.linkOnce.execute(e)}refresh(){return this.refreshOnce.execute()}forkSession(){return this.forkSessionOnce.execute()}destroy(){return this.destroyOnce.execute()}async _authenticate(e){try{let{token:o,refresh_token:r,user:i,is_new_user:n}=await e.authenticate();return this.storeToken(o),this.storeRefreshToken(r),{user:Y(i),isNewUser:n}}catch(o){throw console.warn("Error authenticating session"),H(o)}}async _link(e){try{let o=await e.link();return Y(o)}catch(o){throw console.warn("Error linking account"),H(o)}}async _refresh(){if(!this.api)throw new u("Session has no API instance");let e=this.token,o=this.refreshToken;if(!e||!o)return null;try{let r=await this.api.post(Eo(),{refresh_token:o});return this.storeToken(r.data.token),this.storeRefreshToken(r.data.refresh_token),Y(r.data.user)}catch(r){throw H(r)}}async _destroy(){var e;try{await((e=this.api)==null?void 0:e.post(ko(),{refresh_token:this.refreshToken}))}catch{console.warn("Error destroying session")}this.destroyLocalState()}async _forkSession(){if(!this.api)throw new u("Session has no API instance");let e=this.refreshToken;try{let o=await this.api.post(_o(),{refresh_token:e});return this.storeToken(o.data.token),this.storeRefreshToken(o.data.refresh_token),o.data.new_session_refresh_token}catch(o){throw H(o)}}destroyLocalState(){this.storeToken(null),this.storeRefreshToken(null),E.del(K)}storeToken(e){typeof e=="string"?E.put(ke,e):E.del(ke)}storeRefreshToken(e){typeof e=="string"?E.put(ot,e):E.del(ot)}};import{getAddress as _i}from"@ethersproject/address";import jo from"@walletconnect/web3-provider";import{ErrorCode as xi}from"@ethersproject/logger";var Pe=class extends V{constructor(){super("Wallet timeout");this.type="wallet_error"}},st=t=>{if(t instanceof V)return t;if((t==null?void 0:t.code)&&(t==null?void 0:t.reason)){let e=new Se(t);return t.code===xi.ACTION_REJECTED&&(e.details=me.E4001_USER_REJECTED_REQUEST),e}return t!=null&&t.code?new Se(t):new V("Unknown connector error",t)},zt=class extends z{constructor(o,r,i){super(o);this.type="provider_error";this.code=r,this.data=i}},Se=class extends zt{constructor(o){var n,s,c;let r=o;super(r.message,r.code,r.data);let i=Object.values(me).find(m=>m.eipCode===r.code);this.details=i||me.UNKNOWN_ERROR,r.code===-32002&&((n=r.message)!=null&&n.includes("already pending for origin")?(s=r.message)!=null&&s.includes("wallet_requestPermissions")?this.details=me.E32002_CONNECTION_ALREADY_PENDING:this.details=me.E32002_REQUEST_ALREADY_PENDING:(c=r.message)!=null&&c.includes("Already processing")&&r.message.includes("eth_requestAccounts")&&(this.details=me.E32002_WALLET_LOCKED))}},Li={ERROR_USER_EXISTS:{message:"User already exists for this address",detail:"Try another address!",retryable:!1},ERROR_TIMED_OUT:{message:"Wallet request timed out",detail:"Please try connecting again.",retryable:!0},ERROR_WALLET_CONNECTION:{message:"Could not log in with wallet",detail:"Please try connecting again.",retryable:!0},ERROR_WALLET_NOT_ALLOWED:{message:"You don\u2019t have access to this app",detail:"Try with a different address?",retryable:!0}},Ai={E32002_CONNECTION_ALREADY_PENDING:{eipCode:-32002,message:"Connection request already pending",detail:"Don\u2019t see your wallet modal? Check your other browser windows.",retryable:!1},E32002_REQUEST_ALREADY_PENDING:{eipCode:-32002,message:"Resource request already pending",detail:"Don\u2019t see your wallet modal? Check your other browser windows.",retryable:!1},E32002_WALLET_LOCKED:{eipCode:-32002,message:"Wallet might be locked",detail:"Don\u2019t see your wallet modal? Check your other browser windows.",retryable:!1},E4001_USER_REJECTED_REQUEST:{eipCode:4001,message:"Signature rejected",detail:"Please try signing again.",retryable:!0}},Ei={E4001_DEFAULT_USER_REJECTED_REQUEST:{eipCode:4001,message:"User Rejected Request",detail:"The user rejected the request.",default:!0,retryable:!0},E4100_DEFAULT_UNAUTHORIZED:{eipCode:4100,message:"Unauthorized",detail:"The requested method and/or account has not been authorized by the user.",default:!0,retryable:!1},E4200_DEFAULT_UNSUPPORTED_METHOD:{eipCode:4200,message:"Unsupported Method",detail:"The Provider does not support the requested method.",default:!0,retryable:!1},E4900_DEFAULT_DISCONNECTED:{eipCode:4900,message:"Disconnected",detail:"The Provider is disconnected from all chains.",default:!0,retryable:!0},E4901_DEFAULT_CHAIN_DISCONNECTED:{eipCode:4901,message:"Chain Disconnected",detail:"The Provider is not connected to the requested chain.",default:!0,retryable:!0},E32700_DEFAULT_PARSE_ERROR:{eipCode:-32700,message:"Parse error",detail:"Invalid JSON",default:!0,retryable:!1},E32600_DEFAULT_INVALID_REQUEST:{eipCode:-32600,message:"Invalid request",detail:"JSON is not a valid request object",default:!0,retryable:!1},E32601_DEFAULT_METHOD_NOT_FOUND:{eipCode:-32601,message:"Method not found",detail:"Method does not exist",default:!0,retryable:!1},E32602_DEFAULT_INVALID_PARAMS:{eipCode:-32602,message:"Invalid params",detail:"Invalid method parameters",default:!0,retryable:!1},E32603_DEFAULT_INTERNAL_ERROR:{eipCode:-32603,message:"Internal error",detail:"Internal JSON-RPC error",default:!0,retryable:!0},E32000_DEFAULT_INVALID_INPUT:{eipCode:-32e3,message:"Invalid input",detail:"Missing or invalid parameters",default:!0,retryable:!1},E32001_DEFAULT_RESOURCE_NOT_FOUND:{eipCode:-32001,message:"Resource not found",detail:"Requested resource not found",default:!0,retryable:!1},E32002_DEFAULT_RESOURCE_UNAVAILABLE:{eipCode:-32002,message:"Resource unavailable",detail:"Requested resource not available",default:!0,retryable:!0},E32003_DEFAULT_TRANSACTION_REJECTED:{eipCode:-32003,message:"Transaction rejected",detail:"Transaction creation failed",default:!0,retryable:!0},E32004_DEFAULT_METHOD_NOT_SUPPORTED:{eipCode:-32004,message:"Method not supported",detail:"Method is not implemented",default:!0,retryable:!1},E32005_DEFAULT_LIMIT_EXCEEDED:{eipCode:-32005,message:"Limit exceeded",detail:"Request exceeds defined limit",default:!0,retryable:!1},E32006_DEFAULT_JSON_RPC_VERSION_NOT_SUPPORTED:{eipCode:-32006,message:"JSON-RPC version not supported",detail:"Version of JSON-RPC protocol is not supported",default:!0,retryable:!1}},me={UNKNOWN_ERROR:{eipCode:0,message:"Unknown error",detail:"Unknown error",retryable:!0},...Ei,...Ai},Ge={...Li,...me};var ki=12e4,jt=(t=new Pe,e=ki)=>new Promise((o,r)=>setTimeout(()=>{r(t)},e)),fe=class{constructor(e){this.removeListener=(e,o)=>{if(this.walletProvider)return this.walletProvider.removeListener(e,o)};this.setProvider=e=>{this.walletProvider=e,this._subscriptions.forEach(o=>{var r;(r=this.walletProvider)==null||r.on(o.eventName,o.listener)})};this.walletProvider=e,this._subscriptions=[]}on(e,o){if(this.walletProvider)return this.walletProvider.on(e,o);this._subscriptions.push({eventName:e,listener:o})}request(e){if(!this.walletProvider)throw new V(`A wallet request of type ${e.method} was made before setting a wallet provider.`);return Promise.race([this.walletProvider.request(e),jt()]).catch(o=>{throw st(o)})}},ge=class extends fe{constructor(o){super(o)}},at=class extends fe{constructor(e){super(e)}sendAsync(e,o){throw new Error("sendAsync is no longer supported by EIP-1193. Use the request method instead.")}};import{Web3Provider as Pi}from"@ethersproject/providers";var zo=["metamask","coinbase_wallet","wallet_connect"];import Si from"@coinbase/wallet-sdk";var Ti=()=>{let t=`https://mainnet.infura.io/v3/${tt}`,e=1;return new Si({appName:"Privy",darkMode:!1,headlessMode:!1}).makeWeb3Provider(t,e)},Zt=()=>{if(!window.ethereum)return!1;if(window.ethereum.isMetaMask)return!0;if(window.ethereum.providers){for(let t of window.ethereum.providers)if(t.isMetaMask)return!0}return!1},lt=()=>{if(!window.ethereum)return!1;if(window.ethereum.isCoinbaseWallet)return!0;if(window.ethereum.providers){for(let t of window.ethereum.providers)if(t.isCoinbaseWallet)return!0}return!1},Ve=class{constructor(){this.walletType=null;this.getEthereumProvider=()=>this.ethProvider;this.createSiweMessage=(e,o,r,i,n,s,c)=>`${r} wants you to sign in with your Ethereum account:
2
2
  ${o}
3
3
 
4
4
  ${c}
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var xi=Object.create;var ct=Object.defineProperty;var Li=Object.getOwnPropertyDescriptor;var Ai=Object.getOwnPropertyNames;var Ei=Object.getPrototypeOf,ki=Object.prototype.hasOwnProperty;var _i=(t,e)=>{for(var o in e)ct(t,o,{get:e[o],enumerable:!0})},Io=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of Ai(e))!ki.call(t,i)&&i!==o&&ct(t,i,{get:()=>e[i],enumerable:!(r=Li(e,i))||r.enumerable});return t};var C=(t,e,o)=>(o=t!=null?xi(Ei(t)):{},Io(e||!t||!t.__esModule?ct(o,"default",{value:t,enumerable:!0}):o,t)),Pi=t=>Io(ct({},"__esModule",{value:!0}),t);var Xn={};_i(Xn,{AsExternalProvider:()=>xe,PrivyClient:()=>ze,PrivyConnector:()=>Be,PrivyProvider:()=>Ci,PrivyProxyProvider:()=>ve,VERSION:()=>ht,getAccessToken:()=>bi,usePrivy:()=>N});module.exports=Pi(Xn);var z=require("react");var gt=C(require("axios"));var Mo=C(require("axios")),X=class extends Error{constructor(o,r){super(o);r instanceof Error&&(this.cause=r)}toString(){return`${this.type}: ${this.message}${this.cause?` [cause: ${this.cause}]`:""}`}},ut=class extends X{constructor(o,r,i,n){super(i,n);this.type=o;this.status=r}},p=class extends X{constructor(o,r){super(o,r);this.type="client_error"}};var K=class extends X{constructor(o,r){super(o,r);this.type="connector_error"}},O=t=>{if(t instanceof X)return t;if(!Mo.default.isAxiosError(t))return q(t);if(!t.response)return new ut("api_error",null,t.message,t);let{type:e,message:o,error:r}=t.response.data;return new ut(e||"ApiError",t.response.status,o||r,t)},q=t=>t instanceof X?t:t instanceof Error?new p(t.message,t):new p(`Internal error: ${t}`);var Ro="1.12.0-beta.8";var Wo="https://api.privy.io/v0",Oo=process.env.NEXT_PUBLIC_PRIVY_AUTH_URL||"https://auth.privy.io",pt="4df5e2316331463a9130964bd6078dfa",No=1e4,De=1400,Uo=365*24*60*60,ht=Ro,Do="ES256",Fo="privy.io",Fe="privy:token",mt="privy:refresh_token",se="privy:active_wallet",ft="privy:wallet_type",Go="walletconnect";var wt=class{constructor(e,o,r){this.appId=e,this.sdkVersion=ht,this.session=o,this.defaults=r}async get(e,o){try{return await gt.default.get(e,this.buildConfig({headers:{"Cache-Control":"no-cache"},...o}))}catch(r){throw O(r)}}async post(e,o,r){try{return await gt.default.post(e,o,this.buildConfig(r))}catch(i){throw O(i)}}async delete(e,o){try{return await gt.default.delete(e,this.buildConfig(o))}catch(r){throw O(r)}}buildConfig(e){return e=e||{},e.headers=e.headers||{},e.headers["privy-app-id"]=this.appId,e.headers["privy-client"]=`react-auth:${this.sdkVersion}`,this.session.token!==null&&(e.headers.authorization=`Bearer ${this.session.token}`),{...this.defaults,...e}}};var Ge=C(require("jose"));var we=class{static parse(e){try{return new we(e)}catch{return null}}constructor(e){this.value=e,this._decoded=Ge.decodeJwt(e)}get subject(){return this._decoded.sub}get expiration(){return this._decoded.exp}get issuer(){return this._decoded.iss}get audience(){return this._decoded.aud}isExpired(e=0){let o=Date.now(),r=(this.expiration-e)*1e3;return o>=r}async isValid(e,o,r=0){let i=await Ge.importSPKI(e,Do);try{return await Ge.jwtVerify(this.value,i,{issuer:Fo,audience:o,clockTolerance:r}),!0}catch{return!1}}};var ye=class{constructor(e){this.promise=null;this.fn=e}execute(e){return this.promise===null&&(this.promise=(async()=>{try{return await this.fn(e)}finally{this.promise=null}})()),this.promise}};var qt=class{constructor(){this._cache={}}get(e){return this._cache[e]}put(e,o){o!==void 0?this._cache[e]=o:this.del(e)}del(e){delete this._cache[e]}},Yt=class{get(e){let o=localStorage.getItem(e);return o===null?void 0:JSON.parse(o)}put(e,o){o!==void 0?localStorage.setItem(e,JSON.stringify(o)):this.del(e)}del(e){localStorage.removeItem(e)}};function Ti(){return typeof window<"u"&&window.localStorage?new Yt:new qt}var P=Ti();function be(t){return new Date(t*1e3)}function Ii(t){return t.map(e=>{switch(e.type){case"wallet":return{address:e.address,type:e.type,verifiedAt:be(e.verified_at),chainType:e.chain_type};case"email":return{address:e.address,type:e.type,verifiedAt:be(e.verified_at)};case"phone":return{number:e.phoneNumber,type:e.type,verifiedAt:be(e.verified_at)};case"google_oauth":return{subject:e.subject,email:e.email,name:e.name,type:e.type,verifiedAt:be(e.verified_at)};case"twitter_oauth":return{subject:e.subject,username:e.username,name:e.name,type:e.type,verifiedAt:be(e.verified_at)};case"discord_oauth":return{subject:e.subject,username:e.username,email:e.email,type:e.type,verifiedAt:be(e.verified_at)};case"github_oauth":return{subject:e.subject,username:e.username,name:e.name,email:e.email,type:e.type,verifiedAt:be(e.verified_at)};default:throw new p("Unrecognized account type")}})}function Mi(t,e){return e.find(o=>o.type==="wallet"&&o.address===t)}function Ce(t,e){return t.sort((o,r)=>r.verifiedAt.getTime()-o.verifiedAt.getTime()),t.find(o=>o.type===e)}function Qe(t){let e=t.linkedAccounts.map(s=>s.type==="wallet"?s.address:null),r=P.get(se);if(!r||!e.includes(r)){P.del(se);let s=Ce(t.linkedAccounts,"wallet");s&&(r=s.address,P.put(se,r))}let i=Mi(r,t.linkedAccounts),n=Object.assign({},t);return n.wallet=i&&{address:i.address,chainType:i.chainType,chainId:i.chainId,walletType:i.walletType},n}function pe(t){if(!t)return null;let e=Ii(t.linked_accounts),o=Ce(e,"wallet"),r=Ce(e,"email"),i=Ce(e,"phone"),n=Ce(e,"google_oauth"),s=Ce(e,"twitter_oauth"),u=Ce(e,"discord_oauth"),f=Ce(e,"github_oauth"),h={id:t.id,createdAt:be(t.created_at),linkedAccounts:e,email:r&&{address:r==null?void 0:r.address},phone:i&&{number:i==null?void 0:i.number},wallet:o&&{address:o.address,chainType:o.chainType,chainId:o.chainId,walletType:o.walletType},google:n&&{subject:n.subject,email:n.email,name:n.name},twitter:s&&{subject:s.subject,username:s.username,name:s.name},discord:u&&{subject:u.subject,username:u.username,email:u.email},github:f&&{subject:f.subject,username:f.username,name:f.name,email:f.email}};return Qe(h)}var Vo=()=>"/api/v1/sessions",Ho=()=>"/api/v1/sessions/logout",Bo=()=>"/api/v1/sessions/fork",zo=()=>"/api/v1/siwe/init",jo=()=>"/api/v1/siwe/authenticate",Zo=()=>"/api/v1/siwe/link",$o=()=>"/api/v1/passwordless/init",Ko=()=>"/api/v1/passwordless/authenticate",qo=()=>"/api/v1/passwordless/link",Yo=()=>"/api/v1/passwordless_sms/init",Jo=()=>"/api/v1/passwordless_sms/authenticate",Qo=()=>"/api/v1/passwordless_sms/link",Xo=()=>"/api/v1/oauth/init",er=()=>"/api/v1/oauth/authenticate",tr=()=>"/api/v1/oauth/link",or=()=>"/api/v1/siwe/unlink",rr=()=>"/api/v1/passwordless/unlink",ir=()=>"/api/v1/passwordless_sms/unlink",nr=()=>"/api/v1/oauth/unlink";var Ri=30,yt=class{constructor(){this.authenticateOnce=new ye(async e=>this._authenticate(e)),this.linkOnce=new ye(async e=>this._link(e)),this.refreshOnce=new ye(this._refresh.bind(this)),this.destroyOnce=new ye(this._destroy.bind(this)),this.forkSessionOnce=new ye(this._forkSession.bind(this))}get token(){try{let e=P.get(Fe);return typeof e=="string"?new we(e).value:null}catch(e){return console.error(e),this.destroyLocalState(),null}}get refreshToken(){try{let e=P.get(mt);return typeof e=="string"?e:null}catch(e){return console.error(e),this.destroyLocalState(),null}}hasRefreshCredentials(){return typeof this.token=="string"&&typeof this.refreshToken=="string"}hasActiveToken(){let e=we.parse(this.token);return e!==null&&!e.isExpired(Ri)}async hasValidToken(e,o,r=0){let i=we.parse(this.token);return i!==null&&await i.isValid(e,o,r)}authenticate(e){return this.authenticateOnce.execute(e)}link(e){return this.linkOnce.execute(e)}refresh(){return this.refreshOnce.execute()}forkSession(){return this.forkSessionOnce.execute()}destroy(){return this.destroyOnce.execute()}async _authenticate(e){try{let{token:o,refresh_token:r,user:i,is_new_user:n}=await e.authenticate();return this.storeToken(o),this.storeRefreshToken(r),{user:pe(i),isNewUser:n}}catch(o){throw console.warn("Error authenticating session"),q(o)}}async _link(e){try{let o=await e.link();return pe(o)}catch(o){throw console.warn("Error linking account"),q(o)}}async _refresh(){if(!this.api)throw new p("Session has no API instance");let e=this.token,o=this.refreshToken;if(!e||!o)return null;try{let r=await this.api.post(Vo(),{refresh_token:o});return this.storeToken(r.data.token),this.storeRefreshToken(r.data.refresh_token),pe(r.data.user)}catch(r){throw q(r)}}async _destroy(){var e;try{await((e=this.api)==null?void 0:e.post(Ho(),{refresh_token:this.refreshToken}))}catch{console.warn("Error destroying session")}this.destroyLocalState()}async _forkSession(){if(!this.api)throw new p("Session has no API instance");let e=this.refreshToken;try{let o=await this.api.post(Bo(),{refresh_token:e});return this.storeToken(o.data.token),this.storeRefreshToken(o.data.refresh_token),o.data.new_session_refresh_token}catch(o){throw q(o)}}destroyLocalState(){this.storeToken(null),this.storeRefreshToken(null),P.del(se)}storeToken(e){typeof e=="string"?P.put(Fe,e):P.del(Fe)}storeRefreshToken(e){typeof e=="string"?P.put(mt,e):P.del(mt)}};var lr=require("@ethersproject/address"),Xt=C(require("@walletconnect/web3-provider"));var sr=require("@ethersproject/logger");var Ve=class extends K{constructor(){super("Wallet timeout");this.type="wallet_error"}},bt=t=>{if(t instanceof K)return t;if((t==null?void 0:t.code)&&(t==null?void 0:t.reason)){let e=new He(t);return t.code===sr.ErrorCode.ACTION_REJECTED&&(e.details=Ie.E4001_USER_REJECTED_REQUEST),e}return t!=null&&t.code?new He(t):new K("Unknown connector error",t)},Jt=class extends X{constructor(o,r,i){super(o);this.type="provider_error";this.code=r,this.data=i}},He=class extends Jt{constructor(o){var n,s,u;let r=o;super(r.message,r.code,r.data);let i=Object.values(Ie).find(f=>f.eipCode===r.code);this.details=i||Ie.UNKNOWN_ERROR,r.code===-32002&&((n=r.message)!=null&&n.includes("already pending for origin")?(s=r.message)!=null&&s.includes("wallet_requestPermissions")?this.details=Ie.E32002_CONNECTION_ALREADY_PENDING:this.details=Ie.E32002_REQUEST_ALREADY_PENDING:(u=r.message)!=null&&u.includes("Already processing")&&r.message.includes("eth_requestAccounts")&&(this.details=Ie.E32002_WALLET_LOCKED))}},Wi={ERROR_USER_EXISTS:{message:"User already exists for this address",detail:"Try another address!",retryable:!1},ERROR_TIMED_OUT:{message:"Wallet request timed out",detail:"Please try connecting again.",retryable:!0},ERROR_WALLET_CONNECTION:{message:"Could not log in with wallet",detail:"Please try connecting again.",retryable:!0},ERROR_WALLET_NOT_ALLOWED:{message:"You don\u2019t have access to this app",detail:"Try with a different address?",retryable:!0}},Oi={E32002_CONNECTION_ALREADY_PENDING:{eipCode:-32002,message:"Connection request already pending",detail:"Don\u2019t see your wallet modal? Check your other browser windows.",retryable:!1},E32002_REQUEST_ALREADY_PENDING:{eipCode:-32002,message:"Resource request already pending",detail:"Don\u2019t see your wallet modal? Check your other browser windows.",retryable:!1},E32002_WALLET_LOCKED:{eipCode:-32002,message:"Wallet might be locked",detail:"Don\u2019t see your wallet modal? Check your other browser windows.",retryable:!1},E4001_USER_REJECTED_REQUEST:{eipCode:4001,message:"Signature rejected",detail:"Please try signing again.",retryable:!0}},Ni={E4001_DEFAULT_USER_REJECTED_REQUEST:{eipCode:4001,message:"User Rejected Request",detail:"The user rejected the request.",default:!0,retryable:!0},E4100_DEFAULT_UNAUTHORIZED:{eipCode:4100,message:"Unauthorized",detail:"The requested method and/or account has not been authorized by the user.",default:!0,retryable:!1},E4200_DEFAULT_UNSUPPORTED_METHOD:{eipCode:4200,message:"Unsupported Method",detail:"The Provider does not support the requested method.",default:!0,retryable:!1},E4900_DEFAULT_DISCONNECTED:{eipCode:4900,message:"Disconnected",detail:"The Provider is disconnected from all chains.",default:!0,retryable:!0},E4901_DEFAULT_CHAIN_DISCONNECTED:{eipCode:4901,message:"Chain Disconnected",detail:"The Provider is not connected to the requested chain.",default:!0,retryable:!0},E32700_DEFAULT_PARSE_ERROR:{eipCode:-32700,message:"Parse error",detail:"Invalid JSON",default:!0,retryable:!1},E32600_DEFAULT_INVALID_REQUEST:{eipCode:-32600,message:"Invalid request",detail:"JSON is not a valid request object",default:!0,retryable:!1},E32601_DEFAULT_METHOD_NOT_FOUND:{eipCode:-32601,message:"Method not found",detail:"Method does not exist",default:!0,retryable:!1},E32602_DEFAULT_INVALID_PARAMS:{eipCode:-32602,message:"Invalid params",detail:"Invalid method parameters",default:!0,retryable:!1},E32603_DEFAULT_INTERNAL_ERROR:{eipCode:-32603,message:"Internal error",detail:"Internal JSON-RPC error",default:!0,retryable:!0},E32000_DEFAULT_INVALID_INPUT:{eipCode:-32e3,message:"Invalid input",detail:"Missing or invalid parameters",default:!0,retryable:!1},E32001_DEFAULT_RESOURCE_NOT_FOUND:{eipCode:-32001,message:"Resource not found",detail:"Requested resource not found",default:!0,retryable:!1},E32002_DEFAULT_RESOURCE_UNAVAILABLE:{eipCode:-32002,message:"Resource unavailable",detail:"Requested resource not available",default:!0,retryable:!0},E32003_DEFAULT_TRANSACTION_REJECTED:{eipCode:-32003,message:"Transaction rejected",detail:"Transaction creation failed",default:!0,retryable:!0},E32004_DEFAULT_METHOD_NOT_SUPPORTED:{eipCode:-32004,message:"Method not supported",detail:"Method is not implemented",default:!0,retryable:!1},E32005_DEFAULT_LIMIT_EXCEEDED:{eipCode:-32005,message:"Limit exceeded",detail:"Request exceeds defined limit",default:!0,retryable:!1},E32006_DEFAULT_JSON_RPC_VERSION_NOT_SUPPORTED:{eipCode:-32006,message:"JSON-RPC version not supported",detail:"Version of JSON-RPC protocol is not supported",default:!0,retryable:!1}},Ie={UNKNOWN_ERROR:{eipCode:0,message:"Unknown error",detail:"Unknown error",retryable:!0},...Ni,...Oi},Xe={...Wi,...Ie};var Ui=12e4,Qt=(t=new Ve,e=Ui)=>new Promise((o,r)=>setTimeout(()=>{r(t)},e)),ve=class{constructor(e){this.removeListener=(e,o)=>{if(this.walletProvider)return this.walletProvider.removeListener(e,o)};this.setProvider=e=>{this.walletProvider=e,this._subscriptions.forEach(o=>{var r;(r=this.walletProvider)==null||r.on(o.eventName,o.listener)})};this.walletProvider=e,this._subscriptions=[]}on(e,o){if(this.walletProvider)return this.walletProvider.on(e,o);this._subscriptions.push({eventName:e,listener:o})}request(e){if(!this.walletProvider)throw new K(`A wallet request of type ${e.method} was made before setting a wallet provider.`);return Promise.race([this.walletProvider.request(e),Qt()]).catch(o=>{throw bt(o)})}},xe=class extends ve{constructor(o){super(o)}},Ct=class extends ve{constructor(e){super(e)}sendAsync(e,o){throw new Error("sendAsync is no longer supported by EIP-1193. Use the request method instead.")}};var dr=require("@ethersproject/providers");var ar=["metamask","coinbase_wallet","wallet_connect"];var cr=C(require("@coinbase/wallet-sdk")),Di=()=>{let t=`https://mainnet.infura.io/v3/${pt}`,e=1;return new cr.default({appName:"Privy",darkMode:!1,headlessMode:!1}).makeWeb3Provider(t,e)},eo=()=>{if(!window.ethereum)return!1;if(window.ethereum.isMetaMask)return!0;if(window.ethereum.providers){for(let t of window.ethereum.providers)if(t.isMetaMask)return!0}return!1},vt=()=>{if(!window.ethereum)return!1;if(window.ethereum.isCoinbaseWallet)return!0;if(window.ethereum.providers){for(let t of window.ethereum.providers)if(t.isCoinbaseWallet)return!0}return!1},Be=class{constructor(){this.walletType=null;this.getEthereumProvider=()=>this.ethProvider;this.createSiweMessage=(e,o,r,i,n,s,u)=>`${r} wants you to sign in with your Ethereum account:
1
+ "use strict";var xi=Object.create;var ct=Object.defineProperty;var Li=Object.getOwnPropertyDescriptor;var Ai=Object.getOwnPropertyNames;var Ei=Object.getPrototypeOf,ki=Object.prototype.hasOwnProperty;var _i=(t,e)=>{for(var o in e)ct(t,o,{get:e[o],enumerable:!0})},Io=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of Ai(e))!ki.call(t,i)&&i!==o&&ct(t,i,{get:()=>e[i],enumerable:!(r=Li(e,i))||r.enumerable});return t};var C=(t,e,o)=>(o=t!=null?xi(Ei(t)):{},Io(e||!t||!t.__esModule?ct(o,"default",{value:t,enumerable:!0}):o,t)),Pi=t=>Io(ct({},"__esModule",{value:!0}),t);var Xn={};_i(Xn,{AsExternalProvider:()=>xe,PrivyClient:()=>ze,PrivyConnector:()=>Be,PrivyProvider:()=>Ci,PrivyProxyProvider:()=>ve,VERSION:()=>ht,getAccessToken:()=>bi,usePrivy:()=>N});module.exports=Pi(Xn);var z=require("react");var gt=C(require("axios"));var Mo=C(require("axios")),X=class extends Error{constructor(o,r){super(o);r instanceof Error&&(this.cause=r)}toString(){return`${this.type}: ${this.message}${this.cause?` [cause: ${this.cause}]`:""}`}},ut=class extends X{constructor(o,r,i,n){super(i,n);this.type=o;this.status=r}},p=class extends X{constructor(o,r){super(o,r);this.type="client_error"}};var K=class extends X{constructor(o,r){super(o,r);this.type="connector_error"}},O=t=>{if(t instanceof X)return t;if(!Mo.default.isAxiosError(t))return q(t);if(!t.response)return new ut("api_error",null,t.message,t);let{type:e,message:o,error:r}=t.response.data;return new ut(e||"ApiError",t.response.status,o||r,t)},q=t=>t instanceof X?t:t instanceof Error?new p(t.message,t):new p(`Internal error: ${t}`);var Ro="1.12.1-beta.1";var Wo="https://api.privy.io/v0",Oo=process.env.NEXT_PUBLIC_PRIVY_AUTH_URL||"https://auth.privy.io",pt="4df5e2316331463a9130964bd6078dfa",No=1e4,De=1400,Uo=365*24*60*60,ht=Ro,Do="ES256",Fo="privy.io",Fe="privy:token",mt="privy:refresh_token",se="privy:active_wallet",ft="privy:wallet_type",Go="walletconnect";var wt=class{constructor(e,o,r){this.appId=e,this.sdkVersion=ht,this.session=o,this.defaults=r}async get(e,o){try{return await gt.default.get(e,this.buildConfig({headers:{"Cache-Control":"no-cache"},...o}))}catch(r){throw O(r)}}async post(e,o,r){try{return await gt.default.post(e,o,this.buildConfig(r))}catch(i){throw O(i)}}async delete(e,o){try{return await gt.default.delete(e,this.buildConfig(o))}catch(r){throw O(r)}}buildConfig(e){return e=e||{},e.headers=e.headers||{},e.headers["privy-app-id"]=this.appId,e.headers["privy-client"]=`react-auth:${this.sdkVersion}`,this.session.token!==null&&(e.headers.authorization=`Bearer ${this.session.token}`),{...this.defaults,...e}}};var Ge=C(require("jose"));var we=class{static parse(e){try{return new we(e)}catch{return null}}constructor(e){this.value=e,this._decoded=Ge.decodeJwt(e)}get subject(){return this._decoded.sub}get expiration(){return this._decoded.exp}get issuer(){return this._decoded.iss}get audience(){return this._decoded.aud}isExpired(e=0){let o=Date.now(),r=(this.expiration-e)*1e3;return o>=r}async isValid(e,o,r=0){let i=await Ge.importSPKI(e,Do);try{return await Ge.jwtVerify(this.value,i,{issuer:Fo,audience:o,clockTolerance:r}),!0}catch{return!1}}};var ye=class{constructor(e){this.promise=null;this.fn=e}execute(e){return this.promise===null&&(this.promise=(async()=>{try{return await this.fn(e)}finally{this.promise=null}})()),this.promise}};var qt=class{constructor(){this._cache={}}get(e){return this._cache[e]}put(e,o){o!==void 0?this._cache[e]=o:this.del(e)}del(e){delete this._cache[e]}},Yt=class{get(e){let o=localStorage.getItem(e);return o===null?void 0:JSON.parse(o)}put(e,o){o!==void 0?localStorage.setItem(e,JSON.stringify(o)):this.del(e)}del(e){localStorage.removeItem(e)}};function Ti(){return typeof window<"u"&&window.localStorage?new Yt:new qt}var P=Ti();function be(t){return new Date(t*1e3)}function Ii(t){return t.map(e=>{switch(e.type){case"wallet":return{address:e.address,type:e.type,verifiedAt:be(e.verified_at),chainType:e.chain_type};case"email":return{address:e.address,type:e.type,verifiedAt:be(e.verified_at)};case"phone":return{number:e.phoneNumber,type:e.type,verifiedAt:be(e.verified_at)};case"google_oauth":return{subject:e.subject,email:e.email,name:e.name,type:e.type,verifiedAt:be(e.verified_at)};case"twitter_oauth":return{subject:e.subject,username:e.username,name:e.name,type:e.type,verifiedAt:be(e.verified_at)};case"discord_oauth":return{subject:e.subject,username:e.username,email:e.email,type:e.type,verifiedAt:be(e.verified_at)};case"github_oauth":return{subject:e.subject,username:e.username,name:e.name,email:e.email,type:e.type,verifiedAt:be(e.verified_at)};default:throw new p("Unrecognized account type")}})}function Mi(t,e){return e.find(o=>o.type==="wallet"&&o.address===t)}function Ce(t,e){return t.sort((o,r)=>r.verifiedAt.getTime()-o.verifiedAt.getTime()),t.find(o=>o.type===e)}function Qe(t){let e=t.linkedAccounts.map(s=>s.type==="wallet"?s.address:null),r=P.get(se);if(!r||!e.includes(r)){P.del(se);let s=Ce(t.linkedAccounts,"wallet");s&&(r=s.address,P.put(se,r))}let i=Mi(r,t.linkedAccounts),n=Object.assign({},t);return n.wallet=i&&{address:i.address,chainType:i.chainType,chainId:i.chainId,walletType:i.walletType},n}function pe(t){if(!t)return null;let e=Ii(t.linked_accounts),o=Ce(e,"wallet"),r=Ce(e,"email"),i=Ce(e,"phone"),n=Ce(e,"google_oauth"),s=Ce(e,"twitter_oauth"),u=Ce(e,"discord_oauth"),f=Ce(e,"github_oauth"),h={id:t.id,createdAt:be(t.created_at),linkedAccounts:e,email:r&&{address:r==null?void 0:r.address},phone:i&&{number:i==null?void 0:i.number},wallet:o&&{address:o.address,chainType:o.chainType,chainId:o.chainId,walletType:o.walletType},google:n&&{subject:n.subject,email:n.email,name:n.name},twitter:s&&{subject:s.subject,username:s.username,name:s.name},discord:u&&{subject:u.subject,username:u.username,email:u.email},github:f&&{subject:f.subject,username:f.username,name:f.name,email:f.email}};return Qe(h)}var Vo=()=>"/api/v1/sessions",Ho=()=>"/api/v1/sessions/logout",Bo=()=>"/api/v1/sessions/fork",zo=()=>"/api/v1/siwe/init",jo=()=>"/api/v1/siwe/authenticate",Zo=()=>"/api/v1/siwe/link",$o=()=>"/api/v1/passwordless/init",Ko=()=>"/api/v1/passwordless/authenticate",qo=()=>"/api/v1/passwordless/link",Yo=()=>"/api/v1/passwordless_sms/init",Jo=()=>"/api/v1/passwordless_sms/authenticate",Qo=()=>"/api/v1/passwordless_sms/link",Xo=()=>"/api/v1/oauth/init",er=()=>"/api/v1/oauth/authenticate",tr=()=>"/api/v1/oauth/link",or=()=>"/api/v1/siwe/unlink",rr=()=>"/api/v1/passwordless/unlink",ir=()=>"/api/v1/passwordless_sms/unlink",nr=()=>"/api/v1/oauth/unlink";var Ri=30,yt=class{constructor(){this.authenticateOnce=new ye(async e=>this._authenticate(e)),this.linkOnce=new ye(async e=>this._link(e)),this.refreshOnce=new ye(this._refresh.bind(this)),this.destroyOnce=new ye(this._destroy.bind(this)),this.forkSessionOnce=new ye(this._forkSession.bind(this))}get token(){try{let e=P.get(Fe);return typeof e=="string"?new we(e).value:null}catch(e){return console.error(e),this.destroyLocalState(),null}}get refreshToken(){try{let e=P.get(mt);return typeof e=="string"?e:null}catch(e){return console.error(e),this.destroyLocalState(),null}}hasRefreshCredentials(){return typeof this.token=="string"&&typeof this.refreshToken=="string"}hasActiveToken(){let e=we.parse(this.token);return e!==null&&!e.isExpired(Ri)}async hasValidToken(e,o,r=0){let i=we.parse(this.token);return i!==null&&await i.isValid(e,o,r)}authenticate(e){return this.authenticateOnce.execute(e)}link(e){return this.linkOnce.execute(e)}refresh(){return this.refreshOnce.execute()}forkSession(){return this.forkSessionOnce.execute()}destroy(){return this.destroyOnce.execute()}async _authenticate(e){try{let{token:o,refresh_token:r,user:i,is_new_user:n}=await e.authenticate();return this.storeToken(o),this.storeRefreshToken(r),{user:pe(i),isNewUser:n}}catch(o){throw console.warn("Error authenticating session"),q(o)}}async _link(e){try{let o=await e.link();return pe(o)}catch(o){throw console.warn("Error linking account"),q(o)}}async _refresh(){if(!this.api)throw new p("Session has no API instance");let e=this.token,o=this.refreshToken;if(!e||!o)return null;try{let r=await this.api.post(Vo(),{refresh_token:o});return this.storeToken(r.data.token),this.storeRefreshToken(r.data.refresh_token),pe(r.data.user)}catch(r){throw q(r)}}async _destroy(){var e;try{await((e=this.api)==null?void 0:e.post(Ho(),{refresh_token:this.refreshToken}))}catch{console.warn("Error destroying session")}this.destroyLocalState()}async _forkSession(){if(!this.api)throw new p("Session has no API instance");let e=this.refreshToken;try{let o=await this.api.post(Bo(),{refresh_token:e});return this.storeToken(o.data.token),this.storeRefreshToken(o.data.refresh_token),o.data.new_session_refresh_token}catch(o){throw q(o)}}destroyLocalState(){this.storeToken(null),this.storeRefreshToken(null),P.del(se)}storeToken(e){typeof e=="string"?P.put(Fe,e):P.del(Fe)}storeRefreshToken(e){typeof e=="string"?P.put(mt,e):P.del(mt)}};var lr=require("@ethersproject/address"),Xt=C(require("@walletconnect/web3-provider"));var sr=require("@ethersproject/logger");var Ve=class extends K{constructor(){super("Wallet timeout");this.type="wallet_error"}},bt=t=>{if(t instanceof K)return t;if((t==null?void 0:t.code)&&(t==null?void 0:t.reason)){let e=new He(t);return t.code===sr.ErrorCode.ACTION_REJECTED&&(e.details=Ie.E4001_USER_REJECTED_REQUEST),e}return t!=null&&t.code?new He(t):new K("Unknown connector error",t)},Jt=class extends X{constructor(o,r,i){super(o);this.type="provider_error";this.code=r,this.data=i}},He=class extends Jt{constructor(o){var n,s,u;let r=o;super(r.message,r.code,r.data);let i=Object.values(Ie).find(f=>f.eipCode===r.code);this.details=i||Ie.UNKNOWN_ERROR,r.code===-32002&&((n=r.message)!=null&&n.includes("already pending for origin")?(s=r.message)!=null&&s.includes("wallet_requestPermissions")?this.details=Ie.E32002_CONNECTION_ALREADY_PENDING:this.details=Ie.E32002_REQUEST_ALREADY_PENDING:(u=r.message)!=null&&u.includes("Already processing")&&r.message.includes("eth_requestAccounts")&&(this.details=Ie.E32002_WALLET_LOCKED))}},Wi={ERROR_USER_EXISTS:{message:"User already exists for this address",detail:"Try another address!",retryable:!1},ERROR_TIMED_OUT:{message:"Wallet request timed out",detail:"Please try connecting again.",retryable:!0},ERROR_WALLET_CONNECTION:{message:"Could not log in with wallet",detail:"Please try connecting again.",retryable:!0},ERROR_WALLET_NOT_ALLOWED:{message:"You don\u2019t have access to this app",detail:"Try with a different address?",retryable:!0}},Oi={E32002_CONNECTION_ALREADY_PENDING:{eipCode:-32002,message:"Connection request already pending",detail:"Don\u2019t see your wallet modal? Check your other browser windows.",retryable:!1},E32002_REQUEST_ALREADY_PENDING:{eipCode:-32002,message:"Resource request already pending",detail:"Don\u2019t see your wallet modal? Check your other browser windows.",retryable:!1},E32002_WALLET_LOCKED:{eipCode:-32002,message:"Wallet might be locked",detail:"Don\u2019t see your wallet modal? Check your other browser windows.",retryable:!1},E4001_USER_REJECTED_REQUEST:{eipCode:4001,message:"Signature rejected",detail:"Please try signing again.",retryable:!0}},Ni={E4001_DEFAULT_USER_REJECTED_REQUEST:{eipCode:4001,message:"User Rejected Request",detail:"The user rejected the request.",default:!0,retryable:!0},E4100_DEFAULT_UNAUTHORIZED:{eipCode:4100,message:"Unauthorized",detail:"The requested method and/or account has not been authorized by the user.",default:!0,retryable:!1},E4200_DEFAULT_UNSUPPORTED_METHOD:{eipCode:4200,message:"Unsupported Method",detail:"The Provider does not support the requested method.",default:!0,retryable:!1},E4900_DEFAULT_DISCONNECTED:{eipCode:4900,message:"Disconnected",detail:"The Provider is disconnected from all chains.",default:!0,retryable:!0},E4901_DEFAULT_CHAIN_DISCONNECTED:{eipCode:4901,message:"Chain Disconnected",detail:"The Provider is not connected to the requested chain.",default:!0,retryable:!0},E32700_DEFAULT_PARSE_ERROR:{eipCode:-32700,message:"Parse error",detail:"Invalid JSON",default:!0,retryable:!1},E32600_DEFAULT_INVALID_REQUEST:{eipCode:-32600,message:"Invalid request",detail:"JSON is not a valid request object",default:!0,retryable:!1},E32601_DEFAULT_METHOD_NOT_FOUND:{eipCode:-32601,message:"Method not found",detail:"Method does not exist",default:!0,retryable:!1},E32602_DEFAULT_INVALID_PARAMS:{eipCode:-32602,message:"Invalid params",detail:"Invalid method parameters",default:!0,retryable:!1},E32603_DEFAULT_INTERNAL_ERROR:{eipCode:-32603,message:"Internal error",detail:"Internal JSON-RPC error",default:!0,retryable:!0},E32000_DEFAULT_INVALID_INPUT:{eipCode:-32e3,message:"Invalid input",detail:"Missing or invalid parameters",default:!0,retryable:!1},E32001_DEFAULT_RESOURCE_NOT_FOUND:{eipCode:-32001,message:"Resource not found",detail:"Requested resource not found",default:!0,retryable:!1},E32002_DEFAULT_RESOURCE_UNAVAILABLE:{eipCode:-32002,message:"Resource unavailable",detail:"Requested resource not available",default:!0,retryable:!0},E32003_DEFAULT_TRANSACTION_REJECTED:{eipCode:-32003,message:"Transaction rejected",detail:"Transaction creation failed",default:!0,retryable:!0},E32004_DEFAULT_METHOD_NOT_SUPPORTED:{eipCode:-32004,message:"Method not supported",detail:"Method is not implemented",default:!0,retryable:!1},E32005_DEFAULT_LIMIT_EXCEEDED:{eipCode:-32005,message:"Limit exceeded",detail:"Request exceeds defined limit",default:!0,retryable:!1},E32006_DEFAULT_JSON_RPC_VERSION_NOT_SUPPORTED:{eipCode:-32006,message:"JSON-RPC version not supported",detail:"Version of JSON-RPC protocol is not supported",default:!0,retryable:!1}},Ie={UNKNOWN_ERROR:{eipCode:0,message:"Unknown error",detail:"Unknown error",retryable:!0},...Ni,...Oi},Xe={...Wi,...Ie};var Ui=12e4,Qt=(t=new Ve,e=Ui)=>new Promise((o,r)=>setTimeout(()=>{r(t)},e)),ve=class{constructor(e){this.removeListener=(e,o)=>{if(this.walletProvider)return this.walletProvider.removeListener(e,o)};this.setProvider=e=>{this.walletProvider=e,this._subscriptions.forEach(o=>{var r;(r=this.walletProvider)==null||r.on(o.eventName,o.listener)})};this.walletProvider=e,this._subscriptions=[]}on(e,o){if(this.walletProvider)return this.walletProvider.on(e,o);this._subscriptions.push({eventName:e,listener:o})}request(e){if(!this.walletProvider)throw new K(`A wallet request of type ${e.method} was made before setting a wallet provider.`);return Promise.race([this.walletProvider.request(e),Qt()]).catch(o=>{throw bt(o)})}},xe=class extends ve{constructor(o){super(o)}},Ct=class extends ve{constructor(e){super(e)}sendAsync(e,o){throw new Error("sendAsync is no longer supported by EIP-1193. Use the request method instead.")}};var dr=require("@ethersproject/providers");var ar=["metamask","coinbase_wallet","wallet_connect"];var cr=C(require("@coinbase/wallet-sdk")),Di=()=>{let t=`https://mainnet.infura.io/v3/${pt}`,e=1;return new cr.default({appName:"Privy",darkMode:!1,headlessMode:!1}).makeWeb3Provider(t,e)},eo=()=>{if(!window.ethereum)return!1;if(window.ethereum.isMetaMask)return!0;if(window.ethereum.providers){for(let t of window.ethereum.providers)if(t.isMetaMask)return!0}return!1},vt=()=>{if(!window.ethereum)return!1;if(window.ethereum.isCoinbaseWallet)return!0;if(window.ethereum.providers){for(let t of window.ethereum.providers)if(t.isCoinbaseWallet)return!0}return!1},Be=class{constructor(){this.walletType=null;this.getEthereumProvider=()=>this.ethProvider;this.createSiweMessage=(e,o,r,i,n,s,u)=>`${r} wants you to sign in with your Ethereum account:
2
2
  ${o}
3
3
 
4
4
  ${u}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@privy-io/react-auth",
3
- "version": "1.12.0-beta.8",
3
+ "version": "1.12.1-beta.1",
4
4
  "description": "React client for the Privy Auth API",
5
5
  "keywords": [
6
6
  "authentication",