UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) • 12.3 kB
"use strict";var e=require("react"),t=require("viem"),r=require("./get-is-unified-wallet-UpdjHxOV.js"),n=require("./context-D-ZJYjUg.js"),a=require("./internal-context-BJv4f_bO.js"),i=require("./useWallets-CKAypPZB.js"),s=require("./PrivyPluginContext-DsgaS6n9.js"),o=require("./getEmbeddedConnectedWallet-pJd-l936.js"),l=require("./getPublicClient-BsmZyCGX.js"),c=require("permissionless"),d=require("permissionless/accounts"),u=require("permissionless/clients/pimlico"),m=require("viem/account-abstraction"),p=require("@privy-io/js-sdk-core");let y={address:m.entryPoint06Address,version:"0.6"},g={address:m.entryPoint07Address,version:"0.7"},P=({owner:e,smartWalletType:t,smartWalletVersion:r,publicClient:n})=>{switch(t){case p.SAFE:return d.toSafeSmartAccount({client:n,entryPoint:g,owners:[e],version:"1.4.1"});case p.KERNEL:return d.toKernelSmartAccount({client:n,version:"0.3.1",entryPoint:g,useMetaFactory:"optional",owners:[e]});case p.BICONOMY:return d.toBiconomySmartAccount({client:n,owners:[e],entryPoint:y});case p.LIGHT_ACCOUNT:return d.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 m.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 d.toThirdwebSmartAccount({client:n,entryPoint:g,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}))),f=async({chain:e,paymasterContext:r,account:n,signer:i,user:s,smartWalletsConfig:o,rpcConfig:d,privyAppId:m})=>{if(!(o?.enabled&&s&&n&&i))return;let p=o?.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=s.smartWallet?s.smartWallet.smartWalletType:o.smartWalletType,g=s.smartWallet?s.smartWallet.smartWalletVersion:o.smartWalletVersion,h=l.getJsonRpcEndpointFromChain(e,d,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:l})=>{let d=await P({owner:e,smartWalletType:r,publicClient:i,smartWalletVersion:n}),m=u.createPimlicoClient({transport:t.http(s),entryPoint:d.entryPoint}),p=o?u.createPimlicoClient({transport:t.http(o),entryPoint:d.entryPoint}):void 0;return c.createSmartAccountClient({account:d,chain:a,paymaster:p,paymasterContext:l,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:i,smartWalletType:y,smartWalletVersion:g,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=h,exports.getSmartWalletClient=f,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 P=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:h({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:P,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:P}=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=P.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 f({chain:P.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)}),[P,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: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: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}}}};