UNPKG

@hiero-ledger/sdk

Version:
135 lines (123 loc) 4.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _Transaction = _interopRequireWildcard(require("./transaction/Transaction.cjs")); var _util = require("./util.cjs"); 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("@hashgraph/proto").proto.ITransaction} HieroProto.proto.ITransaction * @typedef {import("@hashgraph/proto").proto.Transaction} HieroProto.proto.Transaction * @typedef {import("@hashgraph/proto").proto.ISignedTransaction} HieroProto.proto.ISignedTransaction * @typedef {import("@hashgraph/proto").proto.SignedTransaction} HieroProto.proto.SignedTransaction * @typedef {import("@hashgraph/proto").proto.IUtilPrngTransactionBody } HieroProto.proto.IUtilPrngTransactionBody * @typedef {import("@hashgraph/proto").proto.UtilPrngTransactionBody} HieroProto.proto.UtilPrngTransactionBody * @typedef {import("@hashgraph/proto").proto.ITransactionResponse} HieroProto.proto.TransactionResponse * @typedef {import("@hashgraph/proto").proto.TransactionBody} HieroProto.proto.TransactionBody * @typedef {import("@hashgraph/proto").proto.ITransactionBody} HieroProto.proto.ITransactionBody * @typedef {import("./account/AccountId.js").default} AccountId * @typedef {import("./transaction/TransactionId.js").default} TransactionId */ /** * @typedef {import("./client/Client.js").default<*, *>} Client * @typedef {import("./channel/Channel.js").default} Channel */ /** * Gets a pseudorandom 32-bit number. Not cryptographically secure. See HIP-351 https://hips.hedera.com/hip/hip-351 */ class PrngTransaction extends _Transaction.default { /** * @param {object} props * @param {?number } [props.range] */ constructor(props = {}) { super(); /** * @private * @type {?number} */ this._range = null; if (props.range != null) { this.setRange(props.range); } } /** * @param {number} newRange * @returns {this} */ setRange(newRange) { this._range = newRange; return this; } get range() { return this._range; } /** * @param {Client} client */ _validateChecksums(client) { if (this._range != null && (0, _util.isNumber)(this._range)) { this._validateChecksums(client); } } /** * @override * @internal * @param {Channel} channel * @param {HieroProto.proto.ITransaction} request * @returns {Promise<HieroProto.proto.TransactionResponse>} */ _execute(channel, request) { return channel.util.prng(request); } /** * @internal * @param {HieroProto.proto.ITransaction[]} transactions * @param {HieroProto.proto.ISignedTransaction[]} signedTransactions * @param {TransactionId[]} transactionIds * @param {AccountId[]} nodeIds * @param {HieroProto.proto.ITransactionBody[]} bodies * @returns {PrngTransaction} */ static _fromProtobuf(transactions, signedTransactions, transactionIds, nodeIds, bodies) { const body = /** @type {HieroProto.proto.ITransactionBody} */ bodies[0]; const transactionRange = /** @type {HieroProto.proto.IUtilPrngTransactionBody} */ body.utilPrng; return _Transaction.default._fromProtobufTransactions(new PrngTransaction({ range: transactionRange.range }), transactions, signedTransactions, transactionIds, nodeIds, bodies); } /** * @override * @protected * @returns {NonNullable<HieroProto.proto.TransactionBody["data"]>} */ _getTransactionDataCase() { return "utilPrng"; } /** * @override * @protected * @returns {HieroProto.proto.IUtilPrngTransactionBody} */ _makeTransactionData() { return { range: this.range }; } /** * @returns {string} */ _getLogId() { const timestamp = /** @type {import("./Timestamp.js").default} */ this._transactionIds.current.validStart; return `RandomGenerate:${timestamp.toString()}`; } } exports.default = PrngTransaction; _Transaction.TRANSACTION_REGISTRY.set("utilPrng", // eslint-disable-next-line @typescript-eslint/unbound-method PrngTransaction._fromProtobuf);