-
Notifications
You must be signed in to change notification settings - Fork 662
/
client.go
77 lines (64 loc) · 2.45 KB
/
client.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package keystore
import (
"context"
"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/utils/rpc"
)
var _ Client = &client{}
// Client interface for Avalanche Keystore API Endpoint
type Client interface {
CreateUser(context.Context, api.UserPass, ...rpc.Option) error
// Returns the usernames of all keystore users
ListUsers(context.Context, ...rpc.Option) ([]string, error)
// Returns the byte representation of the given user
ExportUser(context.Context, api.UserPass, ...rpc.Option) ([]byte, error)
// Import [exportedUser] to [importTo]
ImportUser(ctx context.Context, importTo api.UserPass, exportedUser []byte, options ...rpc.Option) error
// Delete the given user
DeleteUser(context.Context, api.UserPass, ...rpc.Option) error
}
// Client implementation for Avalanche Keystore API Endpoint
type client struct {
requester rpc.EndpointRequester
}
func NewClient(uri string) Client {
return &client{requester: rpc.NewEndpointRequester(
uri+"/ext/keystore",
"keystore",
)}
}
func (c *client) CreateUser(ctx context.Context, user api.UserPass, options ...rpc.Option) error {
return c.requester.SendRequest(ctx, "createUser", &user, &api.EmptyReply{}, options...)
}
func (c *client) ListUsers(ctx context.Context, options ...rpc.Option) ([]string, error) {
res := &ListUsersReply{}
err := c.requester.SendRequest(ctx, "listUsers", struct{}{}, res, options...)
return res.Users, err
}
func (c *client) ExportUser(ctx context.Context, user api.UserPass, options ...rpc.Option) ([]byte, error) {
res := &ExportUserReply{
Encoding: formatting.Hex,
}
err := c.requester.SendRequest(ctx, "exportUser", &user, res, options...)
if err != nil {
return nil, err
}
return formatting.Decode(res.Encoding, res.User)
}
func (c *client) ImportUser(ctx context.Context, user api.UserPass, account []byte, options ...rpc.Option) error {
accountStr, err := formatting.Encode(formatting.Hex, account)
if err != nil {
return err
}
return c.requester.SendRequest(ctx, "importUser", &ImportUserArgs{
UserPass: user,
User: accountStr,
Encoding: formatting.Hex,
}, &api.EmptyReply{}, options...)
}
func (c *client) DeleteUser(ctx context.Context, user api.UserPass, options ...rpc.Option) error {
return c.requester.SendRequest(ctx, "deleteUser", &user, &api.EmptyReply{}, options...)
}