From: Ardis Lu Date: Thu, 23 Nov 2023 06:11:10 +0000 (-0800) Subject: Implement ERC-55 mixed-case checksum address X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=83b2fa9371f94510a6e13194de86060f3f6b4ef2;p=ens2hex Implement ERC-55 mixed-case checksum address --- diff --git a/index.html b/index.html index 218efd9..527e34e 100644 --- a/index.html +++ b/index.html @@ -431,6 +431,25 @@ return addresses; } + // ERC-55: https://ercs.ethereum.org/ERCS/erc-55 + function toChecksumAddress(address) { + address = address.toLowerCase().replace('0x', ''); + // Hash the address (treat it as UTF-8) and return as a hex string + const hash = [...keccak256(address)].map(v => v.toString(16).padStart(2, '0')).join(''); + let ret = '0x'; + + for (let i = 0; i < 40; i++) { + if (parseInt(hash[i], 16) >= 8) { + ret += address[i].toUpperCase(); + } + else { + ret += address[i]; + } + } + + return ret; + } + document.querySelector('form').addEventListener('submit', async event => { event.preventDefault(); @@ -472,7 +491,7 @@ ens.textContent = name; const response = multicallResponse[name]; - address.textContent = response; + address.textContent = toChecksumAddress(response); } spinner.style.setProperty('display', 'none');