Skip to content

Commit

Permalink
coreapi: key tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Jan 1, 2018
1 parent 8df2d1a commit 396c34b
Show file tree
Hide file tree
Showing 6 changed files with 440 additions and 10 deletions.
6 changes: 3 additions & 3 deletions core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ type KeyAPI interface {
// name and returns a base58 encoded multihash of it's public key
Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (Key, error)

// WithAlgorithm is an option for Generate which specifies which algorithm
// WithType is an option for Generate which specifies which algorithm
// should be used for the key. Default is options.RSAKey
//
// Supported algorithms:
// Supported key types:
// * options.RSAKey
// * options.Ed25519Key
WithAlgorithm(algorithm string) options.KeyGenerateOption
WithType(algorithm string) options.KeyGenerateOption

// WithSize is an option for Generate which specifies the size of the key to
// generated. Default is 0
Expand Down
6 changes: 4 additions & 2 deletions core/coreapi/interface/options/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package options
const (
RSAKey = "rsa"
Ed25519Key = "ed25519"

DefaultRSALen = 2048
)

type KeyGenerateSettings struct {
Expand All @@ -20,7 +22,7 @@ type KeyRenameOption func(*KeyRenameSettings) error
func KeyGenerateOptions(opts ...KeyGenerateOption) (*KeyGenerateSettings, error) {
options := &KeyGenerateSettings{
Algorithm: RSAKey,
Size: 0,
Size: -1,
}

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

type KeyOptions struct{}

func (api *KeyOptions) WithAlgorithm(algorithm string) KeyGenerateOption {
func (api *KeyOptions) WithType(algorithm string) KeyGenerateOption {
return func(settings *KeyGenerateSettings) error {
settings.Algorithm = algorithm
return nil
Expand Down
17 changes: 13 additions & 4 deletions core/coreapi/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (k *key) Name() string {
}

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

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

if name == "self" {
return nil, fmt.Errorf("cannot overwrite key with name 'self'")
}

_, err = api.node.Repo.Keystore().Get(name)
if err == nil {
return nil, fmt.Errorf("key with name '%s' already exists", name)
}

var sk crypto.PrivKey
var pk crypto.PubKey

switch options.Algorithm {
case "rsa":
if options.Size == 0 {
return nil, fmt.Errorf("please specify a key size with WithSize option")
if options.Size == -1 {
options.Size = caopts.DefaultRSALen
}

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

return &key{name, pid.String()}, nil
return &key{name, pid.Pretty()}, nil
}

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

0 comments on commit 396c34b

Please sign in to comment.