key.go 3.5 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 12 13 14 15

	peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
	crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
)

16 17 18 19 20 21 22 23 24 25
type KeyAPI struct {
	*CoreAPI
	*caopts.KeyOptions
}

func (api *KeyAPI) Generate(ctx context.Context, name string, opts ...caopts.KeyGenerateOption) (string, error) {
	options, err := caopts.KeyGenerateOptions(opts...)
	if err != nil {
		return "", err
	}
26 27 28 29

	var sk crypto.PrivKey
	var pk crypto.PubKey

30
	switch options.Algorithm {
31
	case "rsa":
32
		if options.Size == 0 {
33 34 35
			return "", fmt.Errorf("please specify a key size with --size")
		}

36
		priv, pub, err := crypto.GenerateKeyPairWithReader(crypto.RSA, options.Size, rand.Reader)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
		if err != nil {
			return "", err
		}

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

		sk = priv
		pk = pub
	default:
52
		return "", fmt.Errorf("unrecognized key type: %s", options.Algorithm)
53 54
	}

55
	err = api.node.Repo.Keystore().Put(name, sk)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	if err != nil {
		return "", err
	}

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

	return pid.String(), nil
}

func (api *KeyAPI) List(ctx context.Context) (map[string]string, error) {
	keys, err := api.node.Repo.Keystore().List()
	if err != nil {
		return nil, err
	}

	sort.Strings(keys)

	out := make(map[string]string, len(keys)+1)
	out["self"] = api.node.Identity.Pretty()

	for _, key := range keys {
		privKey, err := api.node.Repo.Keystore().Get(key)
		if err != nil {
			return nil, err
		}

		pubKey := privKey.GetPublic()

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

		out[key] = pid.Pretty()
	}
	return out, nil
}

97 98 99 100 101 102
func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, opts ...caopts.KeyRenameOption) (string, bool, error) {
	options, err := caopts.KeyRenameOptions(opts...)
	if newName == "self" {
		return "", false, err
	}

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	ks := api.node.Repo.Keystore()

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

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

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

	pubKey := oldKey.GetPublic()

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

	overwrite := false
126
	if options.Force {
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
		exist, err := ks.Has(newName)
		if err != nil {
			return "", false, err
		}

		if exist {
			overwrite = true
			err := ks.Delete(newName)
			if err != nil {
				return "", false, err
			}
		}
	}

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

	return pid.Pretty(), overwrite, ks.Delete(oldName)
}

func (api *KeyAPI) Remove(ctx context.Context, name string) (string, error) {
	ks := api.node.Repo.Keystore()

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

	removed, err := ks.Get(name)
	if err != nil {
		return "", fmt.Errorf("no key named %s was found", name)
	}

	pubKey := removed.GetPublic()

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

	err = ks.Delete(name)
	if err != nil {
		return "", err
	}

	return pid.Pretty(), nil
}

func (api *KeyAPI) core() coreiface.CoreAPI {
177
	return api.CoreAPI
178
}