key.go 3.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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
package coreapi

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

	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"

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

type KeyAPI CoreAPI

func (api *KeyAPI) Generate(ctx context.Context, name string, algorithm string, size int) (string, error) {
	var sk crypto.PrivKey
	var pk crypto.PubKey

	switch algorithm {
	case "rsa":
		if size == 0 {
			return "", fmt.Errorf("please specify a key size with --size")
		}

		priv, pub, err := crypto.GenerateKeyPairWithReader(crypto.RSA, size, rand.Reader)
		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:
		return "", fmt.Errorf("unrecognized key type: %s", algorithm)
	}

	err := api.node.Repo.Keystore().Put(name, sk)
	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
}

func (api *KeyAPI) Rename(ctx context.Context, oldName string, newName string, force bool) (string, bool, error) {
	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
	if force {
		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 {
	return (*CoreAPI)(api)
}