UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) 3.11 kB
import{jsx as e}from"react/jsx-runtime";import{useContext as t,createContext as i,useState as r,useEffect as a}from"react";import{u as n,b as l}from"./get-is-unified-wallet-DvKS5IUO.mjs";import{u as s}from"./context-Bycd1SKV.mjs";import{u as o,a as c,n as m}from"./internal-context-e-Eni5bG.mjs";import{a as d}from"./getEmbeddedConnectedWallet-CM6cDQCS.mjs";import{u as p,g,a as f}from"./smart-wallets-B-nGTJog.mjs";import{u}from"./useWallets-D0lpBS44.mjs";import"viem";import"@privy-io/js-sdk-core";import"tinycolor2";import"ofetch";import"./getPublicClient-A9RSftUZ.mjs";import"permissionless";import"permissionless/accounts";import"permissionless/clients/pimlico";import"viem/account-abstraction";let h=/*#__PURE__*/i({client:void 0,getClientForChain:m});const C=({config:t,children:i})=>/*#__PURE__*/e(y,{config:t,children:/*#__PURE__*/e(v,{children:i})}),w=()=>{let{client:e,getClientForChain:i}=t(h);return{client:e,getClientForChain:i}};let y=({config:t,children:i})=>{let{appId:m}=o(),{chains:f,defaultChain:h,rpcConfig:C,smartWallets:w}=s(),{wallets:y}=u(),{user:v}=n(),[F,b]=r(),{wrapSmartAccountClientWithUIs:j}=p(),x=l(v),I=d(y,x?.address);a((()=>{(async()=>{if(!w?.enabled)return;let e=w.configuredNetworks.find((e=>e.chainId===`eip155:${h.id}`))?h:f.find((e=>w.configuredNetworks.map((e=>e.chainId)).includes(`eip155:${e.id}`)));if(!e)throw new c("Could not find smart wallets chain configurations for any Privy client configured chains. Please configure smart wallets for your chains in the Privy dashboard.");b(await g({chain:e,privyAppId:m,paymasterContext:t?.paymasterContext,rpcConfig:C,account:x,signer:await(I?.getEthereumProvider()),user:v,smartWalletsConfig:w}))})()}),[!!w?.enabled,!!v?.smartWallet,!!I,!!x]);/*#__PURE__*/return e(W.Provider,{value:{client:F,config:t,getClientForChain:async({id:e})=>{let i=await g({chain:f.find((t=>t.id===e)),privyAppId:m,paymasterContext:t?.paymasterContext,rpcConfig:C,account:x,signer:await(I?.getEthereumProvider()),user:v,smartWalletsConfig:w});if(!i)throw new c(`Failed to create smart wallet client for chain id: ${e}`);return j(i)}},children:i})},W=/*#__PURE__*/i({client:void 0,config:void 0,getClientForChain:m}),v=({children:i})=>{let{smartWallets:r}=s(),{client:l,config:c,getClientForChain:m}=(()=>{let{client:e,config:i,getClientForChain:r}=t(W);return{client:e,config:i,getClientForChain:r}})(),{wrapSmartAccountClient:d}=f({clientConfig:c,smartWalletsConfig:r}),{user:p}=n(),{generateSiweMessageForSmartWallet:g,linkSmartWallet:u,hideWalletUIs:C}=o();return a((()=>{(async()=>{if(l&&p&&!p?.smartWallet&&r?.enabled)try{let e=await g({address:l.account.address,chainId:`eip155:${l.chain.id}`});C.current=!0;let t=await l.signMessage({message:e});await u({signature:t,message:e,smartWalletType:r.smartWalletType,smartWalletVersion:r.smartWalletVersion})}catch(e){console.error("Error creating smart wallet:",e)}finally{C.current=!1}})()}),[!!l,!!p?.smartWallet,!!r?.enabled]),/*#__PURE__*/e(h.Provider,{value:{client:p?.smartWallet&&l?d(l):void 0,getClientForChain:m},children:i})};export{C as SmartWalletsProvider,w as useSmartWallets};