forked from Consensys/quorum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
213 lines (177 loc) · 5.14 KB
/
gateway.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package account
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/jpmorganchase/quorum-account-plugin-sdk-go/proto"
)
type service struct {
client proto.AccountServiceClient
mu sync.Mutex
isStreaming bool
}
func (g *service) Status(ctx context.Context) (string, error) {
resp, err := g.client.Status(ctx, &proto.StatusRequest{})
if err != nil {
return "", err
}
if resp == nil {
return "", errors.New("empty response from plugin")
}
return resp.Status, err
}
func (g *service) Open(ctx context.Context, passphrase string) error {
_, err := g.client.Open(ctx, &proto.OpenRequest{Passphrase: passphrase})
return err
}
func (g *service) Close(ctx context.Context) error {
_, err := g.client.Close(ctx, &proto.CloseRequest{})
return err
}
func (g *service) Accounts(ctx context.Context) []accounts.Account {
resp, err := g.client.Accounts(ctx, &proto.AccountsRequest{})
if err != nil {
log.Error("unable to get accounts from plugin account store", "err", err)
return []accounts.Account{}
}
if resp == nil {
log.Error("empty response from plugin")
return []accounts.Account{}
}
return asAccounts(resp.Accounts)
}
func (g *service) Contains(ctx context.Context, account accounts.Account) bool {
resp, err := g.client.Contains(ctx, &proto.ContainsRequest{Address: account.Address.Bytes()})
if err != nil {
log.Error("unable to check contents of plugin account store", "err", err)
}
if resp == nil {
log.Error("empty response from plugin")
return false
}
return resp.IsContained
}
func (g *service) Sign(ctx context.Context, account accounts.Account, toSign []byte) ([]byte, error) {
resp, err := g.client.Sign(ctx, &proto.SignRequest{
Address: account.Address.Bytes(),
ToSign: toSign,
})
if err != nil {
return nil, err
}
if resp == nil {
return nil, errors.New("empty response from plugin")
}
return resp.Sig, nil
}
func (g *service) UnlockAndSign(ctx context.Context, account accounts.Account, toSign []byte, passphrase string) ([]byte, error) {
resp, err := g.client.UnlockAndSign(ctx, &proto.UnlockAndSignRequest{
Address: account.Address.Bytes(),
ToSign: toSign,
Passphrase: passphrase,
})
if err != nil {
return nil, err
}
if resp == nil {
return nil, errors.New("empty response from plugin")
}
return resp.Sig, nil
}
func (g *service) TimedUnlock(ctx context.Context, account accounts.Account, password string, duration time.Duration) error {
_, err := g.client.TimedUnlock(ctx, &proto.TimedUnlockRequest{Address: account.Address.Bytes(), Password: password, Duration: duration.Nanoseconds()})
return err
}
func (g *service) Lock(ctx context.Context, account accounts.Account) error {
_, err := g.client.Lock(ctx, &proto.LockRequest{Address: account.Address.Bytes()})
return err
}
func (g *service) NewAccount(ctx context.Context, newAccountConfig interface{}) (accounts.Account, error) {
byt, err := json.Marshal(newAccountConfig)
if err != nil {
return accounts.Account{}, err
}
req := &proto.NewAccountRequest{
NewAccountConfig: byt,
}
resp, err := g.client.NewAccount(ctx, req)
if err != nil {
return accounts.Account{}, err
}
acct, err := asAccount(resp.Account)
if err != nil {
return accounts.Account{}, err
}
return acct, nil
}
func (g *service) ImportRawKey(ctx context.Context, rawKey string, newAccountConfig interface{}) (accounts.Account, error) {
byt, err := json.Marshal(newAccountConfig)
if err != nil {
return accounts.Account{}, err
}
// validate the rawKey
_, err = crypto.HexToECDSA(rawKey)
if err != nil {
return accounts.Account{}, err
}
req := &proto.ImportRawKeyRequest{
RawKey: rawKey,
NewAccountConfig: byt,
}
resp, err := g.client.ImportRawKey(ctx, req)
if err != nil {
return accounts.Account{}, err
}
acct, err := asAccount(resp.Account)
if err != nil {
return accounts.Account{}, err
}
return acct, nil
}
func asAccounts(pAccts []*proto.Account) []accounts.Account {
accts := make([]accounts.Account, 0, len(pAccts))
for i, pAcct := range pAccts {
acct, err := asAccount(pAcct)
if err != nil {
log.Error("unable to parse account from plugin account store", "index", i, "err", err)
continue
}
accts = append(accts, acct)
}
return accts
}
func asAccount(pAcct *proto.Account) (accounts.Account, error) {
addr := strings.TrimSpace(common.Bytes2Hex(pAcct.Address))
if !common.IsHexAddress(addr) {
return accounts.Account{}, fmt.Errorf("invalid hex address: %v", addr)
}
url, err := ToUrl(pAcct.Url)
if err != nil {
return accounts.Account{}, err
}
acct := accounts.Account{
Address: common.HexToAddress(addr),
URL: url,
}
return acct, nil
}
func ToUrl(strUrl string) (accounts.URL, error) {
if strUrl == "" {
return accounts.URL{}, nil
}
//to parse a string url as an accounts.URL it must first be in json format
toParse := fmt.Sprintf("\"%v\"", strUrl)
var url accounts.URL
if err := url.UnmarshalJSON([]byte(toParse)); err != nil {
return accounts.URL{}, err
}
return url, nil
}