key.go 4.48 KB
Newer Older
1 2 3 4 5 6 7 8 9
package coreapi

import (
	"context"
	"crypto/rand"
	"fmt"
	"sort"

	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
10
	caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
11
	ipfspath "github.com/ipfs/go-ipfs/path"
12

13
	peer "gx/ipfs/QmWNY7dV54ZDYmTA1ykVdwNCqC11mpU4zSUp6XDpLTH9eG/go-libp2p-peer"
14 15 16
	crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
)

17 18 19 20 21
type KeyAPI struct {
	*CoreAPI
	*caopts.KeyOptions
}

22 23 24 25 26
type key struct {
	name   string
	peerId string
}

27
// Name returns the key name
28 29 30 31
func (k *key) Name() string {
	return k.name
}

32
// Path returns the path of the key.
33
func (k *key) Path() coreiface.Path {
Łukasz Magiera's avatar
Łukasz Magiera committed
34
	return &path{path: ipfspath.FromString(ipfspath.Join([]string{"/ipns", k.peerId}))}
35 36
}

37 38
// Generate generates new key, stores it in the keystore under the specified
// name and returns a base58 encoded multihash of its public key.
39
func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (coreiface.Key, error) {
40 41
	options, err := caopts.KeyGenerateOptions(opts...)
	if err != nil {
42
		return nil, err
43
	}
44

Łukasz Magiera's avatar
Łukasz Magiera committed
45 46 47 48 49 50 51 52 53
	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)
	}

54 55 56
	var sk crypto.PrivKey
	var pk crypto.PubKey

57
	switch options.Algorithm {
58
	case "rsa":
Łukasz Magiera's avatar
Łukasz Magiera committed
59 60
		if options.Size == -1 {
			options.Size = caopts.DefaultRSALen
61 62
		}

63
		priv, pub, err := crypto.GenerateKeyPairWithReader(crypto.RSA, options.Size, rand.Reader)
64
		if err != nil {
65
			return nil, err
66 67 68 69 70 71 72
		}

		sk = priv
		pk = pub
	case "ed25519":
		priv, pub, err := crypto.GenerateEd25519Key(rand.Reader)
		if err != nil {
73
			return nil, err
74 75 76 77 78
		}

		sk = priv
		pk = pub
	default:
79
		return nil, fmt.Errorf("unrecognized key type: %s", options.Algorithm)
80 81
	}

82
	err = api.node.Repo.Keystore().Put(name, sk)
83
	if err != nil {
84
		return nil, err
85 86 87 88
	}

	pid, err := peer.IDFromPublicKey(pk)
	if err != nil {
89
		return nil, err
90 91
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
92
	return &key{name, pid.Pretty()}, nil
93 94
}

95
// List returns a list keys stored in keystore.
96
func (api *KeyAPI) List(ctx context.Context) ([]coreiface.Key, error) {
97 98 99 100 101 102 103
	keys, err := api.node.Repo.Keystore().List()
	if err != nil {
		return nil, err
	}

	sort.Strings(keys)

104 105
	out := make([]coreiface.Key, len(keys)+1)
	out[0] = &key{"self", api.node.Identity.Pretty()}
106

107 108
	for n, k := range keys {
		privKey, err := api.node.Repo.Keystore().Get(k)
109 110 111 112 113 114 115 116 117 118 119
		if err != nil {
			return nil, err
		}

		pubKey := privKey.GetPublic()

		pid, err := peer.IDFromPublicKey(pubKey)
		if err != nil {
			return nil, err
		}

120
		out[n+1] = &key{k, pid.Pretty()}
121 122 123 124
	}
	return out, nil
}

125 126
// Rename renames `oldName` to `newName`. Returns the key and whether another
// key was overwritten, or an error.
127
func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, opts ...caopts.KeyRenameOption) (coreiface.Key, bool, error) {
128
	options, err := caopts.KeyRenameOptions(opts...)
129 130
	if err != nil {
		return nil, false, err
131 132
	}

133 134 135
	ks := api.node.Repo.Keystore()

	if oldName == "self" {
136
		return nil, false, fmt.Errorf("cannot rename key with name 'self'")
137 138 139
	}

	if newName == "self" {
140
		return nil, false, fmt.Errorf("cannot overwrite key with name 'self'")
141 142 143 144
	}

	oldKey, err := ks.Get(oldName)
	if err != nil {
145
		return nil, false, fmt.Errorf("no key named %s was found", oldName)
146 147 148 149 150 151
	}

	pubKey := oldKey.GetPublic()

	pid, err := peer.IDFromPublicKey(pubKey)
	if err != nil {
152
		return nil, false, err
153 154 155
	}

	overwrite := false
156
	if options.Force {
157 158
		exist, err := ks.Has(newName)
		if err != nil {
159
			return nil, false, err
160 161 162 163 164 165
		}

		if exist {
			overwrite = true
			err := ks.Delete(newName)
			if err != nil {
166
				return nil, false, err
167 168 169 170 171 172
			}
		}
	}

	err = ks.Put(newName, oldKey)
	if err != nil {
173
		return nil, false, err
174 175
	}

176
	return &key{newName, pid.Pretty()}, overwrite, ks.Delete(oldName)
177 178
}

179
// Remove removes keys from keystore. Returns ipns path of the removed key.
180
func (api *KeyAPI) Remove(ctx context.Context, name string) (coreiface.Path, error) {
181 182 183
	ks := api.node.Repo.Keystore()

	if name == "self" {
184
		return nil, fmt.Errorf("cannot remove key with name 'self'")
185 186 187 188
	}

	removed, err := ks.Get(name)
	if err != nil {
189
		return nil, fmt.Errorf("no key named %s was found", name)
190 191 192 193 194 195
	}

	pubKey := removed.GetPublic()

	pid, err := peer.IDFromPublicKey(pubKey)
	if err != nil {
196
		return nil, err
197 198 199 200
	}

	err = ks.Delete(name)
	if err != nil {
201
		return nil, err
202 203
	}

204
	return (&key{"", pid.Pretty()}).Path(), nil
205 206 207
}

func (api *KeyAPI) core() coreiface.CoreAPI {
208
	return api.CoreAPI
209
}