UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) • 10.7 kB
"use strict";var e=require("react"),t=require("viem"),r=require("./privy-provider-BivyFPW2.js"),n=require("./internal-context-GxD_jR-e.js"),a=require("./getEmbeddedConnectedWallet-DTU4T9TJ.js"),i=require("permissionless"),s=require("permissionless/accounts"),o=require("permissionless/clients/pimlico"),l=require("viem/account-abstraction"),c=require("@privy-io/js-sdk-core");let d={address:l.entryPoint06Address,version:"0.6"},u={address:l.entryPoint07Address,version:"0.7"},p=({owner:e,smartWalletType:t,publicClient:r})=>{switch(t){case c.SAFE:return s.toSafeSmartAccount({client:r,entryPoint:u,owners:[e],version:"1.4.1"});case c.KERNEL:return s.toEcdsaKernelSmartAccount({client:r,version:"0.3.1",entryPoint:u,owners:[e]});case c.BICONOMY:return s.toBiconomySmartAccount({client:r,owners:[e],entryPoint:d});case c.LIGHT_ACCOUNT:return s.toLightSmartAccount({client:r,entryPoint:d,owner:e,version:"1.1.0"});case c.COINBASE_SMART_WALLET:return async function({client:e,owner:t}){let[r]=await t.request({method:"eth_accounts"});if(!r)throw Error("Unable to request embedded owner for eth_accounts");return await l.toCoinbaseSmartAccount({client:e,owners:[{type:"local",source:"privy",address:r,sign:async({hash:e})=>await t.request({method:"secp256k1_sign",params:[e]})}]})}({client:r,owner:e});case c.THIRDWEB:return s.toThirdwebSmartAccount({client:r,entryPoint:u,owner:e});default:throw Error(`Invalid smart account type: ${t}.`)}};const y=({calls:e,chain:t,maxPriorityFeePerGas:r,maxFeePerGas:n,nonce:a})=>e.map((e=>({to:e.to||void 0,data:e.data,value:e.value,chainId:t.id,nonce:a,maxFeePerGas:n,maxPriorityFeePerGas:r}))),m=async({chain:e,paymasterContext:a,embeddedWallet:s,user:l,smartWalletsConfig:c,rpcConfig:d,privyAppId:u})=>{if(!c?.enabled||!l||!s)return;let y=await s.getEthereumProvider(),m=c?.configuredNetworks.find((t=>t.chainId===`eip155:${e.id}`));if(!m)throw new n.PrivyClientError(`The chain ${e.name} (eip155:${e.id}) must be configured in the smart wallet configuration in your dashboard`);let g=l.smartWallet?.smartWalletType??c.smartWalletType,h=r.getJsonRpcEndpointFromChain(e,d,u),P=t.createPublicClient({chain:e,transport:t.http(h)}),f=a??m.paymasterContext,w=await(async({owner:e,smartWalletType:r,chain:n,publicClient:a,bundlerUrl:s,paymasterUrl:l,paymasterContext:c})=>{let d=await p({owner:e,smartWalletType:r,publicClient:a}),u=o.createPimlicoClient({transport:t.http(s),entryPoint:d.entryPoint}),y=l?o.createPimlicoClient({transport:t.http(l),entryPoint:d.entryPoint}):void 0;return i.createSmartAccountClient({account:d,chain:n,paymaster:y,paymasterContext:c,bundlerTransport:t.http(s),userOperation:{estimateFeesPerGas:async()=>{if(["public.pimlico.io","api.pimlico.io"].some((e=>s.includes(e))))return await u.getUserOperationGasPrice().then((e=>e.fast));if(["rpc.zerodev.app"].some((e=>s.includes(e))))return(await u.request({method:"zd_getUserOperationGasPrice",params:[]})).fast;if(["g.alchemy.com"].some((e=>s.includes(e)))){let[e,t]=await Promise.all([a.getBlock({blockTag:"latest"}),u.request({method:"rundler_maxPriorityFeePerGas",params:[]})]);if(!e.baseFeePerGas||!t)throw Error("Failed to get block or maxPriorityFeePerGasEstimate");return{maxFeePerGas:150n*e.baseFeePerGas/100n+BigInt(t.toString()),maxPriorityFeePerGas:BigInt(t.toString())}}{let e=await a.estimateFeesPerGas(),t=s.includes("api.developer.coinbase.com")?175n:150n;return{...e,maxFeePerGas:e.maxFeePerGas*t/100n,maxPriorityFeePerGas:e.maxPriorityFeePerGas*t/100n}}}}})})({owner:y,smartWalletType:g,chain:e,publicClient:P,paymasterContext:f,...m});if(!w)throw new n.PrivyClientError(`Failed to create smart wallet client for chain ${e.name} (eip155:${e.id})`);return w};exports.callsToTransactionRequests=y,exports.getSmartWalletClient=m,exports.useSmartWalletsUIWrapper=()=>{let{hideWalletUIs:e,openPrivyModal:t,isHeadlessSigning:a}=n.usePrivyInternal(),{setModalData:i}=r.usePrivyModal(),{user:s}=r.usePrivyContext(),o=r.getPrivyEthereumWallet(s);return{wrapSmartAccountClientWithUIs:n=>({...n,sendTransaction:async(l,c)=>{if(a({showWalletUIs:c?.uiOptions?.showWalletUIs}))return e.current=!0,await n.sendTransaction(l).finally((()=>e.current=!1));let d=[];"calls"in l&&void 0!==l.calls?d=[...l.calls]:"to"in l&&(d=[{to:l.to,value:l.value||BigInt(0),data:l.data||"0x"}]);let u=async()=>{if(!n.paymaster)return!1;let{paymasterAndData:e,paymasterData:t}=await n.prepareUserOperation({calls:d,maxFeePerGas:l.maxFeePerGas,maxPriorityFeePerGas:l.maxPriorityFeePerGas,nonce:l.nonce?BigInt(l.nonce):void 0});return Number(e??t??0)>0};return new Promise((async(a,p)=>{e.current=!0;let{entropyId:m,entropyIdVerifier:g}=r.getEntropyDetailsForUser(s);i({connectWallet:{recoveryMethod:o.recoveryMethod,connectingWalletAddress:o.address,entropyId:m,entropyIdVerifier:g,onCompleteNavigateTo:r.SendTransactionScreen,onFailure:()=>{}},sendTransaction:{transactionRequests:y({calls:d,chain:n.chain,maxPriorityFeePerGas:l.maxPriorityFeePerGas,maxFeePerGas:l.maxFeePerGas,nonce:l.nonce?BigInt(l.nonce):void 0}),transactingWalletIndex:0,transactingWalletAddress:n.account.address,entropyId:m,entropyIdVerifier:g,getIsSponsored:u,signOnly:!1,onConfirm:()=>n.sendTransaction(l),onSuccess:e=>a(e.hash),onFailure:p,uiOptions:c?.uiOptions??{}}}),t(r.EmbeddedWalletConnectingScreen)})).finally((()=>{e.current=!1}))},signMessage:async(l,c)=>a({showWalletUIs:c?.uiOptions?.showWalletUIs})?(e.current=!0,await n.signMessage(l).finally((()=>e.current=!1))):new Promise((async(a,d)=>{let{entropyId:u,entropyIdVerifier:p}=r.getEntropyDetailsForUser(s);e.current=!0,i({connectWallet:{recoveryMethod:o.recoveryMethod,connectingWalletAddress:o.address,entropyId:u,entropyIdVerifier:p,onCompleteNavigateTo:r.SignRequestScreen,onFailure:()=>{}},signMessage:{method:"personal_sign",data:"string"==typeof l.message?l.message:l.message.raw.toString(),confirmAndSign:()=>n.signMessage(l),onSuccess:e=>a(e),onFailure:d,uiOptions:c?.uiOptions??{}}}),t(r.EmbeddedWalletConnectingScreen)})).finally((()=>{e.current=!1})),signTypedData:async(l,c)=>a({showWalletUIs:c?.uiOptions?.showWalletUIs})?(e.current=!0,await n.signTypedData(l).finally((()=>e.current=!1))):new Promise((async(a,d)=>{e.current=!0;let{entropyId:u,entropyIdVerifier:p}=r.getEntropyDetailsForUser(s);i({connectWallet:{recoveryMethod:o.recoveryMethod,connectingWalletAddress:o.address,entropyId:u,entropyIdVerifier:p,onCompleteNavigateTo:r.SignRequestScreen,onFailure:()=>{}},signMessage:{method:"eth_signTypedData_v4",data:l,confirmAndSign:()=>n.signTypedData(l),onSuccess:e=>a(e),onFailure:d,uiOptions:c?.uiOptions??{}}}),t(r.EmbeddedWalletConnectingScreen)})).finally((()=>{e.current=!1}))})}},exports.useSmartWalletsWrapper=({clientConfig:t,smartWalletsConfig:i})=>{let{hideWalletUIs:s,openPrivyModal:o,appId:l,isHeadlessSigning:c}=n.usePrivyInternal(),d=r.useAppConfig(),{setModalData:u}=r.usePrivyModal(),{user:p}=r.usePrivyContext(),{chains:g}=r.useAppConfig(),{chainId:h,clients:P,setChainId:f,chainIdState:w}=(()=>{let t=e.useRef(),[r,n]=e.useState();return{clients:e.useRef({}),setChainId:e=>{t.current=e,n(e)},chainId:t,chainIdState:r}})(),I=g.find((e=>e.id===w)),{wallets:v}=r.useWallets(),C=a.getEmbeddedConnectedWallet(v),W=r.getPrivyEthereumWallet(p),S=e.useCallback((async()=>h.current),[h]),F=e.useCallback((async({id:e})=>{if(!P.current[e]){let r=await m({chain:g.find((t=>t.id===e)),embeddedWallet:C,user:p,paymasterContext:t?.paymasterContext,smartWalletsConfig:i,rpcConfig:d.rpcConfig,privyAppId:l});r&&(P.current[e]=r)}f(e)}),[g,t,v,p,i]);return{wrapSmartAccountClient:e=>{P.current[e.chain.id]=e,w||f(e.chain.id);return{...e,sendTransaction:async(e,t)=>{let a=P.current[h.current];if(!a)throw new n.PrivyClientError(`Smart wallet client for chain (eip155:${h.current}) not found`);if(delete e.account,c({showWalletUIs:t?.uiOptions?.showWalletUIs}))return s.current=!0,await a.sendTransaction(e).finally((()=>s.current=!1));let i=[];"calls"in e&&void 0!==e.calls?i=[...e.calls]:"to"in e&&(i=[{to:e.to,value:e.value||BigInt(0),data:e.data||"0x"}]);let l=async()=>{if(!a.paymaster)return!1;let{paymasterAndData:t,paymasterData:r}=await a.prepareUserOperation({calls:i,maxFeePerGas:e.maxFeePerGas,maxPriorityFeePerGas:e.maxPriorityFeePerGas,nonce:e.nonce?BigInt(e.nonce):void 0});return Number(t??r??0)>0};return new Promise((async(n,c)=>{s.current=!0;let{entropyId:d,entropyIdVerifier:m}=r.getEntropyDetailsForUser(p);u({connectWallet:{recoveryMethod:W.recoveryMethod,connectingWalletAddress:W.address,entropyId:d,entropyIdVerifier:m,onCompleteNavigateTo:r.SendTransactionScreen,onFailure:()=>{}},sendTransaction:{transactionRequests:y({calls:i,chain:a.chain,maxPriorityFeePerGas:e.maxPriorityFeePerGas,maxFeePerGas:e.maxFeePerGas,nonce:e.nonce?BigInt(e.nonce):void 0}),transactingWalletIndex:0,transactingWalletAddress:a.account.address,entropyId:d,entropyIdVerifier:m,getIsSponsored:l,signOnly:!1,onConfirm:()=>a.sendTransaction(e),onSuccess:e=>n(e.hash),onFailure:c,uiOptions:t?.uiOptions??{}}}),o(r.EmbeddedWalletConnectingScreen)})).finally((()=>{s.current=!1}))},signMessage:async(e,t)=>{let a=P.current[h.current];if(!a)throw new n.PrivyClientError(`Smart wallet client for chain (eip155:${h.current}) not found`);return c({showWalletUIs:t?.uiOptions?.showWalletUIs})?(s.current=!0,await a.signMessage(e).finally((()=>s.current=!1))):new Promise((async(n,i)=>{let{entropyId:l,entropyIdVerifier:c}=r.getEntropyDetailsForUser(p);s.current=!0,u({connectWallet:{recoveryMethod:W.recoveryMethod,connectingWalletAddress:W.address,entropyId:l,entropyIdVerifier:c,onCompleteNavigateTo:r.SignRequestScreen,onFailure:()=>{}},signMessage:{method:"personal_sign",data:"string"==typeof e.message?e.message:e.message.raw.toString(),confirmAndSign:()=>a.signMessage(e),onSuccess:e=>n(e),onFailure:i,uiOptions:t?.uiOptions??{}}}),o(r.EmbeddedWalletConnectingScreen)})).finally((()=>{s.current=!1}))},signTypedData:async(e,t)=>{let a=P.current[h.current];if(!a)throw new n.PrivyClientError(`Smart wallet client for chain (eip155:${h.current}) not found`);return delete e.account,c({showWalletUIs:t?.uiOptions?.showWalletUIs})?(s.current=!0,await a.signTypedData(e).finally((()=>s.current=!1))):new Promise((async(n,i)=>{s.current=!0;let{entropyId:l,entropyIdVerifier:c}=r.getEntropyDetailsForUser(p);u({connectWallet:{recoveryMethod:W.recoveryMethod,connectingWalletAddress:W.address,entropyId:l,entropyIdVerifier:c,onCompleteNavigateTo:r.SignRequestScreen,onFailure:()=>{}},signMessage:{method:"eth_signTypedData_v4",data:e,confirmAndSign:()=>a.signTypedData(e),onSuccess:e=>n(e),onFailure:i,uiOptions:t?.uiOptions??{}}}),o(r.EmbeddedWalletConnectingScreen)})).finally((()=>{s.current=!1}))},getChainId:S,chain:I,switchChain:F}}}};