UNPKG

noob-ethereum

Version:
26 lines (25 loc) 1.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.checksum = void 0; const keccak_1 = __importDefault(require("keccak")); const hexGreaterThan = (limit) => (val) => parseInt(val, 16) >= parseInt(limit, 16); const hexGreaterThan8 = hexGreaterThan('8'); const toCapitalise = (hex) => hexGreaterThan8(hex); /** * Compute the EIP-55 checksum of a supplied Ethereum address * @param {string} - address (e.g. 0x001d3f1ef827552ae1114027bd3ecf1f086ba0f9) * @returns {string} - (e.g. 0x001d3F1ef827552Ae1114027BD3ECF1f086bA0F9) */ const eip55Checksum = (address) => { const formatted = address.startsWith('0x') ? address.substring(2) : address; const keccakHash = (0, keccak_1.default)('keccak256').update(formatted).digest('hex'); return ('0x' + formatted .split('') .map((v, i, arr) => (toCapitalise(keccakHash[i]) ? v.toUpperCase() : v)) .join('')); }; exports.checksum = eip55Checksum;