1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 02:30:39 +08:00

fix(ipfs2/init) identity

This commit is contained in:
Brian Tiger Chow
2014-11-04 19:13:04 -08:00
committed by Juan Batiz-Benet
parent 62fd9166ce
commit e305e45e81

View File

@ -3,6 +3,7 @@ package main
import (
"encoding/base64"
"errors"
"fmt"
"os"
"path/filepath"
@ -92,7 +93,11 @@ func doInit(configRoot string, dspath string, force bool, nBitsForKeypair int) e
}
cfg.Datastore = ds
cfg.Identity = config.Identity{}
identity, err := identityConfig(nBitsForKeypair)
if err != nil {
return err
}
cfg.Identity = identity
// setup the node addresses.
cfg.Addresses = config.Addresses{
@ -106,31 +111,6 @@ func doInit(configRoot string, dspath string, force bool, nBitsForKeypair int) e
IPNS: "/ipns",
}
// TODO guard higher up
if nBitsForKeypair < 1024 {
return errors.New("Bitsize less than 1024 is considered unsafe.")
}
u.POut("generating key pair\n")
sk, pk, err := ci.GenerateKeyPair(ci.RSA, nBitsForKeypair)
if err != nil {
return err
}
// currently storing key unencrypted. in the future we need to encrypt it.
// TODO(security)
skbytes, err := sk.Bytes()
if err != nil {
return err
}
cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
id, err := peer.IDFromPubKey(pk)
if err != nil {
return err
}
cfg.Identity.PeerID = id.Pretty()
cfg.Bootstrap = defaultPeers
// tracking ipfs version used to generate the init folder and adding update checker default setting.
@ -172,3 +152,33 @@ func datastoreConfig(dspath string) (config.Datastore, error) {
return ds, nil
}
func identityConfig(nbits int) (config.Identity, error) {
// TODO guard higher up
ident := config.Identity{}
if nbits < 1024 {
return ident, errors.New("Bitsize less than 1024 is considered unsafe.")
}
fmt.Println("generating key pair...")
sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
if err != nil {
return ident, err
}
// currently storing key unencrypted. in the future we need to encrypt it.
// TODO(security)
skbytes, err := sk.Bytes()
if err != nil {
return ident, err
}
ident.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
id, err := peer.IDFromPubKey(pk)
if err != nil {
return ident, err
}
ident.PeerID = id.Pretty()
return ident, nil
}