8
8
9
9
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
10
10
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
11
+ ipfspath "github.com/ipfs/go-ipfs/path"
11
12
12
- peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB /go-libp2p-peer"
13
+ peer "gx/ipfs/QmWNY7dV54ZDYmTA1ykVdwNCqC11mpU4zSUp6XDpLTH9eG /go-libp2p-peer"
13
14
crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
14
15
)
15
16
@@ -18,10 +19,23 @@ type KeyAPI struct {
18
19
* caopts.KeyOptions
19
20
}
20
21
21
- func (api * KeyAPI ) Generate (ctx context.Context , name string , opts ... caopts.KeyGenerateOption ) (string , error ) {
22
+ type key struct {
23
+ name string
24
+ peerId string
25
+ }
26
+
27
+ func (k * key ) Name () string {
28
+ return k .name
29
+ }
30
+
31
+ func (k * key ) Path () coreiface.Path {
32
+ return & path {path : ipfspath .FromString (ipfspath .Join ([]string {"/ipns/" , k .peerId }))}
33
+ }
34
+
35
+ func (api * KeyAPI ) Generate (ctx context.Context , name string , opts ... caopts.KeyGenerateOption ) (coreiface.Key , error ) {
22
36
options , err := caopts .KeyGenerateOptions (opts ... )
23
37
if err != nil {
24
- return "" , err
38
+ return nil , err
25
39
}
26
40
27
41
var sk crypto.PrivKey
@@ -30,54 +44,54 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
30
44
switch options .Algorithm {
31
45
case "rsa" :
32
46
if options .Size == 0 {
33
- return "" , fmt .Errorf ("please specify a key size with WithSize option" )
47
+ return nil , fmt .Errorf ("please specify a key size with WithSize option" )
34
48
}
35
49
36
50
priv , pub , err := crypto .GenerateKeyPairWithReader (crypto .RSA , options .Size , rand .Reader )
37
51
if err != nil {
38
- return "" , err
52
+ return nil , err
39
53
}
40
54
41
55
sk = priv
42
56
pk = pub
43
57
case "ed25519" :
44
58
priv , pub , err := crypto .GenerateEd25519Key (rand .Reader )
45
59
if err != nil {
46
- return "" , err
60
+ return nil , err
47
61
}
48
62
49
63
sk = priv
50
64
pk = pub
51
65
default :
52
- return "" , fmt .Errorf ("unrecognized key type: %s" , options .Algorithm )
66
+ return nil , fmt .Errorf ("unrecognized key type: %s" , options .Algorithm )
53
67
}
54
68
55
69
err = api .node .Repo .Keystore ().Put (name , sk )
56
70
if err != nil {
57
- return "" , err
71
+ return nil , err
58
72
}
59
73
60
74
pid , err := peer .IDFromPublicKey (pk )
61
75
if err != nil {
62
- return "" , err
76
+ return nil , err
63
77
}
64
78
65
- return pid .String (), nil
79
+ return & key { name , pid .String ()} , nil
66
80
}
67
81
68
- func (api * KeyAPI ) List (ctx context.Context ) (map [ string ] string , error ) {
82
+ func (api * KeyAPI ) List (ctx context.Context ) ([]coreiface. Key , error ) {
69
83
keys , err := api .node .Repo .Keystore ().List ()
70
84
if err != nil {
71
85
return nil , err
72
86
}
73
87
74
88
sort .Strings (keys )
75
89
76
- out := make (map [ string ] string , len (keys )+ 1 )
77
- out ["self" ] = api .node .Identity .Pretty ()
90
+ out := make ([]coreiface. Key , len (keys )+ 1 )
91
+ out [0 ] = & key { "self" , api .node .Identity .Pretty ()}
78
92
79
- for _ , key := range keys {
80
- privKey , err := api .node .Repo .Keystore ().Get (key )
93
+ for n , k := range keys {
94
+ privKey , err := api .node .Repo .Keystore ().Get (k )
81
95
if err != nil {
82
96
return nil , err
83
97
}
@@ -89,88 +103,88 @@ func (api *KeyAPI) List(ctx context.Context) (map[string]string, error) {
89
103
return nil , err
90
104
}
91
105
92
- out [key ] = pid .Pretty ()
106
+ out [n + 1 ] = & key { k , pid .Pretty ()}
93
107
}
94
108
return out , nil
95
109
}
96
110
97
- func (api * KeyAPI ) Rename (ctx context.Context , oldName string , newName string , opts ... caopts.KeyRenameOption ) (string , bool , error ) {
111
+ func (api * KeyAPI ) Rename (ctx context.Context , oldName string , newName string , opts ... caopts.KeyRenameOption ) (coreiface. Key , bool , error ) {
98
112
options , err := caopts .KeyRenameOptions (opts ... )
99
- if newName == "self" {
100
- return "" , false , err
113
+ if err != nil {
114
+ return nil , false , err
101
115
}
102
116
103
117
ks := api .node .Repo .Keystore ()
104
118
105
119
if oldName == "self" {
106
- return "" , false , fmt .Errorf ("cannot rename key with name 'self'" )
120
+ return nil , false , fmt .Errorf ("cannot rename key with name 'self'" )
107
121
}
108
122
109
123
if newName == "self" {
110
- return "" , false , fmt .Errorf ("cannot overwrite key with name 'self'" )
124
+ return nil , false , fmt .Errorf ("cannot overwrite key with name 'self'" )
111
125
}
112
126
113
127
oldKey , err := ks .Get (oldName )
114
128
if err != nil {
115
- return "" , false , fmt .Errorf ("no key named %s was found" , oldName )
129
+ return nil , false , fmt .Errorf ("no key named %s was found" , oldName )
116
130
}
117
131
118
132
pubKey := oldKey .GetPublic ()
119
133
120
134
pid , err := peer .IDFromPublicKey (pubKey )
121
135
if err != nil {
122
- return "" , false , err
136
+ return nil , false , err
123
137
}
124
138
125
139
overwrite := false
126
140
if options .Force {
127
141
exist , err := ks .Has (newName )
128
142
if err != nil {
129
- return "" , false , err
143
+ return nil , false , err
130
144
}
131
145
132
146
if exist {
133
147
overwrite = true
134
148
err := ks .Delete (newName )
135
149
if err != nil {
136
- return "" , false , err
150
+ return nil , false , err
137
151
}
138
152
}
139
153
}
140
154
141
155
err = ks .Put (newName , oldKey )
142
156
if err != nil {
143
- return "" , false , err
157
+ return nil , false , err
144
158
}
145
159
146
- return pid .Pretty (), overwrite , ks .Delete (oldName )
160
+ return & key { newName , pid .Pretty ()} , overwrite , ks .Delete (oldName )
147
161
}
148
162
149
- func (api * KeyAPI ) Remove (ctx context.Context , name string ) (string , error ) {
163
+ func (api * KeyAPI ) Remove (ctx context.Context , name string ) (coreiface. Path , error ) {
150
164
ks := api .node .Repo .Keystore ()
151
165
152
166
if name == "self" {
153
- return "" , fmt .Errorf ("cannot remove key with name 'self'" )
167
+ return nil , fmt .Errorf ("cannot remove key with name 'self'" )
154
168
}
155
169
156
170
removed , err := ks .Get (name )
157
171
if err != nil {
158
- return "" , fmt .Errorf ("no key named %s was found" , name )
172
+ return nil , fmt .Errorf ("no key named %s was found" , name )
159
173
}
160
174
161
175
pubKey := removed .GetPublic ()
162
176
163
177
pid , err := peer .IDFromPublicKey (pubKey )
164
178
if err != nil {
165
- return "" , err
179
+ return nil , err
166
180
}
167
181
168
182
err = ks .Delete (name )
169
183
if err != nil {
170
- return "" , err
184
+ return nil , err
171
185
}
172
186
173
- return pid .Pretty (), nil
187
+ return ( & key { "" , pid .Pretty ()}). Path (), nil
174
188
}
175
189
176
190
func (api * KeyAPI ) core () coreiface.CoreAPI {
0 commit comments