-
Notifications
You must be signed in to change notification settings - Fork 659
/
service.go
118 lines (97 loc) · 3.13 KB
/
service.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package keystore
import (
"fmt"
"net/http"
"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/database/manager"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/version"
)
type service struct {
ks *keystore
}
func (s *service) CreateUser(_ *http.Request, args *api.UserPass, _ *api.EmptyReply) error {
s.ks.log.Debug("Keystore: CreateUser called",
logging.UserString("username", args.Username),
)
return s.ks.CreateUser(args.Username, args.Password)
}
func (s *service) DeleteUser(_ *http.Request, args *api.UserPass, _ *api.EmptyReply) error {
s.ks.log.Debug("Keystore: DeleteUser called",
logging.UserString("username", args.Username),
)
return s.ks.DeleteUser(args.Username, args.Password)
}
type ListUsersReply struct {
Users []string `json:"users"`
}
func (s *service) ListUsers(_ *http.Request, args *struct{}, reply *ListUsersReply) error {
s.ks.log.Debug("Keystore: ListUsers called")
var err error
reply.Users, err = s.ks.ListUsers()
return err
}
type ImportUserArgs struct {
// The username and password of the user being imported
api.UserPass
// The string representation of the user
User string `json:"user"`
// The encoding of [User] ("hex")
Encoding formatting.Encoding `json:"encoding"`
}
func (s *service) ImportUser(r *http.Request, args *ImportUserArgs, _ *api.EmptyReply) error {
s.ks.log.Debug("Keystore: ImportUser called",
logging.UserString("username", args.Username),
)
// Decode the user from string to bytes
user, err := formatting.Decode(args.Encoding, args.User)
if err != nil {
return fmt.Errorf("couldn't decode 'user' to bytes: %w", err)
}
return s.ks.ImportUser(args.Username, args.Password, user)
}
type ExportUserArgs struct {
// The username and password
api.UserPass
// The encoding for the exported user ("hex")
Encoding formatting.Encoding `json:"encoding"`
}
type ExportUserReply struct {
// String representation of the user
User string `json:"user"`
// The encoding for the exported user ("hex")
Encoding formatting.Encoding `json:"encoding"`
}
func (s *service) ExportUser(_ *http.Request, args *ExportUserArgs, reply *ExportUserReply) error {
s.ks.log.Debug("Keystore: ExportUser called",
logging.UserString("username", args.Username),
)
userBytes, err := s.ks.ExportUser(args.Username, args.Password)
if err != nil {
return err
}
// Encode the user from bytes to string
reply.User, err = formatting.Encode(args.Encoding, userBytes)
if err != nil {
return fmt.Errorf("couldn't encode user to string: %w", err)
}
reply.Encoding = args.Encoding
return nil
}
// CreateTestKeystore returns a new keystore that can be utilized for testing
func CreateTestKeystore() (Keystore, error) {
dbManager, err := manager.NewManagerFromDBs([]*manager.VersionedDatabase{
{
Database: memdb.New(),
Version: version.Semantic1_0_0,
},
})
if err != nil {
return nil, err
}
return New(logging.NoLog{}, dbManager), nil
}