@privy-io/react-auth 1.11.0-beta.4 → 1.11.0
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/index.cjs.js +1 -1
- package/dist/index.esm.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var vi=Object.create;var at=Object.defineProperty;var bi=Object.getOwnPropertyDescriptor;var xi=Object.getOwnPropertyNames;var Li=Object.getPrototypeOf,Ei=Object.prototype.hasOwnProperty;var Ai=(t,e)=>{for(var o in e)at(t,o,{get:e[o],enumerable:!0})},So=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of xi(e))!Ei.call(t,i)&&i!==o&&at(t,i,{get:()=>e[i],enumerable:!(r=bi(e,i))||r.enumerable});return t};var v=(t,e,o)=>(o=t!=null?vi(Li(t)):{},So(e||!t||!t.__esModule?at(o,"default",{value:t,enumerable:!0}):o,t)),ki=t=>So(at({},"__esModule",{value:!0}),t);var Yn={};Ai(Yn,{PrivyProvider:()=>yi,VERSION:()=>ct,getAccessToken:()=>wi,usePrivy:()=>W});module.exports=ki(Yn);var z=require("react");var ht=v(require("axios"));var To=v(require("axios")),J=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}]`:""}`}},lt=class extends J{constructor(o,r,i,n){super(i,n);this.type=o;this.status=r}},c=class extends J{constructor(o,r){super(o,r);this.type="client_error"}};var Z=class extends J{constructor(o,r){super(o,r);this.type="connector_error"}},I=t=>{if(t instanceof J)return t;if(!To.default.isAxiosError(t))return $(t);if(!t.response)return new lt("api_error",null,t.message,t);let{type:e,message:o,error:r}=t.response.data;return new lt(e||"ApiError",t.response.status,o||r,t)},$=t=>t instanceof J?t:t instanceof Error?new c(t.message,t):new c(`Internal error: ${t}`);var Io="1.11.0-beta.4";var Mo="https://api.privy.io/v0",Ro=process.env.NEXT_PUBLIC_PRIVY_AUTH_URL||"https://auth.privy.io",dt="4df5e2316331463a9130964bd6078dfa",Wo=1e4,Oe=1400,Oo=365*24*60*60,ct=Io,No="ES256",Uo="privy.io",Ne="privy:token",pt="privy:refresh_token",re="privy:active_wallet",ut="privy:wallet_type",Do="walletconnect";var mt=class{constructor(e,o,r){this.appId=e,this.sdkVersion=ct,this.session=o,this.defaults=r}async get(e,o){try{return await ht.default.get(e,this.buildConfig({headers:{"Cache-Control":"no-cache"},...o}))}catch(r){throw I(r)}}async post(e,o,r){try{return await ht.default.post(e,o,this.buildConfig(r))}catch(i){throw I(i)}}async delete(e,o){try{return await ht.default.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}}};var Ue=v(require("jose"));var fe=class{static parse(e){try{return new fe(e)}catch{return null}}constructor(e){this.value=e,this._decoded=Ue.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 Ue.importSPKI(e,No);try{return await Ue.jwtVerify(this.value,i,{issuer:Uo,audience:o,clockTolerance:r}),!0}catch{return!1}}};var ge=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 Kt=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]}},qt=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 _i(){return typeof window<"u"&&window.localStorage?new qt:new Kt}var A=_i();function Ae(t){return new Date(t*1e3)}function Si(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)};default:throw new c("Unrecognized account type")}})}function Ti(t,e){return e.find(o=>o.type==="wallet"&&o.address===t)}function ke(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=A.get(re);if(!r||!e.includes(r)){A.del(re);let s=ke(t.linkedAccounts,"wallet");s&&(r=s.address,A.put(re,r))}let i=Ti(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 de(t){if(!t)return null;let e=Si(t.linked_accounts),o=ke(e,"wallet"),r=ke(e,"email"),i=ke(e,"phone"),n=ke(e,"google_oauth"),s=ke(e,"twitter_oauth"),d=ke(e,"discord_oauth"),u={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:d&&{subject:d.subject,username:d.username,email:d.email}};return qe(u)}var Fo=()=>"/api/v1/sessions",Vo=()=>"/api/v1/sessions/logout",Go=()=>"/api/v1/sessions/fork",Ho=()=>"/api/v1/siwe/init",Bo=()=>"/api/v1/siwe/authenticate",zo=()=>"/api/v1/siwe/link",jo=()=>"/api/v1/passwordless/init",Zo=()=>"/api/v1/passwordless/authenticate",$o=()=>"/api/v1/passwordless/link",Ko=()=>"/api/v1/passwordless_sms/init",qo=()=>"/api/v1/passwordless_sms/authenticate",Yo=()=>"/api/v1/passwordless_sms/link",Jo=()=>"/api/v1/oauth/init",Qo=()=>"/api/v1/oauth/authenticate",Xo=()=>"/api/v1/oauth/link",er=()=>"/api/v1/siwe/unlink",tr=()=>"/api/v1/passwordless/unlink",or=()=>"/api/v1/passwordless_sms/unlink",rr=()=>"/api/v1/oauth/unlink";var Ii=30,ft=class{constructor(){this.authenticateOnce=new ge(async e=>this._authenticate(e)),this.linkOnce=new ge(async e=>this._link(e)),this.refreshOnce=new ge(this._refresh.bind(this)),this.destroyOnce=new ge(this._destroy.bind(this)),this.forkSessionOnce=new ge(this._forkSession.bind(this))}get token(){try{let e=A.get(Ne);return typeof e=="string"?new fe(e).value:null}catch(e){return console.error(e),this.destroyLocalState(),null}}get refreshToken(){try{let e=A.get(pt);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=fe.parse(this.token);return e!==null&&!e.isExpired(Ii)}async hasValidToken(e,o,r=0){let i=fe.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}=await e.authenticate();return this.storeToken(o),this.storeRefreshToken(r),de(i)}catch(o){throw console.warn("Error authenticating session"),$(o)}}async _link(e){try{let o=await e.link();return de(o)}catch(o){throw console.warn("Error linking account"),$(o)}}async _refresh(){if(!this.api)throw new c("Session has no API instance");let e=this.token,o=this.refreshToken;if(!e||!o)return null;try{let r=await this.api.post(Fo(),{refresh_token:o});return this.storeToken(r.data.token),this.storeRefreshToken(r.data.refresh_token),de(r.data.user)}catch(r){throw $(r)}}async _destroy(){var e;try{await((e=this.api)==null?void 0:e.post(Vo(),{refresh_token:this.refreshToken}))}catch{console.warn("Error destroying session")}this.destroyLocalState()}async _forkSession(){if(!this.api)throw new c("Session has no API instance");let e=this.refreshToken;try{let o=await this.api.post(Go(),{refresh_token:e});return this.storeToken(o.data.token),this.storeRefreshToken(o.data.refresh_token),o.data.new_session_refresh_token}catch(o){throw $(o)}}destroyLocalState(){this.storeToken(null),this.storeRefreshToken(null),A.del(re)}storeToken(e){typeof e=="string"?A.put(Ne,e):A.del(Ne)}storeRefreshToken(e){typeof e=="string"?A.put(pt,e):A.del(pt)}};var sr=require("@ethersproject/address"),Qt=v(require("@walletconnect/web3-provider"));var ir=require("@ethersproject/logger");var De=class extends Z{constructor(){super("Wallet timeout");this.type="wallet_error"}},gt=t=>{if(t instanceof Z)return t;if((t==null?void 0:t.code)&&(t==null?void 0:t.reason)){let e=new Fe(t);return t.code===ir.ErrorCode.ACTION_REJECTED&&(e.details=Pe.E4001_USER_REJECTED_REQUEST),e}return t!=null&&t.code?new Fe(t):new Z("Unknown connector error",t)},Yt=class extends J{constructor(o,r,i){super(o);this.type="provider_error";this.code=r,this.data=i}},Fe=class extends Yt{constructor(o){var n,s,d;let r=o;super(r.message,r.code,r.data);let i=Object.values(Pe).find(u=>u.eipCode===r.code);this.details=i||Pe.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=Pe.E32002_CONNECTION_ALREADY_PENDING:this.details=Pe.E32002_REQUEST_ALREADY_PENDING:(d=r.message)!=null&&d.includes("Already processing")&&r.message.includes("eth_requestAccounts")&&(this.details=Pe.E32002_WALLET_LOCKED))}},Mi={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}},Ri={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}},Wi={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}},Pe={UNKNOWN_ERROR:{eipCode:0,message:"Unknown error",detail:"Unknown error",retryable:!0},...Wi,...Ri},wt={...Mi,...Pe};var Oi=12e4,Jt=(t=new De,e=Oi)=>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 Z(`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 gt(o)})}},Ge=class extends Ve{constructor(o){super(o)}},yt=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 ar=require("@ethersproject/providers");var nr=["metamask","coinbase_wallet","wallet_connect"];var lr=v(require("@coinbase/wallet-sdk")),Ni=()=>{let t=`https://mainnet.infura.io/v3/${dt}`,e=1;return new lr.default({appName:"Privy",darkMode:!1,headlessMode:!1}).makeWeb3Provider(t,e)},Xt=()=>{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},Ct=class{constructor(){this.walletType=null;this.getEthereumProvider=()=>this.ethProvider;this.createSiweMessage=(e,o,r,i,n,s,d)=>`${r} wants you to sign in with your Ethereum account:
|
|
1
|
+
"use strict";var vi=Object.create;var at=Object.defineProperty;var bi=Object.getOwnPropertyDescriptor;var xi=Object.getOwnPropertyNames;var Li=Object.getPrototypeOf,Ei=Object.prototype.hasOwnProperty;var Ai=(t,e)=>{for(var o in e)at(t,o,{get:e[o],enumerable:!0})},So=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of xi(e))!Ei.call(t,i)&&i!==o&&at(t,i,{get:()=>e[i],enumerable:!(r=bi(e,i))||r.enumerable});return t};var v=(t,e,o)=>(o=t!=null?vi(Li(t)):{},So(e||!t||!t.__esModule?at(o,"default",{value:t,enumerable:!0}):o,t)),ki=t=>So(at({},"__esModule",{value:!0}),t);var Yn={};Ai(Yn,{PrivyProvider:()=>yi,VERSION:()=>ct,getAccessToken:()=>wi,usePrivy:()=>W});module.exports=ki(Yn);var z=require("react");var ht=v(require("axios"));var To=v(require("axios")),J=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}]`:""}`}},lt=class extends J{constructor(o,r,i,n){super(i,n);this.type=o;this.status=r}},c=class extends J{constructor(o,r){super(o,r);this.type="client_error"}};var Z=class extends J{constructor(o,r){super(o,r);this.type="connector_error"}},I=t=>{if(t instanceof J)return t;if(!To.default.isAxiosError(t))return $(t);if(!t.response)return new lt("api_error",null,t.message,t);let{type:e,message:o,error:r}=t.response.data;return new lt(e||"ApiError",t.response.status,o||r,t)},$=t=>t instanceof J?t:t instanceof Error?new c(t.message,t):new c(`Internal error: ${t}`);var Io="1.11.0";var Mo="https://api.privy.io/v0",Ro=process.env.NEXT_PUBLIC_PRIVY_AUTH_URL||"https://auth.privy.io",dt="4df5e2316331463a9130964bd6078dfa",Wo=1e4,Oe=1400,Oo=365*24*60*60,ct=Io,No="ES256",Uo="privy.io",Ne="privy:token",pt="privy:refresh_token",re="privy:active_wallet",ut="privy:wallet_type",Do="walletconnect";var mt=class{constructor(e,o,r){this.appId=e,this.sdkVersion=ct,this.session=o,this.defaults=r}async get(e,o){try{return await ht.default.get(e,this.buildConfig({headers:{"Cache-Control":"no-cache"},...o}))}catch(r){throw I(r)}}async post(e,o,r){try{return await ht.default.post(e,o,this.buildConfig(r))}catch(i){throw I(i)}}async delete(e,o){try{return await ht.default.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}}};var Ue=v(require("jose"));var fe=class{static parse(e){try{return new fe(e)}catch{return null}}constructor(e){this.value=e,this._decoded=Ue.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 Ue.importSPKI(e,No);try{return await Ue.jwtVerify(this.value,i,{issuer:Uo,audience:o,clockTolerance:r}),!0}catch{return!1}}};var ge=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 Kt=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]}},qt=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 _i(){return typeof window<"u"&&window.localStorage?new qt:new Kt}var A=_i();function Ae(t){return new Date(t*1e3)}function Si(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)};default:throw new c("Unrecognized account type")}})}function Ti(t,e){return e.find(o=>o.type==="wallet"&&o.address===t)}function ke(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=A.get(re);if(!r||!e.includes(r)){A.del(re);let s=ke(t.linkedAccounts,"wallet");s&&(r=s.address,A.put(re,r))}let i=Ti(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 de(t){if(!t)return null;let e=Si(t.linked_accounts),o=ke(e,"wallet"),r=ke(e,"email"),i=ke(e,"phone"),n=ke(e,"google_oauth"),s=ke(e,"twitter_oauth"),d=ke(e,"discord_oauth"),u={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:d&&{subject:d.subject,username:d.username,email:d.email}};return qe(u)}var Fo=()=>"/api/v1/sessions",Vo=()=>"/api/v1/sessions/logout",Go=()=>"/api/v1/sessions/fork",Ho=()=>"/api/v1/siwe/init",Bo=()=>"/api/v1/siwe/authenticate",zo=()=>"/api/v1/siwe/link",jo=()=>"/api/v1/passwordless/init",Zo=()=>"/api/v1/passwordless/authenticate",$o=()=>"/api/v1/passwordless/link",Ko=()=>"/api/v1/passwordless_sms/init",qo=()=>"/api/v1/passwordless_sms/authenticate",Yo=()=>"/api/v1/passwordless_sms/link",Jo=()=>"/api/v1/oauth/init",Qo=()=>"/api/v1/oauth/authenticate",Xo=()=>"/api/v1/oauth/link",er=()=>"/api/v1/siwe/unlink",tr=()=>"/api/v1/passwordless/unlink",or=()=>"/api/v1/passwordless_sms/unlink",rr=()=>"/api/v1/oauth/unlink";var Ii=30,ft=class{constructor(){this.authenticateOnce=new ge(async e=>this._authenticate(e)),this.linkOnce=new ge(async e=>this._link(e)),this.refreshOnce=new ge(this._refresh.bind(this)),this.destroyOnce=new ge(this._destroy.bind(this)),this.forkSessionOnce=new ge(this._forkSession.bind(this))}get token(){try{let e=A.get(Ne);return typeof e=="string"?new fe(e).value:null}catch(e){return console.error(e),this.destroyLocalState(),null}}get refreshToken(){try{let e=A.get(pt);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=fe.parse(this.token);return e!==null&&!e.isExpired(Ii)}async hasValidToken(e,o,r=0){let i=fe.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}=await e.authenticate();return this.storeToken(o),this.storeRefreshToken(r),de(i)}catch(o){throw console.warn("Error authenticating session"),$(o)}}async _link(e){try{let o=await e.link();return de(o)}catch(o){throw console.warn("Error linking account"),$(o)}}async _refresh(){if(!this.api)throw new c("Session has no API instance");let e=this.token,o=this.refreshToken;if(!e||!o)return null;try{let r=await this.api.post(Fo(),{refresh_token:o});return this.storeToken(r.data.token),this.storeRefreshToken(r.data.refresh_token),de(r.data.user)}catch(r){throw $(r)}}async _destroy(){var e;try{await((e=this.api)==null?void 0:e.post(Vo(),{refresh_token:this.refreshToken}))}catch{console.warn("Error destroying session")}this.destroyLocalState()}async _forkSession(){if(!this.api)throw new c("Session has no API instance");let e=this.refreshToken;try{let o=await this.api.post(Go(),{refresh_token:e});return this.storeToken(o.data.token),this.storeRefreshToken(o.data.refresh_token),o.data.new_session_refresh_token}catch(o){throw $(o)}}destroyLocalState(){this.storeToken(null),this.storeRefreshToken(null),A.del(re)}storeToken(e){typeof e=="string"?A.put(Ne,e):A.del(Ne)}storeRefreshToken(e){typeof e=="string"?A.put(pt,e):A.del(pt)}};var sr=require("@ethersproject/address"),Qt=v(require("@walletconnect/web3-provider"));var ir=require("@ethersproject/logger");var De=class extends Z{constructor(){super("Wallet timeout");this.type="wallet_error"}},gt=t=>{if(t instanceof Z)return t;if((t==null?void 0:t.code)&&(t==null?void 0:t.reason)){let e=new Fe(t);return t.code===ir.ErrorCode.ACTION_REJECTED&&(e.details=Pe.E4001_USER_REJECTED_REQUEST),e}return t!=null&&t.code?new Fe(t):new Z("Unknown connector error",t)},Yt=class extends J{constructor(o,r,i){super(o);this.type="provider_error";this.code=r,this.data=i}},Fe=class extends Yt{constructor(o){var n,s,d;let r=o;super(r.message,r.code,r.data);let i=Object.values(Pe).find(u=>u.eipCode===r.code);this.details=i||Pe.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=Pe.E32002_CONNECTION_ALREADY_PENDING:this.details=Pe.E32002_REQUEST_ALREADY_PENDING:(d=r.message)!=null&&d.includes("Already processing")&&r.message.includes("eth_requestAccounts")&&(this.details=Pe.E32002_WALLET_LOCKED))}},Mi={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}},Ri={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}},Wi={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}},Pe={UNKNOWN_ERROR:{eipCode:0,message:"Unknown error",detail:"Unknown error",retryable:!0},...Wi,...Ri},wt={...Mi,...Pe};var Oi=12e4,Jt=(t=new De,e=Oi)=>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 Z(`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 gt(o)})}},Ge=class extends Ve{constructor(o){super(o)}},yt=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 ar=require("@ethersproject/providers");var nr=["metamask","coinbase_wallet","wallet_connect"];var lr=v(require("@coinbase/wallet-sdk")),Ni=()=>{let t=`https://mainnet.infura.io/v3/${dt}`,e=1;return new lr.default({appName:"Privy",darkMode:!1,headlessMode:!1}).makeWeb3Provider(t,e)},Xt=()=>{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},Ct=class{constructor(){this.walletType=null;this.getEthereumProvider=()=>this.ethProvider;this.createSiweMessage=(e,o,r,i,n,s,d)=>`${r} wants you to sign in with your Ethereum account:
|
|
2
2
|
${o}
|
|
3
3
|
|
|
4
4
|
${d}
|
package/dist/index.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{useEffect as Rt,useState as le}from"react";import Ft from"axios";import mi from"axios";var H=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}]`:""}`}},qe=class extends H{constructor(o,r,i,n){super(i,n);this.type=o;this.status=r}},d=class extends H{constructor(o,r){super(o,r);this.type="client_error"}};var F=class extends H{constructor(o,r){super(o,r);this.type="connector_error"}},k=t=>{if(t instanceof H)return t;if(!mi.isAxiosError(t))return V(t);if(!t.response)return new qe("api_error",null,t.message,t);let{type:e,message:o,error:r}=t.response.data;return new qe(e||"ApiError",t.response.status,o||r,t)},V=t=>t instanceof H?t:t instanceof Error?new d(t.message,t):new d(`Internal error: ${t}`);var go="1.11.0-beta.4";var wo="https://api.privy.io/v0",yo=process.env.NEXT_PUBLIC_PRIVY_AUTH_URL||"https://auth.privy.io",Ye="4df5e2316331463a9130964bd6078dfa",Co=1e4,ve=1400,vo=365*24*60*60,Dt=go,bo="ES256",xo="privy.io",be="privy:token",Je="privy:refresh_token",Z="privy:active_wallet",Qe="privy:wallet_type",Lo="walletconnect";var Xe=class{constructor(e,o,r){this.appId=e,this.sdkVersion=Dt,this.session=o,this.defaults=r}async get(e,o){try{return await Ft.get(e,this.buildConfig({headers:{"Cache-Control":"no-cache"},...o}))}catch(r){throw k(r)}}async post(e,o,r){try{return await Ft.post(e,o,this.buildConfig(r))}catch(i){throw k(i)}}async delete(e,o){try{return await Ft.delete(e,this.buildConfig(o))}catch(r){throw k(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 xe from"jose";var te=class{static parse(e){try{return new te(e)}catch{return null}}constructor(e){this.value=e,this._decoded=xe.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 xe.importSPKI(e,bo);try{return await xe.jwtVerify(this.value,i,{issuer:xo,audience:o,clockTolerance:r}),!0}catch{return!1}}};var oe=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 Vt=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]}},Gt=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 gi(){return typeof window<"u"&&window.localStorage?new Gt:new Vt}var b=gi();function de(t){return new Date(t*1e3)}function wi(t){return t.map(e=>{switch(e.type){case"wallet":return{address:e.address,type:e.type,verifiedAt:de(e.verified_at),chainType:e.chain_type};case"email":return{address:e.address,type:e.type,verifiedAt:de(e.verified_at)};case"phone":return{number:e.phoneNumber,type:e.type,verifiedAt:de(e.verified_at)};case"google_oauth":return{subject:e.subject,email:e.email,name:e.name,type:e.type,verifiedAt:de(e.verified_at)};case"twitter_oauth":return{subject:e.subject,username:e.username,name:e.name,type:e.type,verifiedAt:de(e.verified_at)};case"discord_oauth":return{subject:e.subject,username:e.username,email:e.email,type:e.type,verifiedAt:de(e.verified_at)};default:throw new d("Unrecognized account type")}})}function yi(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 De(t){let e=t.linkedAccounts.map(s=>s.type==="wallet"?s.address:null),r=b.get(Z);if(!r||!e.includes(r)){b.del(Z);let s=ce(t.linkedAccounts,"wallet");s&&(r=s.address,b.put(Z,r))}let i=yi(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 K(t){if(!t)return null;let e=wi(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"),l=ce(e,"discord_oauth"),u={id:t.id,createdAt:de(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:l&&{subject:l.subject,username:l.username,email:l.email}};return De(u)}var Eo=()=>"/api/v1/sessions",Ao=()=>"/api/v1/sessions/logout",ko=()=>"/api/v1/sessions/fork",Po=()=>"/api/v1/siwe/init",_o=()=>"/api/v1/siwe/authenticate",So=()=>"/api/v1/siwe/link",To=()=>"/api/v1/passwordless/init",Io=()=>"/api/v1/passwordless/authenticate",Mo=()=>"/api/v1/passwordless/link",Ro=()=>"/api/v1/passwordless_sms/init",Wo=()=>"/api/v1/passwordless_sms/authenticate",Oo=()=>"/api/v1/passwordless_sms/link",No=()=>"/api/v1/oauth/init",Uo=()=>"/api/v1/oauth/authenticate",Do=()=>"/api/v1/oauth/link",Fo=()=>"/api/v1/siwe/unlink",Vo=()=>"/api/v1/passwordless/unlink",Go=()=>"/api/v1/passwordless_sms/unlink",Ho=()=>"/api/v1/oauth/unlink";var Ci=30,et=class{constructor(){this.authenticateOnce=new oe(async e=>this._authenticate(e)),this.linkOnce=new oe(async e=>this._link(e)),this.refreshOnce=new oe(this._refresh.bind(this)),this.destroyOnce=new oe(this._destroy.bind(this)),this.forkSessionOnce=new oe(this._forkSession.bind(this))}get token(){try{let e=b.get(be);return typeof e=="string"?new te(e).value:null}catch(e){return console.error(e),this.destroyLocalState(),null}}get refreshToken(){try{let e=b.get(Je);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=te.parse(this.token);return e!==null&&!e.isExpired(Ci)}async hasValidToken(e,o,r=0){let i=te.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}=await e.authenticate();return this.storeToken(o),this.storeRefreshToken(r),K(i)}catch(o){throw console.warn("Error authenticating session"),V(o)}}async _link(e){try{let o=await e.link();return K(o)}catch(o){throw console.warn("Error linking account"),V(o)}}async _refresh(){if(!this.api)throw new d("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),K(r.data.user)}catch(r){throw V(r)}}async _destroy(){var e;try{await((e=this.api)==null?void 0:e.post(Ao(),{refresh_token:this.refreshToken}))}catch{console.warn("Error destroying session")}this.destroyLocalState()}async _forkSession(){if(!this.api)throw new d("Session has no API instance");let e=this.refreshToken;try{let o=await this.api.post(ko(),{refresh_token:e});return this.storeToken(o.data.token),this.storeRefreshToken(o.data.refresh_token),o.data.new_session_refresh_token}catch(o){throw V(o)}}destroyLocalState(){this.storeToken(null),this.storeRefreshToken(null),b.del(Z)}storeToken(e){typeof e=="string"?b.put(be,e):b.del(be)}storeRefreshToken(e){typeof e=="string"?b.put(Je,e):b.del(Je)}};import{getAddress as Ai}from"@ethersproject/address";import zo from"@walletconnect/web3-provider";import{ErrorCode as vi}from"@ethersproject/logger";var Le=class extends F{constructor(){super("Wallet timeout");this.type="wallet_error"}},tt=t=>{if(t instanceof F)return t;if((t==null?void 0:t.code)&&(t==null?void 0:t.reason)){let e=new Ee(t);return t.code===vi.ACTION_REJECTED&&(e.details=pe.E4001_USER_REJECTED_REQUEST),e}return t!=null&&t.code?new Ee(t):new F("Unknown connector error",t)},Ht=class extends H{constructor(o,r,i){super(o);this.type="provider_error";this.code=r,this.data=i}},Ee=class extends Ht{constructor(o){var n,s,l;let r=o;super(r.message,r.code,r.data);let i=Object.values(pe).find(u=>u.eipCode===r.code);this.details=i||pe.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=pe.E32002_CONNECTION_ALREADY_PENDING:this.details=pe.E32002_REQUEST_ALREADY_PENDING:(l=r.message)!=null&&l.includes("Already processing")&&r.message.includes("eth_requestAccounts")&&(this.details=pe.E32002_WALLET_LOCKED))}},bi={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}},xi={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}},Li={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}},pe={UNKNOWN_ERROR:{eipCode:0,message:"Unknown error",detail:"Unknown error",retryable:!0},...Li,...xi},ot={...bi,...pe};var Ei=12e4,Bt=(t=new Le,e=Ei)=>new Promise((o,r)=>setTimeout(()=>{r(t)},e)),Ae=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 F(`A wallet request of type ${e.method} was made before setting a wallet provider.`);return Promise.race([this.walletProvider.request(e),Bt()]).catch(o=>{throw tt(o)})}},ke=class extends Ae{constructor(o){super(o)}},rt=class extends Ae{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 ki}from"@ethersproject/providers";var Bo=["metamask","coinbase_wallet","wallet_connect"];import Pi from"@coinbase/wallet-sdk";var _i=()=>{let t=`https://mainnet.infura.io/v3/${Ye}`,e=1;return new Pi({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},nt=()=>{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},it=class{constructor(){this.walletType=null;this.getEthereumProvider=()=>this.ethProvider;this.createSiweMessage=(e,o,r,i,n,s,l)=>`${r} wants you to sign in with your Ethereum account:
|
|
1
|
+
import{useEffect as Rt,useState as le}from"react";import Ft from"axios";import mi from"axios";var H=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}]`:""}`}},qe=class extends H{constructor(o,r,i,n){super(i,n);this.type=o;this.status=r}},d=class extends H{constructor(o,r){super(o,r);this.type="client_error"}};var F=class extends H{constructor(o,r){super(o,r);this.type="connector_error"}},k=t=>{if(t instanceof H)return t;if(!mi.isAxiosError(t))return V(t);if(!t.response)return new qe("api_error",null,t.message,t);let{type:e,message:o,error:r}=t.response.data;return new qe(e||"ApiError",t.response.status,o||r,t)},V=t=>t instanceof H?t:t instanceof Error?new d(t.message,t):new d(`Internal error: ${t}`);var go="1.11.0";var wo="https://api.privy.io/v0",yo=process.env.NEXT_PUBLIC_PRIVY_AUTH_URL||"https://auth.privy.io",Ye="4df5e2316331463a9130964bd6078dfa",Co=1e4,ve=1400,vo=365*24*60*60,Dt=go,bo="ES256",xo="privy.io",be="privy:token",Je="privy:refresh_token",Z="privy:active_wallet",Qe="privy:wallet_type",Lo="walletconnect";var Xe=class{constructor(e,o,r){this.appId=e,this.sdkVersion=Dt,this.session=o,this.defaults=r}async get(e,o){try{return await Ft.get(e,this.buildConfig({headers:{"Cache-Control":"no-cache"},...o}))}catch(r){throw k(r)}}async post(e,o,r){try{return await Ft.post(e,o,this.buildConfig(r))}catch(i){throw k(i)}}async delete(e,o){try{return await Ft.delete(e,this.buildConfig(o))}catch(r){throw k(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 xe from"jose";var te=class{static parse(e){try{return new te(e)}catch{return null}}constructor(e){this.value=e,this._decoded=xe.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 xe.importSPKI(e,bo);try{return await xe.jwtVerify(this.value,i,{issuer:xo,audience:o,clockTolerance:r}),!0}catch{return!1}}};var oe=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 Vt=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]}},Gt=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 gi(){return typeof window<"u"&&window.localStorage?new Gt:new Vt}var b=gi();function de(t){return new Date(t*1e3)}function wi(t){return t.map(e=>{switch(e.type){case"wallet":return{address:e.address,type:e.type,verifiedAt:de(e.verified_at),chainType:e.chain_type};case"email":return{address:e.address,type:e.type,verifiedAt:de(e.verified_at)};case"phone":return{number:e.phoneNumber,type:e.type,verifiedAt:de(e.verified_at)};case"google_oauth":return{subject:e.subject,email:e.email,name:e.name,type:e.type,verifiedAt:de(e.verified_at)};case"twitter_oauth":return{subject:e.subject,username:e.username,name:e.name,type:e.type,verifiedAt:de(e.verified_at)};case"discord_oauth":return{subject:e.subject,username:e.username,email:e.email,type:e.type,verifiedAt:de(e.verified_at)};default:throw new d("Unrecognized account type")}})}function yi(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 De(t){let e=t.linkedAccounts.map(s=>s.type==="wallet"?s.address:null),r=b.get(Z);if(!r||!e.includes(r)){b.del(Z);let s=ce(t.linkedAccounts,"wallet");s&&(r=s.address,b.put(Z,r))}let i=yi(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 K(t){if(!t)return null;let e=wi(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"),l=ce(e,"discord_oauth"),u={id:t.id,createdAt:de(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:l&&{subject:l.subject,username:l.username,email:l.email}};return De(u)}var Eo=()=>"/api/v1/sessions",Ao=()=>"/api/v1/sessions/logout",ko=()=>"/api/v1/sessions/fork",Po=()=>"/api/v1/siwe/init",_o=()=>"/api/v1/siwe/authenticate",So=()=>"/api/v1/siwe/link",To=()=>"/api/v1/passwordless/init",Io=()=>"/api/v1/passwordless/authenticate",Mo=()=>"/api/v1/passwordless/link",Ro=()=>"/api/v1/passwordless_sms/init",Wo=()=>"/api/v1/passwordless_sms/authenticate",Oo=()=>"/api/v1/passwordless_sms/link",No=()=>"/api/v1/oauth/init",Uo=()=>"/api/v1/oauth/authenticate",Do=()=>"/api/v1/oauth/link",Fo=()=>"/api/v1/siwe/unlink",Vo=()=>"/api/v1/passwordless/unlink",Go=()=>"/api/v1/passwordless_sms/unlink",Ho=()=>"/api/v1/oauth/unlink";var Ci=30,et=class{constructor(){this.authenticateOnce=new oe(async e=>this._authenticate(e)),this.linkOnce=new oe(async e=>this._link(e)),this.refreshOnce=new oe(this._refresh.bind(this)),this.destroyOnce=new oe(this._destroy.bind(this)),this.forkSessionOnce=new oe(this._forkSession.bind(this))}get token(){try{let e=b.get(be);return typeof e=="string"?new te(e).value:null}catch(e){return console.error(e),this.destroyLocalState(),null}}get refreshToken(){try{let e=b.get(Je);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=te.parse(this.token);return e!==null&&!e.isExpired(Ci)}async hasValidToken(e,o,r=0){let i=te.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}=await e.authenticate();return this.storeToken(o),this.storeRefreshToken(r),K(i)}catch(o){throw console.warn("Error authenticating session"),V(o)}}async _link(e){try{let o=await e.link();return K(o)}catch(o){throw console.warn("Error linking account"),V(o)}}async _refresh(){if(!this.api)throw new d("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),K(r.data.user)}catch(r){throw V(r)}}async _destroy(){var e;try{await((e=this.api)==null?void 0:e.post(Ao(),{refresh_token:this.refreshToken}))}catch{console.warn("Error destroying session")}this.destroyLocalState()}async _forkSession(){if(!this.api)throw new d("Session has no API instance");let e=this.refreshToken;try{let o=await this.api.post(ko(),{refresh_token:e});return this.storeToken(o.data.token),this.storeRefreshToken(o.data.refresh_token),o.data.new_session_refresh_token}catch(o){throw V(o)}}destroyLocalState(){this.storeToken(null),this.storeRefreshToken(null),b.del(Z)}storeToken(e){typeof e=="string"?b.put(be,e):b.del(be)}storeRefreshToken(e){typeof e=="string"?b.put(Je,e):b.del(Je)}};import{getAddress as Ai}from"@ethersproject/address";import zo from"@walletconnect/web3-provider";import{ErrorCode as vi}from"@ethersproject/logger";var Le=class extends F{constructor(){super("Wallet timeout");this.type="wallet_error"}},tt=t=>{if(t instanceof F)return t;if((t==null?void 0:t.code)&&(t==null?void 0:t.reason)){let e=new Ee(t);return t.code===vi.ACTION_REJECTED&&(e.details=pe.E4001_USER_REJECTED_REQUEST),e}return t!=null&&t.code?new Ee(t):new F("Unknown connector error",t)},Ht=class extends H{constructor(o,r,i){super(o);this.type="provider_error";this.code=r,this.data=i}},Ee=class extends Ht{constructor(o){var n,s,l;let r=o;super(r.message,r.code,r.data);let i=Object.values(pe).find(u=>u.eipCode===r.code);this.details=i||pe.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=pe.E32002_CONNECTION_ALREADY_PENDING:this.details=pe.E32002_REQUEST_ALREADY_PENDING:(l=r.message)!=null&&l.includes("Already processing")&&r.message.includes("eth_requestAccounts")&&(this.details=pe.E32002_WALLET_LOCKED))}},bi={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}},xi={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}},Li={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}},pe={UNKNOWN_ERROR:{eipCode:0,message:"Unknown error",detail:"Unknown error",retryable:!0},...Li,...xi},ot={...bi,...pe};var Ei=12e4,Bt=(t=new Le,e=Ei)=>new Promise((o,r)=>setTimeout(()=>{r(t)},e)),Ae=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 F(`A wallet request of type ${e.method} was made before setting a wallet provider.`);return Promise.race([this.walletProvider.request(e),Bt()]).catch(o=>{throw tt(o)})}},ke=class extends Ae{constructor(o){super(o)}},rt=class extends Ae{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 ki}from"@ethersproject/providers";var Bo=["metamask","coinbase_wallet","wallet_connect"];import Pi from"@coinbase/wallet-sdk";var _i=()=>{let t=`https://mainnet.infura.io/v3/${Ye}`,e=1;return new Pi({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},nt=()=>{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},it=class{constructor(){this.walletType=null;this.getEthereumProvider=()=>this.ethProvider;this.createSiweMessage=(e,o,r,i,n,s,l)=>`${r} wants you to sign in with your Ethereum account:
|
|
2
2
|
${o}
|
|
3
3
|
|
|
4
4
|
${l}
|