@hiero-ledger/sdk
Version:
185 lines (165 loc) • 5.31 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 _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
// 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;