Cleanup address identification

This commit is contained in:
Alexis Sellier
2021-06-03 11:56:53 +02:00
parent 163e104068
commit 86df6db88e
2 changed files with 31 additions and 12 deletions
+11 -12
View File
@@ -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);
});
</script>
@@ -47,14 +43,17 @@
<div class="address">
<span class="icon"><Blockies address={address} /></span>
{#if isOrg}
{#if addressType === AddressType.Org}
<a use:link href={`/orgs/${address}`}>{checksumAddress}</a>
<span class="badge">org</span>
{:else if isContract}
<a href={explorerLink(address, config)} target="_blank">{checksumAddress}</a>
<span class="badge">contract</span>
{:else}
<a href={explorerLink(address, config)} target="_blank">{checksumAddress}</a>
<div class="loading"><Loading small /></div>
{#if addressType === AddressType.Contract}
<span class="badge">contract</span>
{:else if addressType === AddressType.EOA}
<!-- Don't show anything for EOAs -->
{:else}
<div class="loading"><Loading small /></div>
{/if}
{/if}
</div>
+20
View File
@@ -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<AddressType> {
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;
}