forked from TRON-US/go-btfs-api
-
Notifications
You must be signed in to change notification settings - Fork 8
/
key.go
84 lines (71 loc) · 1.65 KB
/
key.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
package shell
import "context"
type Key struct {
Id string
Name string
}
type KeyRenameObject struct {
Id string
Now string
Overwrite bool
Was string
}
type keyListOutput struct {
Keys []*Key
}
type KeyOpt func(*RequestBuilder) error
type keyGen struct{}
var KeyGen keyGen
func (keyGen) Type(alg string) KeyOpt {
return func(rb *RequestBuilder) error {
rb.Option("type", alg)
return nil
}
}
func (keyGen) Size(size int) KeyOpt {
return func(rb *RequestBuilder) error {
rb.Option("size", size)
return nil
}
}
// KeyGen Create a new keypair
func (s *Shell) KeyGen(ctx context.Context, name string, options ...KeyOpt) (*Key, error) {
rb := s.Request("key/gen", name)
for _, opt := range options {
if err := opt(rb); err != nil {
return nil, err
}
}
var out Key
if err := rb.Exec(ctx, &out); err != nil {
return nil, err
}
return &out, nil
}
// KeyList List all local keypairs
func (s *Shell) KeyList(ctx context.Context) ([]*Key, error) {
var out keyListOutput
if err := s.Request("key/list").Exec(ctx, &out); err != nil {
return nil, err
}
return out.Keys, nil
}
// KeyRename Rename a keypair
func (s *Shell) KeyRename(ctx context.Context, old string, new string, force bool) (*KeyRenameObject, error) {
var out KeyRenameObject
if err := s.Request("key/rename", old, new).
Option("force", force).
Exec(ctx, &out); err != nil {
return nil, err
}
return &out, nil
}
// KeyRm remove a keypair
func (s *Shell) KeyRm(ctx context.Context, name string) ([]*Key, error) {
var out keyListOutput
if err := s.Request("key/rm", name).
Exec(ctx, &out); err != nil {
return nil, err
}
return out.Keys, nil
}