github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_associate_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  	"bytes"
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/hashgraph/hedera-protobufs-go/services"
    32  	protobuf "google.golang.org/protobuf/proto"
    33  
    34  	"github.com/stretchr/testify/assert"
    35  
    36  	"github.com/stretchr/testify/require"
    37  )
    38  
    39  func TestUnitTokenAssociateTransactionValidate(t *testing.T) {
    40  	t.Parallel()
    41  
    42  	client, err := _NewMockClient()
    43  	client.SetLedgerID(*NewLedgerIDTestnet())
    44  	require.NoError(t, err)
    45  	client.SetAutoValidateChecksums(true)
    46  	accountID, err := AccountIDFromString("0.0.123-esxsf")
    47  	require.NoError(t, err)
    48  	tokenID, err := TokenIDFromString("0.0.123-esxsf")
    49  	require.NoError(t, err)
    50  
    51  	tokenAssociate := NewTokenAssociateTransaction().
    52  		SetAccountID(accountID).
    53  		SetTokenIDs(tokenID)
    54  
    55  	err = tokenAssociate.validateNetworkOnIDs(client)
    56  	require.NoError(t, err)
    57  }
    58  
    59  func TestUnitTokenAssociateTransactionValidateWrong(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  	tokenAssociate := NewTokenAssociateTransaction().
    72  		SetAccountID(accountID).
    73  		SetTokenIDs(tokenID)
    74  
    75  	err = tokenAssociate.validateNetworkOnIDs(client)
    76  	assert.Error(t, err)
    77  	if err != nil {
    78  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    79  	}
    80  }
    81  
    82  func TestUnitTokenAssociateTransactionMock(t *testing.T) {
    83  	t.Parallel()
    84  
    85  	newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98")
    86  	require.NoError(t, err)
    87  
    88  	call := func(request *services.Transaction) *services.TransactionResponse {
    89  		require.NotEmpty(t, request.SignedTransactionBytes)
    90  		signedTransaction := services.SignedTransaction{}
    91  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
    92  
    93  		require.NotEmpty(t, signedTransaction.BodyBytes)
    94  		transactionBody := services.TransactionBody{}
    95  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
    96  
    97  		require.NotNil(t, transactionBody.TransactionID)
    98  		transactionId := transactionBody.TransactionID.String()
    99  		require.NotEqual(t, "", transactionId)
   100  
   101  		sigMap := signedTransaction.GetSigMap()
   102  		require.NotNil(t, sigMap)
   103  
   104  		key, _ := PrivateKeyFromStringEd25519("302e020100300506032b657004220420d45e1557156908c967804615af59a000be88c7aa7058bfcbe0f46b16c28f887d")
   105  		for _, sigPair := range sigMap.SigPair {
   106  			verified := false
   107  
   108  			switch k := sigPair.Signature.(type) {
   109  			case *services.SignaturePair_Ed25519:
   110  				pbTemp, _ := PublicKeyFromBytesEd25519(sigPair.PubKeyPrefix)
   111  				verified = pbTemp.Verify(signedTransaction.BodyBytes, k.Ed25519)
   112  			case *services.SignaturePair_ECDSASecp256K1:
   113  				pbTemp, _ := PublicKeyFromBytesECDSA(sigPair.PubKeyPrefix)
   114  				verified = pbTemp.Verify(signedTransaction.BodyBytes, k.ECDSASecp256K1)
   115  			}
   116  			require.True(t, verified)
   117  		}
   118  
   119  		require.Equal(t, bytes.Compare(sigMap.SigPair[1].PubKeyPrefix, key.PublicKey().BytesRaw()), 0)
   120  		require.Equal(t, bytes.Compare(sigMap.SigPair[0].PubKeyPrefix, newKey.PublicKey().BytesRaw()), 0)
   121  
   122  		if bod, ok := transactionBody.Data.(*services.TransactionBody_TokenAssociate); ok {
   123  			require.Equal(t, bod.TokenAssociate.Account.GetAccountNum(), int64(123))
   124  			require.Equal(t, bod.TokenAssociate.Tokens[0].TokenNum, int64(123))
   125  		}
   126  
   127  		return &services.TransactionResponse{
   128  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   129  		}
   130  	}
   131  	responses := [][]interface{}{{
   132  		call,
   133  	}}
   134  
   135  	client, server := NewMockClientAndServer(responses)
   136  	defer server.Close()
   137  
   138  	freez, err := NewTokenAssociateTransaction().
   139  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   140  		SetAccountID(AccountID{Account: 123}).
   141  		SetTokenIDs(TokenID{Token: 123}).
   142  		FreezeWith(client)
   143  	require.NoError(t, err)
   144  
   145  	_, err = freez.
   146  		Sign(newKey).
   147  		Execute(client)
   148  	require.NoError(t, err)
   149  }
   150  
   151  func TestUnitTokenAssociateTransactionGet(t *testing.T) {
   152  	t.Parallel()
   153  
   154  	tokenIDs := []TokenID{{Token: 3}, {Token: 5}, {Token: 7}}
   155  
   156  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   157  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   158  
   159  	transaction, err := NewTokenAssociateTransaction().
   160  		SetTransactionID(transactionID).
   161  		SetNodeAccountIDs(nodeAccountID).
   162  		SetTokenIDs(tokenIDs...).
   163  		SetAccountID(AccountID{Account: 7}).
   164  		SetMaxTransactionFee(NewHbar(10)).
   165  		SetTransactionMemo("").
   166  		SetTransactionValidDuration(60 * time.Second).
   167  		SetRegenerateTransactionID(false).
   168  		Freeze()
   169  	require.NoError(t, err)
   170  
   171  	transaction.GetTransactionID()
   172  	transaction.GetNodeAccountIDs()
   173  
   174  	_, err = transaction.GetTransactionHash()
   175  	require.NoError(t, err)
   176  
   177  	transaction.GetAccountID()
   178  	transaction.GetTokenIDs()
   179  	transaction.GetMaxTransactionFee()
   180  	transaction.GetTransactionMemo()
   181  	transaction.GetRegenerateTransactionID()
   182  	_, err = transaction.GetSignatures()
   183  	require.NoError(t, err)
   184  	transaction.GetRegenerateTransactionID()
   185  	transaction.GetMaxTransactionFee()
   186  	transaction.GetRegenerateTransactionID()
   187  }
   188  
   189  func TestUnitTokenAssociateTransactionNothingSet(t *testing.T) {
   190  	t.Parallel()
   191  
   192  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   193  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   194  
   195  	transaction, err := NewTokenAssociateTransaction().
   196  		SetTransactionID(transactionID).
   197  		SetNodeAccountIDs(nodeAccountID).
   198  		Freeze()
   199  	require.NoError(t, err)
   200  
   201  	transaction.GetTransactionID()
   202  	transaction.GetNodeAccountIDs()
   203  
   204  	_, err = transaction.GetTransactionHash()
   205  	require.NoError(t, err)
   206  
   207  	transaction.GetAccountID()
   208  	transaction.GetTokenIDs()
   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  }
   218  
   219  func TestUnitTokenAssociateTransactionProtoCheck(t *testing.T) {
   220  	t.Parallel()
   221  
   222  	tokenID := TokenID{Token: 7}
   223  	tokenID2 := TokenID{Token: 4}
   224  	tokenID3 := TokenID{Token: 3}
   225  	accountID := AccountID{Account: 7}
   226  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   227  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   228  
   229  	transaction, err := NewTokenAssociateTransaction().
   230  		SetTransactionID(transactionID).
   231  		SetNodeAccountIDs(nodeAccountID).
   232  		SetTokenIDs(tokenID, tokenID2, tokenID3).
   233  		SetAccountID(accountID).
   234  		SetMaxTransactionFee(NewHbar(10)).
   235  		SetTransactionMemo("").
   236  		SetTransactionValidDuration(60 * time.Second).
   237  		SetRegenerateTransactionID(false).
   238  		Freeze()
   239  	require.NoError(t, err)
   240  
   241  	transaction.GetTransactionID()
   242  	transaction.GetNodeAccountIDs()
   243  
   244  	proto := transaction.build().GetTokenAssociate()
   245  	require.Equal(t, proto.Tokens[0].String(), tokenID._ToProtobuf().String())
   246  	require.Equal(t, proto.Tokens[1].String(), tokenID2._ToProtobuf().String())
   247  	require.Equal(t, proto.Tokens[2].String(), tokenID3._ToProtobuf().String())
   248  	require.Equal(t, proto.Account.String(), accountID._ToProtobuf().String())
   249  }
   250  
   251  func TestUnitTokenAssociateTransactionCoverage(t *testing.T) {
   252  	t.Parallel()
   253  
   254  	checksum := "dmqui"
   255  	grpc := time.Second * 30
   256  	token := TokenID{Token: 3, checksum: &checksum}
   257  	account := AccountID{Account: 3, checksum: &checksum}
   258  	nodeAccountID := []AccountID{{Account: 10}}
   259  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   260  
   261  	newKey, err := PrivateKeyGenerateEd25519()
   262  	require.NoError(t, err)
   263  
   264  	client, err := _NewMockClient()
   265  	client.SetLedgerID(*NewLedgerIDTestnet())
   266  	require.NoError(t, err)
   267  	client.SetAutoValidateChecksums(true)
   268  
   269  	transaction, err := NewTokenAssociateTransaction().
   270  		SetTransactionID(transactionID).
   271  		SetNodeAccountIDs(nodeAccountID).
   272  		SetTokenIDs(token).
   273  		AddTokenID(token).
   274  		SetAccountID(account).
   275  		SetGrpcDeadline(&grpc).
   276  		SetMaxTransactionFee(NewHbar(3)).
   277  		SetMaxRetry(3).
   278  		SetMaxBackoff(time.Second * 30).
   279  		SetMinBackoff(time.Second * 10).
   280  		SetTransactionMemo("no").
   281  		SetTransactionValidDuration(time.Second * 30).
   282  		SetRegenerateTransactionID(false).
   283  		Freeze()
   284  	require.NoError(t, err)
   285  
   286  	err = transaction.validateNetworkOnIDs(client)
   287  	require.NoError(t, err)
   288  	_, err = transaction.Schedule()
   289  	require.NoError(t, err)
   290  	transaction.GetTransactionID()
   291  	transaction.GetNodeAccountIDs()
   292  	transaction.GetMaxRetry()
   293  	transaction.GetMaxTransactionFee()
   294  	transaction.GetMaxBackoff()
   295  	transaction.GetMinBackoff()
   296  	transaction.GetRegenerateTransactionID()
   297  	byt, err := transaction.ToBytes()
   298  	require.NoError(t, err)
   299  	txFromBytes, err := TransactionFromBytes(byt)
   300  	require.NoError(t, err)
   301  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   302  	require.NoError(t, err)
   303  
   304  	_, err = transaction.GetTransactionHash()
   305  	require.NoError(t, err)
   306  	transaction.GetMaxTransactionFee()
   307  	transaction.GetTransactionMemo()
   308  	transaction.GetRegenerateTransactionID()
   309  	transaction.GetTokenIDs()
   310  	transaction.GetAccountID()
   311  	_, err = transaction.GetSignatures()
   312  	require.NoError(t, err)
   313  	transaction.getName()
   314  	switch b := txFromBytes.(type) {
   315  	case TokenAssociateTransaction:
   316  		b.AddSignature(newKey.PublicKey(), sig)
   317  	}
   318  }