@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 3.41 kB
JavaScript
;var e=require("react/jsx-runtime"),t=require("react"),r=require("./get-is-unified-wallet-D4ouXJpi.js"),i=require("./context-DFq1obBO.js"),a=require("./internal-context-NWsAL807.js"),n=require("./getEmbeddedConnectedWallet-6TsVZJkD.js"),l=require("./smart-wallets-CV78p-ta.js"),s=require("./useWallets-BLFNjTwC.js");require("viem"),require("@privy-io/js-sdk-core"),require("tinycolor2"),require("ofetch"),require("./getPublicClient-CGlodIp_.js"),require("permissionless"),require("permissionless/accounts"),require("permissionless/clients/pimlico"),require("viem/account-abstraction");let o=/*#__PURE__*/t.createContext({client:void 0,getClientForChain:a.notImplemented});let c=({config:o,children:c})=>{let{appId:d}=a.usePrivyInternal(),{chains:m,defaultChain:g,rpcConfig:C,smartWallets:f}=i.useAppConfig(),{wallets:h}=s.useWallets(),{user:p}=r.usePrivyContext(),[W,v]=t.useState(),{wrapSmartAccountClientWithUIs:y}=l.useSmartWalletsUIWrapper(),w=r.getPrivyEthereumWallet(p),x=n.getEmbeddedConnectedWalletWithAddress(h,w?.address);t.useEffect((()=>{(async()=>{if(!f?.enabled)return;let e=f.configuredNetworks.find((e=>e.chainId===`eip155:${g.id}`))?g:m.find((e=>f.configuredNetworks.map((e=>e.chainId)).includes(`eip155:${e.id}`)));if(!e)throw new a.PrivyClientError("Could not find smart wallets chain configurations for any Privy client configured chains. Please configure smart wallets for your chains in the Privy dashboard.");v(await l.getSmartWalletClient({chain:e,privyAppId:d,paymasterContext:o?.paymasterContext,rpcConfig:C,account:w,signer:await(x?.getEthereumProvider()),user:p,smartWalletsConfig:f}))})()}),[!!f?.enabled,!!p?.smartWallet,!!x,!!w]);/*#__PURE__*/return e.jsx(u.Provider,{value:{client:W,config:o,getClientForChain:async({id:e})=>{let t=await l.getSmartWalletClient({chain:m.find((t=>t.id===e)),privyAppId:d,paymasterContext:o?.paymasterContext,rpcConfig:C,account:w,signer:await(x?.getEthereumProvider()),user:p,smartWalletsConfig:f});if(!t)throw new a.PrivyClientError(`Failed to create smart wallet client for chain id: ${e}`);return y(t)}},children:c})},u=/*#__PURE__*/t.createContext({client:void 0,config:void 0,getClientForChain:a.notImplemented}),d=({children:n})=>{let{smartWallets:s}=i.useAppConfig(),{client:c,config:d,getClientForChain:m}=(()=>{let{client:e,config:r,getClientForChain:i}=t.useContext(u);return{client:e,config:r,getClientForChain:i}})(),{wrapSmartAccountClient:g}=l.useSmartWalletsWrapper({clientConfig:d,smartWalletsConfig:s}),{user:C}=r.usePrivyContext(),{generateSiweMessageForSmartWallet:f,linkSmartWallet:h,hideWalletUIs:p}=a.usePrivyInternal();return t.useEffect((()=>{(async()=>{if(c&&C&&!C?.smartWallet&&s?.enabled)try{let e=await f({address:c.account.address,chainId:`eip155:${c.chain.id}`});p.current=!0;let t=await c.signMessage({message:e});await h({signature:t,message:e,smartWalletType:s.smartWalletType,smartWalletVersion:s.smartWalletVersion})}catch(e){console.error("Error creating smart wallet:",e)}finally{p.current=!1}})()}),[!!c,!!C?.smartWallet,!!s?.enabled]),/*#__PURE__*/e.jsx(o.Provider,{value:{client:C?.smartWallet&&c?g(c):void 0,getClientForChain:m},children:n})};exports.SmartWalletsProvider=({config:t,children:r})=>/*#__PURE__*/e.jsx(c,{config:t,children:/*#__PURE__*/e.jsx(d,{children:r})}),exports.useSmartWallets=()=>{let{client:e,getClientForChain:r}=t.useContext(o);return{client:e,getClientForChain:r}};