prysm-pulse/validator/keymanager/imported/backup.go
Radosław Kapka 5569a68452
Code cleanup (#9992)
* Value assigned to a variable is never read before being overwritten

* The result of append is not used anywhere

* Suspicious assignment of range-loop vars detected

* Unused method receiver detected

* Revert "Auxiliary commit to revert individual files from 54edcb445484a2e5d79612e19af8e949b8861253"

This reverts commit bbd1e1beabf7b0c5cfc4f514dcc820062ad6c063.

* Method modifies receiver

* Fix test

* Duplicate imports detected

* Incorrectly formatted error string

* Types of function parameters can be combined

* One more "Unused method receiver detected"

* Unused parameter detected in function
2021-12-07 17:52:39 +00:00

56 lines
1.5 KiB
Go

package imported
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/crypto/bls"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/validator/keymanager"
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 (_ *Keymanager) ExtractKeystores(
_ context.Context, publicKeys []bls.PublicKey, password string,
) ([]*keymanager.Keystore, error) {
lock.Lock()
defer lock.Unlock()
encryptor := keystorev4.New()
keystores := make([]*keymanager.Keystore, len(publicKeys))
for i, pk := range publicKeys {
pubKeyBytes := pk.Marshal()
secretKey, ok := secretKeysCache[bytesutil.ToBytes48(pubKeyBytes)]
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
}
keystores[i] = &keymanager.Keystore{
Crypto: cryptoFields,
ID: id.String(),
Pubkey: fmt.Sprintf("%x", pubKeyBytes),
Version: encryptor.Version(),
Name: encryptor.Name(),
}
}
return keystores, nil
}