UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) 8.23 kB
import{createAbstractClient as e}from"@abstract-foundation/agw-client";import{getBatchTransactionObject as t}from"@abstract-foundation/agw-client/actions";import{hexToNumber as r,createWalletClient as o,custom as n,http as a}from"viem";import{toAccount as i}from"viem/accounts";import{abstractTestnet as s,zksync as c}from"viem/chains";import{ab as l,$ as p,ac as d,b5 as m,q as u,H as h,bE as y,bw as g,bM as I}from"./EmbeddedWalletConnectingScreen-D50stmJi.mjs";import{k as f}from"./events-context-ClaboIDc.mjs";import{aa as v}from"./internal-context-eGSwROSk.mjs";import{S as w}from"./SignRequestScreen-D9o7VhNz.mjs";import{g as C}from"./getEmbeddedConnectedWallet-CM6cDQCS.mjs";import{c as T}from"./smart-wallets-BXT3GZwg.mjs";import"react/jsx-runtime";import"lucide-react";import"react";import"fast-password-entropy";import"secure-password-utilities";import"secure-password-utilities/wordlists";import"@heroicons/react/24/outline/UserCircleIcon";import"styled-components";import"@heroicons/react/24/outline/ArrowLeftIcon";import"@heroicons/react/24/outline/ArrowRightIcon";import"@heroicons/react/24/outline/QuestionMarkCircleIcon";import"@heroicons/react/24/outline/XMarkIcon";import"@heroicons/react/20/solid/CheckIcon";import"@heroicons/react/24/outline/EnvelopeIcon";import"@heroicons/react/24/outline/PhoneIcon";import"react-device-detect";import"@heroicons/react/24/outline/ExclamationTriangleIcon";import"@heroicons/react/24/outline/WalletIcon";import"@heroicons/react/24/outline/ExclamationCircleIcon";import"@heroicons/react/24/outline/Square2StackIcon";import"@privy-io/js-sdk-core";import"zustand";import"@heroicons/react/24/outline";import"@tanstack/react-virtual";import"@heroicons/react/24/outline/CheckIcon";import"qrcode";import"viem/utils";import"eventemitter3";import"@scure/base";import"@walletconnect/ethereum-provider";import"@heroicons/react/24/outline/CreditCardIcon";import"@heroicons/react/24/outline/QrCodeIcon";import"@heroicons/react/24/solid/ArrowsRightLeftIcon";import"@heroicons/react/24/outline/InformationCircleIcon";import"@heroicons/react/24/outline/ClipboardDocumentCheckIcon";import"@heroicons/react/24/outline/ClipboardDocumentIcon";import"@heroicons/react/24/outline/ChevronDownIcon";import"@privy-io/ethereum";import"@heroicons/react/24/outline/CheckCircleIcon";import"@heroicons/react/24/outline/GlobeAltIcon";import"@headlessui/react";import"@privy-io/popup";import"@heroicons/react/24/solid/CheckCircleIcon";import"ofetch";import"@heroicons/react/24/outline/FingerPrintIcon";import"@heroicons/react/24/outline/LockClosedIcon";import"@heroicons/react/24/outline/PencilSquareIcon";import"jose";import"@heroicons/react/24/outline/ArrowPathIcon";import"@heroicons/react/24/outline/EyeIcon";import"@heroicons/react/24/outline/EyeSlashIcon";import"@heroicons/react/24/outline/KeyIcon";import"@heroicons/react/24/outline/ArrowDownTrayIcon";import"@heroicons/react/24/outline/DocumentDuplicateIcon";import"@heroicons/react/24/solid/XCircleIcon";import"@heroicons/react/24/outline/ShieldCheckIcon";import"@privy-io/urls";import"tinycolor2";import"permissionless";import"permissionless/accounts";import"permissionless/clients/pimlico";import"viem/account-abstraction";function S(){let{user:S}=l(),{hideWalletUIs:b,openPrivyModal:P,chains:W,appId:A,rpcConfig:M,client:F}=v(),q=f(),{wallets:x}=p(),{setModalData:E}=d(),_=m(S),k=!!_&&u(_),D=async(t=s.id)=>{let l=C(x);if(!l)throw Error("No connected wallet found");let p="string"==typeof t?r(t):t;if(![s.id,2741,c.id].includes(p))throw Error("Error, only Abstract and ZKSync Era chains are supported");let d=W.find((e=>e.id===p));if(!d)throw Error("Chain not configured");await l.switchChain(d.id);let m=await l.getEthereumProvider(),u=o({account:l.address,transport:n(m)}),h=i({address:l.address,signMessage:u.signMessage,signTransaction:u.signTransaction,signTypedData:u.signTypedData});return await e({chain:d,transport:a(I(d,M,A)),publicTransport:a(I(d,M,A)),signer:h})};return{signMessage:async({message:e},t)=>{let r=await D(t);return new Promise((async(t,o)=>{let{entropyId:n,entropyIdVerifier:a}=h(S);b.current=!0,E({connectWallet:{recoveryMethod:_.recoveryMethod,connectingWalletAddress:_.address,isUnifiedWallet:k,entropyId:n,entropyIdVerifier:a,onCompleteNavigateTo:w,onFailure:()=>{}},signMessage:{method:"personal_sign",data:e,confirmAndSign:()=>r.signMessage({message:e}),onSuccess:e=>t(e),onFailure:o,uiOptions:{isCancellable:!0}}}),P(g)})).finally((()=>{b.current=!1}))},signTypedData:async(e,t)=>{let r=await D(t);return new Promise((async(t,o)=>{b.current=!0;let{entropyId:n,entropyIdVerifier:a}=h(S);E({connectWallet:{recoveryMethod:_.recoveryMethod,connectingWalletAddress:_.address,isUnifiedWallet:k,entropyId:n,entropyIdVerifier:a,onCompleteNavigateTo:w,onFailure:()=>{}},signMessage:{method:"eth_signTypedData_v4",data:e,confirmAndSign:()=>r.signTypedData(e),onSuccess:e=>t(e),onFailure:o,uiOptions:{isCancellable:!0}}}),P(g)})).finally((()=>{b.current=!1}))},sendTransaction:async e=>{let r=await D(e.chainId),o=[],n="calls"in e&&void 0!==e.calls;return o=n?[...e.calls]:[e],new Promise((async(a,i)=>{b.current=!0;let{entropyId:s,entropyIdVerifier:c}=h(S);E({connectWallet:{recoveryMethod:_.recoveryMethod,connectingWalletAddress:_.address,isUnifiedWallet:k,entropyId:s,entropyIdVerifier:c,onCompleteNavigateTo:y,onFailure:()=>{}},sendTransaction:{transactionRequests:T({calls:o,chain:r.chain,maxPriorityFeePerGas:e.maxPriorityFeePerGas,maxFeePerGas:e.maxFeePerGas,nonce:e.nonce?BigInt(e.nonce):void 0}),entropyId:s,entropyIdVerifier:c,transactingWalletAddress:r.account.address,transactingWalletIndex:void 0,prepareTransactionRequest:async()=>{let o=n?t(r.account.address,e):e;return await r.prepareAbstractTransactionRequest(o)},scanTransaction:async()=>{if(!F)throw Error("Privy client not found");let o=n?t(r.account.address,e):e,a=await r.prepareAbstractTransactionRequest(o);return await F.scanTransaction({metadata:{domain:q.embeddedWallets.transactionScanning.domain},chain_id:e.chainId.toString(),request:{method:"eth_sendTransaction",params:[{from:a.from,to:a.to,value:a.value?.toString(),gas:a.gas?.toString(),gasPrice:a.gasPrice?.toString(),nonce:a.nonce?.toString(),data:a.data,eip_712_meta:{paymaster_params:{paymaster:a.paymaster,paymaster_input:a.paymasterInput}}}]}})},signOnly:!1,getIsSponsored:async()=>void 0!==e.paymaster&&void 0!==e.paymasterInput,onConfirm:()=>n?r.sendTransactionBatch(e):r.sendTransaction(e),onSuccess:e=>a(e.hash),onFailure:i,uiOptions:{isCancellable:!0}}}),P(g)})).finally((()=>{b.current=!1}))},signTransaction:async e=>{let r=await D(e.chainId),o=[],n="calls"in e&&void 0!==e.calls;return o=n?[...e.calls]:[e],new Promise((async(a,i)=>{b.current=!0;let{entropyId:s,entropyIdVerifier:c}=h(S);E({connectWallet:{recoveryMethod:_.recoveryMethod,connectingWalletAddress:_.address,isUnifiedWallet:k,entropyId:s,entropyIdVerifier:c,onCompleteNavigateTo:y,onFailure:()=>{}},sendTransaction:{transactionRequests:T({calls:o,chain:r.chain,maxPriorityFeePerGas:e.maxPriorityFeePerGas,maxFeePerGas:e.maxFeePerGas,nonce:e.nonce?BigInt(e.nonce):void 0}),entropyId:s,entropyIdVerifier:c,transactingWalletAddress:r.account.address,transactingWalletIndex:void 0,prepareTransactionRequest:async()=>{let o=n?t(r.account.address,e):e;return await r.prepareAbstractTransactionRequest(o)},scanTransaction:async()=>{if(!F)throw Error("Privy client not found");let o=n?t(r.account.address,e):e,a=await r.prepareAbstractTransactionRequest(o);return await F.scanTransaction({metadata:{domain:q.embeddedWallets.transactionScanning.domain},chain_id:e.chainId.toString(),request:{method:"eth_sendTransaction",params:[{from:a.from,to:a.to,value:a.value?.toString(),gas:a.gas?.toString(),gasPrice:a.gasPrice?.toString(),nonce:a.nonce?.toString(),data:a.data,eip_712_meta:{paymaster_params:{paymaster:a.paymaster,paymaster_input:a.paymasterInput}}}]}})},signOnly:!0,getIsSponsored:async()=>void 0!==e.paymaster&&void 0!==e.paymasterInput,onConfirm:async()=>{let o=n?t(r.account.address,e):e,a=await r.prepareAbstractTransactionRequest(o);return r.signTransaction(a)},onSuccess:e=>a(e.hash),onFailure:i,uiOptions:{isCancellable:!0}}}),P(g)})).finally((()=>{b.current=!1}))}}}export{S as useAbstractSmartWallets};