diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index b7a4baf759c..a8d51f1bf93 100644 --- a/cmd/ipfs/init.go +++ b/cmd/ipfs/init.go @@ -16,10 +16,13 @@ import ( namesys "github.com/ipfs/go-ipfs/namesys" config "github.com/ipfs/go-ipfs/repo/config" fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" + ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto" ) +// The default keypair is 2048-bit RSA const ( nBitsForKeypairDefault = 2048 + keypairTypeDefault = ci.RSA ) var initCmd = &cmds.Command{ @@ -49,6 +52,7 @@ environment variable: }, Options: []cmds.Option{ cmds.IntOption("bits", "b", "Number of bits to use in the generated RSA private key.").Default(nBitsForKeypairDefault), + cmds.IntOption("key-type", "k", "Key type (RSA or Ed25519-id").Default(ci.RSA), cmds.BoolOption("empty-repo", "e", "Don't add and pin help files to the local storage.").Default(false), cmds.StringOption("profile", "p", "Apply profile settings to config. Multiple profiles can be separated by ','"), @@ -90,6 +94,12 @@ environment variable: return } + keyType, _, err := req.Option("k").Int() + if err != nil { + res.SetError(err, cmds.ErrNormal) + return + } + var conf *config.Config f := req.Files() @@ -118,7 +128,7 @@ environment variable: profiles = strings.Split(profile, ",") } - if err := doInit(os.Stdout, req.InvocContext().ConfigRoot, empty, nBitsForKeypair, profiles, conf); err != nil { + if err := doInit(os.Stdout, req.InvocContext().ConfigRoot, empty, nBitsForKeypair, keyType, profiles, conf); err != nil { res.SetError(err, cmds.ErrNormal) return } @@ -130,10 +140,10 @@ Reinitializing would overwrite your keys. `) func initWithDefaults(out io.Writer, repoRoot string) error { - return doInit(out, repoRoot, false, nBitsForKeypairDefault, nil, nil) + return doInit(out, repoRoot, false, nBitsForKeypairDefault, keypairTypeDefault, nil, nil) } -func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles []string, conf *config.Config) error { +func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair, keyType int, confProfiles []string, conf *config.Config) error { if _, err := fmt.Fprintf(out, "initializing IPFS node at %s\n", repoRoot); err != nil { return err } @@ -148,7 +158,7 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con if conf == nil { var err error - conf, err = config.Init(out, nBitsForKeypair) + conf, err = config.Init(out, nBitsForKeypair, keyType) if err != nil { return err } diff --git a/repo/config/init.go b/repo/config/init.go index 1f8ee434be1..3c9dfb2c5ca 100644 --- a/repo/config/init.go +++ b/repo/config/init.go @@ -10,8 +10,8 @@ import ( ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto" ) -func Init(out io.Writer, nBitsForKeypair int) (*Config, error) { - identity, err := identityConfig(out, nBitsForKeypair) +func Init(out io.Writer, nBitsForKeypair, keyType int) (*Config, error) { + identity, err := identityConfig(out, nBitsForKeypair, keyType) if err != nil { return nil, err } @@ -93,15 +93,22 @@ func datastoreConfig() (Datastore, error) { } // identityConfig initializes a new identity. -func identityConfig(out io.Writer, nbits int) (Identity, error) { +func identityConfig(out io.Writer, nbits, keyType int) (Identity, error) { // TODO guard higher up ident := Identity{} - if nbits < 1024 { - return ident, errors.New("Bitsize less than 1024 is considered unsafe.") + + switch keyType { + case ci.RSA: + if nbits < 1024 { + return ident, errors.New("Bitsize less than 1024 is considered unsafe for RSA.") + } + + fmt.Fprintf(out, "generating %v-bit RSA keypair...", nbits) + case ci.Ed25519: + fmt.Fprintf(out, "generating Ed25519 keypair...") } - fmt.Fprintf(out, "generating %v-bit RSA keypair...", nbits) - sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits) + sk, pk, err := ci.GenerateKeyPair(keyType, nbits) if err != nil { return ident, err }