github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/ethereum_transaction_e2e_test.go (about) 1 //go:build all || e2e 2 // +build all e2e 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 "io" 29 30 "testing" 31 32 "github.com/ethereum/go-ethereum/rlp" 33 "github.com/stretchr/testify/assert" 34 "github.com/stretchr/testify/require" 35 ) 36 37 type EIP1559RLP struct { 38 chainId []byte 39 nonce []byte 40 maxPriorityGas []byte 41 maxGas []byte 42 gasLimit []byte 43 to []byte 44 value []byte 45 callData []byte 46 accessList [][]byte 47 recId []byte 48 r []byte 49 s []byte 50 } 51 52 // EncodeRLP writes l as RLP list [ethereumfield1, ethereumfield2...] Omits r,s,v values on first encode 53 func (l *EIP1559RLP) EncodeRLP(w io.Writer) (err error) { 54 fields := []interface{}{ 55 l.chainId, 56 l.nonce, 57 l.maxPriorityGas, 58 l.maxGas, 59 l.gasLimit, 60 l.to, 61 l.value, 62 l.callData, 63 l.accessList, 64 } 65 if len(l.recId) > 0 && len(l.r) > 0 && len(l.s) > 0 { 66 fields = append(fields, l.recId, l.r, l.s) 67 } 68 69 return rlp.Encode(w, fields) 70 } 71 72 // decodeHex is a helper function that decodes a hex string and fails the test if an error occurs. 73 func decodeHex(t *testing.T, s string) []byte { 74 bytes, err := hex.DecodeString(s) 75 if err != nil { 76 t.Fatalf("Failed to decode string %s: %v", s, err) 77 } 78 return bytes 79 } 80 81 // Testing the signer nonce defined in HIP-844 82 func TestIntegrationEthereumTransaction(t *testing.T) { 83 // Skip this test because it is flaky with newest version of Local Node 84 t.Skip() 85 t.Parallel() 86 env := NewIntegrationTestEnv(t) 87 88 smartContractBytecode := []byte("608060405234801561001057600080fd5b506040516104d73803806104d78339818101604052602081101561003357600080fd5b810190808051604051939291908464010000000082111561005357600080fd5b90830190602082018581111561006857600080fd5b825164010000000081118282018810171561008257600080fd5b82525081516020918201929091019080838360005b838110156100af578181015183820152602001610097565b50505050905090810190601f1680156100dc5780820380516001836020036101000a031916815260200191505b506040525050600080546001600160a01b0319163317905550805161010890600190602084019061010f565b50506101aa565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061015057805160ff191683800117855561017d565b8280016001018555821561017d579182015b8281111561017d578251825591602001919060010190610162565b5061018992915061018d565b5090565b6101a791905b808211156101895760008155600101610193565b90565b61031e806101b96000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063368b87721461004657806341c0e1b5146100ee578063ce6d41de146100f6575b600080fd5b6100ec6004803603602081101561005c57600080fd5b81019060208101813564010000000081111561007757600080fd5b82018360208201111561008957600080fd5b803590602001918460018302840111640100000000831117156100ab57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610173945050505050565b005b6100ec6101a2565b6100fe6101ba565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610138578181015183820152602001610120565b50505050905090810190601f1680156101655780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000546001600160a01b0316331461018a5761019f565b805161019d906001906020840190610250565b505b50565b6000546001600160a01b03163314156101b85733ff5b565b60018054604080516020601f600260001961010087891615020190951694909404938401819004810282018101909252828152606093909290918301828280156102455780601f1061021a57610100808354040283529160200191610245565b820191906000526020600020905b81548152906001019060200180831161022857829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061029157805160ff19168380011785556102be565b828001600101855582156102be579182015b828111156102be5782518255916020019190600101906102a3565b506102ca9291506102ce565b5090565b61024d91905b808211156102ca57600081556001016102d456fea264697066735822122084964d4c3f6bc912a9d20e14e449721012d625aa3c8a12de41ae5519752fc89064736f6c63430006000033") 89 ecdsaPrivateKey, _ := PrivateKeyFromStringECDSA("30540201010420ac318ea8ff8d991ab2f16172b4738e74dc35a56681199cfb1c0cb2e7cb560ffda00706052b8104000aa124032200036843f5cb338bbb4cdb21b0da4ea739d910951d6e8a5f703d313efe31afe788f4") 90 aliasAccountId := ecdsaPrivateKey.ToAccountID(0, 0) 91 92 // Create a shallow account for the ECDSA key 93 resp, err := NewTransferTransaction(). 94 AddHbarTransfer(env.Client.GetOperatorAccountID(), NewHbar(-2)). 95 AddHbarTransfer(*aliasAccountId, NewHbar(2)). 96 Execute(env.Client) 97 98 // Create file with the contract bytecode 99 resp, err = NewFileCreateTransaction(). 100 SetNodeAccountIDs(env.NodeAccountIDs). 101 SetKeys(env.OperatorKey.PublicKey()). 102 SetContents(smartContractBytecode). 103 Execute(env.Client) 104 require.NoError(t, err) 105 106 receipt, err := resp.SetValidateStatus(true).GetReceipt(env.Client) 107 require.NoError(t, err) 108 109 fileID := *receipt.FileID 110 assert.NotNil(t, fileID) 111 112 // Create contract to be called by EthereumTransaction 113 resp, err = NewContractCreateTransaction(). 114 SetAdminKey(env.Client.GetOperatorPublicKey()). 115 SetNodeAccountIDs([]AccountID{resp.NodeID}). 116 SetGas(1000000). 117 SetConstructorParameters(NewContractFunctionParameters().AddString("hello from hedera")). 118 SetBytecodeFileID(fileID). 119 SetContractMemo("hedera-sdk-go::TestContractCreateTransaction_Execute"). 120 Execute(env.Client) 121 require.NoError(t, err) 122 123 receipt, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 124 require.NoError(t, err) 125 126 assert.NotNil(t, receipt.ContractID) 127 contractID := *receipt.ContractID 128 129 // Call data for the smart contract 130 contractMsg := "setMessage" 131 msgPointer := &contractMsg 132 133 // build the RLP list that should be signed with the test ECDSA private key 134 list := &EIP1559RLP{ 135 chainId: decodeHex(t, "012a"), 136 nonce: []byte{}, 137 maxPriorityGas: decodeHex(t, "00"), 138 maxGas: decodeHex(t, "d1385c7bf0"), 139 gasLimit: decodeHex(t, "0249f0"), 140 to: decodeHex(t, contractID.ToSolidityAddress()), 141 value: []byte{}, 142 callData: NewContractFunctionParameters().AddString("new message")._Build(msgPointer), 143 accessList: [][]byte{}, 144 } 145 146 bytes, _ := rlp.EncodeToBytes(list) 147 148 // 02 is the type of the transaction EIP1559 and should be concatenated to the RLP by service requirement 149 bytesToSign := append(decodeHex(t, "02"), bytes...) 150 signedBytes := ecdsaPrivateKey.Sign(bytesToSign) 151 152 // Add signature data to the RLP list for EthereumTransaction submition 153 list.recId = decodeHex(t, "01") 154 list.r = signedBytes[:32] 155 list.s = signedBytes[len(signedBytes)-32:] 156 157 ethereumTransactionData, _ := rlp.EncodeToBytes(list) 158 // 02 is the type of the transaction EIP1559 and should be concatenated to the RLP by service requirement 159 resp, err = NewEthereumTransaction().SetEthereumData(append(decodeHex(t, "02"), ethereumTransactionData...)).Execute(env.Client) 160 161 require.NoError(t, err) 162 163 record, _ := resp.GetRecord(env.Client) 164 165 assert.Equal(t, int64(1), record.CallResult.SignerNonce) 166 167 resp, err = NewContractDeleteTransaction(). 168 SetContractID(contractID). 169 SetTransferAccountID(env.Client.GetOperatorAccountID()). 170 Execute(env.Client) 171 require.NoError(t, err) 172 173 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 174 require.NoError(t, err) 175 176 resp, err = NewFileDeleteTransaction(). 177 SetFileID(fileID). 178 Execute(env.Client) 179 require.NoError(t, err) 180 181 _, err = resp.SetValidateStatus(true).GetReceipt(env.Client) 182 require.NoError(t, err) 183 184 err = CloseIntegrationTestEnv(env, nil) 185 require.NoError(t, err) 186 187 }