// SPDX-License-Identifier: Apache-2.0 import AccountId from "../account/AccountId.js"; import Timestamp from "../Timestamp.js"; import * as HieroProto from "@hiero-ledger/proto"; import Long from "long"; import CACHE from "../Cache.js"; /** * @typedef {import("../channel/Channel.js").default} Channel * @typedef {import("../channel/MirrorChannel.js").default} MirrorChannel * @typedef {import("../client/Client.js").default} Client * @typedef {import("./TransactionReceipt.js").default} TransactionReceipt * @typedef {import("./TransactionRecord.js").default} TransactionRecord */ const TRANSACTION_ID_PATTERN = /^([^@]+)@(\d+)\.(\d{1,9})(\?scheduled)?(?:\/(\d+))?$/; /** * The client-generated ID for a transaction. * * This is used for retrieving receipts and records for a transaction, for appending to a file * right after creating it, for instantiating a smart contract with bytecode in a file just created, * and internally by the network for detecting when duplicate transactions are submitted. */ export default class TransactionId { /** * Don't use this method directly. * Use `TransactionId.[generate|withNonce|withValidStart]()` instead. * * @param {?AccountId} accountId * @param {?Timestamp} validStart * @param {?boolean} scheduled * @param {?Long | number} nonce */ constructor(accountId, validStart, scheduled = false, nonce = null) { /** * The Account ID that paid for this transaction. * * @readonly */ this.accountId = accountId; /** * The time from when this transaction is valid. * * When a transaction is submitted there is additionally a validDuration (defaults to 120s) * and together they define a time window that a transaction may be processed in. * * @readonly */ this.validStart = validStart; this.scheduled = scheduled; this.nonce = null; if (nonce != null && nonce != 0) { this.setNonce(nonce); } Object.seal(this); } /** * @param {Long | number} nonce * @returns {TransactionId} */ setNonce(nonce) { this.nonce = typeof nonce === "number" ? Long.fromNumber(nonce) : nonce; return this; } /** * @param {AccountId} accountId * @param {Timestamp} validStart * @returns {TransactionId} */ static withValidStart(accountId, validStart) { return new TransactionId(accountId, validStart); } /** * Generates a new transaction ID for the given account ID. * * Note that transaction IDs are made of the valid start of the transaction and the account * that will be charged the transaction fees for the transaction. * * @param {AccountId | string} id * @returns {TransactionId} */ static generate(id) { return new TransactionId( typeof id === "string" ? AccountId.fromString(id) : new AccountId(id), Timestamp.generate(), ); } /** * Parses `{account}@{seconds}.{nanos}[?scheduled][/{nonce}]`. * * @param {string} wholeId * @returns {TransactionId} * @throws {Error} if the string does not match the format above */ static fromString(wholeId) { const match = TRANSACTION_ID_PATTERN.exec(wholeId); if (match == null) { throw new Error( `invalid transaction ID "${wholeId}", expected {account}@{seconds}.{nanos}[?scheduled][/{nonce}]`, ); } const [, account, seconds, nanos, scheduled, nonce] = match; let accountId; try { accountId = AccountId.fromString(account); } catch (error) { throw new Error( `invalid transaction ID "${wholeId}": invalid account ID "${account}"`, { cause: error }, ); } return new TransactionId( accountId, new Timestamp(Long.fromString(seconds), Long.fromString(nanos)), scheduled != null, nonce != null ? Long.fromString(nonce) : null, ); } /** * @param {boolean} scheduled * @returns {this} */ setScheduled(scheduled) { this.scheduled = scheduled; return this; } /** * @returns {string} */ toString() { if (this.accountId != null && this.validStart != null) { const zeroPaddedNanos = String(this.validStart.nanos).padStart( 9, "0", ); const nonce = this.nonce != null ? "/".concat(this.nonce.toString()) : ""; const scheduled = this.scheduled ? "?scheduled" : ""; return `${this.accountId.toString()}@${this.validStart.seconds.toString()}.${zeroPaddedNanos}${scheduled}${nonce}`; } else { throw new Error("neither `accountId` nor `validStart` are set"); } } /** * @internal * @param {HieroProto.proto.ITransactionID} id * @returns {TransactionId} */ static _fromProtobuf(id) { if (id.accountID != null && id.transactionValidStart != null) { return new TransactionId( AccountId._fromProtobuf(id.accountID), Timestamp._fromProtobuf(id.transactionValidStart), id.scheduled != null ? id.scheduled : undefined, id.nonce != null ? id.nonce : undefined, ); } else { throw new Error( "Neither `nonce` or `accountID` and `transactionValidStart` are set", ); } } /** * @internal * @returns {HieroProto.proto.ITransactionID} */ _toProtobuf() { return { accountID: this.accountId != null ? this.accountId._toProtobuf() : null, transactionValidStart: this.validStart != null ? this.validStart._toProtobuf() : null, scheduled: this.scheduled, nonce: this.nonce != null ? this.nonce.toInt() : null, }; } /** * @param {Uint8Array} bytes * @returns {TransactionId} */ static fromBytes(bytes) { return TransactionId._fromProtobuf( HieroProto.proto.TransactionID.decode(bytes), ); } /** * @returns {Uint8Array} */ toBytes() { return HieroProto.proto.TransactionID.encode( this._toProtobuf(), ).finish(); } /** * @returns {TransactionId} */ clone() { return new TransactionId( this.accountId, this.validStart, this.scheduled, this.nonce, ); } /** * @param {TransactionId} other * @returns {number} */ compare(other) { const comparison = /** @type {AccountId} */ (this.accountId).compare( /** @type {AccountId} */ (other.accountId), ); if (comparison != 0) { return comparison; } return /** @type {Timestamp} */ (this.validStart).compare( /** @type {Timestamp} */ (other.validStart), ); } /** * @param {Client} client * @returns {Promise} */ async getReceipt(client) { return CACHE.transactionReceiptQueryConstructor() .setTransactionId(this) .execute(client); } /** * @param {Client} client * @returns {Promise} */ async getRecord(client) { await this.getReceipt(client); return CACHE.transactionRecordQueryConstructor() .setTransactionId(this) .execute(client); } }