2022-02-01 19:54:19 +00:00
|
|
|
package local
|
2020-08-11 23:15:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/pkg/errors"
|
2021-09-15 22:55:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/crypto/bls"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
2020-08-11 23:15:06 +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.
|
2021-12-07 17:52:39 +00:00
|
|
|
func (_ *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-10-10 02:07:28 +00:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
2020-08-11 23:15:06 +00:00
|
|
|
encryptor := keystorev4.New()
|
2020-10-15 22:31:52 +00:00
|
|
|
keystores := make([]*keymanager.Keystore, len(publicKeys))
|
2020-08-11 23:15:06 +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-11 23:15:06 +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-11 23:15:06 +00:00
|
|
|
Crypto: cryptoFields,
|
|
|
|
ID: id.String(),
|
|
|
|
Pubkey: fmt.Sprintf("%x", pubKeyBytes),
|
|
|
|
Version: encryptor.Version(),
|
|
|
|
Name: encryptor.Name(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keystores, nil
|
|
|
|
}
|