-
Notifications
You must be signed in to change notification settings - Fork 671
/
client.go
73 lines (62 loc) · 1.95 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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package keystore
import (
"time"
"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/utils/rpc"
)
// Client ...
type Client struct {
requester rpc.EndpointRequester
}
// NewClient ...
func NewClient(uri string, requestTimeout time.Duration) *Client {
return &Client{
requester: rpc.NewEndpointRequester(uri, "/ext/keystore", "keystore", requestTimeout),
}
}
// CreateUser ...
func (c *Client) CreateUser(user api.UserPass) (bool, error) {
res := &api.SuccessResponse{}
err := c.requester.SendRequest("createUser", &user, res)
return res.Success, err
}
// ListUsers lists the usernames of all keystore users on the node
func (c *Client) ListUsers() ([]string, error) {
res := &ListUsersReply{}
err := c.requester.SendRequest("listUsers", struct{}{}, res)
return res.Users, err
}
// ExportUser returns the byte representation of the requested [user]
func (c *Client) ExportUser(user api.UserPass) ([]byte, error) {
res := &ExportUserReply{
Encoding: formatting.Hex,
}
err := c.requester.SendRequest("exportUser", &user, res)
if err != nil {
return nil, err
}
return formatting.Decode(res.Encoding, res.User)
}
// ImportUser imports the keystore user in [account] under [user]
func (c *Client) ImportUser(user api.UserPass, account []byte) (bool, error) {
accountStr, err := formatting.Encode(formatting.Hex, account)
if err != nil {
return false, err
}
res := &api.SuccessResponse{}
err = c.requester.SendRequest("importUser", &ImportUserArgs{
UserPass: user,
User: accountStr,
Encoding: formatting.Hex,
}, res)
return res.Success, err
}
// DeleteUser removes [user] from the node's keystore users
func (c *Client) DeleteUser(user api.UserPass) (bool, error) {
res := &api.SuccessResponse{}
err := c.requester.SendRequest("deleteUser", &user, res)
return res.Success, err
}