UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) • 12.2 kB
"use strict";var e=require("react"),t=require("viem"),r=require("./privy-context-CrM9_s6G.js"),n=require("./context-Cg3IEi80.js"),a=require("./internal-context-BJv4f_bO.js"),i=require("./useWallets-dikDynAG.js"),s=require("./PrivyPluginContext-DsgaS6n9.js"),o=require("./getEmbeddedConnectedWallet-pJd-l936.js"),l=require("permissionless"),c=require("permissionless/accounts"),d=require("permissionless/clients/pimlico"),u=require("viem/account-abstraction"),m=require("@privy-io/js-sdk-core");let p={address:u.entryPoint06Address,version:"0.6"},y={address:u.entryPoint07Address,version:"0.7"},g=({owner:e,smartWalletType:t,smartWalletVersion:r,publicClient:n})=>{switch(t){case m.SAFE:return c.toSafeSmartAccount({client:n,entryPoint:y,owners:[e],version:"1.4.1"});case m.KERNEL:return c.toKernelSmartAccount({client:n,version:"0.3.1",entryPoint:y,useMetaFactory:"optional",owners:[e]});case m.BICONOMY:return c.toBiconomySmartAccount({client:n,owners:[e],entryPoint:p});case m.LIGHT_ACCOUNT:return c.toLightSmartAccount({client:n,entryPoint:p,owner:e,version:"1.1.0"});case m.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 m.THIRDWEB:return c.toThirdwebSmartAccount({client:n,entryPoint:y,owner:e});default:throw Error(`Invalid smart account type: ${t}.`)}};const P=({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}))),h=async({chain:e,paymasterContext:r,account:n,signer:s,user:o,smartWalletsConfig:c,rpcConfig:u,privyAppId:m})=>{if(!(c?.enabled&&o&&n&&s))return;let p=c?.configuredNetworks.find((t=>t.chainId===`eip155:${e.id}`));if(!p)throw new a.PrivyClientError(`The chain ${e.name} (eip155:${e.id}) must be configured in the smart wallet configuration in your dashboard`);let y=o.smartWallet?o.smartWallet.smartWalletType:c.smartWalletType,P=o.smartWallet?o.smartWallet.smartWalletVersion:c.smartWalletVersion,h=i.getJsonRpcEndpointFromChain(e,u,m),f=t.createPublicClient({chain:e,transport:t.http(h)}),I=r??p.paymasterContext,F=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}),m=d.createPimlicoClient({transport:t.http(s),entryPoint:u.entryPoint}),p=o?d.createPimlicoClient({transport:t.http(o),entryPoint:u.entryPoint}):void 0;return l.createSmartAccountClient({account:u,chain:a,paymaster:p,paymasterContext:c,bundlerTransport:t.http(s),userOperation:{estimateFeesPerGas:async()=>{if(["public.pimlico.io","api.pimlico.io"].some((e=>s.includes(e))))return await m.getUserOperationGasPrice().then((e=>e.fast));if(["rpc.zerodev.app"].some((e=>s.includes(e))))return(await m.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"}),m.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:s,smartWalletType:y,smartWalletVersion:P,chain:e,publicClient:f,paymasterContext:I,...p});if(!F)throw new a.PrivyClientError(`Failed to create smart wallet client for chain ${e.name} (eip155:${e.id})`);return F};exports.callsToTransactionRequests=P,exports.getSmartWalletClient=h,exports.useSmartWalletsUIWrapper=()=>{let{hideWalletUIs:e,openPrivyModal:t,isHeadlessSigning:n}=a.usePrivyInternal(),{setModalData:l}=r.usePrivyModal(),{user:c}=r.usePrivyContext(),d=s.usePlugins(),u=r.getPrivyEthereumWallet(c),m=!!u&&r.getIsUnifiedWallet(u);return{wrapSmartAccountClientWithUIs:r=>({...r,sendTransaction:async(a,s)=>{let p=[];"calls"in a&&void 0!==a.calls?p=[...a.calls]:"to"in a&&(p=[{to:a.to,value:a.value||BigInt(0),data:a.data||"0x"}]);let y=(await r.prepareUserOperation({calls:p,maxFeePerGas:a.maxFeePerGas,maxPriorityFeePerGas:a.maxPriorityFeePerGas,nonce:a.nonce?BigInt(a.nonce):void 0})).callData,g=d(o.DATA_SUFFIX_PLUGIN_ID);if(g&&(y=g.appendDataSuffix({data:y})),n({showWalletUIs:s?.uiOptions?.showWalletUIs}))return e.current=!0,await r.sendTransaction({callData:y,maxFeePerGas:a.maxFeePerGas,maxPriorityFeePerGas:a.maxPriorityFeePerGas,nonce:a.nonce?BigInt(a.nonce):void 0}).finally((()=>e.current=!1));let h=async()=>{if(!r.paymaster)return!1;let{paymasterData:e,paymasterAndData:t}=await r.prepareUserOperation({calls:p,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,o)=>{e.current=!0;let{entropyId:d,entropyIdVerifier:g}=i.getEntropyDetailsForUser(c);l({connectWallet:{recoveryMethod:u.recoveryMethod,connectingWalletAddress:u.address,isUnifiedWallet:m,entropyId:d,entropyIdVerifier:g,onCompleteNavigateTo:"SendTransactionScreen",onFailure:()=>{}},sendTransaction:{transactionRequests:P({calls:p,chain:r.chain,maxPriorityFeePerGas:a.maxPriorityFeePerGas,maxFeePerGas:a.maxFeePerGas,nonce:a.nonce?BigInt(a.nonce):void 0}),transactingWalletIndex:0,transactingWalletAddress:r.account.address,entropyId:d,entropyIdVerifier:g,getIsSponsored:h,signOnly:!1,onConfirm:async()=>r.sendTransaction({callData:y,maxFeePerGas:a.maxFeePerGas,maxPriorityFeePerGas:a.maxPriorityFeePerGas,nonce:a.nonce?BigInt(a.nonce):void 0}),onSuccess:e=>n(e.hash),onFailure:o,uiOptions:s?.uiOptions??{}}}),t("EmbeddedWalletConnectingScreen")})).finally((()=>{e.current=!1}))},signMessage:async(a,s)=>n({showWalletUIs:s?.uiOptions?.showWalletUIs})?(e.current=!0,await r.signMessage(a).finally((()=>e.current=!1))):new Promise((async(n,o)=>{let{entropyId:d,entropyIdVerifier:p}=i.getEntropyDetailsForUser(c);e.current=!0,l({connectWallet:{recoveryMethod:u.recoveryMethod,connectingWalletAddress:u.address,isUnifiedWallet:m,entropyId:d,entropyIdVerifier:p,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:o,uiOptions:s?.uiOptions??{}}}),t("EmbeddedWalletConnectingScreen")})).finally((()=>{e.current=!1})),signTypedData:async(a,s)=>n({showWalletUIs:s?.uiOptions?.showWalletUIs})?(e.current=!0,await r.signTypedData(a).finally((()=>e.current=!1))):new Promise((async(n,o)=>{e.current=!0;let{entropyId:d,entropyIdVerifier:p}=i.getEntropyDetailsForUser(c);l({connectWallet:{recoveryMethod:u.recoveryMethod,connectingWalletAddress:u.address,isUnifiedWallet:m,entropyId:d,entropyIdVerifier:p,onCompleteNavigateTo:"SignRequestScreen",onFailure:()=>{}},signMessage:{method:"eth_signTypedData_v4",data:a,confirmAndSign:()=>r.signTypedData(a),onSuccess:e=>n(e),onFailure:o,uiOptions:s?.uiOptions??{}}}),t("EmbeddedWalletConnectingScreen")})).finally((()=>{e.current=!1}))})}},exports.useSmartWalletsWrapper=({clientConfig:t,smartWalletsConfig:l})=>{let{hideWalletUIs:c,openPrivyModal:d,appId:u,isHeadlessSigning:m}=a.usePrivyInternal(),p=n.useAppConfig(),{setModalData:y}=r.usePrivyModal(),{user:g}=r.usePrivyContext(),{chains:f}=n.useAppConfig(),I=s.usePlugins(),{chainId:F,clients:w,setChainId:W,chainIdState:x}=(()=>{let t=e.useRef(),[r,n]=e.useState();return{clients:e.useRef({}),setChainId:e=>{t.current=e,n(e)},chainId:t,chainIdState:r}})(),v=f.find((e=>e.id===x)),{wallets:C}=i.useWallets(),G=r.getPrivyEthereumWallet(g),S=o.getEmbeddedConnectedWalletWithAddress(C,G?.address),U=!!G&&r.getIsUnifiedWallet(G),T=e.useCallback((async()=>F.current),[F]),A=e.useCallback((async({id:e})=>{if(!w.current[e]){let r=await h({chain:f.find((t=>t.id===e)),account:G,signer:await(S?.getEthereumProvider()),user:g,paymasterContext:t?.paymasterContext,smartWalletsConfig:l,rpcConfig:p.rpcConfig,privyAppId:u});r&&(w.current[e]=r)}W(e)}),[f,t,C,g,l]);return{wrapSmartAccountClient:e=>{w.current[e.chain.id]=e,x||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`);delete e.account;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=(await r.prepareUserOperation({calls:n,maxFeePerGas:e.maxFeePerGas,maxPriorityFeePerGas:e.maxPriorityFeePerGas,nonce:e.nonce?BigInt(e.nonce):void 0})).callData,l=I(o.DATA_SUFFIX_PLUGIN_ID);if(l&&(s=l.appendDataSuffix({data:s})),m({showWalletUIs:t?.uiOptions?.showWalletUIs}))return c.current=!0,await r.sendTransaction({callData:s,maxFeePerGas:e.maxFeePerGas,maxPriorityFeePerGas:e.maxPriorityFeePerGas,nonce:e.nonce?BigInt(e.nonce):void 0}).finally((()=>c.current=!1));let u=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)=>{c.current=!0;let{entropyId:l,entropyIdVerifier:m}=i.getEntropyDetailsForUser(g);y({connectWallet:{recoveryMethod:G.recoveryMethod,connectingWalletAddress:G.address,isUnifiedWallet:U,entropyId:l,entropyIdVerifier:m,onCompleteNavigateTo:"SendTransactionScreen",onFailure:()=>{}},sendTransaction:{transactionRequests:P({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:l,entropyIdVerifier:m,getIsSponsored:u,signOnly:!1,onConfirm:async()=>r.sendTransaction({callData:s,maxFeePerGas:e.maxFeePerGas,maxPriorityFeePerGas:e.maxPriorityFeePerGas,nonce:e.nonce?BigInt(e.nonce):void 0}),onSuccess:e=>a(e.hash),onFailure:o,uiOptions:t?.uiOptions??{}}}),d("EmbeddedWalletConnectingScreen")})).finally((()=>{c.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 m({showWalletUIs:t?.uiOptions?.showWalletUIs})?(c.current=!0,await r.signMessage(e).finally((()=>c.current=!1))):new Promise((async(n,a)=>{let{entropyId:s,entropyIdVerifier:o}=i.getEntropyDetailsForUser(g);c.current=!0,y({connectWallet:{recoveryMethod:G.recoveryMethod,connectingWalletAddress:G.address,isUnifiedWallet:U,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??{}}}),d("EmbeddedWalletConnectingScreen")})).finally((()=>{c.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,m({showWalletUIs:t?.uiOptions?.showWalletUIs})?(c.current=!0,await r.signTypedData(e).finally((()=>c.current=!1))):new Promise((async(n,a)=>{c.current=!0;let{entropyId:s,entropyIdVerifier:o}=i.getEntropyDetailsForUser(g);y({connectWallet:{recoveryMethod:G.recoveryMethod,connectingWalletAddress:G.address,isUnifiedWallet:U,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??{}}}),d("EmbeddedWalletConnectingScreen")})).finally((()=>{c.current=!1}))},getChainId:T,chain:v,switchChain:A}}}};