Skip to content

Commit 396c34b

Browse files
committed
coreapi: key tests
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 8df2d1a commit 396c34b

File tree

6 files changed

+440
-10
lines changed

6 files changed

+440
-10
lines changed

core/coreapi/interface/interface.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ type KeyAPI interface {
148148
// name and returns a base58 encoded multihash of it's public key
149149
Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (Key, error)
150150

151-
// WithAlgorithm is an option for Generate which specifies which algorithm
151+
// WithType is an option for Generate which specifies which algorithm
152152
// should be used for the key. Default is options.RSAKey
153153
//
154-
// Supported algorithms:
154+
// Supported key types:
155155
// * options.RSAKey
156156
// * options.Ed25519Key
157-
WithAlgorithm(algorithm string) options.KeyGenerateOption
157+
WithType(algorithm string) options.KeyGenerateOption
158158

159159
// WithSize is an option for Generate which specifies the size of the key to
160160
// generated. Default is 0

core/coreapi/interface/options/key.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package options
33
const (
44
RSAKey = "rsa"
55
Ed25519Key = "ed25519"
6+
7+
DefaultRSALen = 2048
68
)
79

810
type KeyGenerateSettings struct {
@@ -20,7 +22,7 @@ type KeyRenameOption func(*KeyRenameSettings) error
2022
func KeyGenerateOptions(opts ...KeyGenerateOption) (*KeyGenerateSettings, error) {
2123
options := &KeyGenerateSettings{
2224
Algorithm: RSAKey,
23-
Size: 0,
25+
Size: -1,
2426
}
2527

2628
for _, opt := range opts {
@@ -48,7 +50,7 @@ func KeyRenameOptions(opts ...KeyRenameOption) (*KeyRenameSettings, error) {
4850

4951
type KeyOptions struct{}
5052

51-
func (api *KeyOptions) WithAlgorithm(algorithm string) KeyGenerateOption {
53+
func (api *KeyOptions) WithType(algorithm string) KeyGenerateOption {
5254
return func(settings *KeyGenerateSettings) error {
5355
settings.Algorithm = algorithm
5456
return nil

core/coreapi/key.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (k *key) Name() string {
2929
}
3030

3131
func (k *key) Path() coreiface.Path {
32-
return &path{path: ipfspath.FromString(ipfspath.Join([]string{"/ipns/", k.peerId}))}
32+
return &path{path: ipfspath.FromString(ipfspath.Join([]string{"/ipns", k.peerId}))}
3333
}
3434

3535
func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (coreiface.Key, error) {
@@ -38,13 +38,22 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
3838
return nil, err
3939
}
4040

41+
if name == "self" {
42+
return nil, fmt.Errorf("cannot overwrite key with name 'self'")
43+
}
44+
45+
_, err = api.node.Repo.Keystore().Get(name)
46+
if err == nil {
47+
return nil, fmt.Errorf("key with name '%s' already exists", name)
48+
}
49+
4150
var sk crypto.PrivKey
4251
var pk crypto.PubKey
4352

4453
switch options.Algorithm {
4554
case "rsa":
46-
if options.Size == 0 {
47-
return nil, fmt.Errorf("please specify a key size with WithSize option")
55+
if options.Size == -1 {
56+
options.Size = caopts.DefaultRSALen
4857
}
4958

5059
priv, pub, err := crypto.GenerateKeyPairWithReader(crypto.RSA, options.Size, rand.Reader)
@@ -76,7 +85,7 @@ func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.Key
7685
return nil, err
7786
}
7887

79-
return &key{name, pid.String()}, nil
88+
return &key{name, pid.Pretty()}, nil
8089
}
8190

8291
func (api *KeyAPI) List(ctx context.Context) ([]coreiface.Key, error) {

0 commit comments

Comments
 (0)