UNPKG

@hashgraph/sdk

Version:
184 lines (164 loc) 5.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var EntityIdHelper = _interopRequireWildcard(require("../EntityIdHelper.cjs")); var HieroProto = _interopRequireWildcard(require("@hashgraph/proto")); var _EvmAddress = _interopRequireDefault(require("../EvmAddress.cjs")); var util = _interopRequireWildcard(require("../util.cjs")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } // SPDX-License-Identifier: Apache-2.0 /** * @typedef {import("long")} Long * @typedef {import("../client/Client.js").default<*, *>} Client */ /** * The ID for a crypto-currency token on Hedera. */ class TokenId { /** * @param {number | Long | import("../EntityIdHelper").IEntityId} props * @param {(number | Long)=} realm * @param {(number | Long)=} num */ constructor(props, realm, num) { const result = EntityIdHelper.constructor(props, realm, num); this.shard = result.shard; this.realm = result.realm; this.num = result.num; /** * @type {string | null} */ this._checksum = null; } /** * @param {string} text * @returns {TokenId} */ static fromString(text) { const result = EntityIdHelper.fromString(text); const id = new TokenId(result); id._checksum = result.checksum; return id; } /** * @internal * @param {HieroProto.proto.ITokenID} id * @returns {TokenId} */ static _fromProtobuf(id) { const tokenId = new TokenId(id.shardNum != null ? id.shardNum : 0, id.realmNum != null ? id.realmNum : 0, id.tokenNum != null ? id.tokenNum : 0); return tokenId; } /** * @returns {string | null} */ get checksum() { return this._checksum; } /** * @deprecated - Use `validateChecksum` instead * @param {Client} client */ validate(client) { console.warn("Deprecated: Use `validateChecksum` instead"); this.validateChecksum(client); } /** * @param {Client} client */ validateChecksum(client) { EntityIdHelper.validateChecksum(this.shard, this.realm, this.num, this._checksum, client); } /** * @param {Uint8Array} bytes * @returns {TokenId} */ static fromBytes(bytes) { return TokenId._fromProtobuf(HieroProto.proto.TokenID.decode(bytes)); } /** * @param {string} address * @deprecated - Use `fromEvmAddress` instead * @returns {TokenId} */ static fromSolidityAddress(address) { return new TokenId(...EntityIdHelper.fromSolidityAddress(address)); } /** * @param {number} shard * @param {number} realm * @param {string} address * @returns {TokenId} */ static fromEvmAddress(shard, realm, address) { const addressBytes = _EvmAddress.default.fromString(address).toBytes(); const isLongZero = util.isLongZeroAddress(addressBytes); if (!isLongZero) { throw new Error("TokenId.fromEvmAddress does not support non-long-zero addresses"); } const [shardLong, realmLong, tokenLong] = EntityIdHelper.fromEvmAddress(shard, realm, address); return new TokenId(shardLong, realmLong, tokenLong); } /** * @deprecated - Use `toEvmAddress` instead * @returns {string} solidity address */ toSolidityAddress() { return EntityIdHelper.toSolidityAddress([this.shard, this.realm, this.num]); } /** * @returns {string} */ toEvmAddress() { return EntityIdHelper.toEvmAddress(this.num); } /** * @internal * @returns {HieroProto.proto.ITokenID} */ _toProtobuf() { return { tokenNum: this.num, shardNum: this.shard, realmNum: this.realm }; } /** * @returns {string} */ toString() { return `${this.shard.toString()}.${this.realm.toString()}.${this.num.toString()}`; } /** * @param {Client} client * @returns {string} */ toStringWithChecksum(client) { return EntityIdHelper.toStringWithChecksum(this.toString(), client); } /** * @returns {Uint8Array} */ toBytes() { return HieroProto.proto.TokenID.encode(this._toProtobuf()).finish(); } /** * @returns {TokenId} */ clone() { const id = new TokenId(this); id._checksum = this._checksum; return id; } /** * @param {TokenId} other * @returns {number} */ compare(other) { return EntityIdHelper.compare([this.shard, this.realm, this.num], [other.shard, other.realm, other.num]); } } exports.default = TokenId;