diff --git a/src/Address.svelte b/src/Address.svelte index 1423e7b9e..bfc3cfe67 100644 --- a/src/Address.svelte +++ b/src/Address.svelte @@ -6,20 +6,16 @@ import Blockies from '@app/Blockies.svelte'; import Loading from '@app/Loading.svelte'; import type { Config } from '@app/config'; + import { identifyAddress, AddressType } from '@app/utils'; export let address: string; export let config: Config; let checksumAddress = ethers.utils.getAddress(address); - let isContract = false; - let isOrg = false; + let addressType: AddressType | null = null; onMount(async () => { - let code = await config.provider.getCode(address); - let bytes = ethers.utils.arrayify(code); - - isContract = bytes.length > 0; - isOrg = ethers.utils.keccak256(bytes) === config.orgs.contractHash; + addressType = await identifyAddress(address, config); }); @@ -47,14 +43,17 @@
- {#if isOrg} + {#if addressType === AddressType.Org} {checksumAddress} org - {:else if isContract} - {checksumAddress} - contract {:else} {checksumAddress} -
+ {#if addressType === AddressType.Contract} + contract + {:else if addressType === AddressType.EOA} + + {:else} +
+ {/if} {/if}
diff --git a/src/utils.ts b/src/utils.ts index d9f9fcfbc..7a954e68c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,6 +4,12 @@ import multibase from 'multibase'; import type { Config } from '@app/config'; import { assert } from '@app/error'; +export enum AddressType { + Contract, + Org, + EOA, +} + export function formatBalance(n: BigNumber) { return ethers.utils.commify(parseFloat(ethers.utils.formatUnits(n)).toFixed(2)); } @@ -118,3 +124,17 @@ export function formatProjectHash(hash: Uint8Array, format: number): string { const suffix = hash.slice(hash.length - sha1Bytes); return ethers.utils.hexlify(suffix).replace(/^0x/, ''); } + +// Identify an address by checking whether it's a contract or an externally-owned address. +export async function identifyAddress(address: string, config: Config): Promise { + let code = await config.provider.getCode(address); + let bytes = ethers.utils.arrayify(code); + + if (bytes.length > 0) { + if (ethers.utils.keccak256(bytes) === config.orgs.contractHash) { + return AddressType.Org; + } + return AddressType.Contract; + } + return AddressType.EOA; +}