@cryptotaxi247 / kubo / commits / e305e45e8

fix(ipfs2/init) identity

Brian Tiger Chow committed Nov 4, 2014 at 19:13 UTC e305e45e8165b7877db7638e633204e40d7ed9ba
1 file changed +36 -26
cmd/ipfs2/init.go
+36 -26
@@ -3,6 +3,7 @@ package main
3 import (
4 "encoding/base64"
5 "errors"
6 + "fmt"
7 "os"
8 "path/filepath"
9
@@ -92,7 +93,11 @@ func doInit(configRoot string, dspath string, force bool, nBitsForKeypair int) e
93 }
94 cfg.Datastore = ds
95
95 - cfg.Identity = config.Identity{}
96 + identity, err := identityConfig(nBitsForKeypair)
97 + if err != nil {
98 + return err
99 + }
100 + cfg.Identity = identity
101
102 // setup the node addresses.
103 cfg.Addresses = config.Addresses{
@@ -106,31 +111,6 @@ func doInit(configRoot string, dspath string, force bool, nBitsForKeypair int) e
111 IPNS: "/ipns",
112 }
113
109 - // TODO guard higher up
110 - if nBitsForKeypair < 1024 {
111 - return errors.New("Bitsize less than 1024 is considered unsafe.")
112 - }
113 -
114 - u.POut("generating key pair\n")
115 - sk, pk, err := ci.GenerateKeyPair(ci.RSA, nBitsForKeypair)
116 - if err != nil {
117 - return err
118 - }
119 -
120 - // currently storing key unencrypted. in the future we need to encrypt it.
121 - // TODO(security)
122 - skbytes, err := sk.Bytes()
123 - if err != nil {
124 - return err
125 - }
126 - cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
127 -
128 - id, err := peer.IDFromPubKey(pk)
129 - if err != nil {
130 - return err
131 - }
132 - cfg.Identity.PeerID = id.Pretty()
133 -
114 cfg.Bootstrap = defaultPeers
115
116 // 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) {
152
153 return ds, nil
154 }
155 +
156 +func identityConfig(nbits int) (config.Identity, error) {
157 + // TODO guard higher up
158 + ident := config.Identity{}
159 + if nbits < 1024 {
160 + return ident, errors.New("Bitsize less than 1024 is considered unsafe.")
161 + }
162 +
163 + fmt.Println("generating key pair...")
164 + sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
165 + if err != nil {
166 + return ident, err
167 + }
168 +
169 + // currently storing key unencrypted. in the future we need to encrypt it.
170 + // TODO(security)
171 + skbytes, err := sk.Bytes()
172 + if err != nil {
173 + return ident, err
174 + }
175 + ident.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
176 +
177 + id, err := peer.IDFromPubKey(pk)
178 + if err != nil {
179 + return ident, err
180 + }
181 + ident.PeerID = id.Pretty()
182 +
183 + return ident, nil
184 +}