UNPKG

@hiero-ledger/sdk

Version:
376 lines (335 loc) 11 kB
// SPDX-License-Identifier: Apache-2.0 import Transaction, { TRANSACTION_REGISTRY, } from "../transaction/Transaction.js"; import Timestamp from "../Timestamp.js"; import * as utf8 from "../encoding/utf8.js"; import FileId from "./FileId.js"; import Key from "../Key.js"; import KeyList from "../KeyList.js"; /** * @namespace proto * @typedef {import("@hashgraph/proto").proto.ITransaction} HieroProto.proto.ITransaction * @typedef {import("@hashgraph/proto").proto.ISignedTransaction} HieroProto.proto.ISignedTransaction * @typedef {import("@hashgraph/proto").proto.TransactionBody} HieroProto.proto.TransactionBody * @typedef {import("@hashgraph/proto").proto.ITransactionBody} HieroProto.proto.ITransactionBody * @typedef {import("@hashgraph/proto").proto.ITransactionResponse} HieroProto.proto.ITransactionResponse * @typedef {import("@hashgraph/proto").proto.IFileUpdateTransactionBody} HieroProto.proto.IFileUpdateTransactionBody */ /** * @typedef {import("../channel/Channel.js").default} Channel * @typedef {import("../client/Client.js").default<*, *>} Client * @typedef {import("../account/AccountId.js").default} AccountId * @typedef {import("../transaction/TransactionId.js").default} TransactionId */ /** * Update a new Hedera™ crypto-currency file. */ export default class FileUpdateTransaction extends Transaction { /** * @param {object} props * @param {FileId | string} [props.fileId] * @param {Key[] | KeyList} [props.keys] * @param {Timestamp | Date} [props.expirationTime] * @param {Uint8Array | string} [props.contents] * @param {?string} [props.fileMemo] */ constructor(props = {}) { super(); /** * @private * @type {?FileId} */ this._fileId = null; /** * @private * @type {?Key[]} */ this._keys = null; /** * @private * @type {?Timestamp} */ this._expirationTime = null; /** * @private * @type {?Uint8Array} */ this._contents = null; /** * @private * @type {?string} */ this._fileMemo = null; if (props.fileId != null) { this.setFileId(props.fileId); } if (props.keys != null) { this.setKeys(props.keys); } if (props.expirationTime != null) { this.setExpirationTime(props.expirationTime); } if (props.contents != null) { this.setContents(props.contents); } if (props.fileMemo && props.fileMemo != null) { this.setFileMemo(props.fileMemo); } } /** * @internal * @param {HieroProto.proto.ITransaction[]} transactions * @param {HieroProto.proto.ISignedTransaction[]} signedTransactions * @param {TransactionId[]} transactionIds * @param {AccountId[]} nodeIds * @param {HieroProto.proto.ITransactionBody[]} bodies * @returns {FileUpdateTransaction} */ static _fromProtobuf( transactions, signedTransactions, transactionIds, nodeIds, bodies, ) { const body = bodies[0]; const update = /** @type {HieroProto.proto.IFileUpdateTransactionBody} */ ( body.fileUpdate ); return Transaction._fromProtobufTransactions( new FileUpdateTransaction({ fileId: update.fileID != null ? FileId._fromProtobuf(update.fileID) : undefined, keys: update.keys != null ? update.keys.keys != null ? update.keys.keys.map((key) => Key._fromProtobufKey(key), ) : undefined : undefined, expirationTime: update.expirationTime != null ? Timestamp._fromProtobuf(update.expirationTime) : undefined, contents: update.contents != null ? update.contents : undefined, fileMemo: update.memo != null ? Object.hasOwn(update.memo, "value") ? update.memo.value : undefined : undefined, }), transactions, signedTransactions, transactionIds, nodeIds, bodies, ); } /** * @returns {?FileId} */ get fileId() { return this._fileId; } /** * Set the keys which must sign any transactions modifying this file. Required. * * All keys must sign to modify the file's contents or keys. No key is required * to sign for extending the expiration time (except the one for the operator account * paying for the transaction). Only one key must sign to delete the file, however. * * To require more than one key to sign to delete a file, add them to a * KeyList and pass that here. * * The network currently requires a file to have at least one key (or key list or threshold key) * but this requirement may be lifted in the future. * * @param {FileId | string} fileId * @returns {this} */ setFileId(fileId) { this._requireNotFrozen(); this._fileId = typeof fileId === "string" ? FileId.fromString(fileId) : fileId.clone(); return this; } /** * @returns {?Key[]} */ get keys() { return this._keys; } /** * Set the keys which must sign any transactions modifying this file. Required. * * All keys must sign to modify the file's contents or keys. No key is required * to sign for extending the expiration time (except the one for the operator account * paying for the transaction). Only one key must sign to delete the file, however. * * To require more than one key to sign to delete a file, add them to a * KeyList and pass that here. * * The network currently requires a file to have at least one key (or key list or threshold key) * but this requirement may be lifted in the future. * * @param {Key[] | KeyList} keys * @returns {this} */ setKeys(keys) { this._requireNotFrozen(); if (keys instanceof KeyList && keys.threshold != null) { throw new Error("Cannot set threshold key as file key"); } this._keys = keys instanceof KeyList ? keys.toArray() : keys; return this; } /** * @returns {?Timestamp} */ get expirationTime() { return this._expirationTime; } /** * Set the instant at which this file will expire, after which its contents will no longer be * available. * * Defaults to 1/4 of a Julian year from the instant FileUpdateTransaction * was invoked. * * May be extended using FileUpdateTransaction#setExpirationTime(Timestamp). * * @param {Timestamp | Date} expirationTime * @returns {this} */ setExpirationTime(expirationTime) { this._requireNotFrozen(); this._expirationTime = expirationTime instanceof Timestamp ? expirationTime : Timestamp.fromDate(expirationTime); return this; } /** * @returns {?Uint8Array} */ get contents() { return this._contents; } /** * Set the given byte array as the file's contents. * * This may be omitted to update an empty file. * * Note that total size for a given transaction is limited to 6KiB (as of March 2020) by the * network; if you exceed this you may receive a HederaPreCheckStatusException * with Status#TransactionOversize. * * In this case, you will need to break the data into chunks of less than ~6KiB and execute this * transaction with the first chunk and then use FileAppendTransaction with * FileAppendTransaction#setContents(Uint8Array) for the remaining chunks. * * @param {Uint8Array | string} contents * @returns {this} */ setContents(contents) { this._requireNotFrozen(); this._contents = contents instanceof Uint8Array ? contents : utf8.encode(contents); return this; } /** * @returns {?string} */ get fileMemo() { return this._fileMemo; } /** * @param {string} memo * @returns {this} */ setFileMemo(memo) { this._requireNotFrozen(); this._fileMemo = memo; return this; } /** * @returns {this} */ clearFileMemo() { this._requireNotFrozen(); this._fileMemo = null; return this; } /** * @param {Client} client */ _validateChecksums(client) { if (this._fileId != null) { this._fileId.validateChecksum(client); } } /** * @override * @internal * @param {Channel} channel * @param {HieroProto.proto.ITransaction} request * @returns {Promise<HieroProto.proto.ITransactionResponse>} */ _execute(channel, request) { return channel.file.updateFile(request); } /** * @override * @protected * @returns {NonNullable<HieroProto.proto.TransactionBody["data"]>} */ _getTransactionDataCase() { return "fileUpdate"; } /** * @override * @protected * @returns {HieroProto.proto.IFileUpdateTransactionBody} */ _makeTransactionData() { return { fileID: this._fileId != null ? this._fileId._toProtobuf() : null, keys: this._keys != null ? { keys: this._keys.map((key) => key._toProtobufKey()), } : null, expirationTime: this._expirationTime != null ? this._expirationTime._toProtobuf() : null, contents: this._contents, memo: this._fileMemo != null ? { value: this._fileMemo, } : null, }; } /** * @returns {string} */ _getLogId() { const timestamp = /** @type {import("../Timestamp.js").default} */ ( this._transactionIds.current.validStart ); return `FileUpdateTransaction:${timestamp.toString()}`; } } // eslint-disable-next-line @typescript-eslint/unbound-method TRANSACTION_REGISTRY.set("fileUpdate", FileUpdateTransaction._fromProtobuf);