github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/ethereum_flow_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 "encoding/hex" 28 "testing" 29 30 "github.com/hashgraph/hedera-protobufs-go/services" 31 "github.com/stretchr/testify/require" 32 protobuf "google.golang.org/protobuf/proto" 33 ) 34 35 func TestUnitEthereumFlowMock(t *testing.T) { 36 t.Parallel() 37 38 call := func(request *services.Transaction) *services.TransactionResponse { 39 require.NotEmpty(t, request.SignedTransactionBytes) 40 signedTransaction := services.SignedTransaction{} 41 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 42 43 require.NotEmpty(t, signedTransaction.BodyBytes) 44 transactionBody := services.TransactionBody{} 45 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 46 47 sigMap := signedTransaction.GetSigMap() 48 require.NotNil(t, sigMap) 49 require.NotEqual(t, 0, len(sigMap.SigPair)) 50 51 if bod, ok := transactionBody.Data.(*services.TransactionBody_EthereumTransaction); ok { 52 require.Equal(t, hex.EncodeToString(bod.EthereumTransaction.GetEthereumData()), "02f87082012a022f2f83018000947e3a9eaf9bcc39e2ffa38eb30bf7a93feacbc181880de0b6b3a764000083123456c001a0df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479a01aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66") 53 require.Nil(t, bod.EthereumTransaction.CallData) 54 } 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 return &services.TransactionResponse{ 71 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 72 } 73 } 74 responses := [][]interface{}{{ 75 call, &services.Response{ 76 Response: &services.Response_TransactionGetReceipt{ 77 TransactionGetReceipt: &services.TransactionGetReceiptResponse{ 78 Header: &services.ResponseHeader{ 79 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 80 ResponseType: services.ResponseType_ANSWER_ONLY, 81 }, 82 Receipt: &services.TransactionReceipt{ 83 Status: services.ResponseCodeEnum_SUCCESS, 84 }, 85 }, 86 }, 87 }, 88 }} 89 90 client, server := NewMockClientAndServer(responses) 91 defer server.Close() 92 93 byt, err := hex.DecodeString("02f87082012a022f2f83018000947e3a9eaf9bcc39e2ffa38eb30bf7a93feacbc181880de0b6b3a764000083123456c001a0df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479a01aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66") 94 require.NoError(t, err) 95 b, err := EthereumTransactionDataFromBytes(byt) 96 require.NoError(t, err) 97 98 _, err = NewEthereumFlow(). 99 SetNodeAccountIDs([]AccountID{{Account: 3}}). 100 SetEthereumData(b). 101 Execute(client) 102 require.NoError(t, err) 103 } 104 105 func TestUnitEthereumFlowMockEthereumDateBytes(t *testing.T) { 106 t.Parallel() 107 108 call := func(request *services.Transaction) *services.TransactionResponse { 109 require.NotEmpty(t, request.SignedTransactionBytes) 110 signedTransaction := services.SignedTransaction{} 111 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 112 113 require.NotEmpty(t, signedTransaction.BodyBytes) 114 transactionBody := services.TransactionBody{} 115 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 116 117 sigMap := signedTransaction.GetSigMap() 118 require.NotNil(t, sigMap) 119 require.NotEqual(t, 0, len(sigMap.SigPair)) 120 121 if bod, ok := transactionBody.Data.(*services.TransactionBody_EthereumTransaction); ok { 122 require.Equal(t, hex.EncodeToString(bod.EthereumTransaction.GetEthereumData()), "02f87082012a022f2f83018000947e3a9eaf9bcc39e2ffa38eb30bf7a93feacbc181880de0b6b3a764000083123456c001a0df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479a01aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66") 123 require.Nil(t, bod.EthereumTransaction.CallData) 124 } 125 126 for _, sigPair := range sigMap.SigPair { 127 verified := false 128 129 switch k := sigPair.Signature.(type) { 130 case *services.SignaturePair_Ed25519: 131 pbTemp, _ := PublicKeyFromBytesEd25519(sigPair.PubKeyPrefix) 132 verified = pbTemp.Verify(signedTransaction.BodyBytes, k.Ed25519) 133 case *services.SignaturePair_ECDSASecp256K1: 134 pbTemp, _ := PublicKeyFromBytesECDSA(sigPair.PubKeyPrefix) 135 verified = pbTemp.Verify(signedTransaction.BodyBytes, k.ECDSASecp256K1) 136 } 137 require.True(t, verified) 138 } 139 140 return &services.TransactionResponse{ 141 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 142 } 143 } 144 responses := [][]interface{}{{ 145 call, &services.Response{ 146 Response: &services.Response_TransactionGetReceipt{ 147 TransactionGetReceipt: &services.TransactionGetReceiptResponse{ 148 Header: &services.ResponseHeader{ 149 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 150 ResponseType: services.ResponseType_ANSWER_ONLY, 151 }, 152 Receipt: &services.TransactionReceipt{ 153 Status: services.ResponseCodeEnum_SUCCESS, 154 }, 155 }, 156 }, 157 }, 158 }} 159 160 client, server := NewMockClientAndServer(responses) 161 defer server.Close() 162 163 byt, err := hex.DecodeString("02f87082012a022f2f83018000947e3a9eaf9bcc39e2ffa38eb30bf7a93feacbc181880de0b6b3a764000083123456c001a0df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479a01aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66") 164 require.NoError(t, err) 165 166 _, err = NewEthereumFlow(). 167 SetNodeAccountIDs([]AccountID{{Account: 3}}). 168 SetEthereumDataBytes(byt). 169 Execute(client) 170 require.NoError(t, err) 171 } 172 173 func TestUnitEthereumFlowCoverage(t *testing.T) { 174 t.Parallel() 175 176 checksum := "dmqui" 177 file := FileID{File: 3, checksum: &checksum} 178 nodeAccountID := []AccountID{{Account: 10}} 179 180 byt, err := hex.DecodeString("02f87082012a022f2f83018000947e3a9eaf9bcc39e2ffa38eb30bf7a93feacbc181880de0b6b3a764000083123456c001a0df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479a01aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66") 181 require.NoError(t, err) 182 183 client := ClientForTestnet() 184 client.SetAutoValidateChecksums(true) 185 186 transaction := NewEthereumFlow(). 187 SetNodeAccountIDs(nodeAccountID). 188 SetCallDataFileID(file). 189 SetEthereumDataBytes(byt). 190 SetMaxGasAllowance(NewHbar(2)) 191 192 transaction._CreateFile(byt, client) 193 194 require.NoError(t, err) 195 transaction.GetTransactionID() 196 transaction.GetNodeAccountIDs() 197 transaction.GetMaxRetry() 198 transaction.GetMaxTransactionFee() 199 transaction.GetRegenerateTransactionID() 200 201 transaction.GetMaxTransactionFee() 202 transaction.GetTransactionMemo() 203 transaction.GetRegenerateTransactionID() 204 transaction.GetEthereumData() 205 transaction.GetCallDataFileID() 206 transaction.GetMaxGasAllowance() 207 _, err = transaction.GetSignatures() 208 require.NoError(t, err) 209 }