UNPKG

@stable-io/cctp-sdk-definitions

Version:

Definitions for the CCTP SDK

70 lines 2.75 kB
// Copyright (c) 2025 Stable Technologies Inc // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. import { encoding, isUint8Array } from "@stable-io/utils"; import { platformOf, addressFormatOf } from "./constants/chains/platforms.js"; export class UniversalAddress { static byteSize = 32; static zeroAddress = new UniversalAddress(new Uint8Array(UniversalAddress.byteSize)); static decodeFunc = { hex: encoding.hex.decode, base58: encoding.base58.decode, bech32: encoding.bech32.decode, }; address; constructor(address, platform) { this.address = (() => { if (typeof address === "string") { const [size, format] = platform ? addressFormatOf(platform) : [32, "hex"]; //assert(size <= UniversalAddress.byteSize); const decoded = UniversalAddress.decodeFunc[format](address); if (decoded.length !== size) throw new Error(`${address} has invalid length of ${decoded.length} for ${platform}`); return decoded.length < UniversalAddress.byteSize ? encoding.bytes.zpad(decoded, UniversalAddress.byteSize) : decoded; } if (isUint8Array(address)) { if (address.length !== UniversalAddress.byteSize) throw new Error(`UniversalAddresses must be ${UniversalAddress.byteSize} bytes long`); return address; } return address.address; })(); } toPlatformAddress(chainOrPlatform) { return platformAddress(chainOrPlatform, this); } unwrap() { return this.address; } toString() { return encoding.hex.encode(this.address, true); } toUint8Array() { return this.address; } toUniversalAddress() { return this; } toJSON() { return this.toString(); } equals(other) { return encoding.bytes.equals(this.address, other.address); } } UniversalAddress; const platformAddrFactory = new Map(); export function registerPlatformAddress(platform, ctr) { if (platformAddrFactory.has(platform)) throw new Error(`Address type for platform ${platform} has already been registered`); platformAddrFactory.set(platform, ctr); } export function platformAddress(domainOrPlatform, address) { const platform = platformOf.get(domainOrPlatform) ?? domainOrPlatform; const addrCtr = platformAddrFactory.get(platform); return new addrCtr(address); } //# sourceMappingURL=address.js.map