@cryptotaxi247 / kubo / commits / 1caf71766

coreapi: name/key review suggestions

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com> This commit was moved from ipfs/interface-go-ipfs-core@8f7e0241ec5a85f3acaf3152107bd9ad202f56a8 This commit was moved from ipfs/boxo@d0730a47d3a8d4e4be5cc9d1886b8b5a32816267

Łukasz Magiera committed Dec 17, 2017 at 03:44 UTC 1caf71766f81e4c950ed4d527b4c5933cdd53726
3 files changed +41 -25
core/coreiface/interface.go
+26 -19
@@ -28,16 +28,21 @@ type Path interface {
28 type Node ipld.Node
29 type Link ipld.Link
30
31 -type IpnsEntry struct {
32 - Name string
33 - Value Path
34 -}
35 -
31 type Reader interface {
32 io.ReadSeeker
33 io.Closer
34 }
35
36 +type IpnsEntry interface {
37 + Name() string
38 + Value() Path
39 +}
40 +
41 +type Key interface {
42 + Name() string
43 + Path() Path
44 +}
45 +
46 // CoreAPI defines an unified interface to IPFS for Go programs.
47 type CoreAPI interface {
48 // Unixfs returns an implementation of Unixfs API
@@ -108,7 +113,7 @@ type DagAPI interface {
113 // You can use .Key API to list and generate more names and their respective keys.
114 type NameAPI interface {
115 // Publish announces new IPNS name
111 - Publish(ctx context.Context, path Path, opts ...options.NamePublishOption) (*IpnsEntry, error)
116 + Publish(ctx context.Context, path Path, opts ...options.NamePublishOption) (IpnsEntry, error)
117
118 // WithValidTime is an option for Publish which specifies for how long the
119 // entry will remain valid. Default value is 24h
@@ -116,8 +121,9 @@ type NameAPI interface {
121
122 // WithKey is an option for Publish which specifies the key to use for
123 // publishing. Default value is "self" which is the node's own PeerID.
124 + // The key parameter must be either PeerID or keystore key alias.
125 //
120 - // You can use .Key API to list and generate more names and their respective keys.
126 + // You can use KeyAPI to list and generate more names and their respective keys.
127 WithKey(key string) options.NamePublishOption
128
129 // Resolve attempts to resolve the newest version of the specified name
@@ -131,41 +137,42 @@ type NameAPI interface {
137 // offline. Default value is false
138 WithLocal(local bool) options.NameResolveOption
139
134 - // WithNoCache is an option for Resolve which specifies when set to true
135 - // disables the use of local name cache. Default value is false
136 - WithNoCache(nocache bool) options.NameResolveOption
140 + // WithCache is an option for Resolve which specifies if cache should be used.
141 + // Default value is true
142 + WithCache(cache bool) options.NameResolveOption
143 }
144
145 // KeyAPI specifies the interface to Keystore
146 type KeyAPI interface {
147 // Generate generates new key, stores it in the keystore under the specified
148 // name and returns a base58 encoded multihash of it's public key
143 - Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (string, error)
149 + Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (Key, error)
150
151 // WithAlgorithm is an option for Generate which specifies which algorithm
146 - // should be used for the key. Default is "rsa"
152 + // should be used for the key. Default is options.RSAKey
153 //
154 // Supported algorithms:
149 - // * rsa
150 - // * ed25519
155 + // * options.RSAKey
156 + // * options.Ed25519Key
157 WithAlgorithm(algorithm string) options.KeyGenerateOption
158
159 // WithSize is an option for Generate which specifies the size of the key to
160 // generated. Default is 0
161 WithSize(size int) options.KeyGenerateOption
162
157 - // Rename renames oldName key to newName.
158 - Rename(ctx context.Context, oldName string, newName string, opts ...options.KeyRenameOption) (string, bool, error)
163 + // Rename renames oldName key to newName. Returns the key and whether another
164 + // key was overwritten, or an error
165 + Rename(ctx context.Context, oldName string, newName string, opts ...options.KeyRenameOption) (Key, bool, error)
166
167 // WithForce is an option for Rename which specifies whether to allow to
168 // replace existing keys.
169 WithForce(force bool) options.KeyRenameOption
170
171 // List lists keys stored in keystore
165 - List(ctx context.Context) (map[string]string, error) //TODO: better key type?
172 + List(ctx context.Context) ([]Key, error)
173
167 - // Remove removes keys from keystore
168 - Remove(ctx context.Context, name string) (string, error)
174 + // Remove removes keys from keystore. Returns ipns path of the removed key
175 + Remove(ctx context.Context, name string) (Path, error)
176 }
177
178 // type ObjectAPI interface {
core/coreiface/options/key.go
+6 -1
@@ -1,5 +1,10 @@
1 package options
2
3 +const (
4 + RSAKey = "rsa"
5 + Ed25519Key = "ed25519"
6 +)
7 +
8 type KeyGenerateSettings struct {
9 Algorithm string
10 Size int
@@ -14,7 +19,7 @@ type KeyRenameOption func(*KeyRenameSettings) error
19
20 func KeyGenerateOptions(opts ...KeyGenerateOption) (*KeyGenerateSettings, error) {
21 options := &KeyGenerateSettings{
17 - Algorithm: "rsa",
22 + Algorithm: RSAKey,
23 Size: 0,
24 }
25
core/coreiface/options/name.go
+9 -5
@@ -4,6 +4,10 @@ import (
4 "time"
5 )
6
7 +const (
8 + DefaultNameValidTime = 24 * time.Hour
9 +)
10 +
11 type NamePublishSettings struct {
12 ValidTime time.Duration
13 Key string
@@ -12,7 +16,7 @@ type NamePublishSettings struct {
16 type NameResolveSettings struct {
17 Recursive bool
18 Local bool
15 - Nocache bool
19 + Cache bool
20 }
21
22 type NamePublishOption func(*NamePublishSettings) error
@@ -20,7 +24,7 @@ type NameResolveOption func(*NameResolveSettings) error
24
25 func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error) {
26 options := &NamePublishSettings{
23 - ValidTime: 24 * time.Hour,
27 + ValidTime: DefaultNameValidTime,
28 Key: "self",
29 }
30
@@ -38,7 +42,7 @@ func NameResolveOptions(opts ...NameResolveOption) (*NameResolveSettings, error)
42 options := &NameResolveSettings{
43 Recursive: false,
44 Local: false,
41 - Nocache: false,
45 + Cache: true,
46 }
47
48 for _, opt := range opts {
@@ -81,9 +85,9 @@ func (api *NameOptions) WithLocal(local bool) NameResolveOption {
85 }
86 }
87
84 -func (api *NameOptions) WithNoCache(nocache bool) NameResolveOption {
88 +func (api *NameOptions) WithCache(cache bool) NameResolveOption {
89 return func(settings *NameResolveSettings) error {
86 - settings.Nocache = nocache
90 + settings.Cache = cache
91 return nil
92 }
93 }