@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 3.49 kB
JavaScript
;var e=require("react/jsx-runtime"),t=require("react"),r=require("./get-is-unified-wallet-UpdjHxOV.js"),i=require("./context-D-ZJYjUg.js"),a=require("./internal-context-BJv4f_bO.js"),n=require("./getEmbeddedConnectedWallet-pJd-l936.js"),l=require("./smart-wallets-D-oPjsFS.js"),s=require("./useWallets-CKAypPZB.js");require("@privy-io/are-addresses-equal"),require("@privy-io/js-sdk-core"),require("tinycolor2"),require("ofetch"),require("viem"),require("./PrivyPluginContext-DsgaS6n9.js"),require("./getPublicClient-BsmZyCGX.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:g,defaultChain:m,rpcConfig:C,smartWallets:f}=i.useAppConfig(),{wallets:p}=s.useWallets(),{user:h}=r.usePrivyContext(),[W,v]=t.useState(),{wrapSmartAccountClientWithUIs:y}=l.useSmartWalletsUIWrapper(),w=r.getPrivyEthereumWallet(h),q=n.getEmbeddedConnectedWalletWithAddress(p,w?.address);t.useEffect((()=>{(async()=>{if(!f?.enabled)return;let e=f.configuredNetworks.find((e=>e.chainId===`eip155:${m.id}`))?m:g.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(q?.getEthereumProvider()),user:h,smartWalletsConfig:f}))})()}),[!!f?.enabled,!!h?.smartWallet,!!q,!!w]);/*#__PURE__*/return e.jsx(u.Provider,{value:{client:W,config:o,getClientForChain:async({id:e})=>{let t=await l.getSmartWalletClient({chain:g.find((t=>t.id===e)),privyAppId:d,paymasterContext:o?.paymasterContext,rpcConfig:C,account:w,signer:await(q?.getEthereumProvider()),user:h,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:g}=(()=>{let{client:e,config:r,getClientForChain:i}=t.useContext(u);return{client:e,config:r,getClientForChain:i}})(),{wrapSmartAccountClient:m}=l.useSmartWalletsWrapper({clientConfig:d,smartWalletsConfig:s}),{user:C}=r.usePrivyContext(),{generateSiweMessageForSmartWallet:f,linkSmartWallet:p,hideWalletUIs:h}=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}`});h.current=!0;let t=await c.signMessage({message:e});await p({signature:t,message:e,smartWalletType:s.smartWalletType,smartWalletVersion:s.smartWalletVersion})}catch(e){console.error("Error creating smart wallet:",e)}finally{h.current=!1}})()}),[!!c,!!C?.smartWallet,!!s?.enabled]),/*#__PURE__*/e.jsx(o.Provider,{value:{client:C?.smartWallet&&c?m(c):void 0,getClientForChain:g},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}};