@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
78 lines (75 loc) • 2.06 kB
JavaScript
import { ref } from 'vue';
import { useWeb3AuthInner } from '../vue/composables/useWeb3AuthInner.js';
import { WalletInitializationError } from '../base/errors/index.js';
import { log } from '../base/loglevel.js';
export { makeAccountLinkingRequest, makeAccountUnlinkingRequest } from './rest.js';
const useLinkAccount = () => {
const {
web3Auth
} = useWeb3AuthInner();
const loading = ref(false);
const error = ref(null);
const linkedAccounts = ref([]);
const linkAccount = async params => {
if (!web3Auth.value) throw WalletInitializationError.notReady();
try {
error.value = null;
loading.value = true;
const result = await web3Auth.value.linkAccount(params);
linkedAccounts.value = result.linkedAccounts;
return result;
} catch (err) {
error.value = err;
} finally {
loading.value = false;
}
};
const unlinkAccount = async address => {
if (!web3Auth.value) throw WalletInitializationError.notReady();
try {
error.value = null;
loading.value = true;
const result = await web3Auth.value.unlinkAccount(address);
linkedAccounts.value = result.linkedAccounts;
return result;
} catch (err) {
log.error("Error unlinking account", err);
error.value = err;
} finally {
loading.value = false;
}
};
return {
loading,
error,
linkAccount,
unlinkAccount,
linkedAccounts
};
};
const useSwitchAccount = () => {
const {
web3Auth
} = useWeb3AuthInner();
const loading = ref(false);
const error = ref(null);
const switchAccount = async account => {
if (!web3Auth.value) throw WalletInitializationError.notReady();
error.value = null;
loading.value = true;
try {
await web3Auth.value.switchAccount(account);
} catch (err) {
log.error("Error switching account", err);
error.value = err;
} finally {
loading.value = false;
}
};
return {
loading,
error,
switchAccount
};
};
export { useLinkAccount, useSwitchAccount };