github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_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  // AccountUpdateTransaction
    32  // Change properties for the given account. Any null field is ignored (left unchanged). This
    33  // transaction must be signed by the existing key for this account. If the transaction is changing
    34  // the key field, then the transaction must be signed by both the old key (from before the change)
    35  // and the new key. The old key must sign for security. The new key must sign as a safeguard to
    36  // avoid accidentally changing to an invalid key, and then having no way to recover.
    37  type AccountUpdateTransaction struct {
    38  	Transaction
    39  	accountID                     *AccountID
    40  	proxyAccountID                *AccountID
    41  	key                           Key
    42  	autoRenewPeriod               *time.Duration
    43  	memo                          string
    44  	receiverSignatureRequired     bool
    45  	expirationTime                *time.Time
    46  	maxAutomaticTokenAssociations int32
    47  	aliasKey                      *PublicKey
    48  	stakedAccountID               *AccountID
    49  	stakedNodeID                  *int64
    50  	declineReward                 bool
    51  }
    52  
    53  // NewAccountUpdateTransaction
    54  // Creates AccoutnUppdateTransaction which changes properties for the given account.
    55  // Any null field is ignored (left unchanged).
    56  // This transaction must be signed by the existing key for this account. If the transaction is changing
    57  // the key field, then the transaction must be signed by both the old key (from before the change)
    58  // and the new key. The old key must sign for security. The new key must sign as a safeguard to
    59  // avoid accidentally changing to an invalid key, and then having no way to recover.
    60  func NewAccountUpdateTransaction() *AccountUpdateTransaction {
    61  	tx := AccountUpdateTransaction{
    62  		Transaction: _NewTransaction(),
    63  	}
    64  	tx.SetAutoRenewPeriod(7890000 * time.Second)
    65  	tx._SetDefaultMaxTransactionFee(NewHbar(2))
    66  
    67  	return &tx
    68  }
    69  
    70  func _AccountUpdateTransactionFromProtobuf(tx Transaction, pb *services.TransactionBody) *AccountUpdateTransaction {
    71  	key, _ := _KeyFromProtobuf(pb.GetCryptoUpdateAccount().GetKey())
    72  	var receiverSignatureRequired bool
    73  
    74  	switch s := pb.GetCryptoUpdateAccount().GetReceiverSigRequiredField().(type) {
    75  	case *services.CryptoUpdateTransactionBody_ReceiverSigRequired:
    76  		receiverSignatureRequired = s.ReceiverSigRequired // nolint
    77  	case *services.CryptoUpdateTransactionBody_ReceiverSigRequiredWrapper:
    78  		receiverSignatureRequired = s.ReceiverSigRequiredWrapper.Value // nolint
    79  	}
    80  
    81  	autoRenew := _DurationFromProtobuf(pb.GetCryptoUpdateAccount().AutoRenewPeriod)
    82  	expiration := _TimeFromProtobuf(pb.GetCryptoUpdateAccount().ExpirationTime)
    83  
    84  	stakedNodeID := pb.GetCryptoUpdateAccount().GetStakedNodeId()
    85  
    86  	var stakeNodeAccountID *AccountID
    87  	if pb.GetCryptoUpdateAccount().GetStakedAccountId() != nil {
    88  		stakeNodeAccountID = _AccountIDFromProtobuf(pb.GetCryptoUpdateAccount().GetStakedAccountId())
    89  	}
    90  
    91  	return &AccountUpdateTransaction{
    92  		Transaction:                   tx,
    93  		accountID:                     _AccountIDFromProtobuf(pb.GetCryptoUpdateAccount().GetAccountIDToUpdate()),
    94  		key:                           key,
    95  		autoRenewPeriod:               &autoRenew,
    96  		memo:                          pb.GetCryptoUpdateAccount().GetMemo().Value,
    97  		receiverSignatureRequired:     receiverSignatureRequired,
    98  		expirationTime:                &expiration,
    99  		maxAutomaticTokenAssociations: pb.GetCryptoUpdateAccount().MaxAutomaticTokenAssociations.GetValue(),
   100  		stakedAccountID:               stakeNodeAccountID,
   101  		stakedNodeID:                  &stakedNodeID,
   102  		declineReward:                 pb.GetCryptoUpdateAccount().GetDeclineReward().GetValue(),
   103  	}
   104  }
   105  
   106  // SetKey Sets the new key for the Account
   107  func (tx *AccountUpdateTransaction) SetKey(key Key) *AccountUpdateTransaction {
   108  	tx._RequireNotFrozen()
   109  	tx.key = key
   110  	return tx
   111  }
   112  
   113  func (tx *AccountUpdateTransaction) GetKey() (Key, error) {
   114  	return tx.key, nil
   115  }
   116  
   117  // SetAccountID Sets the account ID which is being updated in tx transaction.
   118  func (tx *AccountUpdateTransaction) SetAccountID(accountID AccountID) *AccountUpdateTransaction {
   119  	tx._RequireNotFrozen()
   120  	tx.accountID = &accountID
   121  	return tx
   122  }
   123  
   124  func (tx *AccountUpdateTransaction) GetAccountID() AccountID {
   125  	if tx.accountID == nil {
   126  		return AccountID{}
   127  	}
   128  
   129  	return *tx.accountID
   130  }
   131  
   132  // Deprecated
   133  func (tx *AccountUpdateTransaction) SetAliasKey(alias PublicKey) *AccountUpdateTransaction {
   134  	tx._RequireNotFrozen()
   135  	tx.aliasKey = &alias
   136  	return tx
   137  }
   138  
   139  // Deprecated
   140  func (tx *AccountUpdateTransaction) GetAliasKey() PublicKey {
   141  	if tx.aliasKey == nil {
   142  		return PublicKey{}
   143  	}
   144  
   145  	return *tx.aliasKey
   146  }
   147  
   148  func (tx *AccountUpdateTransaction) SetStakedAccountID(id AccountID) *AccountUpdateTransaction {
   149  	tx._RequireNotFrozen()
   150  	tx.stakedAccountID = &id
   151  	return tx
   152  }
   153  
   154  func (tx *AccountUpdateTransaction) GetStakedAccountID() AccountID {
   155  	if tx.stakedAccountID != nil {
   156  		return *tx.stakedAccountID
   157  	}
   158  
   159  	return AccountID{}
   160  }
   161  
   162  func (tx *AccountUpdateTransaction) SetStakedNodeID(id int64) *AccountUpdateTransaction {
   163  	tx._RequireNotFrozen()
   164  	tx.stakedNodeID = &id
   165  	return tx
   166  }
   167  
   168  func (tx *AccountUpdateTransaction) GetStakedNodeID() int64 {
   169  	if tx.stakedNodeID != nil {
   170  		return *tx.stakedNodeID
   171  	}
   172  
   173  	return 0
   174  }
   175  
   176  func (tx *AccountUpdateTransaction) SetDeclineStakingReward(decline bool) *AccountUpdateTransaction {
   177  	tx._RequireNotFrozen()
   178  	tx.declineReward = decline
   179  	return tx
   180  }
   181  
   182  func (tx *AccountUpdateTransaction) ClearStakedAccountID() *AccountUpdateTransaction {
   183  	tx._RequireNotFrozen()
   184  	tx.stakedAccountID = &AccountID{Account: 0}
   185  	return tx
   186  }
   187  
   188  func (tx *AccountUpdateTransaction) ClearStakedNodeID() *AccountUpdateTransaction {
   189  	tx._RequireNotFrozen()
   190  	*tx.stakedNodeID = -1
   191  	return tx
   192  }
   193  
   194  func (tx *AccountUpdateTransaction) GetDeclineStakingReward() bool {
   195  	return tx.declineReward
   196  }
   197  
   198  // SetMaxAutomaticTokenAssociations
   199  // Sets the maximum number of tokens that an Account can be implicitly associated with. Up to a 1000
   200  // including implicit and explicit associations.
   201  func (tx *AccountUpdateTransaction) SetMaxAutomaticTokenAssociations(max int32) *AccountUpdateTransaction {
   202  	tx._RequireNotFrozen()
   203  	tx.maxAutomaticTokenAssociations = max
   204  	return tx
   205  }
   206  
   207  func (tx *AccountUpdateTransaction) GetMaxAutomaticTokenAssociations() int32 {
   208  	return tx.maxAutomaticTokenAssociations
   209  }
   210  
   211  // SetReceiverSignatureRequired
   212  // If true, this account's key must sign any transaction depositing into this account (in
   213  // addition to all withdrawals)
   214  func (tx *AccountUpdateTransaction) SetReceiverSignatureRequired(receiverSignatureRequired bool) *AccountUpdateTransaction {
   215  	tx._RequireNotFrozen()
   216  	tx.receiverSignatureRequired = receiverSignatureRequired
   217  	return tx
   218  }
   219  
   220  func (tx *AccountUpdateTransaction) GetReceiverSignatureRequired() bool {
   221  	return tx.receiverSignatureRequired
   222  }
   223  
   224  // Deprecated
   225  // SetProxyAccountID Sets the ID of the account to which this account is proxy staked.
   226  func (tx *AccountUpdateTransaction) SetProxyAccountID(proxyAccountID AccountID) *AccountUpdateTransaction {
   227  	tx._RequireNotFrozen()
   228  	tx.proxyAccountID = &proxyAccountID
   229  	return tx
   230  }
   231  
   232  // Deprecated
   233  func (tx *AccountUpdateTransaction) GetProxyAccountID() AccountID {
   234  	if tx.proxyAccountID == nil {
   235  		return AccountID{}
   236  	}
   237  
   238  	return *tx.proxyAccountID
   239  }
   240  
   241  // SetAutoRenewPeriod Sets the duration in which it will automatically extend the expiration period.
   242  func (tx *AccountUpdateTransaction) SetAutoRenewPeriod(autoRenewPeriod time.Duration) *AccountUpdateTransaction {
   243  	tx._RequireNotFrozen()
   244  	tx.autoRenewPeriod = &autoRenewPeriod
   245  	return tx
   246  }
   247  
   248  func (tx *AccountUpdateTransaction) GetAutoRenewPeriod() time.Duration {
   249  	if tx.autoRenewPeriod != nil {
   250  		return *tx.autoRenewPeriod
   251  	}
   252  
   253  	return time.Duration(0)
   254  }
   255  
   256  // SetExpirationTime sets the new expiration time to extend to (ignored if equal to or before the current one)
   257  func (tx *AccountUpdateTransaction) SetExpirationTime(expirationTime time.Time) *AccountUpdateTransaction {
   258  	tx._RequireNotFrozen()
   259  	tx.expirationTime = &expirationTime
   260  	return tx
   261  }
   262  
   263  func (tx *AccountUpdateTransaction) GetExpirationTime() time.Time {
   264  	if tx.expirationTime != nil {
   265  		return *tx.expirationTime
   266  	}
   267  	return time.Time{}
   268  }
   269  
   270  // SetAccountMemo sets the new memo to be associated with the account (UTF-8 encoding max 100 bytes)
   271  func (tx *AccountUpdateTransaction) SetAccountMemo(memo string) *AccountUpdateTransaction {
   272  	tx._RequireNotFrozen()
   273  	tx.memo = memo
   274  
   275  	return tx
   276  }
   277  
   278  func (tx *AccountUpdateTransaction) GetAccountMemo() string {
   279  	return tx.memo
   280  }
   281  
   282  // ---- Required Interfaces ---- //
   283  
   284  // Sign uses the provided privateKey to sign the transaction.
   285  func (tx *AccountUpdateTransaction) Sign(
   286  	privateKey PrivateKey,
   287  ) *AccountUpdateTransaction {
   288  	tx.Transaction.Sign(privateKey)
   289  	return tx
   290  }
   291  
   292  // SignWithOperator signs the transaction with client's operator privateKey.
   293  func (tx *AccountUpdateTransaction) SignWithOperator(
   294  	client *Client,
   295  ) (*AccountUpdateTransaction, error) {
   296  	// If the transaction is not signed by the _Operator, we need
   297  	// to sign the transaction with the _Operator
   298  	_, err := tx.Transaction.signWithOperator(client, tx)
   299  	if err != nil {
   300  		return nil, err
   301  	}
   302  	return tx, nil
   303  }
   304  
   305  // SignWith executes the TransactionSigner and adds the resulting signature data to the Transaction's signature map
   306  // with the publicKey as the map key.
   307  func (tx *AccountUpdateTransaction) SignWith(
   308  	publicKey PublicKey,
   309  	signer TransactionSigner,
   310  ) *AccountUpdateTransaction {
   311  	tx.Transaction.SignWith(publicKey, signer)
   312  	return tx
   313  }
   314  
   315  // AddSignature adds a signature to the transaction.
   316  func (tx *AccountUpdateTransaction) AddSignature(publicKey PublicKey, signature []byte) *AccountUpdateTransaction {
   317  	tx.Transaction.AddSignature(publicKey, signature)
   318  	return tx
   319  }
   320  
   321  // When execution is attempted, a single attempt will timeout when tx deadline is reached. (The SDK may subsequently retry the execution.)
   322  func (tx *AccountUpdateTransaction) SetGrpcDeadline(deadline *time.Duration) *AccountUpdateTransaction {
   323  	tx.Transaction.SetGrpcDeadline(deadline)
   324  	return tx
   325  }
   326  
   327  func (tx *AccountUpdateTransaction) Freeze() (*AccountUpdateTransaction, error) {
   328  	return tx.FreezeWith(nil)
   329  }
   330  
   331  func (tx *AccountUpdateTransaction) FreezeWith(client *Client) (*AccountUpdateTransaction, error) {
   332  	_, err := tx.Transaction.freezeWith(client, tx)
   333  	return tx, err
   334  }
   335  
   336  // SetMaxTransactionFee sets the maximum transaction fee the operator (paying account) is willing to pay.
   337  func (tx *AccountUpdateTransaction) SetMaxTransactionFee(fee Hbar) *AccountUpdateTransaction {
   338  	tx._RequireNotFrozen()
   339  	tx.Transaction.SetMaxTransactionFee(fee)
   340  	return tx
   341  }
   342  
   343  // SetRegenerateTransactionID sets if transaction IDs should be regenerated when `TRANSACTION_EXPIRED` is received
   344  func (tx *AccountUpdateTransaction) SetRegenerateTransactionID(regenerateTransactionID bool) *AccountUpdateTransaction {
   345  	tx._RequireNotFrozen()
   346  	tx.Transaction.SetRegenerateTransactionID(regenerateTransactionID)
   347  	return tx
   348  }
   349  
   350  // SetTransactionMemo sets the memo for this AccountUpdateTransaction.
   351  func (tx *AccountUpdateTransaction) SetTransactionMemo(memo string) *AccountUpdateTransaction {
   352  	tx._RequireNotFrozen()
   353  	tx.Transaction.SetTransactionMemo(memo)
   354  	return tx
   355  }
   356  
   357  // SetTransactionValidDuration sets the valid duration for this AccountUpdateTransaction.
   358  func (tx *AccountUpdateTransaction) SetTransactionValidDuration(duration time.Duration) *AccountUpdateTransaction {
   359  	tx._RequireNotFrozen()
   360  	tx.Transaction.SetTransactionValidDuration(duration)
   361  	return tx
   362  }
   363  
   364  // ToBytes serialise the tx to bytes, no matter if it is signed (locked), or not
   365  func (tx *AccountUpdateTransaction) ToBytes() ([]byte, error) {
   366  	bytes, err := tx.Transaction.toBytes(tx)
   367  	if err != nil {
   368  		return nil, err
   369  	}
   370  	return bytes, nil
   371  }
   372  
   373  // SetTransactionID sets the TransactionID for this AccountUpdateTransaction.
   374  func (tx *AccountUpdateTransaction) SetTransactionID(transactionID TransactionID) *AccountUpdateTransaction {
   375  	tx._RequireNotFrozen()
   376  
   377  	tx.Transaction.SetTransactionID(transactionID)
   378  	return tx
   379  }
   380  
   381  // SetNodeAccountIDs sets the _Node AccountID for this AccountUpdateTransaction.
   382  func (tx *AccountUpdateTransaction) SetNodeAccountIDs(nodeID []AccountID) *AccountUpdateTransaction {
   383  	tx._RequireNotFrozen()
   384  	tx.Transaction.SetNodeAccountIDs(nodeID)
   385  	return tx
   386  }
   387  
   388  // SetMaxRetry sets the max number of errors before execution will fail.
   389  func (tx *AccountUpdateTransaction) SetMaxRetry(count int) *AccountUpdateTransaction {
   390  	tx.Transaction.SetMaxRetry(count)
   391  	return tx
   392  }
   393  
   394  // SetMaxBackoff The maximum amount of time to wait between retries.
   395  // Every retry attempt will increase the wait time exponentially until it reaches this time.
   396  func (tx *AccountUpdateTransaction) SetMaxBackoff(max time.Duration) *AccountUpdateTransaction {
   397  	tx.Transaction.SetMaxBackoff(max)
   398  	return tx
   399  }
   400  
   401  // SetMinBackoff sets the minimum amount of time to wait between retries.
   402  func (tx *AccountUpdateTransaction) SetMinBackoff(min time.Duration) *AccountUpdateTransaction {
   403  	tx.Transaction.SetMinBackoff(min)
   404  	return tx
   405  }
   406  
   407  func (tx *AccountUpdateTransaction) SetLogLevel(level LogLevel) *AccountUpdateTransaction {
   408  	tx.Transaction.SetLogLevel(level)
   409  	return tx
   410  }
   411  
   412  func (tx *AccountUpdateTransaction) Execute(client *Client) (TransactionResponse, error) {
   413  	return tx.Transaction.execute(client, tx)
   414  }
   415  
   416  func (tx *AccountUpdateTransaction) Schedule() (*ScheduleCreateTransaction, error) {
   417  	return tx.Transaction.schedule(tx)
   418  }
   419  
   420  // ----------- Overridden functions ----------------
   421  
   422  func (tx *AccountUpdateTransaction) getName() string {
   423  	return "AccountUpdateTransaction"
   424  }
   425  
   426  func (tx *AccountUpdateTransaction) validateNetworkOnIDs(client *Client) error {
   427  	if client == nil || !client.autoValidateChecksums {
   428  		return nil
   429  	}
   430  
   431  	if tx.accountID != nil {
   432  		if err := tx.accountID.ValidateChecksum(client); err != nil {
   433  			return err
   434  		}
   435  	}
   436  
   437  	if tx.proxyAccountID != nil {
   438  		if err := tx.proxyAccountID.ValidateChecksum(client); err != nil {
   439  			return err
   440  		}
   441  	}
   442  
   443  	return nil
   444  }
   445  
   446  func (tx *AccountUpdateTransaction) build() *services.TransactionBody {
   447  	body := tx.buildProtoBody()
   448  
   449  	pb := services.TransactionBody{
   450  		TransactionFee:           tx.transactionFee,
   451  		Memo:                     tx.Transaction.memo,
   452  		TransactionValidDuration: _DurationToProtobuf(tx.GetTransactionValidDuration()),
   453  		TransactionID:            tx.transactionID._ToProtobuf(),
   454  		Data: &services.TransactionBody_CryptoUpdateAccount{
   455  			CryptoUpdateAccount: body,
   456  		},
   457  	}
   458  
   459  	body.MaxAutomaticTokenAssociations = &wrapperspb.Int32Value{Value: tx.maxAutomaticTokenAssociations}
   460  
   461  	return &pb
   462  }
   463  func (tx *AccountUpdateTransaction) buildScheduled() (*services.SchedulableTransactionBody, error) {
   464  	return &services.SchedulableTransactionBody{
   465  		TransactionFee: tx.transactionFee,
   466  		Memo:           tx.Transaction.memo,
   467  		Data: &services.SchedulableTransactionBody_CryptoUpdateAccount{
   468  			CryptoUpdateAccount: tx.buildProtoBody(),
   469  		},
   470  	}, nil
   471  }
   472  func (tx *AccountUpdateTransaction) buildProtoBody() *services.CryptoUpdateTransactionBody {
   473  	body := &services.CryptoUpdateTransactionBody{
   474  		ReceiverSigRequiredField: &services.CryptoUpdateTransactionBody_ReceiverSigRequiredWrapper{
   475  			ReceiverSigRequiredWrapper: &wrapperspb.BoolValue{Value: tx.receiverSignatureRequired},
   476  		},
   477  		Memo:                          &wrapperspb.StringValue{Value: tx.memo},
   478  		DeclineReward:                 &wrapperspb.BoolValue{Value: tx.declineReward},
   479  		MaxAutomaticTokenAssociations: &wrapperspb.Int32Value{Value: tx.maxAutomaticTokenAssociations},
   480  	}
   481  
   482  	if tx.autoRenewPeriod != nil {
   483  		body.AutoRenewPeriod = _DurationToProtobuf(*tx.autoRenewPeriod)
   484  	}
   485  
   486  	if tx.expirationTime != nil {
   487  		body.ExpirationTime = _TimeToProtobuf(*tx.expirationTime)
   488  	}
   489  
   490  	if tx.accountID != nil {
   491  		body.AccountIDToUpdate = tx.accountID._ToProtobuf()
   492  	}
   493  
   494  	if tx.key != nil {
   495  		body.Key = tx.key._ToProtoKey()
   496  	}
   497  
   498  	if tx.stakedAccountID != nil {
   499  		body.StakedId = &services.CryptoUpdateTransactionBody_StakedAccountId{StakedAccountId: tx.stakedAccountID._ToProtobuf()}
   500  	} else if tx.stakedNodeID != nil {
   501  		body.StakedId = &services.CryptoUpdateTransactionBody_StakedNodeId{StakedNodeId: *tx.stakedNodeID}
   502  	}
   503  
   504  	return body
   505  }
   506  
   507  func (tx *AccountUpdateTransaction) getMethod(channel *_Channel) _Method {
   508  	return _Method{
   509  		transaction: channel._GetCrypto().UpdateAccount,
   510  	}
   511  }
   512  
   513  func (tx *AccountUpdateTransaction) _ConstructScheduleProtobuf() (*services.SchedulableTransactionBody, error) {
   514  	return tx.buildScheduled()
   515  }