github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/file_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 "bytes" 28 "encoding/hex" 29 "testing" 30 "time" 31 32 "github.com/hashgraph/hedera-protobufs-go/services" 33 "github.com/stretchr/testify/require" 34 protobuf "google.golang.org/protobuf/proto" 35 ) 36 37 func TestUnitFileCreateTransactionMock(t *testing.T) { 38 t.Parallel() 39 40 call := func(request *services.Transaction) *services.TransactionResponse { 41 require.NotEmpty(t, request.SignedTransactionBytes) 42 signedTransaction := services.SignedTransaction{} 43 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 44 45 require.NotEmpty(t, signedTransaction.BodyBytes) 46 transactionBody := services.TransactionBody{} 47 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 48 49 require.NotNil(t, transactionBody.TransactionID) 50 transactionId := transactionBody.TransactionID.String() 51 require.NotEqual(t, "", transactionId) 52 53 sigMap := signedTransaction.GetSigMap() 54 require.NotNil(t, sigMap) 55 56 for _, sigPair := range sigMap.SigPair { 57 verified := false 58 59 switch k := sigPair.Signature.(type) { 60 case *services.SignaturePair_Ed25519: 61 pbTemp, _ := PublicKeyFromBytesEd25519(sigPair.PubKeyPrefix) 62 verified = pbTemp.Verify(signedTransaction.BodyBytes, k.Ed25519) 63 case *services.SignaturePair_ECDSASecp256K1: 64 pbTemp, _ := PublicKeyFromBytesECDSA(sigPair.PubKeyPrefix) 65 verified = pbTemp.Verify(signedTransaction.BodyBytes, k.ECDSASecp256K1) 66 } 67 require.True(t, verified) 68 } 69 70 if bod, ok := transactionBody.Data.(*services.TransactionBody_FileCreate); ok { 71 require.Equal(t, bod.FileCreate.Memo, "go sdk e2e tests") 72 require.Equal(t, hex.EncodeToString(bod.FileCreate.Keys.Keys[0].GetEd25519()), "87deee7c3e3bd41d94d710531d529a1ae0c2e0463b27250072e177879267f501") 73 require.Equal(t, bytes.Compare(bod.FileCreate.Contents, []byte("Hello, World")), 0) 74 } 75 76 return &services.TransactionResponse{ 77 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 78 } 79 } 80 responses := [][]interface{}{{ 81 call, 82 }} 83 84 client, server := NewMockClientAndServer(responses) 85 defer server.Close() 86 newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98") 87 require.NoError(t, err) 88 89 _, err = NewFileCreateTransaction(). 90 SetKeys(newKey). 91 SetNodeAccountIDs([]AccountID{{Account: 3}}). 92 SetContents([]byte("Hello, World")). 93 SetMemo("go sdk e2e tests"). 94 Execute(client) 95 require.NoError(t, err) 96 } 97 98 func TestUnitFileCreateTransactionGet(t *testing.T) { 99 t.Parallel() 100 101 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 102 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 103 104 newKey, err := PrivateKeyGenerateEd25519() 105 106 transaction, err := NewFileCreateTransaction(). 107 SetTransactionID(transactionID). 108 SetNodeAccountIDs(nodeAccountID). 109 SetKeys(newKey). 110 SetContents([]byte("Hello, World")). 111 SetMemo(""). 112 SetMaxTransactionFee(NewHbar(10)). 113 SetTransactionMemo(""). 114 SetTransactionValidDuration(60 * time.Second). 115 SetRegenerateTransactionID(false). 116 Freeze() 117 require.NoError(t, err) 118 119 transaction.GetTransactionID() 120 transaction.GetNodeAccountIDs() 121 122 _, err = transaction.GetTransactionHash() 123 require.NoError(t, err) 124 125 transaction.GetKeys() 126 transaction.GetMaxTransactionFee() 127 transaction.GetTransactionMemo() 128 transaction.GetRegenerateTransactionID() 129 _, err = transaction.GetSignatures() 130 require.NoError(t, err) 131 transaction.GetRegenerateTransactionID() 132 transaction.GetMaxTransactionFee() 133 transaction.GetContents() 134 transaction.GetRegenerateTransactionID() 135 transaction.GetMemo() 136 } 137 138 func TestUnitFileCreateTransactionSetNothing(t *testing.T) { 139 t.Parallel() 140 141 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 142 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 143 144 transaction, err := NewFileCreateTransaction(). 145 SetTransactionID(transactionID). 146 SetNodeAccountIDs(nodeAccountID). 147 Freeze() 148 require.NoError(t, err) 149 150 transaction.GetTransactionID() 151 transaction.GetNodeAccountIDs() 152 153 _, err = transaction.GetTransactionHash() 154 require.NoError(t, err) 155 156 transaction.GetKeys() 157 transaction.GetMaxTransactionFee() 158 transaction.GetTransactionMemo() 159 transaction.GetRegenerateTransactionID() 160 _, err = transaction.GetSignatures() 161 require.NoError(t, err) 162 transaction.GetRegenerateTransactionID() 163 transaction.GetMaxTransactionFee() 164 transaction.GetContents() 165 transaction.GetRegenerateTransactionID() 166 transaction.GetMemo() 167 } 168 169 func TestUnitFileCreateTransactionProtoCheck(t *testing.T) { 170 t.Parallel() 171 172 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 173 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 174 175 newKey, err := PrivateKeyGenerateEd25519() 176 require.NoError(t, err) 177 178 transaction, err := NewFileCreateTransaction(). 179 SetTransactionID(transactionID). 180 SetNodeAccountIDs(nodeAccountID). 181 SetKeys(newKey). 182 SetContents([]byte{5, 6}). 183 SetExpirationTime(time.Unix(4, 56)). 184 SetMemo("memo"). 185 SetMaxTransactionFee(NewHbar(10)). 186 SetTransactionMemo(""). 187 SetTransactionValidDuration(60 * time.Second). 188 SetRegenerateTransactionID(false). 189 Freeze() 190 require.NoError(t, err) 191 192 transaction.GetTransactionID() 193 transaction.GetNodeAccountIDs() 194 195 proto := transaction.build().GetFileCreate() 196 require.Equal(t, proto.Keys.Keys[0].String(), newKey._ToProtoKey().String()) 197 require.Equal(t, proto.Contents, []byte{5, 6}) 198 require.Equal(t, proto.ExpirationTime.String(), _TimeToProtobuf(time.Unix(4, 56)).String()) 199 require.Equal(t, proto.Memo, "memo") 200 } 201 202 func TestUnitFileCreateTransactionCoverage(t *testing.T) { 203 t.Parallel() 204 205 grpc := time.Second * 30 206 nodeAccountID := []AccountID{{Account: 10}} 207 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 208 209 newKey, err := PrivateKeyGenerateEd25519() 210 require.NoError(t, err) 211 212 client, err := _NewMockClient() 213 client.SetLedgerID(*NewLedgerIDTestnet()) 214 require.NoError(t, err) 215 client.SetAutoValidateChecksums(true) 216 217 transaction, err := NewFileCreateTransaction(). 218 SetTransactionID(transactionID). 219 SetNodeAccountIDs(nodeAccountID). 220 SetKeys(newKey). 221 SetMemo("yes"). 222 SetExpirationTime(time.Unix(23, 32)). 223 SetContents([]byte{0}). 224 SetGrpcDeadline(&grpc). 225 SetMaxTransactionFee(NewHbar(3)). 226 SetMaxRetry(3). 227 SetMaxBackoff(time.Second * 30). 228 SetMinBackoff(time.Second * 10). 229 SetTransactionMemo("no"). 230 SetTransactionValidDuration(time.Second * 30). 231 SetRegenerateTransactionID(false). 232 Freeze() 233 require.NoError(t, err) 234 235 _, err = transaction.Schedule() 236 require.NoError(t, err) 237 transaction.GetTransactionID() 238 transaction.GetNodeAccountIDs() 239 transaction.GetMaxRetry() 240 transaction.GetMaxTransactionFee() 241 transaction.GetMaxBackoff() 242 transaction.GetMinBackoff() 243 transaction.GetRegenerateTransactionID() 244 byt, err := transaction.ToBytes() 245 require.NoError(t, err) 246 txFromBytes, err := TransactionFromBytes(byt) 247 require.NoError(t, err) 248 sig, err := newKey.SignTransaction(&transaction.Transaction) 249 require.NoError(t, err) 250 251 _, err = transaction.GetTransactionHash() 252 require.NoError(t, err) 253 transaction.GetMaxTransactionFee() 254 transaction.GetTransactionMemo() 255 transaction.GetRegenerateTransactionID() 256 transaction.GetKeys() 257 transaction.GetMemo() 258 transaction.GetExpirationTime() 259 transaction.GetContents() 260 _, err = transaction.GetSignatures() 261 require.NoError(t, err) 262 transaction.getName() 263 switch b := txFromBytes.(type) { 264 case FileCreateTransaction: 265 b.AddSignature(newKey.PublicKey(), sig) 266 } 267 }