github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/file_update_transaction.go (about)

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  import (
    24  	"time"
    25  
    26  	"google.golang.org/protobuf/types/known/wrapperspb"
    27  
    28  	"github.com/hashgraph/hedera-protobufs-go/services"
    29  )
    30  
    31  // FileUpdateTransaction
    32  // Modify the metadata and/or contents of a file. If a field is not set in the transaction body, the
    33  // corresponding file attribute will be unchanged. This transaction must be signed by all the keys
    34  // in the top level of a key list (M-of-M) of the file being updated. If the keys themselves are
    35  // being updated, then the transaction must also be signed by all the new keys. If the keys contain
    36  // additional KeyList or ThresholdKey then M-of-M secondary KeyList or ThresholdKey signing
    37  // requirements must be meet
    38  type FileUpdateTransaction struct {
    39  	Transaction
    40  	fileID         *FileID
    41  	keys           *KeyList
    42  	expirationTime *time.Time
    43  	contents       []byte
    44  	memo           string
    45  }
    46  
    47  // NewFileUpdateTransaction creates a FileUpdateTransaction which modifies the metadata and/or contents of a file.
    48  // If a field is not set in the transaction body, the corresponding file attribute will be unchanged.
    49  // tx transaction must be signed by all the keys in the top level of a key list (M-of-M) of the file being updated.
    50  // If the keys themselves are being updated, then the transaction must also be signed by all the new keys. If the keys contain
    51  // additional KeyList or ThresholdKey then M-of-M secondary KeyList or ThresholdKey signing
    52  // requirements must be meet
    53  func NewFileUpdateTransaction() *FileUpdateTransaction {
    54  	tx := FileUpdateTransaction{
    55  		Transaction: _NewTransaction(),
    56  	}
    57  	tx._SetDefaultMaxTransactionFee(NewHbar(5))
    58  	return &tx
    59  }
    60  
    61  func _FileUpdateTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *FileUpdateTransaction {
    62  	keys, _ := _KeyListFromProtobuf(pb.GetFileUpdate().GetKeys())
    63  	expiration := _TimeFromProtobuf(pb.GetFileUpdate().GetExpirationTime())
    64  
    65  	return &FileUpdateTransaction{
    66  		Transaction:    tx,
    67  		fileID:         _FileIDFromProtobuf(pb.GetFileUpdate().GetFileID()),
    68  		keys:           &keys,
    69  		expirationTime: &expiration,
    70  		contents:       pb.GetFileUpdate().GetContents(),
    71  		memo:           pb.GetFileUpdate().GetMemo().Value,
    72  	}
    73  }
    74  
    75  // SetFileID Sets the FileID to be updated
    76  func (tx *FileUpdateTransaction) SetFileID(fileID FileID) *FileUpdateTransaction {
    77  	tx._RequireNotFrozen()
    78  	tx.fileID = &fileID
    79  	return tx
    80  }
    81  
    82  // GetFileID returns the FileID to be updated
    83  func (tx *FileUpdateTransaction) GetFileID() FileID {
    84  	if tx.fileID == nil {
    85  		return FileID{}
    86  	}
    87  
    88  	return *tx.fileID
    89  }
    90  
    91  // SetKeys Sets the new list of keys that can modify or delete the file
    92  func (tx *FileUpdateTransaction) SetKeys(keys ...Key) *FileUpdateTransaction {
    93  	tx._RequireNotFrozen()
    94  	if tx.keys == nil {
    95  		tx.keys = &KeyList{keys: []Key{}}
    96  	}
    97  	keyList := NewKeyList()
    98  	keyList.AddAll(keys)
    99  
   100  	tx.keys = keyList
   101  
   102  	return tx
   103  }
   104  
   105  func (tx *FileUpdateTransaction) GetKeys() KeyList {
   106  	if tx.keys != nil {
   107  		return *tx.keys
   108  	}
   109  
   110  	return KeyList{}
   111  }
   112  
   113  // SetExpirationTime Sets the new expiry time
   114  func (tx *FileUpdateTransaction) SetExpirationTime(expiration time.Time) *FileUpdateTransaction {
   115  	tx._RequireNotFrozen()
   116  	tx.expirationTime = &expiration
   117  	return tx
   118  }
   119  
   120  // GetExpirationTime returns the new expiry time
   121  func (tx *FileUpdateTransaction) GetExpirationTime() time.Time {
   122  	if tx.expirationTime != nil {
   123  		return *tx.expirationTime
   124  	}
   125  
   126  	return time.Time{}
   127  }
   128  
   129  // SetContents Sets the new contents that should overwrite the file's current contents
   130  func (tx *FileUpdateTransaction) SetContents(contents []byte) *FileUpdateTransaction {
   131  	tx._RequireNotFrozen()
   132  	tx.contents = contents
   133  	return tx
   134  }
   135  
   136  // GetContents returns the new contents that should overwrite the file's current contents
   137  func (tx *FileUpdateTransaction) GetContents() []byte {
   138  	return tx.contents
   139  }
   140  
   141  // SetFileMemo Sets the new memo to be associated with the file (UTF-8 encoding max 100 bytes)
   142  func (tx *FileUpdateTransaction) SetFileMemo(memo string) *FileUpdateTransaction {
   143  	tx._RequireNotFrozen()
   144  	tx.memo = memo
   145  
   146  	return tx
   147  }
   148  
   149  // GeFileMemo
   150  // Deprecated: use GetFileMemo()
   151  func (tx *FileUpdateTransaction) GeFileMemo() string {
   152  	return tx.memo
   153  }
   154  
   155  func (tx *FileUpdateTransaction) GetFileMemo() string {
   156  	return tx.memo
   157  }
   158  
   159  // ----- Required Interfaces ------- //
   160  
   161  // Sign uses the provided privateKey to sign the transaction.
   162  func (tx *FileUpdateTransaction) Sign(
   163  	privateKey PrivateKey,
   164  ) *FileUpdateTransaction {
   165  	tx.Transaction.Sign(privateKey)
   166  	return tx
   167  }
   168  
   169  // SignWithOperator signs the transaction with client's operator privateKey.
   170  func (tx *FileUpdateTransaction) SignWithOperator(
   171  	client *Client,
   172  ) (*FileUpdateTransaction, error) {
   173  	// If the transaction is not signed by the _Operator, we need
   174  	// to sign the transaction with the _Operator
   175  	_, err := tx.Transaction.signWithOperator(client, tx)
   176  	if err != nil {
   177  		return nil, err
   178  	}
   179  	return tx, nil
   180  }
   181  
   182  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   183  // with the publicKey as the map key.
   184  func (tx *FileUpdateTransaction) SignWith(
   185  	publicKey PublicKey,
   186  	signer TransactionSigner,
   187  ) *FileUpdateTransaction {
   188  	tx.Transaction.SignWith(publicKey, signer)
   189  	return tx
   190  }
   191  
   192  // AddSignature adds a signature to the transaction.
   193  func (tx *FileUpdateTransaction) AddSignature(publicKey PublicKey, signature []byte) *FileUpdateTransaction {
   194  	tx.Transaction.AddSignature(publicKey, signature)
   195  	return tx
   196  }
   197  
   198  // When execution is attempted, a single attempt will timeout when tx deadline is reached. (The SDK may subsequently retry the execution.)
   199  func (tx *FileUpdateTransaction) SetGrpcDeadline(deadline *time.Duration) *FileUpdateTransaction {
   200  	tx.Transaction.SetGrpcDeadline(deadline)
   201  	return tx
   202  }
   203  
   204  func (tx *FileUpdateTransaction) Freeze() (*FileUpdateTransaction, error) {
   205  	return tx.FreezeWith(nil)
   206  }
   207  
   208  func (tx *FileUpdateTransaction) FreezeWith(client *Client) (*FileUpdateTransaction, error) {
   209  	_, err := tx.Transaction.freezeWith(client, tx)
   210  	return tx, err
   211  }
   212  
   213  // SetMaxTransactionFee sets the maximum transaction fee the operator (paying account) is willing to pay.
   214  func (tx *FileUpdateTransaction) SetMaxTransactionFee(fee Hbar) *FileUpdateTransaction {
   215  	tx._RequireNotFrozen()
   216  	tx.Transaction.SetMaxTransactionFee(fee)
   217  	return tx
   218  }
   219  
   220  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   221  func (tx *FileUpdateTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *FileUpdateTransaction {
   222  	tx._RequireNotFrozen()
   223  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   224  	return tx
   225  }
   226  
   227  // SetTransactionMemo sets the memo for this FileUpdateTransaction.
   228  func (tx *FileUpdateTransaction) SetTransactionMemo(memo string) *FileUpdateTransaction {
   229  	tx._RequireNotFrozen()
   230  	tx.Transaction.SetTransactionMemo(memo)
   231  	return tx
   232  }
   233  
   234  // SetTransactionValidDuration sets the valid duration for this FileUpdateTransaction.
   235  func (tx *FileUpdateTransaction) SetTransactionValidDuration(duration time.Duration) *FileUpdateTransaction {
   236  	tx._RequireNotFrozen()
   237  	tx.Transaction.SetTransactionValidDuration(duration)
   238  	return tx
   239  }
   240  
   241  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   242  func (tx *FileUpdateTransaction) ToBytes() ([]byte, error) {
   243  	bytes, err := tx.Transaction.toBytes(tx)
   244  	if err != nil {
   245  		return nil, err
   246  	}
   247  	return bytes, nil
   248  }
   249  
   250  // SetTransactionID sets the TransactionID for this FileUpdateTransaction.
   251  func (tx *FileUpdateTransaction) SetTransactionID(transactionID TransactionID) *FileUpdateTransaction {
   252  	tx._RequireNotFrozen()
   253  
   254  	tx.Transaction.SetTransactionID(transactionID)
   255  	return tx
   256  }
   257  
   258  // SetNodeAccountID sets the _Node AccountID for this FileUpdateTransaction.
   259  func (tx *FileUpdateTransaction) SetNodeAccountIDs(nodeID []AccountID) *FileUpdateTransaction {
   260  	tx._RequireNotFrozen()
   261  	tx.Transaction.SetNodeAccountIDs(nodeID)
   262  	return tx
   263  }
   264  
   265  // SetMaxRetry sets the max number of errors before execution will fail.
   266  func (tx *FileUpdateTransaction) SetMaxRetry(count int) *FileUpdateTransaction {
   267  	tx.Transaction.SetMaxRetry(count)
   268  	return tx
   269  }
   270  
   271  // SetMaxBackoff The maximum amount of time to wait between retries.
   272  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   273  func (tx *FileUpdateTransaction) SetMaxBackoff(max time.Duration) *FileUpdateTransaction {
   274  	tx.Transaction.SetMaxBackoff(max)
   275  	return tx
   276  }
   277  
   278  // SetMinBackoff sets the minimum amount of time to wait between retries.
   279  func (tx *FileUpdateTransaction) SetMinBackoff(min time.Duration) *FileUpdateTransaction {
   280  	tx.Transaction.SetMinBackoff(min)
   281  	return tx
   282  }
   283  
   284  func (tx *FileUpdateTransaction) SetLogLevel(level LogLevel) *FileUpdateTransaction {
   285  	tx.Transaction.SetLogLevel(level)
   286  	return tx
   287  }
   288  
   289  func (tx *FileUpdateTransaction) Execute(client *Client) (TransactionResponse, error) {
   290  	return tx.Transaction.execute(client, tx)
   291  }
   292  
   293  func (tx *FileUpdateTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   294  	return tx.Transaction.schedule(tx)
   295  }
   296  
   297  // ----------- Overridden functions ----------------
   298  
   299  func (tx *FileUpdateTransaction) getName() string {
   300  	return "FileUpdateTransaction"
   301  }
   302  func (tx *FileUpdateTransaction) validateNetworkOnIDs(client *Client) error {
   303  	if client == nil || !client.autoValidateChecksums {
   304  		return nil
   305  	}
   306  
   307  	if tx.fileID != nil {
   308  		if err := tx.fileID.ValidateChecksum(client); err != nil {
   309  			return err
   310  		}
   311  	}
   312  
   313  	return nil
   314  }
   315  func (tx *FileUpdateTransaction) build() *services.TransactionBody {
   316  	return &services.TransactionBody{
   317  		TransactionFee:           tx.transactionFee,
   318  		Memo:                     tx.Transaction.memo,
   319  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   320  		TransactionID:            tx.transactionID._ToProtobuf(),
   321  		Data: &services.TransactionBody_FileUpdate{
   322  			FileUpdate: tx.buildProtoBody(),
   323  		},
   324  	}
   325  }
   326  func (tx *FileUpdateTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   327  	return &services.SchedulableTransactionBody{
   328  		TransactionFee: tx.transactionFee,
   329  		Memo:           tx.Transaction.memo,
   330  		Data: &services.SchedulableTransactionBody_FileUpdate{
   331  			FileUpdate: tx.buildProtoBody(),
   332  		},
   333  	}, nil
   334  }
   335  func (tx *FileUpdateTransaction) buildProtoBody() *services.FileUpdateTransactionBody {
   336  	body := &services.FileUpdateTransactionBody{
   337  		Memo: &wrapperspb.StringValue{Value: tx.memo},
   338  	}
   339  	if tx.fileID != nil {
   340  		body.FileID = tx.fileID._ToProtobuf()
   341  	}
   342  
   343  	if tx.expirationTime != nil {
   344  		body.ExpirationTime = _TimeToProtobuf(*tx.expirationTime)
   345  	}
   346  
   347  	if tx.keys != nil {
   348  		body.Keys = tx.keys._ToProtoKeyList()
   349  	}
   350  
   351  	if tx.contents != nil {
   352  		body.Contents = tx.contents
   353  	}
   354  
   355  	return body
   356  }
   357  func (tx *FileUpdateTransaction) getMethod(channel *_Channel) _Method {
   358  	return _Method{
   359  		transaction: channel._GetFile().UpdateFile,
   360  	}
   361  }
   362  func (tx *FileUpdateTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   363  	return tx.buildScheduled()
   364  }