@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 4.46 kB
JavaScript
import{useMemo as e}from"react";import{create as o,rawSign as r}from"@privy-io/js-sdk-core";import{aa as t,P as i}from"./internal-context-eGSwROSk.mjs";import{ab as n,ac as c,bd as a,q as s,bv as l,bw as p,cP as m}from"./EmbeddedWalletConnectingScreen-D50stmJi.mjs";import{k as u}from"./events-context-ClaboIDc.mjs";import{u as d}from"./use-sign-with-user-signer-DaTvrd5T.mjs";import"ofetch";import"react/jsx-runtime";import"lucide-react";import"viem";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"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"@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";const h=()=>{let{refreshSessionAndUser:r,privy:i}=t();return e((()=>({async createWallet(e){let t=await o(i,{request:{chain_type:e.chainType}}),n=await r();if(!n)throw Error("User must be authenticated before creating a Privy wallet");return{user:n,wallet:t}}})),[r,i])};function I(){let{user:o,getAccessToken:r}=n(),{walletProxy:m,openModal:d,client:h}=t(),{setModalData:I}=c(),w=u();return e((()=>({exportWallet:e=>new Promise((async(t,n)=>{if(!o)return void n(new i("User must be authenticated before exporting their Privy wallet"));let c=a(o,e.address);c?"spark"===c.chainType&&s(c)?await r()?m?(I({connectWallet:{recoveryMethod:c.recoveryMethod,connectingWalletAddress:c.address,isUnifiedWallet:!0,onCompleteNavigateTo:l,onFailure:n,shouldForceMFA:!0},keyExport:{appId:w.id,appClientId:w.appClientId,origin:h.apiUrl,address:c.address,hdWalletIndex:c.walletIndex,chainType:c.chainType,walletId:c.id,isUnifiedWallet:!0,imported:c.imported,onSuccess:t,onFailure:n}}),d(p)):n(new i("Wallet not ready for export")):n(new i("Must have valid access token.")):n(Error("Export via extended-chains is only supported for unified stack wallets")):n(new i("User must have an embedded wallet."))}))})),[o,r,m,d,h,I,w])}const w=()=>{let{user:o}=n(),{privy:i}=t(),{signWithUserSigner:c}=d();return e((()=>({async signRawHash({address:e,chainType:t,hash:n}){if(!o)throw Error("User must be authenticated before signing");let a=m(o,t).filter((e=>s(e))).find((o=>o.address===e));if(!a)throw Error("Wallet not found");let l=await r(i,c,{wallet_id:a.id,params:{hash:n}});if(l.data&&"signature"in l.data&&"hex"===l.data.encoding)return{signature:l.data.signature};throw Error("Failed to sign")}})),[i,c,o])};export{h as useCreateWallet,I as useExportWallet,w as useSignRawHash};