github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_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 TestUnitAccountUpdateTransactionValidate(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 48 accountUpdate := NewAccountUpdateTransaction(). 49 SetProxyAccountID(accountID) 50 51 err = accountUpdate.validateNetworkOnIDs(client) 52 require.NoError(t, err) 53 } 54 55 func TestUnitAccountUpdateTransactionValidateWrong(t *testing.T) { 56 t.Parallel() 57 58 client, err := _NewMockClient() 59 client.SetLedgerID(*NewLedgerIDTestnet()) 60 require.NoError(t, err) 61 client.SetAutoValidateChecksums(true) 62 accountID, err := AccountIDFromString("0.0.123-rmkykd") 63 require.NoError(t, err) 64 65 accountUpdate := NewAccountUpdateTransaction(). 66 SetProxyAccountID(accountID) 67 68 err = accountUpdate.validateNetworkOnIDs(client) 69 assert.Error(t, err) 70 if err != nil { 71 assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error()) 72 } 73 } 74 75 func TestUnitAccountUpdateTransactionMock(t *testing.T) { 76 t.Parallel() 77 78 call := func(request *services.Transaction) *services.TransactionResponse { 79 require.NotEmpty(t, request.SignedTransactionBytes) 80 signedTransaction := services.SignedTransaction{} 81 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 82 83 require.NotEmpty(t, signedTransaction.BodyBytes) 84 transactionBody := services.TransactionBody{} 85 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 86 87 require.NotNil(t, transactionBody.TransactionID) 88 transactionId := transactionBody.TransactionID.String() 89 require.NotEqual(t, "", transactionId) 90 91 sigMap := signedTransaction.GetSigMap() 92 require.NotNil(t, sigMap) 93 94 for _, sigPair := range sigMap.SigPair { 95 verified := false 96 97 switch k := sigPair.Signature.(type) { 98 case *services.SignaturePair_Ed25519: 99 pbTemp, _ := PublicKeyFromBytesEd25519(sigPair.PubKeyPrefix) 100 verified = pbTemp.Verify(signedTransaction.BodyBytes, k.Ed25519) 101 case *services.SignaturePair_ECDSASecp256K1: 102 pbTemp, _ := PublicKeyFromBytesECDSA(sigPair.PubKeyPrefix) 103 verified = pbTemp.Verify(signedTransaction.BodyBytes, k.ECDSASecp256K1) 104 } 105 require.True(t, verified) 106 } 107 108 if bod, ok := transactionBody.Data.(*services.TransactionBody_CryptoUpdateAccount); ok { 109 require.Equal(t, bod.CryptoUpdateAccount.Memo.Value, "no") 110 require.Equal(t, bod.CryptoUpdateAccount.AccountIDToUpdate.GetAccountNum(), int64(123)) 111 //alias := services.Key{} 112 //_ = protobuf.Unmarshal(bod.CryptoUpdateAccount.Alias, &alias) 113 //require.Equal(t, hex.EncodeToString(alias.GetEd25519()), "1480272863d39c42f902bc11601a968eaf30ad662694e3044c86d5df46fabfd2") 114 } 115 116 return &services.TransactionResponse{ 117 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 118 } 119 } 120 responses := [][]interface{}{{ 121 call, 122 }} 123 124 client, server := NewMockClientAndServer(responses) 125 defer server.Close() 126 //302a300506032b65700321001480272863d39c42f902bc11601a968eaf30ad662694e3044c86d5df46fabfd2 127 newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420278184257eb568d0e5fcfc1df99828b039b4776da05855dc5af105996e6200d1") 128 require.NoError(t, err) 129 130 tran := TransactionIDGenerate(AccountID{Account: 3}) 131 132 _, err = NewAccountUpdateTransaction(). 133 SetNodeAccountIDs([]AccountID{{Account: 3}}). 134 SetTransactionID(tran). 135 SetAccountMemo("no"). 136 SetAccountID(AccountID{Account: 123}). 137 SetAliasKey(newKey.PublicKey()). 138 Execute(client) 139 require.NoError(t, err) 140 } 141 142 func TestUnitAccountUpdateTransactionGet(t *testing.T) { 143 t.Parallel() 144 145 spenderAccountID1 := AccountID{Account: 7} 146 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 147 148 key, err := PrivateKeyGenerateEd25519() 149 150 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 151 152 transaction, err := NewAccountUpdateTransaction(). 153 SetTransactionID(transactionID). 154 SetNodeAccountIDs(nodeAccountID). 155 SetAccountID(spenderAccountID1). 156 SetKey(key). 157 SetProxyAccountID(spenderAccountID1). 158 SetAccountMemo(""). 159 SetReceiverSignatureRequired(true). 160 SetMaxAutomaticTokenAssociations(2). 161 SetAutoRenewPeriod(60 * time.Second). 162 SetTransactionMemo(""). 163 SetTransactionValidDuration(60 * time.Second). 164 Freeze() 165 require.NoError(t, err) 166 167 transaction.GetTransactionID() 168 transaction.GetNodeAccountIDs() 169 170 _, err = transaction.GetTransactionHash() 171 require.NoError(t, err) 172 173 transaction.GetAccountID() 174 transaction.GetMaxTransactionFee() 175 transaction.GetTransactionMemo() 176 transaction.GetRegenerateTransactionID() 177 _, err = transaction.GetSignatures() 178 require.NoError(t, err) 179 transaction.GetRegenerateTransactionID() 180 transaction.GetMaxTransactionFee() 181 transaction.GetMaxAutomaticTokenAssociations() 182 transaction.GetProxyAccountID() 183 transaction.GetRegenerateTransactionID() 184 transaction.GetKey() 185 transaction.GetAutoRenewPeriod() 186 transaction.GetReceiverSignatureRequired() 187 } 188 189 func TestUnitAccountUpdateTransactionSetNothing(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 := NewAccountUpdateTransaction(). 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.GetMaxTransactionFee() 209 transaction.GetTransactionMemo() 210 transaction.GetRegenerateTransactionID() 211 _, err = transaction.GetSignatures() 212 require.NoError(t, err) 213 transaction.GetRegenerateTransactionID() 214 transaction.GetMaxTransactionFee() 215 transaction.GetMaxAutomaticTokenAssociations() 216 transaction.GetProxyAccountID() 217 transaction.GetRegenerateTransactionID() 218 transaction.GetKey() 219 transaction.GetAutoRenewPeriod() 220 transaction.GetReceiverSignatureRequired() 221 } 222 223 func TestUnitAccountUpdateTransactionProtoCheck(t *testing.T) { 224 t.Parallel() 225 226 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 227 stackedAccountID := AccountID{Account: 5} 228 accountID := AccountID{Account: 6} 229 230 key, err := PrivateKeyGenerateEd25519() 231 require.NoError(t, err) 232 233 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 234 235 transaction, err := NewAccountUpdateTransaction(). 236 SetTransactionID(transactionID). 237 SetNodeAccountIDs(nodeAccountID). 238 SetKey(key). 239 SetAccountID(accountID). 240 SetAccountMemo("ty"). 241 SetReceiverSignatureRequired(true). 242 SetMaxAutomaticTokenAssociations(2). 243 SetStakedAccountID(stackedAccountID). 244 SetDeclineStakingReward(true). 245 SetAutoRenewPeriod(60 * time.Second). 246 SetExpirationTime(time.Unix(34, 56)). 247 SetTransactionMemo(""). 248 SetTransactionValidDuration(60 * time.Second). 249 Freeze() 250 require.NoError(t, err) 251 252 transaction.GetTransactionID() 253 transaction.GetNodeAccountIDs() 254 255 proto := transaction.build().GetCryptoUpdateAccount() 256 require.Equal(t, proto.AccountIDToUpdate.String(), accountID._ToProtobuf().String()) 257 require.Equal(t, proto.Key.String(), key._ToProtoKey().String()) 258 require.Equal(t, proto.Memo.Value, "ty") 259 require.Equal(t, proto.ReceiverSigRequiredField.(*services.CryptoUpdateTransactionBody_ReceiverSigRequiredWrapper).ReceiverSigRequiredWrapper.Value, true) 260 require.Equal(t, proto.MaxAutomaticTokenAssociations.GetValue(), int32(2)) 261 require.Equal(t, proto.StakedId.(*services.CryptoUpdateTransactionBody_StakedAccountId).StakedAccountId.String(), 262 stackedAccountID._ToProtobuf().String()) 263 require.Equal(t, proto.DeclineReward.Value, true) 264 require.Equal(t, proto.AutoRenewPeriod.String(), _DurationToProtobuf(60*time.Second).String()) 265 require.Equal(t, proto.ExpirationTime.String(), _TimeToProtobuf(time.Unix(34, 56)).String()) 266 } 267 268 func TestUnitAccountUpdateTransactionCoverage(t *testing.T) { 269 t.Parallel() 270 271 checksum := "dmqui" 272 grpc := time.Second * 30 273 account := AccountID{Account: 3, checksum: &checksum} 274 nodeAccountID := []AccountID{{Account: 10}} 275 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 276 277 key, err := PrivateKeyGenerateEd25519() 278 require.NoError(t, err) 279 280 client, err := _NewMockClient() 281 client.SetLedgerID(*NewLedgerIDTestnet()) 282 require.NoError(t, err) 283 client.SetAutoValidateChecksums(true) 284 285 transaction, err := NewAccountUpdateTransaction(). 286 SetTransactionID(transactionID). 287 SetNodeAccountIDs(nodeAccountID). 288 SetKey(key). 289 SetAccountID(account). 290 SetAccountMemo("ty"). 291 SetReceiverSignatureRequired(true). 292 SetMaxAutomaticTokenAssociations(2). 293 SetStakedAccountID(account). 294 SetStakedNodeID(4). 295 SetDeclineStakingReward(true). 296 SetAutoRenewPeriod(60 * time.Second). 297 SetTransactionMemo(""). 298 SetTransactionValidDuration(60 * time.Second). 299 SetMaxTransactionFee(NewHbar(3)). 300 SetMaxRetry(3). 301 SetMaxBackoff(time.Second * 30). 302 SetMinBackoff(time.Second * 10). 303 SetTransactionMemo("no"). 304 SetTransactionValidDuration(time.Second * 30). 305 SetRegenerateTransactionID(false). 306 SetGrpcDeadline(&grpc). 307 Freeze() 308 require.NoError(t, err) 309 310 transaction.validateNetworkOnIDs(client) 311 312 _, err = transaction.Schedule() 313 require.NoError(t, err) 314 transaction.GetTransactionID() 315 transaction.GetNodeAccountIDs() 316 transaction.GetMaxRetry() 317 transaction.GetMaxTransactionFee() 318 transaction.GetMaxBackoff() 319 transaction.GetMinBackoff() 320 transaction.GetRegenerateTransactionID() 321 byt, err := transaction.ToBytes() 322 require.NoError(t, err) 323 txFromBytes, err := TransactionFromBytes(byt) 324 require.NoError(t, err) 325 sig, err := key.SignTransaction(&transaction.Transaction) 326 require.NoError(t, err) 327 328 _, err = transaction.GetTransactionHash() 329 require.NoError(t, err) 330 transaction.GetMaxTransactionFee() 331 transaction.GetTransactionMemo() 332 transaction.GetRegenerateTransactionID() 333 transaction.GetStakedAccountID() 334 transaction.GetStakedNodeID() 335 transaction.ClearStakedAccountID() 336 transaction.ClearStakedNodeID() 337 transaction.GetDeclineStakingReward() 338 transaction.GetExpirationTime() 339 transaction.GetAccountMemo() 340 341 _, err = transaction.GetSignatures() 342 require.NoError(t, err) 343 transaction.getName() 344 switch b := txFromBytes.(type) { 345 case *AccountUpdateTransaction: 346 b.AddSignature(key.PublicKey(), sig) 347 } 348 }