UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

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