@toruslabs/metadata-helpers
Version:
Helper methods for metadata
158 lines (154 loc) • 5.3 kB
JavaScript
;
var sha3_js = require('@noble/hashes/sha3.js');
var assert = require('./assert.js');
// Use native regexes instead of superstruct for maximum performance.
// Pre-compiled regex for maximum performance - avoids recompilation on each call
const HEX_REGEX = /^(?:0x)?[0-9a-f]+$/iu;
const STRICT_HEX_REGEX = /^0x[0-9a-f]+$/iu;
const HEX_ADDRESS_REGEX = /^0x[0-9a-f]{40}$/u;
const HEX_CHECKSUM_ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/u;
const isString = value => typeof value === "string";
/**
* Check if a string is a valid hex string.
*
* @param value - The value to check.
* @returns Whether the value is a valid hex string.
*/
function isHexString(value) {
return isString(value) && HEX_REGEX.test(value);
}
/**
* Strictly check if a string is a valid hex string. A valid hex string must
* start with the "0x"-prefix.
*
* @param value - The value to check.
* @returns Whether the value is a valid hex string.
*/
function isStrictHexString(value) {
return isString(value) && STRICT_HEX_REGEX.test(value);
}
/**
* Check if a string is a valid hex address.
*
* @param value - The value to check.
* @returns Whether the value is a valid hex address.
*/
function isHexAddress(value) {
return isString(value) && HEX_ADDRESS_REGEX.test(value);
}
/**
* Check if a string is a valid hex checksum address.
*
* @param value - The value to check.
* @returns Whether the value is a valid hex checksum address.
*/
function isHexChecksumAddress(value) {
return isString(value) && HEX_CHECKSUM_ADDRESS_REGEX.test(value);
}
/**
* Assert that a value is a valid hex string.
*
* @param value - The value to check.
* @throws If the value is not a valid hex string.
*/
function assertIsHexString(value) {
assert.assert(isHexString(value), "Value must be a hexadecimal string.");
}
/**
* Assert that a value is a valid hex string. A valid hex string must start with
* the "0x"-prefix.
*
* @param value - The value to check.
* @throws If the value is not a valid hex string.
*/
function assertIsStrictHexString(value) {
assert.assert(isStrictHexString(value), 'Value must be a hexadecimal string, starting with "0x".');
}
/**
* Encode a passed hex string as an ERC-55 mixed-case checksum address.
* This is the unmemoized version, primarily used for testing.
*
* @param hexAddress - The hex address to encode.
* @returns The address encoded according to ERC-55.
* @see https://eips.ethereum.org/EIPS/eip-55
*/
function getChecksumAddress(hexAddress) {
assert.assert(isHexChecksumAddress(hexAddress), "Invalid hex address.");
const address = remove0x(hexAddress).toLowerCase();
const hashBytes = sha3_js.keccak_256(new TextEncoder().encode(address));
const {
length
} = address;
const result = new Array(length); // Pre-allocate array
for (let i = 0; i < length; i++) {
const byteIndex = i >> 1; // Faster than Math.floor(i / 2)
const nibbleIndex = i & 1; // Faster than i % 2
const byte = hashBytes[byteIndex];
const nibble = nibbleIndex === 0 ? byte >> 4 : byte & 0x0f;
result[i] = nibble >= 8 ? address[i].toUpperCase() : address[i];
}
return `0x${result.join("")}`;
}
/**
* Validate that the passed hex string is a valid ERC-55 mixed-case
* checksum address.
*
* @param possibleChecksum - The hex address to check.
* @returns True if the address is a checksum address.
*/
function isValidChecksumAddress(possibleChecksum) {
if (!isHexChecksumAddress(possibleChecksum)) {
return false;
}
return getChecksumAddress(possibleChecksum) === possibleChecksum;
}
/**
* Validate that the passed prefixed hex string is an all-lowercase
* hex address, or a valid mixed-case checksum address.
*
* @param possibleAddress - Input parameter to check against.
* @returns Whether or not the input is a valid hex address.
*/
function isValidHexAddress(possibleAddress) {
return isHexAddress(possibleAddress) || isValidChecksumAddress(possibleAddress);
}
/**
* Add the `0x`-prefix to a hexadecimal string. If the string already has the
* prefix, it is returned as-is.
*
* @param hexadecimal - The hexadecimal string to add the prefix to.
* @returns The prefixed hexadecimal string.
*/
function add0x(hexadecimal) {
if (hexadecimal.startsWith("0x")) {
return hexadecimal;
}
if (hexadecimal.startsWith("0X")) {
return `0x${hexadecimal.substring(2)}`;
}
return `0x${hexadecimal}`;
}
/**
* Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have
* the prefix, it is returned as-is.
*
* @param hexadecimal - The hexadecimal string to remove the prefix from.
* @returns The un-prefixed hexadecimal string.
*/
function remove0x(hexadecimal) {
if (hexadecimal.startsWith("0x") || hexadecimal.startsWith("0X")) {
return hexadecimal.substring(2);
}
return hexadecimal;
}
exports.add0x = add0x;
exports.assertIsHexString = assertIsHexString;
exports.assertIsStrictHexString = assertIsStrictHexString;
exports.getChecksumAddress = getChecksumAddress;
exports.isHexAddress = isHexAddress;
exports.isHexChecksumAddress = isHexChecksumAddress;
exports.isHexString = isHexString;
exports.isStrictHexString = isStrictHexString;
exports.isValidChecksumAddress = isValidChecksumAddress;
exports.isValidHexAddress = isValidHexAddress;
exports.remove0x = remove0x;