@@ -6,31 +6,50 @@ import (
6
6
7
7
iface "github.com/ipfs/boxo/coreiface"
8
8
caopts "github.com/ipfs/boxo/coreiface/options"
9
- "github.com/ipfs/boxo/coreiface/path"
9
+ "github.com/ipfs/boxo/ipns"
10
+ "github.com/ipfs/boxo/path"
10
11
"github.com/libp2p/go-libp2p/core/peer"
11
12
)
12
13
13
14
type KeyAPI HttpApi
14
15
15
- type keyOutput struct {
16
- JName string `json:"Name"`
17
- Id string
16
+ type key struct {
17
+ name string
18
+ pid peer.ID
19
+ path path.Path
20
+ }
21
+
22
+ func newKey (name , pidStr string ) (* key , error ) {
23
+ pid , err := peer .Decode (pidStr )
24
+ if err != nil {
25
+ return nil , err
26
+ }
18
27
19
- pid peer.ID
28
+ path , err := path .NewPath ("/ipns/" + ipns .NameFromPeer (pid ).String ())
29
+ if err != nil {
30
+ return nil , err
31
+ }
32
+
33
+ return & key {name : name , pid : pid , path : path }, nil
20
34
}
21
35
22
- func (k * keyOutput ) Name () string {
23
- return k .JName
36
+ func (k * key ) Name () string {
37
+ return k .name
24
38
}
25
39
26
- func (k * keyOutput ) Path () path.Path {
27
- return path . New ( "/ipns/" + k . Id )
40
+ func (k * key ) Path () path.Path {
41
+ return k . path
28
42
}
29
43
30
- func (k * keyOutput ) ID () peer.ID {
44
+ func (k * key ) ID () peer.ID {
31
45
return k .pid
32
46
}
33
47
48
+ type keyOutput struct {
49
+ Name string
50
+ Id string
51
+ }
52
+
34
53
func (api * KeyAPI ) Generate (ctx context.Context , name string , opts ... caopts.KeyGenerateOption ) (iface.Key , error ) {
35
54
options , err := caopts .KeyGenerateOptions (opts ... )
36
55
if err != nil {
@@ -45,8 +64,8 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
45
64
if err != nil {
46
65
return nil , err
47
66
}
48
- out . pid , err = peer . Decode ( out . Id )
49
- return & out , err
67
+
68
+ return newKey ( out . Name , out . Id )
50
69
}
51
70
52
71
func (api * KeyAPI ) Rename (ctx context.Context , oldName string , newName string , opts ... caopts.KeyRenameOption ) (iface.Key , bool , error ) {
@@ -68,25 +87,29 @@ func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, o
68
87
return nil , false , err
69
88
}
70
89
71
- id := & keyOutput {JName : out .Now , Id : out .Id }
72
- id .pid , err = peer .Decode (id .Id )
73
- return id , out .Overwrite , err
90
+ key , err := newKey (out .Now , out .Id )
91
+ if err != nil {
92
+ return nil , false , err
93
+ }
94
+
95
+ return key , out .Overwrite , err
74
96
}
75
97
76
98
func (api * KeyAPI ) List (ctx context.Context ) ([]iface.Key , error ) {
77
- var out struct { Keys []* keyOutput }
99
+ var out struct {
100
+ Keys []keyOutput
101
+ }
78
102
if err := api .core ().Request ("key/list" ).Exec (ctx , & out ); err != nil {
79
103
return nil , err
80
104
}
81
105
82
106
res := make ([]iface.Key , len (out .Keys ))
83
107
for i , k := range out .Keys {
84
- var err error
85
- k .pid , err = peer .Decode (k .Id )
108
+ key , err := newKey (k .Name , k .Id )
86
109
if err != nil {
87
110
return nil , err
88
111
}
89
- res [i ] = k
112
+ res [i ] = key
90
113
}
91
114
92
115
return res , nil
@@ -98,24 +121,21 @@ func (api *KeyAPI) Self(ctx context.Context) (iface.Key, error) {
98
121
return nil , err
99
122
}
100
123
101
- var err error
102
- out := keyOutput {JName : "self" , Id : id .ID }
103
- out .pid , err = peer .Decode (out .Id )
104
- return & out , err
124
+ return newKey ("self" , id .ID )
105
125
}
106
126
107
127
func (api * KeyAPI ) Remove (ctx context.Context , name string ) (iface.Key , error ) {
108
- var out struct { Keys []keyOutput }
128
+ var out struct {
129
+ Keys []keyOutput
130
+ }
109
131
if err := api .core ().Request ("key/rm" , name ).Exec (ctx , & out ); err != nil {
110
132
return nil , err
111
133
}
112
134
if len (out .Keys ) != 1 {
113
135
return nil , errors .New ("got unexpected number of keys back" )
114
136
}
115
137
116
- var err error
117
- out .Keys [0 ].pid , err = peer .Decode (out .Keys [0 ].Id )
118
- return & out .Keys [0 ], err
138
+ return newKey (out .Keys [0 ].Name , out .Keys [0 ].Id )
119
139
}
120
140
121
141
func (api * KeyAPI ) core () * HttpApi {
0 commit comments