2020-08-21 19:43:03 +00:00
|
|
|
package derived
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
2020-08-21 19:43:03 +00:00
|
|
|
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ExtractKeystores retrieves the secret keys for specified public keys
|
|
|
|
// in the function input, encrypts them using the specified password,
|
|
|
|
// and returns their respective EIP-2335 keystores.
|
|
|
|
func (dr *Keymanager) ExtractKeystores(
|
2020-10-12 08:11:05 +00:00
|
|
|
_ context.Context, publicKeys []bls.PublicKey, password string,
|
2020-10-15 22:31:52 +00:00
|
|
|
) ([]*keymanager.Keystore, error) {
|
2020-08-21 19:43:03 +00:00
|
|
|
encryptor := keystorev4.New()
|
2020-10-15 22:31:52 +00:00
|
|
|
keystores := make([]*keymanager.Keystore, len(publicKeys))
|
2020-10-10 02:07:28 +00:00
|
|
|
lock.RLock()
|
|
|
|
defer lock.RUnlock()
|
2020-08-21 19:43:03 +00:00
|
|
|
for i, pk := range publicKeys {
|
|
|
|
pubKeyBytes := pk.Marshal()
|
2020-10-10 02:07:28 +00:00
|
|
|
secretKey, ok := secretKeysCache[bytesutil.ToBytes48(pubKeyBytes)]
|
2020-08-21 19:43:03 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"secret key for public key %#x not found in cache",
|
|
|
|
pubKeyBytes,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
cryptoFields, err := encryptor.Encrypt(secretKey.Marshal(), password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(
|
|
|
|
err,
|
|
|
|
"could not encrypt secret key for public key %#x",
|
|
|
|
pubKeyBytes,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
id, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-15 22:31:52 +00:00
|
|
|
keystores[i] = &keymanager.Keystore{
|
2020-08-21 19:43:03 +00:00
|
|
|
Crypto: cryptoFields,
|
|
|
|
ID: id.String(),
|
|
|
|
Pubkey: fmt.Sprintf("%x", pubKeyBytes),
|
|
|
|
Version: encryptor.Version(),
|
|
|
|
Name: encryptor.Name(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keystores, nil
|
|
|
|
}
|