UNPKG

@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

79 lines (78 loc) 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.randomAptosAddress = exports.shortenAptosAddress = exports.toAptosAddressSafe = exports.toAptosAddress = exports.aptosAddressValidator = exports.isAptosLongAddress = exports.aptosAddressLongRegExp = exports.isAptosAddress = exports.aptosAddressRegExp = void 0; const index_js_1 = require("../../../common/binary/index.js"); const address_js_1 = require("../../common/address.js"); const common_js_1 = require("../../common/common.js"); const domain_js_1 = require("../../union/enum/domain.js"); exports.aptosAddressRegExp = new RegExp(/^0x[a-fA-F0-9]{1,64}$/); /** * Checks Short AND Long formats of address with or without 0-pad * * @example 0x1 * @example 0xeeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b */ function isAptosAddress(address) { return exports.aptosAddressRegExp.test(address); } exports.isAptosAddress = isAptosAddress; exports.aptosAddressLongRegExp = new RegExp(/^0x[a-fA-F0-9]{64}$/); /** * Checks Long formats of address with or without 0-pad * * @example 0xeeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b */ function isAptosLongAddress(address) { return exports.aptosAddressLongRegExp.test(address); } exports.isAptosLongAddress = isAptosLongAddress; /** * Checks Long format of address with 0-pad */ exports.aptosAddressValidator = (0, common_js_1.createLayer1fulValidator)(domain_js_1.BlockchainLayer1Enum.APTOS, isAptosAddress); /** * Check and convert Aptos-compatible addresses * * @note it does normalization (0-pad and lowercase) * @example 0x1 * @example 0xeeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b * @see https://aptos.dev/concepts/accounts */ function toAptosAddress(address) { const safe = toAptosAddressSafe(address); if (!safe) throw new address_js_1.InvalidAddressError(domain_js_1.BlockchainLayer1Enum.APTOS, address); return safe; } exports.toAptosAddress = toAptosAddress; function toAptosAddressSafe(address) { if (isAptosAddress(address)) { const lowercaseAddress = address.toLowerCase(); const addressWithoutPrefix = lowercaseAddress.startsWith("0x") ? lowercaseAddress.slice(2) : lowercaseAddress; const addressWithPadding = addressWithoutPrefix.padStart(64, "0"); return `0x${addressWithPadding}`; } return undefined; } exports.toAptosAddressSafe = toAptosAddressSafe; /** * Removes 0-pad from address */ function shortenAptosAddress(address) { if (address.startsWith("0x")) { // Remove the '0x' prefix and then strip leading zeros let trimmedAddress = address.slice(2).replace(/^0+/, ""); return ("0x" + (trimmedAddress || "0")); } return address; } exports.shortenAptosAddress = shortenAptosAddress; /** * Generates random Aptos address * * @example 0xeeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b */ function randomAptosAddress() { return toAptosAddress((0, index_js_1.randomBinary)(32)); } exports.randomAptosAddress = randomAptosAddress;