load both pub/priv p2p keys

Signed-off-by: Leo Chen <leo@harmony.one>
pull/405/head
Leo Chen 6 years ago
parent e74788d6ef
commit 94be80920d
  1. 2
      cmd/bootnode/main.go
  2. 2
      cmd/client/txgen/main.go
  3. 21
      internal/utils/utils.go
  4. 6
      internal/utils/utils_test.go

@ -64,7 +64,7 @@ func main() {
// Init logging. // Init logging.
loggingInit(*logFolder, *ip, *port) loggingInit(*logFolder, *ip, *port)
privKey, err := utils.LoadKeyFromFile(*keyFile) privKey, _, err := utils.LoadKeyFromFile(*keyFile)
if err != nil { if err != nil {
panic(err) panic(err)
} }

@ -72,7 +72,7 @@ func main() {
var bcPeer *p2p.Peer var bcPeer *p2p.Peer
var shardIDLeaderMap map[uint32]p2p.Peer var shardIDLeaderMap map[uint32]p2p.Peer
priKey, err := utils.LoadKeyFromFile(*keyFile) priKey, _, err := utils.LoadKeyFromFile(*keyFile)
if err != nil { if err != nil {
panic(err) panic(err)
} }

@ -150,19 +150,20 @@ func Load(path string, v interface{}) error {
} }
// LoadPrivateKey parses the key string in base64 format and return PrivKey // LoadPrivateKey parses the key string in base64 format and return PrivKey
func LoadPrivateKey(key string) (p2p_crypto.PrivKey, error) { func LoadPrivateKey(key string) (p2p_crypto.PrivKey, p2p_crypto.PubKey, error) {
if key != "" { if key != "" {
k1, err := p2p_crypto.ConfigDecodeKey(key) k1, err := p2p_crypto.ConfigDecodeKey(key)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to decode key: %v", err) return nil, nil, fmt.Errorf("failed to decode key: %v", err)
} }
priKey, err := p2p_crypto.UnmarshalPrivateKey(k1) priKey, err := p2p_crypto.UnmarshalPrivateKey(k1)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to unmarshal private key: %v", err) return nil, nil, fmt.Errorf("failed to unmarshal private key: %v", err)
} }
return priKey, nil pubKey := priKey.GetPublic()
return priKey, pubKey, nil
} }
return nil, fmt.Errorf("empty key string") return nil, nil, fmt.Errorf("empty key string")
} }
// SavePrivateKey convert the PrivKey to base64 format and return string // SavePrivateKey convert the PrivKey to base64 format and return string
@ -194,13 +195,13 @@ func SaveKeyToFile(keyfile string, key p2p_crypto.PrivKey) (err error) {
// LoadKeyFromFile load private key from keyfile // LoadKeyFromFile load private key from keyfile
// If the private key is not loadable or no file, it will generate // If the private key is not loadable or no file, it will generate
// a new random private key // a new random private key
func LoadKeyFromFile(keyfile string) (key p2p_crypto.PrivKey, err error) { func LoadKeyFromFile(keyfile string) (key p2p_crypto.PrivKey, pk p2p_crypto.PubKey, err error) {
var keyStruct PrivKeyStore var keyStruct PrivKeyStore
err = Load(keyfile, &keyStruct) err = Load(keyfile, &keyStruct)
if err != nil { if err != nil {
log.Print("No priviate key can be loaded from file", "keyfile", keyfile) log.Print("No priviate key can be loaded from file", "keyfile", keyfile)
log.Print("Using random private key") log.Print("Using random private key")
key, _, err = GenKeyP2PRand() key, pk, err = GenKeyP2PRand()
if err != nil { if err != nil {
log.Panic("LoadKeyFromFile", "GenKeyP2PRand Error", err) log.Panic("LoadKeyFromFile", "GenKeyP2PRand Error", err)
} }
@ -208,8 +209,8 @@ func LoadKeyFromFile(keyfile string) (key p2p_crypto.PrivKey, err error) {
if err != nil { if err != nil {
log.Print("LoadKeyFromFile", "failed to save key to keyfile", err) log.Print("LoadKeyFromFile", "failed to save key to keyfile", err)
} }
return key, nil return key, pk, nil
} }
key, err = LoadPrivateKey(keyStruct.Key) key, pk, err = LoadPrivateKey(keyStruct.Key)
return key, err return key, pk, err
} }

@ -107,7 +107,7 @@ func TestSaveLoadPrivateKey(t *testing.T) {
t.Fatalf("failed to save private key: %v", err) t.Fatalf("failed to save private key: %v", err)
} }
pk1, err := LoadPrivateKey(str) pk1, _, err := LoadPrivateKey(str)
if err != nil { if err != nil {
t.Fatalf("failed to load key: %v", err) t.Fatalf("failed to load key: %v", err)
} }
@ -135,7 +135,7 @@ func TestSaveLoadKeyFile(t *testing.T) {
t.Fatalf("failed to save key to file: %v", err) t.Fatalf("failed to save key to file: %v", err)
} }
key1, err := LoadKeyFromFile(filename) key1, _, err := LoadKeyFromFile(filename)
if err != nil { if err != nil {
t.Fatalf("failed to load key from file (%s): %v", filename, err) t.Fatalf("failed to load key from file (%s): %v", filename, err)
} }
@ -144,7 +144,7 @@ func TestSaveLoadKeyFile(t *testing.T) {
t.Fatalf("loaded key is not equal to the saved one") t.Fatalf("loaded key is not equal to the saved one")
} }
key2, err := LoadKeyFromFile(nonexist) key2, _, err := LoadKeyFromFile(nonexist)
if err != nil { if err != nil {
t.Fatalf("failed to load key from non-exist file: %v", err) t.Fatalf("failed to load key from non-exist file: %v", err)

Loading…
Cancel
Save