This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
server.go
305 lines (253 loc) · 7.37 KB
/
server.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package keys
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"hash"
"strings"
"github.com/hyperledger/burrow/crypto"
hex "github.com/tmthrgd/go-hex"
"golang.org/x/crypto/ripemd160"
"google.golang.org/grpc"
)
//------------------------------------------------------------------------
// all cli commands pass through the http KeyStore
// the KeyStore process also maintains the unlocked accounts
func StandAloneServer(keysDir string, AllowBadFilePermissions bool) *grpc.Server {
grpcServer := grpc.NewServer()
RegisterKeysServer(grpcServer, NewFilesystemKeyStore(keysDir, AllowBadFilePermissions))
return grpcServer
}
//------------------------------------------------------------------------
// handlers
func (k *FilesystemKeyStore) GenerateKey(ctx context.Context, in *GenRequest) (*GenResponse, error) {
curveT, err := crypto.CurveTypeFromString(in.CurveType)
if err != nil {
return nil, err
}
key, err := k.Gen(in.Passphrase, curveT)
if err != nil {
return nil, fmt.Errorf("error generating key %s %s", curveT, err)
}
addrH := key.Address.String()
if in.KeyName != "" {
err = coreNameAdd(k.keysDirPath, in.KeyName, addrH)
if err != nil {
return nil, err
}
}
return &GenResponse{Address: addrH}, nil
}
func (k *FilesystemKeyStore) Export(ctx context.Context, in *ExportRequest) (*ExportResponse, error) {
addr, err := getNameAddr(k.keysDirPath, in.GetName(), in.GetAddress())
if err != nil {
return nil, err
}
addrB, err := crypto.AddressFromHexString(addr)
if err != nil {
return nil, err
}
// No phrase needed for public key. I hope.
key, err := k.GetKey(in.GetPassphrase(), addrB.Bytes())
if err != nil {
return nil, err
}
return &ExportResponse{
Address: addrB[:],
CurveType: key.CurveType.String(),
Publickey: key.PublicKey.PublicKey[:],
Privatekey: key.PrivateKey.PrivateKey[:],
}, nil
}
func (k *FilesystemKeyStore) PublicKey(ctx context.Context, in *PubRequest) (*PubResponse, error) {
addr, err := getNameAddr(k.keysDirPath, in.GetName(), in.GetAddress())
if err != nil {
return nil, err
}
addrB, err := crypto.AddressFromHexString(addr)
if err != nil {
return nil, err
}
// No phrase needed for public key. I hope.
key, err := k.GetKey("", addrB.Bytes())
if key == nil {
return nil, err
}
return &PubResponse{CurveType: key.CurveType.String(), PublicKey: key.Pubkey()}, nil
}
func (k *FilesystemKeyStore) Sign(ctx context.Context, in *SignRequest) (*SignResponse, error) {
addr, err := getNameAddr(k.keysDirPath, in.GetName(), in.GetAddress())
if err != nil {
return nil, err
}
addrB, err := crypto.AddressFromHexString(addr)
if err != nil {
return nil, err
}
key, err := k.GetKey(in.GetPassphrase(), addrB[:])
if err != nil {
return nil, err
}
sig, err := key.PrivateKey.Sign(in.GetMessage())
if err != nil {
return nil, err
}
return &SignResponse{Signature: sig}, err
}
func (k *FilesystemKeyStore) Verify(ctx context.Context, in *VerifyRequest) (*VerifyResponse, error) {
if in.GetPublicKey() == nil {
return nil, fmt.Errorf("must provide a pubkey")
}
if in.GetMessage() == nil {
return nil, fmt.Errorf("must provide a message")
}
if in.GetSignature() == nil {
return nil, fmt.Errorf("must provide a signature")
}
sig := in.GetSignature()
pubkey, err := crypto.PublicKeyFromBytes(in.GetPublicKey(), sig.GetCurveType())
if err != nil {
return nil, err
}
err = pubkey.Verify(in.GetMessage(), sig)
if err != nil {
return nil, err
}
return &VerifyResponse{}, nil
}
func (k *FilesystemKeyStore) Hash(ctx context.Context, in *HashRequest) (*HashResponse, error) {
var hasher hash.Hash
switch in.GetHashtype() {
case "ripemd160":
hasher = ripemd160.New()
case "sha256":
hasher = sha256.New()
// case "sha3":
default:
return nil, fmt.Errorf("unknown hash type %v", in.GetHashtype())
}
hasher.Write(in.GetMessage())
return &HashResponse{Hash: hex.EncodeUpperToString(hasher.Sum(nil))}, nil
}
func (k *FilesystemKeyStore) ImportJSON(ctx context.Context, in *ImportJSONRequest) (*ImportResponse, error) {
keyJSON := []byte(in.GetJSON())
addr := isValidKeyJson(keyJSON)
if addr != nil {
_, err := writeKey(k.keysDirPath, addr, keyJSON)
if err != nil {
return nil, err
}
} else {
j1 := new(struct {
CurveType string
Address string
PublicKey string
AddressHash string
PrivateKey string
})
err := json.Unmarshal([]byte(in.GetJSON()), &j1)
if err != nil {
return nil, err
}
addr, err = hex.DecodeString(j1.Address)
if err != nil {
return nil, err
}
curveT, err := crypto.CurveTypeFromString(j1.CurveType)
if err != nil {
return nil, err
}
privKey, err := hex.DecodeString(j1.PrivateKey)
if err != nil {
return nil, err
}
key, err := NewKeyFromPriv(curveT, privKey)
if err != nil {
return nil, err
}
// store the new key
if err = k.StoreKey(in.GetPassphrase(), key); err != nil {
return nil, err
}
}
return &ImportResponse{Address: hex.EncodeUpperToString(addr)}, nil
}
func (k *FilesystemKeyStore) Import(ctx context.Context, in *ImportRequest) (*ImportResponse, error) {
curveT, err := crypto.CurveTypeFromString(in.GetCurveType())
if err != nil {
return nil, err
}
key, err := NewKeyFromPriv(curveT, in.GetKeyBytes())
if err != nil {
return nil, err
}
// store the new key
if err = k.StoreKey(in.GetPassphrase(), key); err != nil {
return nil, err
}
if in.GetName() != "" {
if err := coreNameAdd(k.keysDirPath, in.GetName(), key.Address.String()); err != nil {
return nil, err
}
}
return &ImportResponse{Address: hex.EncodeUpperToString(key.Address[:])}, nil
}
func (k *FilesystemKeyStore) List(ctx context.Context, in *ListRequest) (*ListResponse, error) {
byname, err := coreNameList(k.keysDirPath)
if err != nil {
return nil, err
}
var list []*KeyID
if in.KeyName != "" {
if addr, ok := byname[in.KeyName]; ok {
list = append(list, &KeyID{KeyName: getAddressNames(addr, byname), Address: addr})
} else {
if addr, err := crypto.AddressFromHexString(in.KeyName); err == nil {
_, err := k.GetKey("", addr[:])
if err == nil {
address := addr.String()
list = append(list, &KeyID{Address: address, KeyName: getAddressNames(address, byname)})
}
}
}
} else {
// list all address
datadir, err := returnDataDir(k.keysDirPath)
if err != nil {
return nil, err
}
addrs, err := getAllAddresses(datadir)
if err != nil {
return nil, err
}
for _, addr := range addrs {
list = append(list, &KeyID{KeyName: getAddressNames(addr, byname), Address: addr})
}
}
return &ListResponse{Key: list}, nil
}
func getAddressNames(address string, byname map[string]string) []string {
names := make([]string, 0)
for name, addr := range byname {
if address == addr {
names = append(names, name)
}
}
return names
}
func (k *FilesystemKeyStore) RemoveName(ctx context.Context, in *RemoveNameRequest) (*RemoveNameResponse, error) {
if in.GetKeyName() == "" {
return nil, fmt.Errorf("please specify a name")
}
return &RemoveNameResponse{}, coreNameRm(k.keysDirPath, in.GetKeyName())
}
func (k *FilesystemKeyStore) AddName(ctx context.Context, in *AddNameRequest) (*AddNameResponse, error) {
if in.GetKeyname() == "" {
return nil, fmt.Errorf("please specify a name")
}
if in.GetAddress() == "" {
return nil, fmt.Errorf("please specify an address")
}
return &AddNameResponse{}, coreNameAdd(k.keysDirPath, in.GetKeyname(), strings.ToUpper(in.GetAddress()))
}