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

35 lines (34 loc) 1.64 kB
import { InvalidAddressError } from "../../common/address.js"; import { createLayer1fulValidator } from "../../common/common.js"; import { BlockchainLayer1Enum } from "../../union/enum/domain.js"; // Tezos addresses start with tz1, tz2, tz3 (for user accounts) and are Base58 encoded, exactly 36 characters long. const tezosAddressRegExp = new RegExp(/^tz[1-3][1-9A-HJ-NP-Za-km-z]{33}/); export function isTezosAddress(address) { return tezosAddressRegExp.test(address); } export const tezosAddressValidator = createLayer1fulValidator(BlockchainLayer1Enum.TEZOS, isTezosAddress); export function toTezosAddress(address) { const safe = toTezosAddressSafe(address); if (!safe) throw new InvalidAddressError(BlockchainLayer1Enum.TEZOS, address); return safe; } export function toTezosAddressSafe(address) { if (isTezosAddress(address)) return address; return undefined; } // Example of a Tezos zero address - this is just a placeholder // @note TEZOS doesn't really have a concept of ZERO address export const TEZOS_ZERO_ADDRESS = toTezosAddress("tz1ZZZZZZZZZZZZZZZZZZZZZZZZZZZZNkiRg"); export function randomTezosAddress() { const prefixOptions = ["tz1", "tz2", "tz3"]; const base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; let address = prefixOptions[Math.floor(Math.random() * prefixOptions.length)]; // Ensuring the total length is exactly 36 characters for (let i = 0; i < 33; i++) { const randomIndex = Math.floor(Math.random() * base58Chars.length); address += base58Chars[randomIndex]; } return toTezosAddress(address); }