@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
49 lines (48 loc) • 2.13 kB
JavaScript
import { InvalidContractAddressError } from "../../common/contract-address.js";
import { createLayer1fulValidator } from "../../common/common.js";
import { BlockchainLayer1Enum } from "../../union/enum/domain.js";
// example: A.0x01658d9b94068f3c.CommonNFT
export const flowContractRegExp = new RegExp(/^A\.0*x*[0-9a-f]{16}\.[0-9A-Za-z_]{3,}/);
export function isFlowContractAddress(raw) {
return flowContractRegExp.test(raw);
}
export const flowContractAddressValidator = createLayer1fulValidator(BlockchainLayer1Enum.FLOW, isFlowContractAddress);
export function toFlowContractAddress(value) {
const safe = toFlowContractAddressSafe(value);
if (!safe)
throw new InvalidContractAddressError(BlockchainLayer1Enum.FLOW, value);
return safe;
}
export function toFlowContractAddressSafe(raw) {
if (isFlowContractAddress(raw))
return raw;
return undefined;
}
export function randomFlowContractAddress(contractName = generateRandomContractName()) {
// Function to generate a random hexadecimal character
function getRandomHexChar() {
const hexChars = "0123456789abcdef";
return hexChars[Math.floor(Math.random() * hexChars.length)];
}
// Generate a 16-character hexadecimal string
let hexString = "";
for (let i = 0; i < 16; i++) {
hexString += getRandomHexChar();
}
// Construct the full contract address
return toFlowContractAddress(`A.0x${hexString}.${contractName}`);
}
function generateRandomContractName() {
// Function to generate a random alphanumeric character
function getRandomAlphaNumChar() {
const alphaNumChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
return alphaNumChars[Math.floor(Math.random() * alphaNumChars.length)];
}
// Generate a random contract name of a variable length between 3 and 10 characters
let contractName = "";
const contractNameLength = Math.floor(Math.random() * 8) + 3; // Length between 3 and 10
for (let i = 0; i < contractNameLength; i++) {
contractName += getRandomAlphaNumChar();
}
return contractName;
}