Skip to content

Commit 96fcb3a

Browse files
authored
Fix Pubkey Not Specified Issues in External Keystore Imports (#6914)
* utilize pubkey from privkey if missing pubkey field in keystore * change tool as well * Merge refs/heads/master into strange-keystore * Merge refs/heads/master into strange-keystore
1 parent f0de09d commit 96fcb3a

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

tools/keystores/main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,25 @@ func readAndDecryptKeystore(fullPath string, password string) error {
242242
}
243243
return err
244244
}
245-
publicKeyBytes, err := hex.DecodeString(keystoreFile.Pubkey)
246-
if err != nil {
247-
return errors.Wrapf(err, "could not parse public key for keystore at path: %s", fullPath)
245+
246+
var pubKeyBytes []byte
247+
// Attempt to use the pubkey present in the keystore itself as a field. If unavailable,
248+
// then utilize the public key directly from the private key.
249+
if keystoreFile.Pubkey != "" {
250+
pubKeyBytes, err = hex.DecodeString(keystoreFile.Pubkey)
251+
if err != nil {
252+
return errors.Wrap(err, "could not decode pubkey from keystore")
253+
}
254+
} else {
255+
privKey, err := bls.SecretKeyFromBytes(privKeyBytes)
256+
if err != nil {
257+
return errors.Wrap(err, "could not initialize private key from bytes")
258+
}
259+
pubKeyBytes = privKey.PublicKey().Marshal()
248260
}
249261
fmt.Printf("\nDecrypted keystore %s\n", au.BrightMagenta(fullPath))
250262
fmt.Printf("Privkey: %#x\n", au.BrightGreen(privKeyBytes))
251-
fmt.Printf("Pubkey: %#x\n", au.BrightGreen(publicKeyBytes))
263+
fmt.Printf("Pubkey: %#x\n", au.BrightGreen(pubKeyBytes))
252264
return nil
253265
}
254266

validator/keymanager/v2/direct/import.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/k0kubun/go-ansi"
1212
"github.com/pkg/errors"
13+
"github.com/prysmaticlabs/prysm/shared/bls"
1314
"github.com/prysmaticlabs/prysm/shared/promptutil"
1415
"github.com/prysmaticlabs/prysm/validator/flags"
1516
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
@@ -88,9 +89,20 @@ func (dr *Keymanager) attemptDecryptKeystore(
8889
} else if err != nil {
8990
return nil, nil, "", errors.Wrap(err, "could not decrypt keystore")
9091
}
91-
pubKeyBytes, err := hex.DecodeString(keystore.Pubkey)
92-
if err != nil {
93-
return nil, nil, "", errors.Wrap(err, "could not decode pubkey from keystore")
92+
var pubKeyBytes []byte
93+
// Attempt to use the pubkey present in the keystore itself as a field. If unavailable,
94+
// then utilize the public key directly from the private key.
95+
if keystore.Pubkey != "" {
96+
pubKeyBytes, err = hex.DecodeString(keystore.Pubkey)
97+
if err != nil {
98+
return nil, nil, "", errors.Wrap(err, "could not decode pubkey from keystore")
99+
}
100+
} else {
101+
privKey, err := bls.SecretKeyFromBytes(privKeyBytes)
102+
if err != nil {
103+
return nil, nil, "", errors.Wrap(err, "could not initialize private key from bytes")
104+
}
105+
pubKeyBytes = privKey.PublicKey().Marshal()
94106
}
95107
return privKeyBytes, pubKeyBytes, password, nil
96108
}

0 commit comments

Comments
 (0)