Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

@metamask/utils

Package Overview
Dependencies
Maintainers
9
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metamask/utils - npm Package Compare versions

Comparing version
11.4.1
to
11.4.2
+8
-1
CHANGELOG.md

@@ -10,2 +10,8 @@ # Changelog

## [11.4.2]
### Fixed
- Improve performance of `isValidChecksumAddress` and `isValidHexAddress` functions ([#248](https://github.com/MetaMask/utils/pull/248))
## [11.4.1]

@@ -425,3 +431,4 @@

[Unreleased]: https://github.com/MetaMask/utils/compare/v11.4.1...HEAD
[Unreleased]: https://github.com/MetaMask/utils/compare/v11.4.2...HEAD
[11.4.2]: https://github.com/MetaMask/utils/compare/v11.4.1...v11.4.2
[11.4.1]: https://github.com/MetaMask/utils/compare/v11.4.0...v11.4.1

@@ -428,0 +435,0 @@ [11.4.0]: https://github.com/MetaMask/utils/compare/v11.3.0...v11.4.0

+65
-23

@@ -6,3 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.remove0x = exports.add0x = exports.isValidChecksumAddress = exports.getChecksumAddress = exports.getChecksumAddressUnmemoized = exports.isValidHexAddress = exports.assertIsStrictHexString = exports.assertIsHexString = exports.isStrictHexString = exports.isHexString = exports.HexChecksumAddressStruct = exports.HexAddressStruct = exports.StrictHexStruct = exports.HexStruct = void 0;
exports.remove0x = exports.add0x = exports.isValidHexAddress = exports.isValidHexAddressUnmemoized = exports.isValidChecksumAddress = exports.isValidChecksumAddressUnmemoized = exports.getChecksumAddress = exports.getChecksumAddressUnmemoized = exports.assertIsStrictHexString = exports.assertIsHexString = exports.isHexChecksumAddress = exports.isHexAddress = exports.isStrictHexString = exports.isHexString = exports.HexChecksumAddressStruct = exports.HexAddressStruct = exports.StrictHexStruct = exports.HexStruct = void 0;
const superstruct_1 = require("@metamask/superstruct");

@@ -12,6 +12,13 @@ const sha3_1 = require("@noble/hashes/sha3");

const assert_1 = require("./assert.cjs");
exports.HexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^(?:0x)?[0-9a-f]+$/iu);
exports.StrictHexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-f]+$/iu);
exports.HexAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-f]{40}$/u);
exports.HexChecksumAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-fA-F]{40}$/u);
// 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;
exports.HexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), HEX_REGEX);
exports.StrictHexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), STRICT_HEX_REGEX);
exports.HexAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), HEX_ADDRESS_REGEX);
exports.HexChecksumAddressStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), HEX_CHECKSUM_ADDRESS_REGEX);
const isString = (value) => typeof value === 'string';
/**

@@ -24,3 +31,3 @@ * Check if a string is a valid hex string.

function isHexString(value) {
return (0, superstruct_1.is)(value, exports.HexStruct);
return isString(value) && HEX_REGEX.test(value);
}

@@ -36,6 +43,26 @@ exports.isHexString = isHexString;

function isStrictHexString(value) {
return (0, superstruct_1.is)(value, exports.StrictHexStruct);
return isString(value) && STRICT_HEX_REGEX.test(value);
}
exports.isStrictHexString = isStrictHexString;
/**
* 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);
}
exports.isHexAddress = isHexAddress;
/**
* 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);
}
exports.isHexChecksumAddress = isHexChecksumAddress;
/**
* Assert that a value is a valid hex string.

@@ -62,14 +89,2 @@ *

/**
* 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 ((0, superstruct_1.is)(possibleAddress, exports.HexAddressStruct) ||
isValidChecksumAddress(possibleAddress));
}
exports.isValidHexAddress = isValidHexAddress;
/**
* Encode a passed hex string as an ERC-55 mixed-case checksum address.

@@ -83,3 +98,3 @@ * This is the unmemoized version, primarily used for testing.

function getChecksumAddressUnmemoized(hexAddress) {
(0, assert_1.assert)((0, superstruct_1.is)(hexAddress, exports.HexChecksumAddressStruct), 'Invalid hex address.');
(0, assert_1.assert)(isHexChecksumAddress(hexAddress), 'Invalid hex address.');
const address = remove0x(hexAddress).toLowerCase();

@@ -117,4 +132,4 @@ const hashBytes = (0, sha3_1.keccak_256)(address);

*/
function isValidChecksumAddress(possibleChecksum) {
if (!(0, superstruct_1.is)(possibleChecksum, exports.HexChecksumAddressStruct)) {
function isValidChecksumAddressUnmemoized(possibleChecksum) {
if (!isHexChecksumAddress(possibleChecksum)) {
return false;

@@ -124,4 +139,31 @@ }

}
exports.isValidChecksumAddress = isValidChecksumAddress;
exports.isValidChecksumAddressUnmemoized = isValidChecksumAddressUnmemoized;
/**
* 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.
*/
exports.isValidChecksumAddress = (0, lodash_memoize_1.default)(isValidChecksumAddressUnmemoized);
/**
* 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 isValidHexAddressUnmemoized(possibleAddress) {
return (isHexAddress(possibleAddress) || (0, exports.isValidChecksumAddress)(possibleAddress));
}
exports.isValidHexAddressUnmemoized = isValidHexAddressUnmemoized;
/**
* 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.
*/
exports.isValidHexAddress = (0, lodash_memoize_1.default)(isValidHexAddressUnmemoized);
/**
* Add the `0x`-prefix to a hexadecimal string. If the string already has the

@@ -128,0 +170,0 @@ * prefix, it is returned as-is.

/// <reference types="lodash" />
import type { Struct } from "@metamask/superstruct";
import { type Struct } from "@metamask/superstruct";
export type Hex = `0x${string}`;

@@ -24,2 +24,16 @@ export declare const HexStruct: Struct<string, null>;

/**
* Check if a string is a valid hex address.
*
* @param value - The value to check.
* @returns Whether the value is a valid hex address.
*/
export declare function isHexAddress(value: unknown): value is Hex;
/**
* 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.
*/
export declare function isHexChecksumAddress(value: unknown): value is Hex;
/**
* Assert that a value is a valid hex string.

@@ -40,10 +54,2 @@ *

/**
* 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.
*/
export declare function isValidHexAddress(possibleAddress: Hex): boolean;
/**
* Encode a passed hex string as an ERC-55 mixed-case checksum address.

@@ -73,4 +79,28 @@ * This is the unmemoized version, primarily used for testing.

*/
export declare function isValidChecksumAddress(possibleChecksum: Hex): boolean;
export declare function isValidChecksumAddressUnmemoized(possibleChecksum: Hex): boolean;
/**
* 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.
*/
export declare const isValidChecksumAddress: typeof isValidChecksumAddressUnmemoized & import("lodash").MemoizedFunction;
/**
* 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.
*/
export declare function isValidHexAddressUnmemoized(possibleAddress: Hex): boolean;
/**
* 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.
*/
export declare const isValidHexAddress: typeof isValidHexAddressUnmemoized & import("lodash").MemoizedFunction;
/**
* Add the `0x`-prefix to a hexadecimal string. If the string already has the

@@ -77,0 +107,0 @@ * prefix, it is returned as-is.

/// <reference types="lodash" />
import type { Struct } from "@metamask/superstruct";
import { type Struct } from "@metamask/superstruct";
export type Hex = `0x${string}`;

@@ -24,2 +24,16 @@ export declare const HexStruct: Struct<string, null>;

/**
* Check if a string is a valid hex address.
*
* @param value - The value to check.
* @returns Whether the value is a valid hex address.
*/
export declare function isHexAddress(value: unknown): value is Hex;
/**
* 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.
*/
export declare function isHexChecksumAddress(value: unknown): value is Hex;
/**
* Assert that a value is a valid hex string.

@@ -40,10 +54,2 @@ *

/**
* 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.
*/
export declare function isValidHexAddress(possibleAddress: Hex): boolean;
/**
* Encode a passed hex string as an ERC-55 mixed-case checksum address.

@@ -73,4 +79,28 @@ * This is the unmemoized version, primarily used for testing.

*/
export declare function isValidChecksumAddress(possibleChecksum: Hex): boolean;
export declare function isValidChecksumAddressUnmemoized(possibleChecksum: Hex): boolean;
/**
* 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.
*/
export declare const isValidChecksumAddress: typeof isValidChecksumAddressUnmemoized & import("lodash").MemoizedFunction;
/**
* 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.
*/
export declare function isValidHexAddressUnmemoized(possibleAddress: Hex): boolean;
/**
* 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.
*/
export declare const isValidHexAddress: typeof isValidHexAddressUnmemoized & import("lodash").MemoizedFunction;
/**
* Add the `0x`-prefix to a hexadecimal string. If the string already has the

@@ -77,0 +107,0 @@ * prefix, it is returned as-is.

@@ -1,9 +0,16 @@

import { is, pattern, string } from "@metamask/superstruct";
import { pattern, string } from "@metamask/superstruct";
import { keccak_256 as keccak256 } from "@noble/hashes/sha3";
import memoize from "lodash.memoize";
import { assert } from "./assert.mjs";
export const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu);
export const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu);
export const HexAddressStruct = pattern(string(), /^0x[0-9a-f]{40}$/u);
export const HexChecksumAddressStruct = pattern(string(), /^0x[0-9a-fA-F]{40}$/u);
// 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;
export const HexStruct = pattern(string(), HEX_REGEX);
export const StrictHexStruct = pattern(string(), STRICT_HEX_REGEX);
export const HexAddressStruct = pattern(string(), HEX_ADDRESS_REGEX);
export const HexChecksumAddressStruct = pattern(string(), HEX_CHECKSUM_ADDRESS_REGEX);
const isString = (value) => typeof value === 'string';
/**

@@ -16,3 +23,3 @@ * Check if a string is a valid hex string.

export function isHexString(value) {
return is(value, HexStruct);
return isString(value) && HEX_REGEX.test(value);
}

@@ -27,5 +34,23 @@ /**

export function isStrictHexString(value) {
return is(value, StrictHexStruct);
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.
*/
export 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.
*/
export function isHexChecksumAddress(value) {
return isString(value) && HEX_CHECKSUM_ADDRESS_REGEX.test(value);
}
/**
* Assert that a value is a valid hex string.

@@ -50,13 +75,2 @@ *

/**
* 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.
*/
export function isValidHexAddress(possibleAddress) {
return (is(possibleAddress, HexAddressStruct) ||
isValidChecksumAddress(possibleAddress));
}
/**
* Encode a passed hex string as an ERC-55 mixed-case checksum address.

@@ -70,3 +84,3 @@ * This is the unmemoized version, primarily used for testing.

export function getChecksumAddressUnmemoized(hexAddress) {
assert(is(hexAddress, HexChecksumAddressStruct), 'Invalid hex address.');
assert(isHexChecksumAddress(hexAddress), 'Invalid hex address.');
const address = remove0x(hexAddress).toLowerCase();

@@ -103,4 +117,4 @@ const hashBytes = keccak256(address);

*/
export function isValidChecksumAddress(possibleChecksum) {
if (!is(possibleChecksum, HexChecksumAddressStruct)) {
export function isValidChecksumAddressUnmemoized(possibleChecksum) {
if (!isHexChecksumAddress(possibleChecksum)) {
return false;

@@ -111,2 +125,28 @@ }

/**
* 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.
*/
export const isValidChecksumAddress = memoize(isValidChecksumAddressUnmemoized);
/**
* 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.
*/
export function isValidHexAddressUnmemoized(possibleAddress) {
return (isHexAddress(possibleAddress) || isValidChecksumAddress(possibleAddress));
}
/**
* 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.
*/
export const isValidHexAddress = memoize(isValidHexAddressUnmemoized);
/**
* Add the `0x`-prefix to a hexadecimal string. If the string already has the

@@ -113,0 +153,0 @@ * prefix, it is returned as-is.

@@ -17,3 +17,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.remove0x = exports.add0x = exports.isValidChecksumAddress = exports.getChecksumAddress = exports.isValidHexAddress = exports.assertIsStrictHexString = exports.assertIsHexString = exports.isStrictHexString = exports.isHexString = exports.HexChecksumAddressStruct = exports.HexAddressStruct = exports.StrictHexStruct = exports.HexStruct = void 0;
exports.remove0x = exports.add0x = exports.isValidChecksumAddress = exports.getChecksumAddress = exports.isValidHexAddress = exports.assertIsStrictHexString = exports.assertIsHexString = exports.isHexChecksumAddress = exports.isHexAddress = exports.isStrictHexString = exports.isHexString = exports.HexChecksumAddressStruct = exports.HexAddressStruct = exports.StrictHexStruct = exports.HexStruct = void 0;
__exportStar(require("./assert.cjs"), exports);

@@ -35,2 +35,4 @@ __exportStar(require("./base64.cjs"), exports);

Object.defineProperty(exports, "isStrictHexString", { enumerable: true, get: function () { return hex_1.isStrictHexString; } });
Object.defineProperty(exports, "isHexAddress", { enumerable: true, get: function () { return hex_1.isHexAddress; } });
Object.defineProperty(exports, "isHexChecksumAddress", { enumerable: true, get: function () { return hex_1.isHexChecksumAddress; } });
Object.defineProperty(exports, "assertIsHexString", { enumerable: true, get: function () { return hex_1.assertIsHexString; } });

@@ -37,0 +39,0 @@ Object.defineProperty(exports, "assertIsStrictHexString", { enumerable: true, get: function () { return hex_1.assertIsStrictHexString; } });

@@ -11,3 +11,3 @@ export * from "./assert.cjs";

export type { Hex } from "./hex.cjs";
export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x, } from "./hex.cjs";
export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, isHexAddress, isHexChecksumAddress, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x, } from "./hex.cjs";
export * from "./json.cjs";

@@ -14,0 +14,0 @@ export * from "./keyring.cjs";

@@ -11,3 +11,3 @@ export * from "./assert.mjs";

export type { Hex } from "./hex.mjs";
export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x, } from "./hex.mjs";
export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, isHexAddress, isHexChecksumAddress, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x, } from "./hex.mjs";
export * from "./json.mjs";

@@ -14,0 +14,0 @@ export * from "./keyring.mjs";

@@ -10,3 +10,3 @@ export * from "./assert.mjs";

export * from "./errors.mjs";
export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x } from "./hex.mjs";
export { HexStruct, StrictHexStruct, HexAddressStruct, HexChecksumAddressStruct, isHexString, isStrictHexString, isHexAddress, isHexChecksumAddress, assertIsHexString, assertIsStrictHexString, isValidHexAddress, getChecksumAddress, isValidChecksumAddress, add0x, remove0x } from "./hex.mjs";
export * from "./json.mjs";

@@ -13,0 +13,0 @@ export * from "./keyring.mjs";

{
"name": "@metamask/utils",
"version": "11.4.1",
"version": "11.4.2",
"description": "Various JavaScript/TypeScript utilities of wide relevance to the MetaMask codebase",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/MetaMask/utils#readme",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet