UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

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