UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) 3.2 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,c as l}from"./get-is-unified-wallet-Boa8DYrQ.mjs";import{u as s}from"./context-WTldtXS8.mjs";import{u as o,a as c,n as m}from"./internal-context-Z-fyxadS.mjs";import{a as d}from"./getEmbeddedConnectedWallet-JzK4iD-L.mjs";import{u as p,g,a as u}from"./smart-wallets-DOAgnxKI.mjs";import{u as f}from"./useWallets-CR0z5IIt.mjs";import"@privy-io/are-addresses-equal";import"@privy-io/js-sdk-core";import"tinycolor2";import"ofetch";import"viem";import"./PrivyPluginContext-2QN2dVUw.mjs";import"./getPublicClient-B4uPLKn-.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(W,{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:u,defaultChain:h,rpcConfig:C,smartWallets:w}=s(),{wallets:y}=f(),{user:W}=n(),[F,j]=r(),{wrapSmartAccountClientWithUIs:P}=p(),b=l(W),x=d(y,b?.address);a((()=>{(async()=>{if(!w?.enabled)return;let e=w.configuredNetworks.find((e=>e.chainId===`eip155:${h.id}`))?h:u.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.");j(await g({chain:e,privyAppId:m,paymasterContext:t?.paymasterContext,rpcConfig:C,account:b,signer:await(x?.getEthereumProvider()),user:W,smartWalletsConfig:w}))})()}),[!!w?.enabled,!!W?.smartWallet,!!x,!!b]);/*#__PURE__*/return e(v.Provider,{value:{client:F,config:t,getClientForChain:async({id:e})=>{let i=await g({chain:u.find((t=>t.id===e)),privyAppId:m,paymasterContext:t?.paymasterContext,rpcConfig:C,account:b,signer:await(x?.getEthereumProvider()),user:W,smartWalletsConfig:w});if(!i)throw new c(`Failed to create smart wallet client for chain id: ${e}`);return P(i)}},children:i})},v=/*#__PURE__*/i({client:void 0,config:void 0,getClientForChain:m}),W=({children:i})=>{let{smartWallets:r}=s(),{client:l,config:c,getClientForChain:m}=(()=>{let{client:e,config:i,getClientForChain:r}=t(v);return{client:e,config:i,getClientForChain:r}})(),{wrapSmartAccountClient:d}=u({clientConfig:c,smartWalletsConfig:r}),{user:p}=n(),{generateSiweMessageForSmartWallet:g,linkSmartWallet:f,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 f({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};