@rarible/types
Version:
`@rarible/types` is a TypeScript library that provides type definitions and interfaces for the Rarible ecosystem. This package is designed to facilitate seamless integration with Rarible's APIs and services, ensuring type safety and improved developer exp
37 lines (36 loc) • 1.32 kB
JavaScript
import { randomBinary } from "../../../common/binary/index.js";
import { InvalidAddressError } from "../../common/address.js";
import { createLayer1fulValidator } from "../../common/common.js";
import { BlockchainLayer1Enum } from "../../union/enum/index.js";
export const evmAddressRegExp = new RegExp(/^0x[a-fA-F0-9]{40}$/);
export function isEVMAddress(raw) {
return evmAddressRegExp.test(raw);
}
export const evmAddressValidator = createLayer1fulValidator(BlockchainLayer1Enum.ETHEREUM, isEVMAddress);
/**
* Check and convert EVM-compatible addresses
* @note it also convert it to lowercase
*/
export function toEVMAddress(value) {
const parsed = toEVMAddressSafe(value);
if (!parsed)
throw new InvalidAddressError(BlockchainLayer1Enum.ETHEREUM, value);
return parsed;
}
function normalizeEVMAddress(str) {
return str.toLowerCase();
}
/**
* Check and convert EVM-compatible addresses
* @deprecated please use toEVMAddress instead
*/
export const toEVMAddressStrict = toEVMAddress;
export function toEVMAddressSafe(raw) {
if (isEVMAddress(raw))
return normalizeEVMAddress(raw);
return undefined;
}
export const EVM_ZERO_ADDRESS = toEVMAddress("0x0000000000000000000000000000000000000000");
export function randomEVMAddress() {
return toEVMAddress(randomBinary(20));
}