diff --git a/apps/ui/package.json b/apps/ui/package.json index 173fa9c14..b55820f63 100644 --- a/apps/ui/package.json +++ b/apps/ui/package.json @@ -34,6 +34,7 @@ "@ethersproject/abstract-signer": "^5.7.0", "@ethersproject/address": "^5.7.0", "@ethersproject/bignumber": "^5.7.0", + "@ethersproject/bytes": "^5.7.0", "@ethersproject/constants": "^5.7.0", "@ethersproject/contracts": "^5.7.0", "@ethersproject/hash": "^5.7.0", diff --git a/apps/ui/src/composables/useActions.ts b/apps/ui/src/composables/useActions.ts index 5b1eea9c7..c5e070ccf 100644 --- a/apps/ui/src/composables/useActions.ts +++ b/apps/ui/src/composables/useActions.ts @@ -111,13 +111,9 @@ export function useActions() { async function aliasableSigner(): Promise { const network = getNetwork(offchainNetworkId); - if (!(await alias.isValid())) { - await alias.create(address => - wrapPromise(offchainNetworkId, network.actions.setAlias(auth.web3, address)) - ); - } - - return alias.wallet.value || auth.web3; + return alias.getAliasWallet(address => + wrapPromise(offchainNetworkId, network.actions.setAlias(auth.web3, address)) + ); } async function predictSpaceAddress(networkId: NetworkID, salt: string): Promise { diff --git a/apps/ui/src/composables/useAlias.ts b/apps/ui/src/composables/useAlias.ts index 6686bd127..97a9c8387 100644 --- a/apps/ui/src/composables/useAlias.ts +++ b/apps/ui/src/composables/useAlias.ts @@ -1,5 +1,6 @@ import { Wallet } from '@ethersproject/wallet'; import { getDefaultProvider } from '@ethersproject/providers'; +import { isHexString } from '@ethersproject/bytes'; import { enabledNetworks, getNetwork, offchainNetworks } from '@/networks'; import pkg from '../../package.json'; @@ -9,17 +10,9 @@ const networkId = offchainNetworks.filter(network => enabledNetworks.includes(ne const network = getNetwork(networkId); export function useAlias() { + const provider = getDefaultProvider(); const { web3 } = useWeb3(); - const wallet = computed(() => { - const provider = getDefaultProvider(); - const privateKey = aliases.value[web3.value.account]; - - if (!privateKey) return null; - - return new Wallet(privateKey, provider); - }); - async function create(networkCreateActionFn: (address: string) => Promise) { const newAliasWallet = Wallet.createRandom(); @@ -31,19 +24,28 @@ export function useAlias() { [web3.value.account]: newAliasWallet.privateKey } }; + + return new Wallet(newAliasWallet.privateKey, provider); } - async function isValid() { - if (!wallet.value) return false; + async function existingAliasWallet(privateKey: string) { + if (!isHexString(privateKey)) return null; const registeredAlias = await network.api.loadAlias( web3.value.account, - wallet.value.address, + new Wallet(privateKey, provider).address, Math.floor(Date.now() / 1000) - 60 * 60 * 24 * 30 ); - return !!registeredAlias; + return registeredAlias ? new Wallet(privateKey, provider) : null; + } + + async function getAliasWallet(networkCreateActionFn: (address: string) => Promise) { + return ( + (await existingAliasWallet(aliases.value[web3.value.account])) || + (await create(networkCreateActionFn)) + ); } - return { wallet, isValid, create }; + return { getAliasWallet }; }