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

     1  //go:build all || unit
     2  // +build all unit
     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/hashgraph/hedera-protobufs-go/services"
    31  	protobuf "google.golang.org/protobuf/proto"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  func TestUnitTokenUpdateTransactionValidate(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	client, err := _NewMockClient()
    42  	client.SetLedgerID(*NewLedgerIDTestnet())
    43  	require.NoError(t, err)
    44  	client.SetAutoValidateChecksums(true)
    45  	accountID, err := AccountIDFromString("0.0.123-esxsf")
    46  	require.NoError(t, err)
    47  	tokenID, err := TokenIDFromString("0.0.123-esxsf")
    48  	require.NoError(t, err)
    49  
    50  	tokenUpdate := NewTokenUpdateTransaction().
    51  		SetTokenID(tokenID).
    52  		SetAutoRenewAccount(accountID).
    53  		SetTreasuryAccountID(accountID)
    54  
    55  	err = tokenUpdate.validateNetworkOnIDs(client)
    56  	require.NoError(t, err)
    57  }
    58  
    59  func TestUnitTokenUpdateTransactionValidateWrong(t *testing.T) {
    60  	t.Parallel()
    61  
    62  	client, err := _NewMockClient()
    63  	client.SetLedgerID(*NewLedgerIDTestnet())
    64  	require.NoError(t, err)
    65  	client.SetAutoValidateChecksums(true)
    66  	accountID, err := AccountIDFromString("0.0.123-rmkykd")
    67  	require.NoError(t, err)
    68  	tokenID, err := TokenIDFromString("0.0.123-rmkykd")
    69  	require.NoError(t, err)
    70  
    71  	tokenUpdate := NewTokenUpdateTransaction().
    72  		SetTokenID(tokenID).
    73  		SetAutoRenewAccount(accountID).
    74  		SetTreasuryAccountID(accountID)
    75  
    76  	err = tokenUpdate.validateNetworkOnIDs(client)
    77  	assert.Error(t, err)
    78  	if err != nil {
    79  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    80  	}
    81  }
    82  
    83  func TestUnitTokenUpdateTransactionGet(t *testing.T) {
    84  	t.Parallel()
    85  
    86  	checksum := "dmqui"
    87  	grpc := time.Second * 30
    88  	accountID := AccountID{Account: 3, checksum: &checksum}
    89  	token := TokenID{Token: 3, checksum: &checksum}
    90  
    91  	newKey, err := PrivateKeyGenerateEd25519()
    92  	require.NoError(t, err)
    93  
    94  	nodeAccountID := []AccountID{{Account: 10}}
    95  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
    96  
    97  	client, err := _NewMockClient()
    98  	client.SetLedgerID(*NewLedgerIDTestnet())
    99  	require.NoError(t, err)
   100  	client.SetAutoValidateChecksums(true)
   101  
   102  	transaction, err := NewTokenUpdateTransaction().
   103  		SetTransactionID(transactionID).
   104  		SetNodeAccountIDs(nodeAccountID).
   105  		SetTokenName("ffff").
   106  		SetTokenSymbol("F").
   107  		SetTokenMemo("fnord").
   108  		SetTokenMetadata([]byte{1, 2, 3}).
   109  		SetTreasuryAccountID(accountID).
   110  		SetTokenID(token).
   111  		SetAdminKey(newKey).
   112  		SetFreezeKey(newKey).
   113  		SetWipeKey(newKey).
   114  		SetKycKey(newKey).
   115  		SetSupplyKey(newKey).
   116  		SetPauseKey(newKey).
   117  		SetMetadataKey(newKey).
   118  		SetKeyVerificationMode(NO_VALIDATION).
   119  		SetExpirationTime(time.Now()).
   120  		SetAutoRenewPeriod(60 * time.Second).
   121  		SetAutoRenewAccount(accountID).
   122  		SetMaxTransactionFee(NewHbar(10)).
   123  		SetTransactionMemo("").
   124  		SetTransactionValidDuration(60 * time.Second).
   125  		SetRegenerateTransactionID(false).
   126  		SetGrpcDeadline(&grpc).
   127  		SetMaxRetry(3).
   128  		SetMaxBackoff(time.Second * 30).
   129  		SetMinBackoff(time.Second * 10).
   130  		Freeze()
   131  	require.NoError(t, err)
   132  
   133  	transaction.GetTransactionID()
   134  	transaction.GetNodeAccountIDs()
   135  
   136  	_, err = transaction.GetTransactionHash()
   137  	require.NoError(t, err)
   138  
   139  	err = transaction.validateNetworkOnIDs(client)
   140  	require.NoError(t, err)
   141  	_, err = transaction.Schedule()
   142  	require.NoError(t, err)
   143  	transaction.GetTokenName()
   144  	transaction.GetTokenSymbol()
   145  	transaction.GetTreasuryAccountID()
   146  	transaction.GetAdminKey()
   147  	transaction.GetFreezeKey()
   148  	transaction.GetWipeKey()
   149  	transaction.GetKycKey()
   150  	transaction.GetSupplyKey()
   151  	transaction.GetPauseKey()
   152  	transaction.GetMetadataKey()
   153  	transaction.GetKeyVerificationMode()
   154  	transaction.GetExpirationTime()
   155  	transaction.GetMaxTransactionFee()
   156  	transaction.GetTransactionMemo()
   157  	transaction.GetRegenerateTransactionID()
   158  	_, err = transaction.GetSignatures()
   159  	require.NoError(t, err)
   160  	transaction.GetRegenerateTransactionID()
   161  	transaction.GetMaxTransactionFee()
   162  	transaction.GetRegenerateTransactionID()
   163  	byt, err := transaction.ToBytes()
   164  	require.NoError(t, err)
   165  	txFromBytes, err := TransactionFromBytes(byt)
   166  	require.NoError(t, err)
   167  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   168  	require.NoError(t, err)
   169  	transaction.getName()
   170  	transaction.GetMaxRetry()
   171  	transaction.GetMaxBackoff()
   172  	transaction.GetMinBackoff()
   173  	switch b := txFromBytes.(type) {
   174  	case TokenUpdateTransaction:
   175  		b.AddSignature(newKey.PublicKey(), sig)
   176  	}
   177  }
   178  
   179  func TestUnitTokenUpdateTransactionNothingSet(t *testing.T) {
   180  	t.Parallel()
   181  
   182  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   183  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   184  
   185  	transaction, err := NewTokenUpdateTransaction().
   186  		SetTransactionID(transactionID).
   187  		SetNodeAccountIDs(nodeAccountID).
   188  		Freeze()
   189  	require.NoError(t, err)
   190  
   191  	transaction.GetTransactionID()
   192  	transaction.GetNodeAccountIDs()
   193  
   194  	_, err = transaction.GetTransactionHash()
   195  	require.NoError(t, err)
   196  
   197  	transaction.GetTokenName()
   198  	transaction.GetTokenSymbol()
   199  	transaction.GetTreasuryAccountID()
   200  	transaction.GetAdminKey()
   201  	transaction.GetFreezeKey()
   202  	transaction.GetWipeKey()
   203  	transaction.GetKycKey()
   204  	transaction.GetSupplyKey()
   205  	transaction.GetPauseKey()
   206  	transaction.GetMetadataKey()
   207  	transaction.GetKeyVerificationMode()
   208  	transaction.GetExpirationTime()
   209  	transaction.GetMaxTransactionFee()
   210  	transaction.GetTransactionMemo()
   211  	transaction.GetRegenerateTransactionID()
   212  	_, err = transaction.GetSignatures()
   213  	require.NoError(t, err)
   214  	transaction.GetRegenerateTransactionID()
   215  	transaction.GetMaxTransactionFee()
   216  	transaction.GetRegenerateTransactionID()
   217  	proto := transaction.build().GetTokenUpdate()
   218  	require.Nil(t, proto.Token)
   219  	require.Nil(t, proto.AutoRenewAccount)
   220  	require.Nil(t, proto.AdminKey)
   221  	require.Nil(t, proto.Expiry)
   222  	require.Nil(t, proto.FeeScheduleKey)
   223  	require.Nil(t, proto.FreezeKey)
   224  	require.Nil(t, proto.KycKey)
   225  	require.Nil(t, proto.FeeScheduleKey)
   226  	require.Nil(t, proto.PauseKey)
   227  	require.Nil(t, proto.SupplyKey)
   228  	require.Nil(t, proto.MetadataKey)
   229  	require.Nil(t, proto.Treasury)
   230  }
   231  
   232  func TestUnitTokenUpdateTransactionKeyCheck(t *testing.T) {
   233  	t.Parallel()
   234  
   235  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   236  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   237  
   238  	keys := make([]PrivateKey, 8)
   239  	var err error
   240  
   241  	for i := 0; i < len(keys); i++ {
   242  		keys[i], err = PrivateKeyGenerateEd25519()
   243  		require.NoError(t, err)
   244  	}
   245  
   246  	transaction, err := NewTokenUpdateTransaction().
   247  		SetTransactionID(transactionID).
   248  		SetNodeAccountIDs(nodeAccountID).
   249  		SetAdminKey(keys[0]).
   250  		SetFreezeKey(keys[1]).
   251  		SetWipeKey(keys[2]).
   252  		SetKycKey(keys[3]).
   253  		SetSupplyKey(keys[4]).
   254  		SetPauseKey(keys[5]).
   255  		SetFeeScheduleKey(keys[6]).
   256  		SetMetadataKey(keys[7]).
   257  		Freeze()
   258  	require.NoError(t, err)
   259  
   260  	transaction.GetTransactionID()
   261  	transaction.GetNodeAccountIDs()
   262  
   263  	proto := transaction.build().GetTokenUpdate()
   264  	require.Equal(t, proto.AdminKey.String(), keys[0]._ToProtoKey().String())
   265  	require.Equal(t, proto.FreezeKey.String(), keys[1]._ToProtoKey().String())
   266  	require.Equal(t, proto.WipeKey.String(), keys[2]._ToProtoKey().String())
   267  	require.Equal(t, proto.KycKey.String(), keys[3]._ToProtoKey().String())
   268  	require.Equal(t, proto.SupplyKey.String(), keys[4]._ToProtoKey().String())
   269  	require.Equal(t, proto.PauseKey.String(), keys[5]._ToProtoKey().String())
   270  	require.Equal(t, proto.FeeScheduleKey.String(), keys[6]._ToProtoKey().String())
   271  	require.Equal(t, proto.MetadataKey.String(), keys[7]._ToProtoKey().String())
   272  }
   273  
   274  func TestUnitTokenUpdateTransactionMock(t *testing.T) {
   275  	t.Parallel()
   276  
   277  	newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98")
   278  	require.NoError(t, err)
   279  
   280  	call := func(request *services.Transaction) *services.TransactionResponse {
   281  		require.NotEmpty(t, request.SignedTransactionBytes)
   282  		signedTransaction := services.SignedTransaction{}
   283  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
   284  
   285  		require.NotEmpty(t, signedTransaction.BodyBytes)
   286  		transactionBody := services.TransactionBody{}
   287  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
   288  
   289  		require.NotNil(t, transactionBody.TransactionID)
   290  		transactionId := transactionBody.TransactionID.String()
   291  		require.NotEqual(t, "", transactionId)
   292  
   293  		sigMap := signedTransaction.GetSigMap()
   294  		require.NotNil(t, sigMap)
   295  
   296  		return &services.TransactionResponse{
   297  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   298  		}
   299  	}
   300  	responses := [][]interface{}{{
   301  		call,
   302  	}}
   303  
   304  	client, server := NewMockClientAndServer(responses)
   305  	defer server.Close()
   306  
   307  	checksum := "dmqui"
   308  	accountID := AccountID{Account: 3, checksum: &checksum}
   309  	token := TokenID{Token: 3, checksum: &checksum}
   310  
   311  	freez, err := NewTokenUpdateTransaction().
   312  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   313  		SetTokenID(token).
   314  		SetTreasuryAccountID(accountID).
   315  		SetAdminKey(newKey).
   316  		SetFreezeKey(newKey).
   317  		SetWipeKey(newKey).
   318  		SetKycKey(newKey).
   319  		SetSupplyKey(newKey).
   320  		SetPauseKey(newKey).
   321  		SetMetadataKey(newKey).
   322  		FreezeWith(client)
   323  	require.NoError(t, err)
   324  
   325  	_, err = freez.Sign(newKey).Execute(client)
   326  	require.NoError(t, err)
   327  }