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

64 lines (63 loc) 2.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.randomAptosFunctionName = exports.randomAptosFunctionAddress = exports.InvalidAptosFunctionAddressError = exports.parseAptosFunctionAddressSafe = exports.parseAptosFunctionAddress = exports.toAptosFunctionAddressSafe = exports.toAptosFunctionAddress = exports.isAptosFunctionAddress = exports.aptosFunctionAddressRegexp = void 0; const utils_1 = require("@rarible/utils"); const index_js_1 = require("../address/index.js"); const index_js_2 = require("../module/index.js"); exports.aptosFunctionAddressRegexp = new RegExp(/^0x[a-fA-F0-9]{1,64}::[a-zA-Z_][a-zA-Z0-9_]*::[a-zA-Z_][a-zA-Z0-9_]*$/); function isAptosFunctionAddress(raw) { return exports.aptosFunctionAddressRegexp.test(raw); } exports.isAptosFunctionAddress = isAptosFunctionAddress; function toAptosFunctionAddress(raw) { const parsed = toAptosFunctionAddressSafe(raw); if (!parsed) throw new InvalidAptosFunctionAddressError(raw); return parsed; } exports.toAptosFunctionAddress = toAptosFunctionAddress; function toAptosFunctionAddressSafe(raw) { if (isAptosFunctionAddress(raw)) return raw; return undefined; } exports.toAptosFunctionAddressSafe = toAptosFunctionAddressSafe; function parseAptosFunctionAddress(str) { const parsed = parseAptosFunctionAddressSafe(str); if (!parsed) throw new InvalidAptosFunctionAddressError(str); return parsed; } exports.parseAptosFunctionAddress = parseAptosFunctionAddress; function parseAptosFunctionAddressSafe(str) { const parsed = toAptosFunctionAddressSafe(str); if (parsed) { const [resource, moduleName, functionName] = parsed.split("::"); return { resource: (0, index_js_1.toAptosAddress)(resource), moduleName, functionName, }; } return undefined; } exports.parseAptosFunctionAddressSafe = parseAptosFunctionAddressSafe; class InvalidAptosFunctionAddressError extends utils_1.CustomError { constructor(str) { super(`Invalid Aptos function address - ${str}`); } } exports.InvalidAptosFunctionAddressError = InvalidAptosFunctionAddressError; /** * Generates random Aptos function address * * @example 0xeeff357ea5c1a4e7bc11b2b17ff2dc2dcca69750bfef1e1ebcaccf8c8018175b::coin::swap */ function randomAptosFunctionAddress() { return `${(0, index_js_1.randomAptosAddress)()}::${(0, index_js_2.randomAptosModuleName)()}::${randomAptosFunctionName()}`; } exports.randomAptosFunctionAddress = randomAptosFunctionAddress; function randomAptosFunctionName() { return (0, index_js_2.randomAptosModuleName)(); } exports.randomAptosFunctionName = randomAptosFunctionName;