github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_dissociate_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 TestUnitTokenDissociateTransactionValidate(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 tokenDissociate := NewTokenDissociateTransaction(). 52 SetAccountID(accountID). 53 SetTokenIDs(tokenID) 54 55 err = tokenDissociate.validateNetworkOnIDs(client) 56 require.NoError(t, err) 57 } 58 59 func TestUnitTokenDissociateTransactionValidateWrong(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 tokenDissociate := NewTokenDissociateTransaction(). 72 SetAccountID(accountID). 73 SetTokenIDs(tokenID) 74 75 err = tokenDissociate.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 TestUnitTokenDissociateTransactionGet(t *testing.T) { 83 t.Parallel() 84 85 tokenID := TokenID{Token: 7} 86 87 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 88 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 89 90 transaction, err := NewTokenDissociateTransaction(). 91 SetTransactionID(transactionID). 92 SetNodeAccountIDs(nodeAccountID). 93 SetTokenIDs(tokenID, TokenID{Token: 4}, TokenID{Token: 3}). 94 SetAccountID(AccountID{Account: 3}). 95 SetMaxTransactionFee(NewHbar(10)). 96 SetTransactionMemo(""). 97 SetTransactionValidDuration(60 * time.Second). 98 SetRegenerateTransactionID(false). 99 Freeze() 100 require.NoError(t, err) 101 102 transaction.GetTransactionID() 103 transaction.GetNodeAccountIDs() 104 105 _, err = transaction.GetTransactionHash() 106 require.NoError(t, err) 107 108 transaction.GetTokenIDs() 109 transaction.GetAccountID() 110 transaction.GetMaxTransactionFee() 111 transaction.GetTransactionMemo() 112 transaction.GetRegenerateTransactionID() 113 _, err = transaction.GetSignatures() 114 require.NoError(t, err) 115 transaction.GetRegenerateTransactionID() 116 transaction.GetMaxTransactionFee() 117 transaction.GetRegenerateTransactionID() 118 } 119 120 func TestUnitTokenDissociateTransactionNothingSet(t *testing.T) { 121 t.Parallel() 122 123 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 124 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 125 126 transaction, err := NewTokenDissociateTransaction(). 127 SetTransactionID(transactionID). 128 SetNodeAccountIDs(nodeAccountID). 129 Freeze() 130 require.NoError(t, err) 131 132 transaction.GetTransactionID() 133 transaction.GetNodeAccountIDs() 134 135 _, err = transaction.GetTransactionHash() 136 require.NoError(t, err) 137 138 transaction.GetTokenIDs() 139 transaction.GetAccountID() 140 transaction.GetMaxTransactionFee() 141 transaction.GetTransactionMemo() 142 transaction.GetRegenerateTransactionID() 143 _, err = transaction.GetSignatures() 144 require.NoError(t, err) 145 transaction.GetRegenerateTransactionID() 146 transaction.GetMaxTransactionFee() 147 transaction.GetRegenerateTransactionID() 148 } 149 150 func TestUnitTokenDissociateTransactionProtoCheck(t *testing.T) { 151 t.Parallel() 152 153 tokenID := TokenID{Token: 7} 154 tokenID2 := TokenID{Token: 4} 155 tokenID3 := TokenID{Token: 3} 156 accountID := AccountID{Account: 7} 157 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 158 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 159 160 transaction, err := NewTokenDissociateTransaction(). 161 SetTransactionID(transactionID). 162 SetNodeAccountIDs(nodeAccountID). 163 SetTokenIDs(tokenID, tokenID2, tokenID3). 164 SetAccountID(accountID). 165 SetMaxTransactionFee(NewHbar(10)). 166 SetTransactionMemo(""). 167 SetTransactionValidDuration(60 * time.Second). 168 SetRegenerateTransactionID(false). 169 Freeze() 170 require.NoError(t, err) 171 172 transaction.GetTransactionID() 173 transaction.GetNodeAccountIDs() 174 175 proto := transaction.build().GetTokenDissociate() 176 require.Equal(t, proto.Tokens[0].String(), tokenID._ToProtobuf().String()) 177 require.Equal(t, proto.Tokens[1].String(), tokenID2._ToProtobuf().String()) 178 require.Equal(t, proto.Tokens[2].String(), tokenID3._ToProtobuf().String()) 179 require.Equal(t, proto.Account.String(), accountID._ToProtobuf().String()) 180 } 181 182 func TestUnitTokenDissociateTransactionCoverage(t *testing.T) { 183 t.Parallel() 184 185 checksum := "dmqui" 186 grpc := time.Second * 30 187 token := TokenID{Token: 3, checksum: &checksum} 188 account := AccountID{Account: 3, checksum: &checksum} 189 nodeAccountID := []AccountID{{Account: 10}} 190 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 191 192 newKey, err := PrivateKeyGenerateEd25519() 193 require.NoError(t, err) 194 195 client, err := _NewMockClient() 196 client.SetLedgerID(*NewLedgerIDTestnet()) 197 require.NoError(t, err) 198 client.SetAutoValidateChecksums(true) 199 200 transaction, err := NewTokenDissociateTransaction(). 201 SetTransactionID(transactionID). 202 SetNodeAccountIDs(nodeAccountID). 203 SetTokenIDs(token). 204 AddTokenID(token). 205 SetAccountID(account). 206 SetGrpcDeadline(&grpc). 207 SetMaxTransactionFee(NewHbar(3)). 208 SetMaxRetry(3). 209 SetMaxBackoff(time.Second * 30). 210 SetMinBackoff(time.Second * 10). 211 SetTransactionMemo("no"). 212 SetTransactionValidDuration(time.Second * 30). 213 SetRegenerateTransactionID(false). 214 Freeze() 215 require.NoError(t, err) 216 217 err = transaction.validateNetworkOnIDs(client) 218 require.NoError(t, err) 219 _, err = transaction.Schedule() 220 require.NoError(t, err) 221 transaction.GetTransactionID() 222 transaction.GetNodeAccountIDs() 223 transaction.GetMaxRetry() 224 transaction.GetMaxTransactionFee() 225 transaction.GetMaxBackoff() 226 transaction.GetMinBackoff() 227 transaction.GetRegenerateTransactionID() 228 byt, err := transaction.ToBytes() 229 require.NoError(t, err) 230 txFromBytes, err := TransactionFromBytes(byt) 231 require.NoError(t, err) 232 sig, err := newKey.SignTransaction(&transaction.Transaction) 233 require.NoError(t, err) 234 235 _, err = transaction.GetTransactionHash() 236 require.NoError(t, err) 237 transaction.GetMaxTransactionFee() 238 transaction.GetTransactionMemo() 239 transaction.GetRegenerateTransactionID() 240 transaction.GetTokenIDs() 241 transaction.GetAccountID() 242 _, err = transaction.GetSignatures() 243 require.NoError(t, err) 244 transaction.getName() 245 switch b := txFromBytes.(type) { 246 case TokenDissociateTransaction: 247 b.AddSignature(newKey.PublicKey(), sig) 248 } 249 } 250 251 func TestUnitTokenDissociateTransactionMock(t *testing.T) { 252 t.Parallel() 253 254 newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98") 255 require.NoError(t, err) 256 257 call := func(request *services.Transaction) *services.TransactionResponse { 258 require.NotEmpty(t, request.SignedTransactionBytes) 259 signedTransaction := services.SignedTransaction{} 260 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 261 262 require.NotEmpty(t, signedTransaction.BodyBytes) 263 transactionBody := services.TransactionBody{} 264 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 265 266 require.NotNil(t, transactionBody.TransactionID) 267 transactionId := transactionBody.TransactionID.String() 268 require.NotEqual(t, "", transactionId) 269 270 sigMap := signedTransaction.GetSigMap() 271 require.NotNil(t, sigMap) 272 273 key, _ := PrivateKeyFromStringEd25519("302e020100300506032b657004220420d45e1557156908c967804615af59a000be88c7aa7058bfcbe0f46b16c28f887d") 274 for _, sigPair := range sigMap.SigPair { 275 verified := false 276 277 switch k := sigPair.Signature.(type) { 278 case *services.SignaturePair_Ed25519: 279 pbTemp, _ := PublicKeyFromBytesEd25519(sigPair.PubKeyPrefix) 280 verified = pbTemp.Verify(signedTransaction.BodyBytes, k.Ed25519) 281 case *services.SignaturePair_ECDSASecp256K1: 282 pbTemp, _ := PublicKeyFromBytesECDSA(sigPair.PubKeyPrefix) 283 verified = pbTemp.Verify(signedTransaction.BodyBytes, k.ECDSASecp256K1) 284 } 285 require.True(t, verified) 286 } 287 288 require.Equal(t, bytes.Compare(sigMap.SigPair[1].PubKeyPrefix, key.PublicKey().BytesRaw()), 0) 289 require.Equal(t, bytes.Compare(sigMap.SigPair[0].PubKeyPrefix, newKey.PublicKey().BytesRaw()), 0) 290 291 if bod, ok := transactionBody.Data.(*services.TransactionBody_TokenAssociate); ok { 292 require.Equal(t, bod.TokenAssociate.Account.GetAccountNum(), int64(123)) 293 require.Equal(t, bod.TokenAssociate.Tokens[0].TokenNum, int64(123)) 294 } 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 freez, err := NewTokenDissociateTransaction(). 308 SetNodeAccountIDs([]AccountID{{Account: 3}}). 309 SetAccountID(AccountID{Account: 123}). 310 SetTokenIDs(TokenID{Token: 123}). 311 FreezeWith(client) 312 require.NoError(t, err) 313 314 _, err = freez. 315 Sign(newKey). 316 Execute(client) 317 require.NoError(t, err) 318 }