go-ipfs-config: Add Init Alternative allowing specification of ED25519 key

This commit is contained in:
Will Scott 2020-04-20 17:37:46 -07:00
parent 48dc791903
commit d02cdb3963

View File

@ -1,17 +1,23 @@
package config
import (
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"time"
"github.com/ipfs/interface-go-ipfs-core/options"
ci "github.com/libp2p/go-libp2p-core/crypto"
peer "github.com/libp2p/go-libp2p-core/peer"
)
func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
identity, err := identityConfig(out, nBitsForKeypair)
return InitWithOptions(out, []options.KeyGenerateOption{options.Key.Size(nBitsForKeypair)})
}
func InitWithOptions(out io.Writer, opts []options.KeyGenerateOption) (*Config, error) {
identity, err := identityConfig(out, opts)
if err != nil {
return nil, err
}
@ -165,18 +171,43 @@ func flatfsSpec() map[string]interface{} {
}
// identityConfig initializes a new identity.
func identityConfig(out io.Writer, nbits int) (Identity, error) {
func identityConfig(out io.Writer, opts []options.KeyGenerateOption) (Identity, error) {
// TODO guard higher up
ident := Identity{}
if nbits < ci.MinRsaKeyBits {
return ident, ci.ErrRsaKeyTooSmall
}
fmt.Fprintf(out, "generating %v-bit RSA keypair...", nbits)
sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
settings, err := options.KeyGenerateOptions(opts...)
if err != nil {
return ident, err
}
var sk ci.PrivKey
var pk ci.PubKey
fmt.Fprintf(out, "generating %s keypair...", settings.Algorithm)
switch settings.Algorithm {
case "rsa":
if settings.Size == -1 {
settings.Size = options.DefaultRSALen
}
priv, pub, err := ci.GenerateKeyPair(ci.RSA, settings.Size)
if err != nil {
return ident, err
}
sk = priv
pk = pub
case "ed25519":
priv, pub, err := ci.GenerateEd25519Key(rand.Reader)
if err != nil {
return ident, err
}
sk = priv
pk = pub
default:
return ident, fmt.Errorf("unrecognized key type: %s", settings.Algorithm)
}
fmt.Fprintf(out, "done\n")
// currently storing key unencrypted. in the future we need to encrypt it.