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

31 lines (30 loc) 1.38 kB
import { InvalidAddressError } from "../../common/address.js"; import { createLayer1fulValidator } from "../../common/common.js"; import { BlockchainLayer1Enum } from "../../union/enum/domain.js"; // Tezos contract addresses usually start with KT1 and are Base58 encoded. const tezosContractAddressRegExp = new RegExp(/^KT1[1-9A-HJ-NP-Za-km-z]{33}/); export function isTezosContractAddress(address) { return tezosContractAddressRegExp.test(address); } export const tezosContractAddressValidator = createLayer1fulValidator(BlockchainLayer1Enum.TEZOS, isTezosContractAddress); export function toTezosContractAddress(address) { const safe = toTezosContractAddressSafe(address); if (!safe) throw new InvalidAddressError(BlockchainLayer1Enum.TEZOS, address); return safe; } export function toTezosContractAddressSafe(address) { if (isTezosContractAddress(address)) return address; return undefined; } export function randomTezosContractAddress() { const base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; let address = "KT1"; for (let i = 0; i < 33; i++) { // The length of the rest of the address should be 33 characters const randomIndex = Math.floor(Math.random() * base58Chars.length); address += base58Chars[randomIndex]; } return toTezosContractAddress(address); }