UNPKG

@zlattice/lattice-js

Version:

Lattice blockchain TypeScript SDK with dual module support (CJS + ESM)

50 lines 1.71 kB
import { Base58Impl } from "../../utils/base58.js"; import { ADDRESS_TITLE, ADDRESS_VERSION, HEX_PREFIX } from "../constants.js"; // This is a address class. Can be converted to eth address or zltc address class Address { constructor(address) { this.address = address; } /** * @description convert eth address to zltc address, * @returns {string} zltc address */ toZLTC() { if (!this.address) { return HEX_PREFIX; } if (this.address.startsWith(ADDRESS_TITLE)) { return this.address; } if (this.address.startsWith(HEX_PREFIX)) { const base58 = new Base58Impl(); const converted = base58.checkEncode(Buffer.from(this.address.substring(2), "hex"), ADDRESS_VERSION); return `${ADDRESS_TITLE}_${converted}`; } return this.address; } /** * @description convert zltc address to eth address, * @returns {string} eth address */ toETH() { if (!this.address) { return ADDRESS_TITLE; } const splitArr = this.address.split("_"); if (splitArr.length !== 2) { throw new Error(`invalid address ${this.address}`); } if (splitArr[0] !== ADDRESS_TITLE) { throw new Error(`invalid address ${this.address}`); } const base58 = new Base58Impl(); const { result: dec, version } = base58.checkDecode(splitArr[1]); if (version !== ADDRESS_VERSION) { throw new Error(`invalid address ${this.address}`); } return `${HEX_PREFIX}${dec.toString("hex")}`; } } export { Address }; //# sourceMappingURL=address.js.map