move the key save/load functions to utils package

Signed-off-by: Leo Chen <leo@harmony.one>
pull/348/head
Leo Chen 6 years ago
parent 21f2d46e29
commit e7b867425f
  1. 41
      cmd/bootnode/main.go

@ -13,7 +13,6 @@ import (
"github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/p2p/p2pimpl"
libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-crypto"
ma "github.com/multiformats/go-multiaddr"
)
@ -24,10 +23,6 @@ var (
commit string
)
type privKey struct {
Key string `json:"key"`
}
func printVersion(me string) {
fmt.Fprintf(os.Stderr, "Harmony (C) 2019. %v, version %v-%v (%v %v)\n", path.Base(me), version, commit, builtBy, builtAt)
os.Exit(0)
@ -73,7 +68,7 @@ func main() {
opts := []libp2p.Option{
libp2p.ListenAddrs(listen),
}
privKey, err := loadKey(*keyFile)
privKey, err := utils.LoadKeyFromFile(*keyFile)
if err != nil {
panic(err)
}
@ -87,37 +82,3 @@ func main() {
log.Info("bootnode", "BN_MA", fmt.Sprintf("/ipv/%s/tcp/%s/p2p/%s", *ip, *port, host.GetID().Pretty()))
}
// saveKey save private key to keyfile
func saveKey(keyfile string, key crypto.PrivKey) (err error) {
str, err := utils.SavePrivateKey(key)
if err != nil {
return
}
keyStruct := privKey{Key: str}
err = utils.Save(keyfile, &keyStruct)
return
}
// loadKey load private key from keyfile
func loadKey(keyfile string) (key crypto.PrivKey, err error) {
var keyStruct privKey
err = utils.Load(keyfile, &keyStruct)
if err != nil {
log.Warn("No priviate key can be loaded from file", "keyfile", keyfile)
log.Warn("Using random private key")
key, _, err = utils.GenKeyP2PRand()
if err != nil {
panic(err)
}
err = saveKey(keyfile, key)
if err != nil {
log.Warn("bootnode", "failed to save key to keyfile", err)
}
return key, nil
}
key, err = utils.LoadPrivateKey(keyStruct.Key)
return key, err
}

Loading…
Cancel
Save