Skip to content

Commit

Permalink
refactor: use single public function to handle handling alias
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed May 27, 2024
1 parent b0d11fa commit f0349eb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
1 change: 1 addition & 0 deletions apps/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 3 additions & 7 deletions apps/ui/src/composables/useActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,9 @@ export function useActions() {
async function aliasableSigner(): Promise<Web3Provider | Wallet> {
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<string | null> {
Expand Down
30 changes: 16 additions & 14 deletions apps/ui/src/composables/useAlias.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<unknown>) {
const newAliasWallet = Wallet.createRandom();

Expand All @@ -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<unknown>) {
return (
(await existingAliasWallet(aliases.value[web3.value.account])) ||
(await create(networkCreateActionFn))
);
}

return { wallet, isValid, create };
return { getAliasWallet };
}

0 comments on commit f0349eb

Please sign in to comment.