UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

186 lines (170 loc) • 25 kB
import{useEffect as e,useState as t,createContext as n,useContext as i,useMemo as a,useCallback as l}from"react";import{create as o}from"zustand";import{u as r,n as s,o as c,p as d}from"./context-DLtU3JQy.mjs";import{styled as h,css as p}from"styled-components";import{jsx as u,jsxs as m}from"react/jsx-runtime";import*as w from"react-device-detect";import{r as y,m as g,k as f,c as b,i as v,s as x,f as k,w as C,S as _}from"./use-export-wallet-_wu5ex5t.mjs";import{u as T}from"./events-context-CI0iqAXA.mjs";import{isAddress as z}from"viem";import{getAddress as $,toHex as P}from"viem/utils";import{P as S,u as j}from"./internal-context-Z-fyxadS.mjs";import{P as I}from"./privy-context-DrMxzgOR.mjs";import{u as N}from"./useWallets-BzNCTucF.mjs";const E=o((()=>({listings:[],loading:!1}))),L=E.setState,A=e=>E.getState().listings.find((({slug:t})=>R(t)===R(e))),O=e=>{let t;try{t=new URL(e).hostname}catch(e){return}return E.getState().listings.find((({homepage:e})=>{let n;try{n=new URL(e).hostname}catch(e){return!1}return t.includes(n)}))},U=(t=!0)=>{let n=r(),i=E((e=>e.listings)),a=E((e=>e.loading)),l=!!i.length;return e((()=>{t&&!l&&(L({listings:[],loading:!0}),(async e=>{let t=new URL("v3/wallets","https://explorer-api.walletconnect.com");t.searchParams.append("projectId",e);let n=await fetch(t);return n.ok?Object.values((await n.json()).listings).sort((({slug:e},{slug:t})=>"rainbow"===e&&"metamask"===t?1:"metamask"===e||"rainbow"===e?-1:1)):(console.debug(`Failed to fetch WalletConnect listings: ${await n.text().catch((()=>"No response."))}`),[])})(n.walletConnectCloudProjectId).then((e=>L({listings:e,loading:!1}))).catch((e=>{console.error(e),L({listings:[],loading:!1})})))}),[t,l,n.walletConnectCloudProjectId]),{listings:i,loading:a}},W=e=>{let{listings:t}=U();return t.find((({slug:t})=>R(t)===R(e)))};function R(e){return"cryptocom"===e?"cryptocom-defi":"binance"===e?"binance-defi":e.replace(/[-_]wallet$/,"")}function M(e){return["wallet_connect","backpack","binance","binanceus","bitget_wallet","bybit_wallet","cryptocom","haha_wallet","jupiter","metamask","okx_wallet","phantom","rabby_wallet","rainbow","ronin_wallet","safe","solflare","uniswap","zerion","kraken_wallet"].some((t=>e?.includes(t)))}function q(e){return"ethereum"===e.type}function B(e,t){if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++){let i=e[n],a=t[n];if(i?.address!==a?.address||i&&a&&q(i)&&q(a)&&i?.chainId!==a?.chainId||i?.connectorType!==a?.connectorType||i?.connectedAt!==a?.connectedAt||i?.walletClientType!==a?.walletClientType||i?.isConnected!==a?.isConnected||i?.linked!==a?.linked)return!1}return!0}const D=(e,t)=>e.rpcTimeouts&&e.rpcTimeouts[t]||s;function F(e){return"ethereum"===e.chainType}class J extends y{buildConnectedWallet(e,t,n,i){let a=async()=>!!this.wallets.find((t=>$(t.address)===$(e)));return{type:"ethereum",address:$(e),chainId:t,meta:n,imported:i,switchChain:async n=>{let i,l;if(!a)throw new S("Wallet is not currently connected.");let o=this.wallets.find((t=>$(t.address)===$(e)))?.chainId;if(!o)throw new S("Unable to determine current chainId.");if("number"==typeof n?(i=`0x${n.toString(16)}`,l=n):(i=n,l=Number(n)),o===g(i))return;let r=this.chains.find((e=>e.id===l));if(!r)throw new S(`Unsupported chainId: ${n}`);let s=async()=>{await this.proxyProvider.request({method:"wallet_switchEthereumChain",params:[{chainId:i}]})};try{return await s()}catch(e){if(((e,t)=>{switch(t){case"coinbase_wallet":case"base_account":return e.message.includes("addEthereumChain");case"rabby_wallet":return e.message.includes("Unrecognized chain ID");default:return 4902===e.code||e.message?.includes("4902")}})(e,this.walletClientType))return"rabby_wallet"===this.walletClientType&&await f(300),await this.proxyProvider.request({method:"wallet_addEthereumChain",params:[{chainId:i,chainName:r.name,nativeCurrency:r.nativeCurrency,rpcUrls:[r.rpcUrls.default?.http[0]??""],blockExplorerUrls:[r.blockExplorers?.default.url??""]}]}),s();if("rainbow"===this.walletClientType&&e.message?.includes("wallet_switchEthereumChain"))throw new S(`Rainbow does not support the chainId ${t}`);throw e}},connectedAt:Date.now(),walletClientType:this.walletClientType,connectorType:this.connectorType,isConnected:a,getEthereumProvider:async()=>{if(!await a())throw new S("Wallet is not currently connected.");return this.proxyProvider},sign:async e=>{if(!await a())throw new S("Wallet is not currently connected.");return await this.sign(e)},disconnect:()=>{this.disconnect()}}}async syncAccounts(e){let t;b.debug("Syncing accounts",{wallet:this.walletClientType});let n=e;try{if(void 0===n){b.rpcRequest("eth_accounts",this.walletClientType);let e=await v((()=>this.proxyProvider.request({method:"eth_accounts"})),{maxAttempts:10,delayMs:500});b.rpcResponse("eth_accounts",this.walletClientType),Array.isArray(e)&&(n=e)}}catch(e){b.warn("Wallet did not respond to eth_accounts, using prefetched accounts",{wallet:this.walletClientType})}if(!n||!Array.isArray(n)||n.length<=0||!n[0])return void b.debug("No accounts found",{wallet:this.walletClientType});let i=n[0];if(!z(i))return;let a=$(i),l=[];if("privy"===this.walletClientType){let e=x.get(c(a));this.chains.find((t=>t.id===Number(e)))||(x.del(c(a)),e=null),t=e||`0x${this.defaultChain.id.toString(16)}`;try{await this.proxyProvider.request({method:"wallet_switchEthereumChain",params:[{chainId:t}]})}catch(e){b.warn("Unable to switch embedded wallet chain on initialization",{wallet:this.walletClientType,chainId:t})}}else try{b.rpcRequest("eth_chainId",this.walletClientType);let e=await v((()=>this.proxyProvider.request({method:"eth_chainId"})),{maxAttempts:10,delayMs:500});if(b.rpcResponse("eth_chainId",this.walletClientType),"string"==typeof e)t=e;else{if("number"!=typeof e)throw Error("Invalid chainId returned from provider");t=`0x${e.toString(16)}`}}catch(e){b.warn("Failed to get chainId from provider, using default",{wallet:this.walletClientType,defaultChainId:d}),t=d}let o=g(t);if(!l.find((e=>$(e.address)===a))){let e={name:this.walletBranding.name,icon:"string"==typeof this.walletBranding.icon?this.walletBranding.icon:void 0,id:this.walletBranding.id};l.push(this.buildConnectedWallet($(i),o,e,"embedded_imported"===this.connectorType))}B(l,this.wallets)||(b.debug("Wallets updated",{wallet:this.walletClientType,address:a,chainId:o}),this.wallets=l,this.emit("walletsUpdated"))}async getConnectedWallet(){let e=await this.proxyProvider.request({method:"eth_accounts"});return this.wallets.sort(((e,t)=>t.connectedAt-e.connectedAt)).find((t=>e.find((e=>$(e)===$(t.address)))))||null}async isConnected(){let e=await this.proxyProvider.request({method:"eth_accounts"});return Array.isArray(e)&&e.length>0}async sign(e){return await this.connect({showPrompt:!1}),this.proxyProvider.request({method:"personal_sign",params:[P(e),this.wallets[0]?.address]})}subscribeListeners(){this.proxyProvider.on("accountsChanged",this.onAccountsChanged),this.proxyProvider.on("chainChanged",this.onChainChanged),this.proxyProvider.on("disconnect",this.onDisconnect),this.proxyProvider.on("connect",this.onConnect)}unsubscribeListeners(){this.proxyProvider.removeListener("accountsChanged",this.onAccountsChanged),this.proxyProvider.removeListener("chainChanged",this.onChainChanged),this.proxyProvider.removeListener("disconnect",this.onDisconnect),this.proxyProvider.removeListener("connect",this.onConnect)}constructor(e,t,n,i){super(e),this.chainType="ethereum",this.onAccountsChanged=e=>{b.providerEvent("accountsChanged",this.walletClientType,{accountsCount:e.length}),0===e.length?this.onDisconnect():this.syncAccounts(e)},this.onChainChanged=e=>{b.providerEvent("chainChanged",this.walletClientType,{chainId:e}),this.wallets.forEach((t=>{t.chainId=g(e),"privy"===this.walletClientType&&x.put(c(t.address),e)})),this.emit("walletsUpdated")},this.onDisconnect=()=>{b.providerEvent("disconnect",this.walletClientType),this.connected=!1,this.wallets=[],this.emit("walletsUpdated")},this.onConnect=async()=>{b.providerEvent("connect",this.walletClientType),"base_account"===this.connectorType&&this.connected||(this.connected=!0,"coinbase_wallet"===this.connectorType&&await f(500),this.syncAccounts())},this.wallets=[],this.walletClientType=e,this.chains=t,this.defaultChain=n,this.rpcConfig=i,this.rpcTimeoutDuration=D(i,e),this.connected=!1,this.initialized=!1}}class K{on(e,t){if(this.walletProvider)return this.walletProvider.on(e,t);this._subscriptions.push({eventName:e,listener:t})}async request(e){if(!this.walletProvider)throw new S(`A wallet request of type ${e.method} was made before setting a wallet provider.`);return Promise.race([this.walletProvider.request(e),this.walletTimeout()]).catch((e=>{throw k(e)}))}constructor(e,t){this.removeListener=(e,t)=>{if(this.walletProvider)try{return this.walletProvider.removeListener(e,t)}catch(e){console.warn("Unable to remove wallet provider listener")}},this.walletTimeout=(e=new C,t=this.rpcTimeoutDuration)=>new Promise(((n,i)=>setTimeout((()=>{i(e)}),t))),this.setWalletProvider=e=>{this.walletProvider&&this._subscriptions.forEach((e=>{this.removeListener(e.eventName,e.listener)})),this.walletProvider=e,this._subscriptions.forEach((e=>{this.walletProvider?.on(e.eventName,e.listener)}))},this.walletProvider=e,this.rpcTimeoutDuration=t||s,this._subscriptions=[]}}const G=n=>{let[i,a]=t("auto");return e((()=>{let e=new ResizeObserver((e=>{a(e[0]?.contentRect.height??"auto")}));return n.current&&e.observe(n.current),()=>{n.current&&e.unobserve(n.current)}}),[n.current]),i},H=h.div` text-align: left; flex-grow: 1; `,Q=h.div` display: flex; flex-direction: column; justify-content: flex-end; flex-grow: 1; `,X=h.div` display: flex; flex-direction: column; gap: 8px; /* for Internet Explorer, Edge */ -ms-overflow-style: none; /* for Firefox */ scrollbar-width: none; /* for Chrome, Safari, and Opera */ &::-webkit-scrollbar { display: none; } `,V=h(X)` ${e=>"light"===e.$colorScheme?"background: linear-gradient(var(--privy-color-background), var(--privy-color-background) 70%) bottom, linear-gradient(rgba(0, 0, 0, 0) 20%, rgba(0, 0, 0, 0.06)) bottom;":"dark"===e.$colorScheme?"background: linear-gradient(var(--privy-color-background), var(--privy-color-background) 70%) bottom, linear-gradient(rgba(255, 255, 255, 0) 20%, rgba(255, 255, 255, 0.06)) bottom;":void 0} background-repeat: no-repeat; background-size: 100% 32px, 100% 16px; background-attachment: local, scroll; max-height: 400px; overflow-y: auto; scrollbar-width: none; padding: 3px; `;let Y=p` && { width: 100%; font-size: 16px; line-height: 24px; min-height: 56px; /* Tablet and Up */ @media (min-width: 440px) { font-size: 14px; } display: flex; gap: 12px; align-items: center; color: var(--privy-color-foreground); padding: 10px 12px; border: 1px solid var(--privy-color-foreground-4) !important; border-radius: var(--privy-border-radius-md); transition: background-color 200ms ease; cursor: pointer; &:hover { background-color: var(--privy-color-background-2); } &:disabled { cursor: pointer; background-color: var(--privy-color-background-2); } } `;const Z=h.div` text-align: center; font-size: 14px; margin-bottom: 24px; `,ee=h.button.attrs({className:"login-method-button"})` ${Y} `;h.a` ${Y} `;const te=h.div` width: 32px; height: 32px; border-radius: ${e=>e.$fullSize?"0":"4px"}; background: ${e=>e.$fullSize?"transparent":"var(--privy-color-background-2)"}; display: flex; align-items: center; justify-content: center; flex-shrink: 0; svg { width: ${e=>e.$fullSize?"32px":"18px"}; height: ${e=>e.$fullSize?"32px":"18px"}; color: ${e=>e.$fullSize?"inherit":"var(--privy-color-icon-muted)"}; } `,ne=h.div` width: 100%; height: 100%; min-height: inherit; display: flex; flex-direction: column; ${e=>e.$if?"display: none;":""} `,ie=h.div` width: 100%; height: 100%; padding: ${e=>e.$withPadding?"64px 0px":"0px"}; `,ae=h.div` display: flex; flex-direction: column; align-items: center; justify-content: center; margin-bottom: 32px; gap: 12px; & h3 { font-size: 18px; font-style: normal; font-weight: 600; line-height: 24px; } & p { max-width: 300px; font-size: 14px; font-style: normal; font-weight: 400; line-height: 20px; } `,le=({success:e,fail:t})=>/*#__PURE__*/m(re,{children:[/*#__PURE__*/u(oe,{children:/*#__PURE__*/u(ce,{className:e?"success":t?"fail":""})}),/*#__PURE__*/u(oe,{children:/*#__PURE__*/u(se,{className:e?"success":t?"fail":""})})]});let oe=h.span` && { position: absolute; top: 0; left: 0; z-index: 2; } `,re=h.span` position: relative; width: 82px; height: 82px; display: inline-block; `;const se=h.span` && { width: 82px; height: 82px; border-width: 4px; border-style: solid; border-color: ${e=>e.color??"var(--privy-color-icon-subtle)"}; border-bottom-color: transparent; border-radius: 50%; display: inline-block; box-sizing: border-box; animation: rotation 1.2s linear infinite; transition: border-color 800ms; } @keyframes rotation { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } &&&.success { border-color: var(--privy-color-icon-success); border-bottom-color: var(--privy-color-icon-success); } &&&.fail { border-color: var(--privy-color-icon-error); border-bottom-color: var(--privy-color-icon-error); } `,ce=h(se)` && { border-bottom-color: ${e=>e.color??"var(--privy-color-border-default)"}; border-color: ${e=>e.color??"var(--privy-color-border-default)"}; animation: none; opacity: 0.5; } `,de=e=>/*#__PURE__*/u(he,{color:e.color||"var(--privy-color-foreground-3)"});let he=h(se)` && { height: 1rem; width: 1rem; margin: 2px 0; border-width: 1.5px; /* Override default Loader to match button transitions */ transition: border-color 200ms ease; } `;const pe=["metamask","phantom","brave_wallet","rainbow","uniswap_wallet_extension","uniswap_extension","rabby_wallet","bybit_wallet","ronin_wallet","haha_wallet","crypto.com_wallet_extension","crypto.com_onchain","binance","bitget_wallet","coinbase_wallet","coinbase_smart_wallet","base_account","metamask","trust","safe","rainbow","uniswap","zerion","argent","spot","omni","cryptocom","blockchain","safepal","bitget_wallet","zengo","1inch","binance","exodus","mew_wallet","alphawallet","keyring_pro","mathwallet","unstoppable","obvious","ambire","internet_money_wallet","coin98","abc_wallet","arculus_wallet","haha","cling_wallet","broearn","copiosa","burrito_wallet","enjin_wallet","plasma_wallet","avacus","bee","pitaka","pltwallet","minerva","kryptogo","prema","slingshot","kriptonio","timeless","secux","bitizen","blocto","okx_wallet","safemoon","rabby_wallet","bybit_wallet","ronin_wallet","haha_wallet","privy","unknown","phantom","solflare","glow","backpack","jupiter","mobile_wallet_adapter"],ue=Object.freeze({phantom:{client:"phantom",name:"Phantom",installLink:w.isFirefox?"https://addons.mozilla.org/en-US/firefox/addon/phantom-app/":"https://chrome.google.com/webstore/detail/phantom/bfnaelmomeimhlpmgjnjophhpkkoljpa?hl=en",chainTypes:["ethereum","solana"],get isInstalled(){if("phantom"in window){let e=window;if(e?.phantom?.ethereum?.isPhantom&&e?.phantom?.ethereum?.chainId||e?.phantom?.solana?.isPhantom)return!0}return!1},getMobileRedirect({useUniversalLink:e,isSolana:t,connectOnly:n}){let i=we({client:this.client,isSolana:t,connectOnly:n});return`${e?"phantom://":"https://phantom.app/ul/"}browse/${i}?ref=${i}`}},solflare:{client:"solflare",name:"Solflare",installLink:w.isFirefox?"https://addons.mozilla.org/es/firefox/addon/solflare-wallet/":"https://chromewebstore.google.com/detail/solflare-wallet/bhhhlbepdkbapadjdnnojkbgioiodbic",chainTypes:["solana"],get isInstalled(){return"solflare"in window&&!!window?.solflare?.isSolflare},getMobileRedirect({useUniversalLink:e,isSolana:t,connectOnly:n}){let i=we({client:this.client,isSolana:t,connectOnly:n});return`${e?"solflare://ul/v1/":"https://solflare.com/ul/v1/"}browse/${i}?ref=${i}`}},jupiter:{client:"jupiter",name:"Jupiter Wallet",installLink:"https://chromewebstore.google.com/detail/jupiter-wallet/iledlaeogohbilgbfhmbgkgmpplbfboh",chainTypes:["solana"],get isInstalled(){return"jupiter"in window&&!!window?.jupiter?.isJupiter},getMobileRedirect({isSolana:e,connectOnly:t}){let n=we({client:this.client,isSolana:e,connectOnly:t});return`jupjupjup://jup.ag/browse/${n}?ref=${n}`}},backpack:{client:"backpack",name:"Backpack",installLink:"https://chromewebstore.google.com/detail/backpack/aflkmfhebedbjioipglgcbcmnbpgliof",chainTypes:["ethereum","solana"],get isInstalled(){return!(!("backpack"in window)||!window?.backpack?.ethereum?.isBackpack&&!window?.backpack?.solana?.isBackpack)},getMobileRedirect({useUniversalLink:e,isSolana:t,connectOnly:n}){let i=we({client:this.client,isSolana:t,connectOnly:n});return`${e?"backpack://ul/v1/":"https://backpack.app/ul/v1/"}browse/${i}?ref=${i}`}},okx_wallet:{client:"okx_wallet",name:"OKX Wallet",installLink:"https://chromewebstore.google.com/detail/okx-wallet/mcohilncbfahbmgdjkbpemcciiolgcge",chainTypes:["ethereum","solana"],get isInstalled(){return"okxwallet"in window&&!!window?.okxwallet?.isOkxWallet},getMobileRedirect({isSolana:e,connectOnly:t}){return`okx://wallet/dapp/url?dappUrl=${we({client:this.client,isSolana:e,connectOnly:t})}`}}});function me({connectorType:e,walletClientType:t}){for(let n of pe)if(e===n||t===n)return ue[n]}function we({client:e,isSolana:t,connectOnly:n}){let i=new URL(window.location.href);return i.searchParams.set("privy_connector",t?"solana_adapter":"injected"),i.searchParams.set("privy_wallet_client",e),i.searchParams.set("privy_connect_only",String(n)),encodeURIComponent(i.href.replace(/\/$/g,""))}class ye extends _{get walletBranding(){return{id:this.id,name:this.name,icon:this.icon}}async initialize(){this.initialized=!0,this.emit("initialized")}async connect(){throw Error("connect called for an uninstalled wallet via the SolanaNullConnector")}constructor({id:e,name:t,icon:n}){super({name:t},!1),this.connectorType="null",this.proxyProvider=new K(void 0,s),this.disconnect=async()=>{throw Error("disconnect called for an uninstalled wallet via the SolanaNullConnector")},this.promptConnection=async()=>{throw Error("promptConnection called for an uninstalled wallet via the SolanaNullConnector")},this.id=e,this.name=t,this.icon=n}}function ge(e){return"solana"===e.chainType}function fe(e){return"solana"===e.type}let be=/*#__PURE__*/n({});const ve=({children:n})=>{let i=r(),[a,l]=t({});return T("login",{onComplete:({loginAccount:e})=>{e&&"passkey"!==e.type&&"cross_app"!==e.type&&("wallet"!==e.type||"privy"!==e.walletClientType)&&(x.put(xe(i.id),e.type),"wallet"===e.type?(x.put(ke(i.id),e.walletClientType),x.put(Ce(i.id),e.chainType),l({accountType:e.type,walletClientType:e.walletClientType,chainType:e.chainType})):(x.del(ke(i.id)),x.del(Ce(i.id)),l({accountType:e.type})))}}),e((()=>{if(!i.id)return;let e=x.get(xe(i.id)),t=x.get(ke(i.id)),n=x.get(Ce(i.id));e&&l("wallet"===e?{accountType:e,walletClientType:t,chainType:n}:{accountType:e})}),[i.id]),/*#__PURE__*/u(be.Provider,{value:a,children:n})};let xe=e=>`privy:${e}:recent-login-method`,ke=e=>`privy:${e}:recent-login-wallet-client`,Ce=e=>`privy:${e}:recent-login-chain-type`;const _e=()=>i(be);class Te extends J{get walletBranding(){return{id:this.id,name:this.name,icon:this.icon}}async initialize(){this.initialized=!0,this.emit("initialized")}async connect(){throw Error("connect called for an uninstalled wallet via the EthereumNullConnector")}disconnect(){throw Error("disconnect called for an uninstalled wallet via the EthereumNullConnector")}promptConnection(e){throw Error(`promptConnection called for an uninstalled wallet via the EthereumNullConnector for ${e}`)}constructor({id:e,name:t,icon:n,walletClientType:i,defaultChain:a}){super(i,[],a,{}),this.connectorType="null",this.proxyProvider=new K(void 0,s),this.id=e,this.name=t,this.icon=n}}const ze=({...e})=>/*#__PURE__*/m("svg",{xmlns:"http://www.w3.org/2000/svg",width:"32",height:"32",viewBox:"0 0 32 32",fill:"none",...e,children:[/*#__PURE__*/u("rect",{width:"32",height:"32",rx:"6",fill:"#121314"}),/*#__PURE__*/u("g",{transform:"translate(4, 4)",children:/*#__PURE__*/m("svg",{width:"24",height:"24",viewBox:"0 0 318.6 318.6",children:[/*#__PURE__*/u("style",{children:".s1{stroke-linecap:round;stroke-linejoin:round}.s2{fill:#e4761b;stroke:#e4761b}.s3{fill:#f6851b;stroke:#f6851b}"}),/*#__PURE__*/u("path",{fill:"#e2761b",stroke:"#e2761b",className:"s1",d:"m274.1 35.5-99.5 73.9L193 65.8z"}),/*#__PURE__*/u("path",{d:"m44.4 35.5 98.7 74.6-17.5-44.3zm193.9 171.3-26.5 40.6 56.7 15.6 16.3-55.3zm-204.4.9L50.1 263l56.7-15.6-26.5-40.6z",className:"s1 s2"}),/*#__PURE__*/u("path",{d:"m103.6 138.2-15.8 23.9 56.3 2.5-2-60.5zm111.3 0-39-34.8-1.3 61.2 56.2-2.5zM106.8 247.4l33.8-16.5-29.2-22.8zm71.1-16.5 33.9 16.5-4.7-39.3z",className:"s1 s2"}),/*#__PURE__*/u("path",{fill:"#d7c1b3",stroke:"#d7c1b3",className:"s1",d:"m211.8 247.4-33.9-16.5 2.7 22.1-.3 9.3zm-105 0 31.5 14.9-.2-9.3 2.5-22.1z"}),/*#__PURE__*/u("path",{fill:"#233447",stroke:"#233447",className:"s1",d:"m138.8 193.5-28.2-8.3 19.9-9.1zm40.9 0 8.3-17.4 20 9.1z"}),/*#__PURE__*/u("path",{fill:"#cd6116",stroke:"#cd6116",className:"s1",d:"m106.8 247.4 4.8-40.6-31.3.9zM207 206.8l4.8 40.6 26.5-39.7zm23.8-44.7-56.2 2.5 5.2 28.9 8.3-17.4 20 9.1zm-120.2 23.1 20-9.1 8.2 17.4 5.3-28.9-56.3-2.5z"}),/*#__PURE__*/u("path",{fill:"#e4751f",stroke:"#e4751f",className:"s1",d:"m87.8 162.1 23.6 46-.8-22.9zm120.3 23.1-1 22.9 23.7-46zm-64-20.6-5.3 28.9 6.6 34.1 1.5-44.9zm30.5 0-2.7 18 1.2 45 6.7-34.1z"}),/*#__PURE__*/u("path",{d:"m179.8 193.5-6.7 34.1 4.8 3.3 29.2-22.8 1-22.9zm-69.2-8.3.8 22.9 29.2 22.8 4.8-3.3-6.6-34.1z",className:"s3"}),/*#__PURE__*/u("path",{fill:"#c0ad9e",stroke:"#c0ad9e",className:"s1",d:"m180.3 262.3.3-9.3-2.5-2.2h-37.7l-2.3 2.2.2 9.3-31.5-14.9 11 9 22.3 15.5h38.3l22.4-15.5 11-9z"}),/*#__PURE__*/u("path",{fill:"#161616",stroke:"#161616",className:"s1",d:"m177.9 230.9-4.8-3.3h-27.7l-4.8 3.3-2.5 22.1 2.3-2.2h37.7l2.5 2.2z"}),/*#__PURE__*/u("path",{fill:"#763d16",stroke:"#763d16",className:"s1",d:"m278.3 114.2 8.5-40.8-12.7-37.9-96.2 71.4 37 31.3 52.3 15.3 11.6-13.5-5-3.6 8-7.3-6.2-4.8 8-6.1zM31.8 73.4l8.5 40.8-5.4 4 8 6.1-6.1 4.8 8 7.3-5 3.6 11.5 13.5 52.3-15.3 37-31.3-96.2-71.4z"}),/*#__PURE__*/u("path",{d:"m267.2 153.5-52.3-15.3 15.9 23.9-23.7 46 31.2-.4h46.5zm-163.6-15.3-52.3 15.3-17.4 54.2h46.4l31.1.4-23.6-46zm71 26.4 3.3-57.7 15.2-41.1h-67.5l15 41.1 3.5 57.7 1.2 18.2.1 44.8h27.7l.2-44.8z",className:"s3"})]})})]});function $e(e){let{login:t}=i(I);return T("login",e),{login:t}}function Pe(e){let{logout:t}=i(I);return T("logout",e),{logout:t}}function Se(e){let{connectWallet:t}=i(I);return T("connectWallet",e),{connectWallet:t}}const je=e=>{T("fundWallet",e);let{fundWallet:t}=j();return{fundWallet:({address:e,options:n})=>t(e,n)}},Ie=o((()=>({isModalOpen:!1,resolvers:null})));let Ne=o((()=>({})));const Ee=(e,t)=>Ne((n=>n[e]?.[t])),Le=(e,t,n)=>{Ne.setState((i=>({[e]:{...i[e],[t]:n}})))},Ae=async()=>{let e,t,n=new Promise(((n,i)=>{e=n,t=i}));return Ne.setState({}),Ie.setState({isModalOpen:!0,resolvers:{resolve:e,reject:t}}),await n},Oe=()=>{let e=Ie.getState().resolvers;e||console.warn("useActiveWalletStore: Called close while modal was not open"),Ie.setState({isModalOpen:!1}),e?.resolve({wallet:Ie.getState().wallet})},Ue=({address:e,client:t,appId:n})=>{let i=`${t}:${e}`;n&&x.put(Re(n),i),Ie.setState({wallet:i})},We=()=>Ie((e=>e.isModalOpen)),Re=e=>`privy:${e}:active-wallet-connection`,Me=()=>{let t=r(),{wallets:n}=N(),{connectors:i}=j(),o=a((()=>i.filter(ge).flatMap((e=>e.wallets))),[i]),s=Ie((e=>e.wallet));e((()=>{if(!t.id)return;let e=x.get(Re(t.id));Ie.setState({wallet:e})}),[t.id]);let c=a((()=>{if(!s)return;let[,e]=s.split(":");return n.find((t=>t.address===e))||o.find((t=>t.address===e))}),[s,n,o]),d=a((()=>c?"ethereum"===c.type?c.chainId:"solana":void 0),[c]);return{connect:async e=>{e?.reset&&(x.del(Re(t.id)),Ie.setState({wallet:void 0}));let{wallet:i}=await Ae();if(!i)return{};let[,a]=i.split(":"),l=n.find((e=>e.address===a));if(l)return{wallet:l,network:l.chainId};let r=o.find((e=>e.address===a));return r?{wallet:r,network:"solana"}:{}},setActiveWallet:l((e=>{Ue({address:e.address,client:e.walletClientType,appId:t.id})}),[t.id]),wallet:c,network:d}};export{ee as A,de as B,te as C,ie as D,J as E,ae as F,H as G,ne as H,V as I,Z as J,Q as K,se as L,ze as M,le as N,ce as O,K as P,X as Q,ve as R,ye as S,A as a,D as b,U as c,B as d,me as e,fe as f,O as g,F as h,q as i,ge as j,Te as k,$e as l,ue as m,Pe as n,Se as o,je as p,Me as q,_e as r,M as s,Ee as t,G as u,Le as v,Ue as w,We as x,Oe as y,W as z};