mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-03 07:18:12 +08:00
refactor(config/init) add hooks to identity generation. useful for displaying messages to the user
@jbenet this commit re-introduces the peer identity line. It's very useful. I understand it may have been removed because of the clashing of the IDs. To alleviate this, this commit places some negative space between the two lines. 'to test' -> 'to get started' as a stronger call to action ``` λ. ipfs2 init -f initializing ipfs node at /Users/btc/.go-ipfs generating key pair...done. peer identity: QmWzjxNEYKjDAxuHJqvtLP1dZTDjreBSUsArWoHai1v9yP to get started, enter: ipfs cat QmYpv2VEsxzTTXRYX3PjDg961cnJE3kY1YDXLycHGQ3zZB ``` License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
This commit is contained in:
parent
1c4c7412b4
commit
c8c062fe13
@ -103,10 +103,23 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = addTheWelcomeFile(conf, func(k u.Key) {
|
||||
fmt.Printf("\nto get started, enter: ipfs cat %s\n", k)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// addTheWelcomeFile adds a file containing the welcome message to the newly
|
||||
// minted node. On success, it calls onSuccess
|
||||
func addTheWelcomeFile(conf *config.Config, onSuccess func(u.Key)) error {
|
||||
// TODO extract this file creation operation into a function
|
||||
nd, err := core.NewIpfsNode(conf, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
defer nd.Close()
|
||||
|
||||
@ -115,15 +128,15 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
|
||||
|
||||
defnd, err := imp.BuildDagFromReader(reader, nd.DAG, nd.Pinning.GetManual(), chunk.DefaultSplitter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
k, err := defnd.Key()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to write test file: %s", err)
|
||||
return fmt.Errorf("failed to write test file: %s", err)
|
||||
}
|
||||
fmt.Printf("done.\nto test, enter: ipfs cat %s\n", k)
|
||||
return nil, nil
|
||||
onSuccess(k)
|
||||
return nil
|
||||
}
|
||||
|
||||
func datastoreConfig(dspath string) (config.Datastore, error) {
|
||||
@ -152,7 +165,12 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
|
||||
return nil, err
|
||||
}
|
||||
|
||||
identity, err := identityConfig(nBitsForKeypair)
|
||||
identity, err := identityConfig(nBitsForKeypair, func() {
|
||||
fmt.Printf("generating key pair...")
|
||||
}, func(ident config.Identity) {
|
||||
fmt.Printf("done.\n")
|
||||
fmt.Printf("peer identity: %s\n", ident.PeerID)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -202,14 +220,17 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
func identityConfig(nbits int) (config.Identity, error) {
|
||||
// identityConfig initializes a new identity. It calls onBegin when it begins
|
||||
// to generate the identity and it calls onSuccess once the operation is
|
||||
// completed successfully
|
||||
func identityConfig(nbits int, onBegin func(), onSuccess func(config.Identity)) (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...")
|
||||
onBegin()
|
||||
sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
|
||||
if err != nil {
|
||||
return ident, err
|
||||
@ -228,8 +249,7 @@ func identityConfig(nbits int) (config.Identity, error) {
|
||||
return ident, err
|
||||
}
|
||||
ident.PeerID = id.Pretty()
|
||||
fmt.Println("peer identity: %s", id.Pretty())
|
||||
|
||||
onSuccess(ident)
|
||||
return ident, nil
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user