github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/live_hash_add_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 TestUnitLiveHashAddTransactionValidate(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 addLiveHash := NewLiveHashAddTransaction(). 49 SetAccountID(accountID) 50 51 err = addLiveHash.validateNetworkOnIDs(client) 52 require.NoError(t, err) 53 } 54 55 func TestUnitLiveHashAddTransactionValidateWrong(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 addLiveHash := NewLiveHashAddTransaction(). 66 SetAccountID(accountID) 67 68 err = addLiveHash.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 TestUnitLiveHashAddTransactionGet(t *testing.T) { 76 t.Parallel() 77 78 accountID := AccountID{Account: 7} 79 80 newKey, err := PrivateKeyGenerateEd25519() 81 82 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 83 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 84 85 transaction, err := NewLiveHashAddTransaction(). 86 SetTransactionID(transactionID). 87 SetNodeAccountIDs(nodeAccountID). 88 SetKeys(newKey). 89 SetAccountID(accountID). 90 SetHash([]byte{}). 91 SetMaxTransactionFee(NewHbar(10)). 92 SetTransactionMemo(""). 93 SetTransactionValidDuration(60 * time.Second). 94 SetRegenerateTransactionID(false). 95 Freeze() 96 require.NoError(t, err) 97 98 transaction.GetTransactionID() 99 transaction.GetNodeAccountIDs() 100 101 _, err = transaction.GetTransactionHash() 102 require.NoError(t, err) 103 104 transaction.GetAccountID() 105 transaction.GetMaxTransactionFee() 106 transaction.GetTransactionMemo() 107 transaction.GetRegenerateTransactionID() 108 _, err = transaction.GetSignatures() 109 require.NoError(t, err) 110 transaction.GetRegenerateTransactionID() 111 transaction.GetMaxTransactionFee() 112 transaction.GetKeys() 113 transaction.GetRegenerateTransactionID() 114 transaction.GetHash() 115 } 116 117 func TestUnitLiveHashAddTransactionSetNothing(t *testing.T) { 118 t.Parallel() 119 120 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 121 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 122 123 transaction, err := NewLiveHashAddTransaction(). 124 SetTransactionID(transactionID). 125 SetNodeAccountIDs(nodeAccountID). 126 Freeze() 127 require.NoError(t, err) 128 129 transaction.GetTransactionID() 130 transaction.GetNodeAccountIDs() 131 132 _, err = transaction.GetTransactionHash() 133 require.NoError(t, err) 134 135 transaction.GetAccountID() 136 transaction.GetMaxTransactionFee() 137 transaction.GetTransactionMemo() 138 transaction.GetRegenerateTransactionID() 139 _, err = transaction.GetSignatures() 140 require.NoError(t, err) 141 transaction.GetRegenerateTransactionID() 142 transaction.GetMaxTransactionFee() 143 transaction.GetKeys() 144 transaction.GetRegenerateTransactionID() 145 transaction.GetHash() 146 } 147 148 func TestUnitLiveHashFromBytes(t *testing.T) { 149 t.Parallel() 150 151 liveHash := LiveHash{ 152 AccountID: AccountID{Account: 3}, 153 Hash: []byte{1}, 154 } 155 156 byt := liveHash.ToBytes() 157 fromBytes, err := LiveHashFromBytes(byt) 158 require.NoError(t, err) 159 require.Equal(t, fromBytes.AccountID.Account, uint64(3)) 160 } 161 162 func TestUnitLiveHashAddTransactionCoverage(t *testing.T) { 163 t.Parallel() 164 165 checksum := "dmqui" 166 grpc := time.Second * 30 167 account := AccountID{Account: 3, checksum: &checksum} 168 nodeAccountID := []AccountID{{Account: 10}} 169 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 170 171 newKey, err := PrivateKeyGenerateEd25519() 172 require.NoError(t, err) 173 174 client, err := _NewMockClient() 175 client.SetLedgerID(*NewLedgerIDTestnet()) 176 require.NoError(t, err) 177 client.SetAutoValidateChecksums(true) 178 179 transaction, err := NewLiveHashAddTransaction(). 180 SetTransactionID(transactionID). 181 SetNodeAccountIDs(nodeAccountID). 182 SetKeys(newKey). 183 SetHash([]byte{1}). 184 SetAccountID(account). 185 SetDuration(time.Second * 30). 186 SetGrpcDeadline(&grpc). 187 SetMaxTransactionFee(NewHbar(3)). 188 SetMaxRetry(3). 189 SetMaxBackoff(time.Second * 30). 190 SetMinBackoff(time.Second * 10). 191 SetTransactionMemo("no"). 192 SetTransactionValidDuration(time.Second * 30). 193 SetRegenerateTransactionID(false). 194 Freeze() 195 require.NoError(t, err) 196 197 transaction.validateNetworkOnIDs(client) 198 199 transaction.GetTransactionID() 200 transaction.GetNodeAccountIDs() 201 transaction.GetMaxRetry() 202 transaction.GetMaxTransactionFee() 203 transaction.GetMaxBackoff() 204 transaction.GetMinBackoff() 205 transaction.GetRegenerateTransactionID() 206 byt, err := transaction.ToBytes() 207 require.NoError(t, err) 208 txFromBytes, err := TransactionFromBytes(byt) 209 require.NoError(t, err) 210 sig, err := newKey.SignTransaction(&transaction.Transaction) 211 require.NoError(t, err) 212 213 _, err = transaction.GetTransactionHash() 214 require.NoError(t, err) 215 transaction.GetMaxTransactionFee() 216 transaction.GetTransactionMemo() 217 transaction.GetRegenerateTransactionID() 218 transaction.GetKeys() 219 transaction.GetAccountID() 220 transaction.GetHash() 221 transaction.GetKeys() 222 transaction.GetDuration() 223 _, err = transaction.GetSignatures() 224 require.NoError(t, err) 225 transaction.getName() 226 switch b := txFromBytes.(type) { 227 case LiveHashAddTransaction: 228 b.AddSignature(newKey.PublicKey(), sig) 229 } 230 } 231 232 func TestUnitLiveHashAddTransactionMock(t *testing.T) { 233 t.Parallel() 234 235 newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98") 236 require.NoError(t, err) 237 238 call := func(request *services.Transaction) *services.TransactionResponse { 239 require.NotEmpty(t, request.SignedTransactionBytes) 240 signedTransaction := services.SignedTransaction{} 241 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 242 243 require.NotEmpty(t, signedTransaction.BodyBytes) 244 transactionBody := services.TransactionBody{} 245 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 246 247 require.NotNil(t, transactionBody.TransactionID) 248 transactionId := transactionBody.TransactionID.String() 249 require.NotEqual(t, "", transactionId) 250 251 sigMap := signedTransaction.GetSigMap() 252 require.NotNil(t, sigMap) 253 254 return &services.TransactionResponse{ 255 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 256 } 257 } 258 responses := [][]interface{}{{ 259 call, 260 }} 261 262 client, server := NewMockClientAndServer(responses) 263 defer server.Close() 264 265 freez, err := NewLiveHashAddTransaction(). 266 SetNodeAccountIDs([]AccountID{{Account: 3}}). 267 SetKeys(newKey). 268 SetAccountID(AccountID{Account: 3}). 269 SetHash([]byte{123}). 270 FreezeWith(client) 271 require.NoError(t, err) 272 273 _, err = freez.Sign(newKey).Execute(client) 274 require.NoError(t, err) 275 }