forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethereum_flow_unit_test.go
80 lines (67 loc) · 2.89 KB
/
ethereum_flow_unit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//go:build all || unit
// +build all unit
package hedera
import (
"encoding/hex"
"testing"
"github.com/hashgraph/hedera-protobufs-go/services"
"github.com/stretchr/testify/require"
protobuf "google.golang.org/protobuf/proto"
)
func TestUnitMockEthereumFlow(t *testing.T) {
call := func(request *services.Transaction) *services.TransactionResponse {
require.NotEmpty(t, request.SignedTransactionBytes)
signedTransaction := services.SignedTransaction{}
_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
require.NotEmpty(t, signedTransaction.BodyBytes)
transactionBody := services.TransactionBody{}
_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
sigMap := signedTransaction.GetSigMap()
require.NotNil(t, sigMap)
require.NotEqual(t, 0, len(sigMap.SigPair))
if bod, ok := transactionBody.Data.(*services.TransactionBody_EthereumTransaction); ok {
require.Equal(t, hex.EncodeToString(bod.EthereumTransaction.GetEthereumData()), "02f87082012a022f2f83018000947e3a9eaf9bcc39e2ffa38eb30bf7a93feacbc181880de0b6b3a764000083123456c001a0df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479a01aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66")
require.Nil(t, bod.EthereumTransaction.CallData)
}
for _, sigPair := range sigMap.SigPair {
verified := false
switch k := sigPair.Signature.(type) {
case *services.SignaturePair_Ed25519:
pbTemp, _ := PublicKeyFromBytesEd25519(sigPair.PubKeyPrefix)
verified = pbTemp.Verify(signedTransaction.BodyBytes, k.Ed25519)
case *services.SignaturePair_ECDSASecp256K1:
pbTemp, _ := PublicKeyFromBytesECDSA(sigPair.PubKeyPrefix)
verified = pbTemp.Verify(signedTransaction.BodyBytes, k.ECDSASecp256K1)
}
require.True(t, verified)
}
return &services.TransactionResponse{
NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
}
}
responses := [][]interface{}{{
call, &services.Response{
Response: &services.Response_TransactionGetReceipt{
TransactionGetReceipt: &services.TransactionGetReceiptResponse{
Header: &services.ResponseHeader{
NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
ResponseType: services.ResponseType_ANSWER_ONLY,
},
Receipt: &services.TransactionReceipt{
Status: services.ResponseCodeEnum_SUCCESS,
},
},
},
},
}}
client, server := NewMockClientAndServer(responses)
defer server.Close()
byt, err := hex.DecodeString("02f87082012a022f2f83018000947e3a9eaf9bcc39e2ffa38eb30bf7a93feacbc181880de0b6b3a764000083123456c001a0df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479a01aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66")
require.NoError(t, err)
b, err := EthereumTransactionDataFromBytes(byt)
_, err = NewEthereumFlow().
SetNodeAccountIDs([]AccountID{{Account: 3}}).
SetEthereumData(b).
Execute(client)
require.NoError(t, err)
}