prysm-pulse/validator/keymanager/v2/direct/backup.go
Raul Jordan 86b2bd5edd
Implement Accounts-V2 Backup for Direct Keymanager (#6936)
* progress on export feature
* Merge branch 'master' into impl-export
* rename func
* imports
* confs
* export single works
* impl export
* Merge branch 'master' into impl-export
* cleanup exports code
* rename to backup
* rename to backup
* Merge branch 'master' into impl-export
* full test for backup
* Merge branch 'impl-export' of github.com:prysmaticlabs/prysm into impl-export
* gazelle fix
* terence feedback
* merge confs
* Merge branch 'master' into impl-export
* refactor into single select accounts function
* gaz
* Merge refs/heads/master into impl-export
* victor's feedback
* validate yes or no
* confs
* close the zip file in defer
* Merge refs/heads/master into impl-export
* Merge refs/heads/master into impl-export
2020-08-11 23:15:06 +00:00

54 lines
1.5 KiB
Go

package direct
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
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(
ctx context.Context, publicKeys []bls.PublicKey, password string,
) ([]*v2keymanager.Keystore, error) {
encryptor := keystorev4.New()
keystores := make([]*v2keymanager.Keystore, len(publicKeys))
for i, pk := range publicKeys {
pubKeyBytes := pk.Marshal()
secretKey, ok := dr.keysCache[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] = &v2keymanager.Keystore{
Crypto: cryptoFields,
ID: id.String(),
Pubkey: fmt.Sprintf("%x", pubKeyBytes),
Version: encryptor.Version(),
Name: encryptor.Name(),
}
}
return keystores, nil
}