Last active
May 29, 2024 05:42
-
-
Save ardislu/2dc994a16b2a4a39fa840834c47919e5 to your computer and use it in GitHub Desktop.
JavaScript implementation of ERC-55 ("Mixed-case checksum address encoding") using noble-hashes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript implementation of ERC-55 ("Mixed-case checksum address encoding") using noble-hashes | |
// https://ercs.ethereum.org/ERCS/erc-55 | |
// https://github.com/paulmillr/noble-hashes | |
// import { keccak_256 } from '@noble/hashes/sha3'; | |
const { keccak_256 } = await import('https://esm.sh/@noble/hashes@1.4.0/esm/sha3.js'); | |
function toChecksumAddress(address) { | |
address = address.toLowerCase().replace('0x', ''); | |
// Hash the address (treat it as UTF-8) and return as a hex string | |
const hash = [...keccak_256(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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment