github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/topic_create_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 TestUnitTopicCreateTransactionValidate(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 topicCreate := NewTopicCreateTransaction(). 49 SetAutoRenewAccountID(accountID) 50 51 err = topicCreate.validateNetworkOnIDs(client) 52 require.NoError(t, err) 53 } 54 55 func TestUnitTopicCreateTransactionValidateWrong(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 topicCreate := NewTopicCreateTransaction(). 66 SetAutoRenewAccountID(accountID) 67 68 err = topicCreate.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 TestUnitTopicCreateTransactionGet(t *testing.T) { 76 t.Parallel() 77 78 accountID := AccountID{Account: 3} 79 80 newKey, err := PrivateKeyGenerateEd25519() 81 require.NoError(t, err) 82 83 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 84 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 85 86 transaction, err := NewTopicCreateTransaction(). 87 SetTransactionID(transactionID). 88 SetNodeAccountIDs(nodeAccountID). 89 SetAutoRenewAccountID(accountID). 90 SetAdminKey(newKey). 91 SetSubmitKey(newKey). 92 SetTopicMemo("ad"). 93 SetAutoRenewPeriod(60 * time.Second). 94 SetMaxTransactionFee(NewHbar(10)). 95 SetTransactionMemo(""). 96 SetTransactionValidDuration(60 * time.Second). 97 SetRegenerateTransactionID(false). 98 Freeze() 99 require.NoError(t, err) 100 101 transaction.GetTransactionID() 102 transaction.GetNodeAccountIDs() 103 104 _, err = transaction.GetTransactionHash() 105 require.NoError(t, err) 106 107 transaction.GetAutoRenewAccountID() 108 transaction.GetAdminKey() 109 transaction.GetSubmitKey() 110 transaction.GetTopicMemo() 111 transaction.GetAutoRenewPeriod() 112 transaction.GetMaxTransactionFee() 113 transaction.GetTransactionMemo() 114 transaction.GetRegenerateTransactionID() 115 _, err = transaction.GetSignatures() 116 require.NoError(t, err) 117 transaction.GetRegenerateTransactionID() 118 transaction.GetMaxTransactionFee() 119 transaction.GetRegenerateTransactionID() 120 } 121 122 func TestUnitTopicCreateTransactionNothingSet(t *testing.T) { 123 t.Parallel() 124 125 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 126 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 127 128 transaction, err := NewTopicCreateTransaction(). 129 SetTransactionID(transactionID). 130 SetNodeAccountIDs(nodeAccountID). 131 Freeze() 132 require.NoError(t, err) 133 134 transaction.GetTransactionID() 135 transaction.GetNodeAccountIDs() 136 137 _, err = transaction.GetTransactionHash() 138 require.NoError(t, err) 139 140 transaction.GetAutoRenewAccountID() 141 transaction.GetAdminKey() 142 transaction.GetSubmitKey() 143 transaction.GetTopicMemo() 144 transaction.GetAutoRenewPeriod() 145 transaction.GetMaxTransactionFee() 146 transaction.GetTransactionMemo() 147 transaction.GetRegenerateTransactionID() 148 _, err = transaction.GetSignatures() 149 require.NoError(t, err) 150 transaction.GetRegenerateTransactionID() 151 transaction.GetMaxTransactionFee() 152 transaction.GetRegenerateTransactionID() 153 } 154 155 func TestUnitTopicCreateTransactionProtoCheck(t *testing.T) { 156 t.Parallel() 157 158 accountID := AccountID{Account: 23} 159 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 160 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 161 162 newKey, err := PrivateKeyGenerateEd25519() 163 require.NoError(t, err) 164 newKey2, err := PrivateKeyGenerateEd25519() 165 require.NoError(t, err) 166 167 transaction, err := NewTopicCreateTransaction(). 168 SetTransactionID(transactionID). 169 SetNodeAccountIDs(nodeAccountID). 170 SetAdminKey(newKey). 171 SetSubmitKey(newKey2). 172 SetAutoRenewAccountID(accountID). 173 SetTopicMemo("memo"). 174 SetAutoRenewPeriod(time.Second * 3). 175 Freeze() 176 require.NoError(t, err) 177 178 transaction.GetTransactionID() 179 transaction.GetNodeAccountIDs() 180 181 proto := transaction.build().GetConsensusCreateTopic() 182 require.Equal(t, proto.AdminKey.String(), newKey._ToProtoKey().String()) 183 require.Equal(t, proto.SubmitKey.String(), newKey2._ToProtoKey().String()) 184 require.Equal(t, proto.Memo, "memo") 185 require.Equal(t, proto.AutoRenewPeriod.Seconds, _DurationToProtobuf(time.Second*3).Seconds) 186 require.Equal(t, proto.AutoRenewAccount.String(), accountID._ToProtobuf().String()) 187 } 188 189 func TestUnitTopicCreateTransactionCoverage(t *testing.T) { 190 t.Parallel() 191 192 checksum := "dmqui" 193 grpc := time.Second * 30 194 account := AccountID{Account: 3, checksum: &checksum} 195 nodeAccountID := []AccountID{{Account: 10}} 196 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 197 198 newKey, err := PrivateKeyGenerateEd25519() 199 require.NoError(t, err) 200 201 client, err := _NewMockClient() 202 client.SetLedgerID(*NewLedgerIDTestnet()) 203 require.NoError(t, err) 204 client.SetAutoValidateChecksums(true) 205 206 transaction, err := NewTopicCreateTransaction(). 207 SetTransactionID(transactionID). 208 SetNodeAccountIDs(nodeAccountID). 209 SetAdminKey(newKey). 210 SetTopicMemo("ad"). 211 SetSubmitKey(newKey). 212 SetAutoRenewAccountID(account). 213 SetAutoRenewPeriod(time.Second * 30). 214 SetGrpcDeadline(&grpc). 215 SetMaxTransactionFee(NewHbar(3)). 216 SetMaxRetry(3). 217 SetMaxBackoff(time.Second * 30). 218 SetMinBackoff(time.Second * 10). 219 SetTransactionMemo("no"). 220 SetTransactionValidDuration(time.Second * 30). 221 SetRegenerateTransactionID(false). 222 Freeze() 223 require.NoError(t, err) 224 225 err = transaction.validateNetworkOnIDs(client) 226 require.NoError(t, err) 227 _, err = transaction.Schedule() 228 require.NoError(t, err) 229 transaction.GetTransactionID() 230 transaction.GetNodeAccountIDs() 231 transaction.GetMaxRetry() 232 transaction.GetMaxTransactionFee() 233 transaction.GetMaxBackoff() 234 transaction.GetMinBackoff() 235 transaction.GetRegenerateTransactionID() 236 byt, err := transaction.ToBytes() 237 require.NoError(t, err) 238 txFromBytes, err := TransactionFromBytes(byt) 239 require.NoError(t, err) 240 sig, err := newKey.SignTransaction(&transaction.Transaction) 241 require.NoError(t, err) 242 243 _, err = transaction.GetTransactionHash() 244 require.NoError(t, err) 245 transaction.GetMaxTransactionFee() 246 transaction.GetTransactionMemo() 247 transaction.GetRegenerateTransactionID() 248 transaction.GetAdminKey() 249 transaction.GetSubmitKey() 250 transaction.GetTopicMemo() 251 transaction.GetAutoRenewAccountID() 252 transaction.GetAutoRenewPeriod() 253 _, err = transaction.GetSignatures() 254 require.NoError(t, err) 255 transaction.getName() 256 switch b := txFromBytes.(type) { 257 case TopicCreateTransaction: 258 b.AddSignature(newKey.PublicKey(), sig) 259 } 260 } 261 262 func TestUnitTopicCreateTransactionMock(t *testing.T) { 263 t.Parallel() 264 265 newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98") 266 require.NoError(t, err) 267 268 call := func(request *services.Transaction) *services.TransactionResponse { 269 require.NotEmpty(t, request.SignedTransactionBytes) 270 signedTransaction := services.SignedTransaction{} 271 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 272 273 require.NotEmpty(t, signedTransaction.BodyBytes) 274 transactionBody := services.TransactionBody{} 275 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 276 277 require.NotNil(t, transactionBody.TransactionID) 278 transactionId := transactionBody.TransactionID.String() 279 require.NotEqual(t, "", transactionId) 280 281 sigMap := signedTransaction.GetSigMap() 282 require.NotNil(t, sigMap) 283 284 return &services.TransactionResponse{ 285 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 286 } 287 } 288 responses := [][]interface{}{{ 289 call, 290 }} 291 292 client, server := NewMockClientAndServer(responses) 293 defer server.Close() 294 295 freez, err := NewTopicCreateTransaction(). 296 SetNodeAccountIDs([]AccountID{{Account: 3}}). 297 SetAdminKey(newKey). 298 SetSubmitKey(newKey). 299 FreezeWith(client) 300 require.NoError(t, err) 301 302 _, err = freez.Sign(newKey).Execute(client) 303 require.NoError(t, err) 304 } 305 306 func TestUnitTopicCreateTransactionSerialization(t *testing.T) { 307 t.Parallel() 308 309 nodeAccountID := []AccountID{{Account: 10}} 310 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 311 newKey, err := PrivateKeyGenerateEd25519() 312 require.NoError(t, err) 313 314 topicCreate, err := NewTopicCreateTransaction(). 315 SetTransactionID(transactionID). 316 SetAdminKey(newKey). 317 SetNodeAccountIDs(nodeAccountID). 318 SetSubmitKey(newKey). 319 SetTopicMemo("ad"). 320 SetAutoRenewPeriod(time.Second * 30). 321 Freeze() 322 require.NoError(t, err) 323 324 transactionBytes, err := topicCreate.ToBytes() 325 require.NoError(t, err) 326 327 txParsed, err := TransactionFromBytes(transactionBytes) 328 require.NoError(t, err) 329 330 result, ok := txParsed.(TopicCreateTransaction) 331 require.True(t, ok) 332 333 require.Equal(t, topicCreate.GetTopicMemo(), result.GetTopicMemo()) 334 require.Equal(t, topicCreate.GetAutoRenewPeriod(), result.GetAutoRenewPeriod()) 335 adminKey, _ := result.GetAdminKey() 336 require.Equal(t, newKey.PublicKey(), adminKey) 337 submitKey, _ := result.GetSubmitKey() 338 require.Equal(t, newKey.PublicKey(), submitKey) 339 }