@hiero-ledger/sdk
Version:
184 lines (164 loc) • 5.14 kB
JavaScript
"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
*/
/**
* Unique identifier for a topic (used by the consensus service).
*/
class TopicId {
/**
* @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 {TopicId}
*/
static fromString(text) {
const result = EntityIdHelper.fromString(text);
const id = new TopicId(result);
id._checksum = result.checksum;
return id;
}
/**
* @internal
* @param {HieroProto.proto.ITopicID} id
* @returns {TopicId}
*/
static _fromProtobuf(id) {
const topicId = new TopicId(id.shardNum != null ? id.shardNum : 0, id.realmNum != null ? id.realmNum : 0, id.topicNum != null ? id.topicNum : 0);
return topicId;
}
/**
* @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 {TopicId}
*/
static fromBytes(bytes) {
return TopicId._fromProtobuf(HieroProto.proto.TopicID.decode(bytes));
}
/**
* @param {string} address
* @deprecated - Use `fromEvmAddress` instead
* @returns {TopicId}
*/
static fromSolidityAddress(address) {
const [shard, realm, topic] = EntityIdHelper.fromSolidityAddress(address);
return new TopicId(shard, realm, topic);
}
/**
* @param {number} shard
* @param {number} realm
* @param {string} address
* @returns {TopicId}
*/
static fromEvmAddress(shard, realm, address) {
const addressBytes = _EvmAddress.default.fromString(address).toBytes();
const isLongZero = util.isLongZeroAddress(addressBytes);
if (!isLongZero) {
throw new Error("TopicId.fromEvmAddress does not support non-long-zero addresses");
}
const [shardLong, realmLong, topicLong] = EntityIdHelper.fromEvmAddress(shard, realm, address);
return new TopicId(shardLong, realmLong, topicLong);
}
/**
* @deprecated - Use `toEvmAddress` instead
* @returns {string}
*/
toSolidityAddress() {
return EntityIdHelper.toSolidityAddress([this.shard, this.realm, this.num]);
}
/**
* @returns {string} EVM-compatible address representation of the entity
*/
toEvmAddress() {
return EntityIdHelper.toEvmAddress(this.num);
}
/**
* @returns {HieroProto.proto.ITopicID}
*/
_toProtobuf() {
return {
topicNum: 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.TopicID.encode(this._toProtobuf()).finish();
}
/**
* @returns {TopicId}
*/
clone() {
const id = new TopicId(this);
id._checksum = this._checksum;
return id;
}
/**
* @param {TopicId} other
* @returns {number}
*/
compare(other) {
return EntityIdHelper.compare([this.shard, this.realm, this.num], [other.shard, other.realm, other.num]);
}
}
exports.default = TopicId;