-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathclient_api_ten.go
131 lines (115 loc) · 4.24 KB
/
client_api_ten.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package clientapi
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ten-protocol/go-ten/go/responses"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ten-protocol/go-ten/go/common"
"github.com/ten-protocol/go-ten/go/common/host"
)
// TenAPI implements Ten-specific JSON RPC operations.
type TenAPI struct {
host host.Host
rpcKey []byte
logger gethlog.Logger
}
func NewTenAPI(host host.Host, logger gethlog.Logger) *TenAPI {
return &TenAPI{
host: host,
logger: logger,
}
}
// Version returns the protocol version of the Obscuro network.
func (api *TenAPI) Version() string {
return fmt.Sprintf("%d", api.host.Config().TenChainID)
}
// Health returns the health status of TEN host + enclave + db
func (api *TenAPI) Health(ctx context.Context) (*host.HealthCheck, error) {
return api.host.HealthCheck(ctx)
}
// Config returns the config status of TEN host + enclave + db
func (api *TenAPI) Config() (*ChecksumFormattedTenNetworkConfig, error) {
config, err := api.host.TenConfig()
if err != nil {
return nil, err
}
return checksumFormatted(config), nil
}
func (api *TenAPI) RpcKey() ([]byte, error) {
if api.rpcKey != nil {
return api.rpcKey, nil
}
var err error
api.rpcKey, err = api.host.EnclaveClient().RPCEncryptionKey(context.Background())
if err != nil {
return nil, err
}
return api.rpcKey, nil
}
type CrossChainProof struct {
Proof hexutil.Bytes
Root gethcommon.Hash
}
func (api *TenAPI) GetCrossChainProof(ctx context.Context, messageType string, crossChainMessage gethcommon.Hash) (CrossChainProof, error) {
proof, root, err := api.host.Storage().FetchCrossChainProof(messageType, crossChainMessage)
if err != nil {
return CrossChainProof{}, err
}
encodedProof, err := rlp.EncodeToBytes(proof)
if err != nil {
return CrossChainProof{}, err
}
return CrossChainProof{
Proof: encodedProof,
Root: root,
}, nil
}
func (api *TenAPI) EncryptedRPC(ctx context.Context, encryptedParams common.EncryptedRPCRequest) (responses.EnclaveResponse, error) {
var enclaveResponse *responses.EnclaveResponse
var sysError error
if encryptedParams.IsTx {
enclaveResponse, sysError = api.host.SubmitAndBroadcastTx(ctx, encryptedParams.Req)
} else {
enclaveResponse, sysError = api.host.EnclaveClient().EncryptedRPC(ctx, encryptedParams.Req)
}
if sysError != nil {
api.logger.Error("Enclave System Error. Function EncryptedRPC", log.ErrKey, sysError)
return responses.EnclaveResponse{
Err: &responses.InternalErrMsg,
}, nil
}
return *enclaveResponse, nil
}
// ChecksumFormattedTenNetworkConfig serialises the addresses as EIP55 checksum addresses.
type ChecksumFormattedTenNetworkConfig struct {
ManagementContractAddress gethcommon.AddressEIP55
L1StartHash gethcommon.Hash
MessageBusAddress gethcommon.AddressEIP55
L2MessageBusAddress gethcommon.AddressEIP55
ImportantContracts map[string]gethcommon.AddressEIP55 // map of contract name to address
TransactionPostProcessorAddress gethcommon.AddressEIP55
PublicSystemContracts map[string]gethcommon.AddressEIP55
}
func checksumFormatted(info *common.TenNetworkInfo) *ChecksumFormattedTenNetworkConfig {
importantContracts := make(map[string]gethcommon.AddressEIP55)
for name, addr := range info.ImportantContracts {
importantContracts[name] = gethcommon.AddressEIP55(addr)
}
publicSystemContracts := make(map[string]gethcommon.AddressEIP55)
for name, addr := range info.PublicSystemContracts {
publicSystemContracts[name] = gethcommon.AddressEIP55(addr)
}
return &ChecksumFormattedTenNetworkConfig{
ManagementContractAddress: gethcommon.AddressEIP55(info.ManagementContractAddress),
L1StartHash: info.L1StartHash,
MessageBusAddress: gethcommon.AddressEIP55(info.MessageBusAddress),
L2MessageBusAddress: gethcommon.AddressEIP55(info.L2MessageBusAddress),
ImportantContracts: importantContracts,
TransactionPostProcessorAddress: gethcommon.AddressEIP55(info.TransactionPostProcessorAddress),
PublicSystemContracts: publicSystemContracts,
}
}