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

     1  //go:build all || e2e
     2  // +build all e2e
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func TestIntegrationAccountUpdateTransactionCanExecute(t *testing.T) {
    36  	t.Parallel()
    37  	env := NewIntegrationTestEnv(t)
    38  
    39  	newKey, err := PrivateKeyGenerateEd25519()
    40  	require.NoError(t, err)
    41  
    42  	newKey2, err := GeneratePrivateKey()
    43  	require.NoError(t, err)
    44  
    45  	newBalance := NewHbar(2)
    46  
    47  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
    48  
    49  	resp, err := NewAccountCreateTransaction().
    50  		SetKey(newKey.PublicKey()).
    51  		SetNodeAccountIDs(env.NodeAccountIDs).
    52  		SetInitialBalance(newBalance).
    53  		Execute(env.Client)
    54  
    55  	require.NoError(t, err)
    56  
    57  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
    58  	require.NoError(t, err)
    59  
    60  	accountID := *receipt.AccountID
    61  	require.NoError(t, err)
    62  
    63  	tx, err := NewAccountUpdateTransaction().
    64  		SetAccountID(accountID).
    65  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    66  		SetExpirationTime(time.Now().Add(time.Hour * 24 * 92)).
    67  		SetKey(newKey2.PublicKey()).
    68  		FreezeWith(env.Client)
    69  	require.NoError(t, err)
    70  
    71  	tx.Sign(newKey)
    72  	tx.Sign(newKey2)
    73  
    74  	resp, err = tx.Execute(env.Client)
    75  	require.NoError(t, err)
    76  
    77  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
    78  	require.NoError(t, err)
    79  
    80  	info, err := NewAccountInfoQuery().
    81  		SetAccountID(accountID).
    82  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    83  		SetMaxQueryPayment(NewHbar(1)).
    84  		Execute(env.Client)
    85  	require.NoError(t, err)
    86  
    87  	assert.Equal(t, newKey2.PublicKey().String(), info.Key.String())
    88  
    89  	txDelete, err := NewAccountDeleteTransaction().
    90  		SetAccountID(accountID).
    91  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
    92  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
    93  		FreezeWith(env.Client)
    94  
    95  	require.NoError(t, err)
    96  
    97  	txDelete.Sign(newKey2)
    98  
    99  	resp, err = txDelete.Execute(env.Client)
   100  	require.NoError(t, err)
   101  
   102  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   103  
   104  	require.NoError(t, err)
   105  
   106  	err = CloseIntegrationTestEnv(env, nil)
   107  	require.NoError(t, err)
   108  }
   109  
   110  func TestIntegrationAccountUpdateTransactionNoSigning(t *testing.T) {
   111  	t.Parallel()
   112  	env := NewIntegrationTestEnv(t)
   113  
   114  	newKey, err := PrivateKeyGenerateEd25519()
   115  	require.NoError(t, err)
   116  
   117  	newKey2, err := GeneratePrivateKey()
   118  	require.NoError(t, err)
   119  
   120  	newBalance := NewHbar(2)
   121  
   122  	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   123  
   124  	resp, err := NewAccountCreateTransaction().
   125  		SetKey(newKey.PublicKey()).
   126  		SetNodeAccountIDs(env.NodeAccountIDs).
   127  		SetInitialBalance(newBalance).
   128  		Execute(env.Client)
   129  
   130  	require.NoError(t, err)
   131  
   132  	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   133  	require.NoError(t, err)
   134  
   135  	accountID := *receipt.AccountID
   136  	require.NoError(t, err)
   137  
   138  	_, err = NewAccountUpdateTransaction().
   139  		SetAccountID(accountID).
   140  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   141  		SetKey(newKey2.PublicKey()).
   142  		Execute(env.Client)
   143  	require.NoError(t, err)
   144  
   145  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   146  	require.NoError(t, err)
   147  
   148  	info, err := NewAccountInfoQuery().
   149  		SetAccountID(accountID).
   150  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   151  		SetMaxQueryPayment(NewHbar(1)).
   152  		Execute(env.Client)
   153  	require.NoError(t, err)
   154  
   155  	assert.Equal(t, newKey.PublicKey().String(), info.Key.String())
   156  
   157  	txDelete, err := NewAccountDeleteTransaction().
   158  		SetAccountID(accountID).
   159  		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   160  		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   161  		FreezeWith(env.Client)
   162  
   163  	require.NoError(t, err)
   164  
   165  	txDelete.Sign(newKey)
   166  
   167  	resp, err = txDelete.Execute(env.Client)
   168  	require.NoError(t, err)
   169  
   170  	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   171  	require.NoError(t, err)
   172  
   173  	err = CloseIntegrationTestEnv(env, nil)
   174  	require.NoError(t, err)
   175  }
   176  
   177  func TestIntegrationAccountUpdateTransactionAccountIDNotSet(t *testing.T) {
   178  	t.Parallel()
   179  	env := NewIntegrationTestEnv(t)
   180  
   181  	_, err := NewAccountUpdateTransaction().
   182  		SetNodeAccountIDs(env.NodeAccountIDs).
   183  		Execute(env.Client)
   184  	assert.Error(t, err)
   185  	if err != nil {
   186  		assert.Contains(t, err.Error(), "exceptional precheck status ACCOUNT_ID_DOES_NOT_EXIST")
   187  	}
   188  
   189  	err = CloseIntegrationTestEnv(env, nil)
   190  	require.NoError(t, err)
   191  }
   192  
   193  // func TestAccountUpdateTransactionAddSignature_Execute(t *testing.T) {
   194  //	env := NewIntegrationTestEnv(t)
   195  //
   196  //	newKey, err := PrivateKeyGenerateEd25519()
   197  //	require.NoError(t, err)
   198  //
   199  //	newKey2, err := GeneratePrivateKey()
   200  //	require.NoError(t, err)
   201  //
   202  //	newBalance := NewHbar(2)
   203  //
   204  //	assert.Equal(t, 2*HbarUnits.Hbar._NumberOfTinybar(), newBalance.tinybar)
   205  //
   206  //	resp, err := NewAccountCreateTransaction().
   207  //		SetKey(newKey.PublicKey()).
   208  //		SetNodeAccountIDs(env.NodeAccountIDs).
   209  //		SetInitialBalance(newBalance).
   210  //		Execute(env.Client)
   211  //
   212  //	require.NoError(t, err)
   213  //
   214  //	receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client)
   215  //	require.NoError(t, err)
   216  //
   217  //	accountID := *receipt.AccountID
   218  //	require.NoError(t, err)
   219  //
   220  //	tx, err := NewAccountUpdateTransaction().
   221  //		SetAccountID(accountID).
   222  //		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   223  //		SetExpirationTime(time.Now().Add(time.Hour * 24 * 120)).
   224  //		SetTransactionID(TransactionIDGenerate(accountID)).
   225  //		SetKey(newKey2.PublicKey()).
   226  //		FreezeWith(env.Client)
   227  //	require.NoError(t, err)
   228  //
   229  //	updateBytes, err := tx.ToBytes()
   230  //	require.NoError(t, err)
   231  //
   232  //	sig1, err := newKey.SignTransaction(&tx.Transaction)
   233  //	require.NoError(t, err)
   234  //	sig2, err := newKey2.SignTransaction(&tx.Transaction)
   235  //	require.NoError(t, err)
   236  //
   237  //	tx2, err := TransactionFromBytes(updateBytes)
   238  //	require.NoError(t, err)
   239  //
   240  //	switch newTx := tx2.(type) {
   241  //	case AccountUpdateTransaction:
   242  //		resp, err = newTx.AddSignature(newKey.PublicKey(), sig1).AddSignature(newKey2.PublicKey(), sig2).Execute(env.Client)
   243  //		require.NoError(t, err)
   244  //	}
   245  //
   246  //	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   247  //	require.NoError(t, err)
   248  //
   249  //	info, err := NewAccountInfoQuery().
   250  //		SetAccountID(accountID).
   251  //		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   252  //		SetMaxQueryPayment(NewHbar(1)).
   253  //		Execute(env.Client)
   254  //	require.NoError(t, err)
   255  //
   256  //	assert.Equal(t, newKey2.PublicKey().String(), info.Key.String())
   257  //
   258  //	txDelete, err := NewAccountDeleteTransaction().
   259  //		SetAccountID(accountID).
   260  //		SetTransferAccountID(env.Client.GetOperatorAccountID()).
   261  //		SetNodeAccountIDs([]AccountID{resp.NodeID}).
   262  //		FreezeWith(env.Client)
   263  //
   264  //	require.NoError(t, err)
   265  //
   266  //	txDelete.Sign(newKey2)
   267  //
   268  //	resp, err = txDelete.Execute(env.Client)
   269  //	require.NoError(t, err)
   270  //
   271  //	_, err = resp.SetValidateStatus(true).GetReceipt(env.Client)
   272  //
   273  //	require.NoError(t, err)
   274  //
   275  //	err = CloseIntegrationTestEnv(env, nil)
   276  //	require.NoError(t, err)
   277  //}