Allow to set key hex as a flag

This commit is contained in:
Dmitry 2018-06-04 08:16:54 +03:00 committed by Dmitry Shulyak
parent 5d0120ee28
commit 68966af049
1 changed files with 25 additions and 3 deletions

View File

@ -2,6 +2,8 @@
package main package main
import ( import (
"crypto/ecdsa"
"encoding/hex"
"flag" "flag"
"net" "net"
"os" "os"
@ -31,6 +33,7 @@ func main() {
var ( var (
listenAddr = flag.String("addr", ":30301", "listen address") listenAddr = flag.String("addr", ":30301", "listen address")
nodeKeyFile = flag.String("nodekey", "", "private key filename") nodeKeyFile = flag.String("nodekey", "", "private key filename")
keydata = flag.String("keydata", "", "hex encoded private key")
verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)") verbosity = flag.Int("verbosity", int(log.LvlInfo), "log verbosity (0-9)")
vmodule = flag.String("vmodule", "", "log verbosity pattern") vmodule = flag.String("vmodule", "", "log verbosity pattern")
nursery = bootnodes{} nursery = bootnodes{}
@ -45,10 +48,29 @@ func main() {
} }
log.Root().SetHandler(glogger) log.Root().SetHandler(glogger)
nodeKey, err := crypto.LoadECDSA(*nodeKeyFile) if len(*nodeKeyFile) == 0 && len(*keydata) == 0 {
log.Crit("either `nodekey` or `keydata` must be provided")
}
var (
nodeKey *ecdsa.PrivateKey
err error
)
if len(*nodeKeyFile) != 0 {
nodeKey, err = crypto.LoadECDSA(*nodeKeyFile)
if err != nil { if err != nil {
log.Crit("Failed to load ecdsa key from", "file", *nodeKeyFile, "error", err) log.Crit("Failed to load ecdsa key from", "file", *nodeKeyFile, "error", err)
} }
} else if len(*keydata) != 0 {
log.Warn("key will be visible in process list. should be used only for tests")
key, err := hex.DecodeString(*keydata)
if err != nil {
log.Crit("unable to decode hex", "data", keydata, "error", err)
}
nodeKey, err = crypto.ToECDSA(key)
if err != nil {
log.Crit("unable to convert decoded hex into ecdsa.PrivateKey", "data", key, "error", err)
}
}
addr, err := net.ResolveUDPAddr("udp", *listenAddr) addr, err := net.ResolveUDPAddr("udp", *listenAddr)
if err != nil { if err != nil {