UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) • 10.4 kB
import{useRef as e,useState as t,useCallback as r}from"react";import{http as a,createPublicClient as n}from"viem";import{u as i,b as s,g as o}from"./privy-context-V2csHgUI.mjs";import{c3 as l,ar as c,am as d,ae as u,z as m,bU as p,bV as y,bx as g}from"./SignRequestScreen-NOqdMm22.mjs";import{P as f}from"./paths-B891YjWw.mjs";import{u as h}from"./internal-context-LlMdpfkb.mjs";import{g as w}from"./getEmbeddedConnectedWallet-CSSBWE2p.mjs";import{createSmartAccountClient as P}from"permissionless";import{toThirdwebSmartAccount as I,toLightSmartAccount as W,toBiconomySmartAccount as v,toKernelSmartAccount as F,toSafeSmartAccount as x}from"permissionless/accounts";import{createPimlicoClient as G}from"permissionless/clients/pimlico";import{toCoinbaseSmartAccount as C,entryPoint06Address as T,entryPoint07Address as U}from"viem/account-abstraction";import{THIRDWEB as b,COINBASE_SMART_WALLET as M,LIGHT_ACCOUNT as S,BICONOMY as O,KERNEL as V,SAFE as A}from"@privy-io/js-sdk-core";let D={address:T,version:"0.6"},N={address:U,version:"0.7"},_=({owner:e,smartWalletType:t,smartWalletVersion:r,publicClient:a})=>{switch(t){case A:return x({client:a,entryPoint:N,owners:[e],version:"1.4.1"});case V:return F({client:a,version:"0.3.1",entryPoint:N,useMetaFactory:"optional",owners:[e]});case O:return v({client:a,owners:[e],entryPoint:D});case S:return W({client:a,entryPoint:D,owner:e,version:"1.1.0"});case M:return async function({client:e,owner:t,version:r}){let[a]=await t.request({method:"eth_accounts"});if(!a)throw Error("Unable to request embedded owner for eth_accounts");return await C({client:e,owners:[{type:"local",source:"privy",address:a,sign:async({hash:e})=>await t.request({method:"secp256k1_sign",params:[e]})}],version:r})}({client:a,owner:e,version:r??"1"});case b:return I({client:a,entryPoint:N,owner:e});default:throw Error(`Invalid smart account type: ${t}.`)}};const B=({calls:e,chain:t,maxPriorityFeePerGas:r,maxFeePerGas:a,nonce:n})=>e.map((e=>({to:e.to||void 0,data:e.data,value:e.value,chainId:t.id,nonce:n,maxFeePerGas:a,maxPriorityFeePerGas:r}))),$=async({chain:e,paymasterContext:t,account:r,signer:i,user:s,smartWalletsConfig:o,rpcConfig:c,privyAppId:d})=>{if(!(o?.enabled&&s&&r&&i))return;let u=o?.configuredNetworks.find((t=>t.chainId===`eip155:${e.id}`));if(!u)throw new f(`The chain ${e.name} (eip155:${e.id}) must be configured in the smart wallet configuration in your dashboard`);let m=s.smartWallet?s.smartWallet.smartWalletType:o.smartWalletType,p=s.smartWallet?s.smartWallet.smartWalletVersion:o.smartWalletVersion,y=l(e,c,d),g=n({chain:e,transport:a(y)}),h=t??u.paymasterContext,w=await(async({owner:e,smartWalletType:t,smartWalletVersion:r,chain:n,publicClient:i,bundlerUrl:s,paymasterUrl:o,paymasterContext:l})=>{let c=await _({owner:e,smartWalletType:t,publicClient:i,smartWalletVersion:r}),d=G({transport:a(s),entryPoint:c.entryPoint}),u=o?G({transport:a(o),entryPoint:c.entryPoint}):void 0;return P({account:c,chain:n,paymaster:u,paymasterContext:l,bundlerTransport:a(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,t]=await Promise.all([i.getBlock({blockTag:"latest"}),d.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:p,chain:e,publicClient:g,paymasterContext:h,...u});if(!w)throw new f(`Failed to create smart wallet client for chain ${e.name} (eip155:${e.id})`);return w},q=({clientConfig:a,smartWalletsConfig:n})=>{let{hideWalletUIs:l,openPrivyModal:P,appId:I,isHeadlessSigning:W}=h(),v=c(),{setModalData:F}=d(),{user:x}=i(),{chains:G}=c(),{chainId:C,clients:T,setChainId:U,chainIdState:b}=(()=>{let r=e(),[a,n]=t();return{clients:e({}),setChainId:e=>{r.current=e,n(e)},chainId:r,chainIdState:a}})(),M=G.find((e=>e.id===b)),{wallets:S}=u(),O=s(x),V=w(S),A=!!O&&o(O),D=r((async()=>C.current),[C]),N=r((async({id:e})=>{if(!T.current[e]){let t=await $({chain:G.find((t=>t.id===e)),account:O,signer:await(V?.getEthereumProvider()),user:x,paymasterContext:a?.paymasterContext,smartWalletsConfig:n,rpcConfig:v.rpcConfig,privyAppId:I});t&&(T.current[e]=t)}U(e)}),[G,a,S,x,n]);return{wrapSmartAccountClient:e=>{T.current[e.chain.id]=e,b||U(e.chain.id);return{...e,sendTransaction:async(e,t)=>{let r=T.current[C.current];if(!r)throw new f(`Smart wallet client for chain (eip155:${C.current}) not found`);if(delete e.account,W({showWalletUIs:t?.uiOptions?.showWalletUIs}))return l.current=!0,await r.sendTransaction(e).finally((()=>l.current=!1));let a=[];"calls"in e&&void 0!==e.calls?a=[...e.calls]:"to"in e&&(a=[{to:e.to,value:e.value||BigInt(0),data:e.data||"0x"}]);let n=async()=>{let{paymasterAndData:t,paymasterData:n,maxFeePerGas:i,maxPriorityFeePerGas:s}=await r.prepareUserOperation({calls:a,maxFeePerGas:e.maxFeePerGas,maxPriorityFeePerGas:e.maxPriorityFeePerGas,nonce:e.nonce?BigInt(e.nonce):void 0});return 0===Number(i)&&0===Number(s)||Number(t??n??0)>0};return new Promise((async(i,s)=>{l.current=!0;let{entropyId:o,entropyIdVerifier:c}=m(x);F({connectWallet:{recoveryMethod:O.recoveryMethod,connectingWalletAddress:O.address,isUnifiedWallet:A,entropyId:o,entropyIdVerifier:c,onCompleteNavigateTo:p,onFailure:()=>{}},sendTransaction:{transactionRequests:B({calls:a,chain:r.chain,maxPriorityFeePerGas:e.maxPriorityFeePerGas,maxFeePerGas:e.maxFeePerGas,nonce:e.nonce?BigInt(e.nonce):void 0}),transactingWalletIndex:0,transactingWalletAddress:r.account.address,entropyId:o,entropyIdVerifier:c,getIsSponsored:n,signOnly:!1,onConfirm:()=>r.sendTransaction(e),onSuccess:e=>i(e.hash),onFailure:s,uiOptions:t?.uiOptions??{}}}),P(y)})).finally((()=>{l.current=!1}))},signMessage:async(e,t)=>{let r=T.current[C.current];if(!r)throw new f(`Smart wallet client for chain (eip155:${C.current}) not found`);return W({showWalletUIs:t?.uiOptions?.showWalletUIs})?(l.current=!0,await r.signMessage(e).finally((()=>l.current=!1))):new Promise((async(a,n)=>{let{entropyId:i,entropyIdVerifier:s}=m(x);l.current=!0,F({connectWallet:{recoveryMethod:O.recoveryMethod,connectingWalletAddress:O.address,isUnifiedWallet:A,entropyId:i,entropyIdVerifier:s,onCompleteNavigateTo:g,onFailure:()=>{}},signMessage:{method:"personal_sign",data:"string"==typeof e.message?e.message:e.message.raw.toString(),confirmAndSign:()=>r.signMessage(e),onSuccess:e=>a(e),onFailure:n,uiOptions:t?.uiOptions??{}}}),P(y)})).finally((()=>{l.current=!1}))},signTypedData:async(e,t)=>{let r=T.current[C.current];if(!r)throw new f(`Smart wallet client for chain (eip155:${C.current}) not found`);return delete e.account,W({showWalletUIs:t?.uiOptions?.showWalletUIs})?(l.current=!0,await r.signTypedData(e).finally((()=>l.current=!1))):new Promise((async(a,n)=>{l.current=!0;let{entropyId:i,entropyIdVerifier:s}=m(x);F({connectWallet:{recoveryMethod:O.recoveryMethod,connectingWalletAddress:O.address,isUnifiedWallet:A,entropyId:i,entropyIdVerifier:s,onCompleteNavigateTo:g,onFailure:()=>{}},signMessage:{method:"eth_signTypedData_v4",data:e,confirmAndSign:()=>r.signTypedData(e),onSuccess:e=>a(e),onFailure:n,uiOptions:t?.uiOptions??{}}}),P(y)})).finally((()=>{l.current=!1}))},getChainId:D,chain:M,switchChain:N}}}},j=()=>{let{hideWalletUIs:e,openPrivyModal:t,isHeadlessSigning:r}=h(),{setModalData:a}=d(),{user:n}=i(),l=s(n),c=!!l&&o(l);return{wrapSmartAccountClientWithUIs:i=>({...i,sendTransaction:async(s,o)=>{if(r({showWalletUIs:o?.uiOptions?.showWalletUIs}))return e.current=!0,await i.sendTransaction(s).finally((()=>e.current=!1));let d=[];"calls"in s&&void 0!==s.calls?d=[...s.calls]:"to"in s&&(d=[{to:s.to,value:s.value||BigInt(0),data:s.data||"0x"}]);let u=async()=>{if(!i.paymaster)return!1;let{paymasterData:e,paymasterAndData:t}=await i.prepareUserOperation({calls:d,maxFeePerGas:s.maxFeePerGas,maxPriorityFeePerGas:s.maxPriorityFeePerGas,nonce:s.nonce?BigInt(s.nonce):void 0});return Number(t??e??0)>0};return new Promise((async(r,g)=>{e.current=!0;let{entropyId:f,entropyIdVerifier:h}=m(n);a({connectWallet:{recoveryMethod:l.recoveryMethod,connectingWalletAddress:l.address,isUnifiedWallet:c,entropyId:f,entropyIdVerifier:h,onCompleteNavigateTo:p,onFailure:()=>{}},sendTransaction:{transactionRequests:B({calls:d,chain:i.chain,maxPriorityFeePerGas:s.maxPriorityFeePerGas,maxFeePerGas:s.maxFeePerGas,nonce:s.nonce?BigInt(s.nonce):void 0}),transactingWalletIndex:0,transactingWalletAddress:i.account.address,entropyId:f,entropyIdVerifier:h,getIsSponsored:u,signOnly:!1,onConfirm:()=>i.sendTransaction(s),onSuccess:e=>r(e.hash),onFailure:g,uiOptions:o?.uiOptions??{}}}),t(y)})).finally((()=>{e.current=!1}))},signMessage:async(s,o)=>r({showWalletUIs:o?.uiOptions?.showWalletUIs})?(e.current=!0,await i.signMessage(s).finally((()=>e.current=!1))):new Promise((async(r,d)=>{let{entropyId:u,entropyIdVerifier:p}=m(n);e.current=!0,a({connectWallet:{recoveryMethod:l.recoveryMethod,connectingWalletAddress:l.address,isUnifiedWallet:c,entropyId:u,entropyIdVerifier:p,onCompleteNavigateTo:g,onFailure:()=>{}},signMessage:{method:"personal_sign",data:"string"==typeof s.message?s.message:s.message.raw.toString(),confirmAndSign:()=>i.signMessage(s),onSuccess:e=>r(e),onFailure:d,uiOptions:o?.uiOptions??{}}}),t(y)})).finally((()=>{e.current=!1})),signTypedData:async(s,o)=>r({showWalletUIs:o?.uiOptions?.showWalletUIs})?(e.current=!0,await i.signTypedData(s).finally((()=>e.current=!1))):new Promise((async(r,d)=>{e.current=!0;let{entropyId:u,entropyIdVerifier:p}=m(n);a({connectWallet:{recoveryMethod:l.recoveryMethod,connectingWalletAddress:l.address,isUnifiedWallet:c,entropyId:u,entropyIdVerifier:p,onCompleteNavigateTo:g,onFailure:()=>{}},signMessage:{method:"eth_signTypedData_v4",data:s,confirmAndSign:()=>i.signTypedData(s),onSuccess:e=>r(e),onFailure:d,uiOptions:o?.uiOptions??{}}}),t(y)})).finally((()=>{e.current=!1}))})}};export{q as a,B as c,$ as g,j as u};