Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Cleanup address identification
Alexis Sellier committed 4 years ago
commit 86df6db88e65123ed06579573ea087dc0fc9c9b9
parent 163e1040688f086a7637675ba2ede6e198199e32
2 files changed +31 -12
modified 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);
  });
</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>
modified 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<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;
+
}