This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
138 lines (111 loc) · 2.78 KB
/
rpc.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
132
133
134
135
136
137
138
package rockside
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/ethereum/go-ethereum/ethclient"
)
type RPCClient struct {
endpoint *url.URL
authHTTPClient *http.Client
*ethclient.Client
}
type rpcRequest struct {
ID uint `json:"id"`
Version string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params"`
}
type rpcResponse struct {
ID uint `json:"id"`
Version string `json:"jsonrpc"`
Result interface{} `json:"result,omitempty"`
Error *rpcError `json:"error,omitempty"`
}
type rpcError struct {
endpoint string
Code int `json:"code"`
Message string `json:"message"`
}
func (e *rpcError) Error() string {
return fmt.Sprintf("rpc error: %s (code=%d, url=%s)", e.Message, e.Code, e.endpoint)
}
func (r *RPCClient) SendRocksideTransaction(tx Transaction) (string, error) {
return r.sendTransaction(tx)
}
func (r *RPCClient) SendTransactionFromSmartWallet(tx Transaction) (string, error) {
accounts, err := r.EthAccounts()
if err != nil {
return "", nil
}
var found bool
for _, a := range accounts {
if strings.ToLower(a) == strings.ToLower(tx.From) {
found = true
}
}
if !found {
return "", fmt.Errorf("transaction 'from' address '%s' is not one of your existing Rockside smart wallets", tx.From)
}
return r.sendTransaction(tx)
}
func (r *RPCClient) EthAccounts() ([]string, error) {
body := &rpcRequest{ID: 1, Version: "2.0",
Method: "eth_accounts",
Params: []string{},
}
resp := struct {
Result []string `json:"result"`
}{}
if err := r.post(body, &resp); err != nil {
return []string{}, err
}
return resp.Result, nil
}
func (r *RPCClient) sendTransaction(tx Transaction) (string, error) {
if err := tx.validateFields(); err != nil {
return "", err
}
body := &rpcRequest{ID: 1, Version: "2.0",
Method: "eth_sendTransaction",
Params: []Transaction{tx},
}
resp := struct {
Result string `json:"result"`
}{}
if err := r.post(body, &resp); err != nil {
return "", err
}
return resp.Result, nil
}
func (r *RPCClient) post(body interface{}, decode interface{}) error {
b, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("cannot marshal RPC request to JSON: %s", err)
}
resp, err := r.authHTTPClient.Post(r.endpoint.String(), "", bytes.NewReader(b))
if err != nil {
return err
}
var rpcResp rpcResponse
if err := json.NewDecoder(resp.Body).Decode(&rpcResp); err != nil {
return fmt.Errorf("cannot decode JSON RPC response: %s", err)
}
if err := rpcResp.Error; err != nil {
err.endpoint = r.endpoint.String()
return err
}
if decode != nil {
b, err := json.Marshal(rpcResp)
if err != nil {
return err
}
if err := json.Unmarshal(b, decode); err != nil {
return err
}
}
return nil
}