UNPKG

@web3auth/no-modal

Version:
75 lines (72 loc) 2.01 kB
import { useState, useCallback } from 'react'; import { useWeb3AuthInner } from '../react/hooks/useWeb3AuthInner.js'; import { WalletInitializationError } from '../base/errors/index.js'; export { makeAccountLinkingRequest, makeAccountUnlinkingRequest } from './rest.js'; const useLinkAccount = () => { const { web3Auth } = useWeb3AuthInner(); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [linkedAccounts, setLinkedAccounts] = useState([]); const linkAccount = useCallback(async params => { if (!web3Auth) throw WalletInitializationError.notReady(); setLoading(true); setError(null); try { const result = await web3Auth.linkAccount(params); setLinkedAccounts(result.linkedAccounts); return result; } catch (err) { setError(err); } finally { setLoading(false); } }, [web3Auth]); const unlinkAccount = useCallback(async address => { if (!web3Auth) throw WalletInitializationError.notReady(); setLoading(true); setError(null); try { const result = await web3Auth.unlinkAccount(address); setLinkedAccounts(result.linkedAccounts); return result; } catch (err) { setError(err); } finally { setLoading(false); } }, [web3Auth]); return { loading, error, linkAccount, unlinkAccount, linkedAccounts }; }; const useSwitchAccount = () => { const { web3Auth } = useWeb3AuthInner(); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const switchAccount = useCallback(async account => { if (!web3Auth) throw WalletInitializationError.notReady(); setLoading(true); setError(null); try { await web3Auth.switchAccount(account); } catch (err) { setError(err); } finally { setLoading(false); } }, [web3Auth]); return { loading, error, switchAccount }; }; export { useLinkAccount, useSwitchAccount };