prysm-pulse/validator/keymanager/imported/enable_disable.go
Radosław Kapka aa69e5edcc
Allow to create an empty imported wallet (#8251)
* reload keys into empty wallet

# Conflicts:
#	validator/accounts/accounts.go

* removed warning on wallet creation

* export AccountsKeystoreRepresentation type

* rename error message variable

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2021-01-12 16:52:01 +00:00

63 lines
1.9 KiB
Go

package imported
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
)
// DisableAccounts disables public keys from the user's wallet.
func (dr *Keymanager) DisableAccounts(ctx context.Context, pubKeys [][]byte) error {
if pubKeys == nil || len(pubKeys) < 1 {
return errors.New("no public keys specified to disable")
}
lock.Lock()
defer lock.Unlock()
for _, pk := range pubKeys {
if _, ok := dr.disabledPublicKeys[bytesutil.ToBytes48(pk)]; !ok {
dr.disabledPublicKeys[bytesutil.ToBytes48(pk)] = true
}
}
return dr.rewriteDisabledKeysToDisk(ctx)
}
// EnableAccounts enables public keys from a user's wallet if they are disabled.
func (dr *Keymanager) EnableAccounts(ctx context.Context, pubKeys [][]byte) error {
if pubKeys == nil || len(pubKeys) < 1 {
return errors.New("no public keys specified to enable")
}
lock.Lock()
defer lock.Unlock()
for _, pk := range pubKeys {
delete(dr.disabledPublicKeys, bytesutil.ToBytes48(pk))
}
return dr.rewriteDisabledKeysToDisk(ctx)
}
func (dr *Keymanager) rewriteDisabledKeysToDisk(ctx context.Context) error {
encoded, err := dr.wallet.ReadFileAtPath(ctx, AccountsPath, AccountsKeystoreFileName)
if err != nil {
return errors.Wrap(err, "could not read keystore file for accounts")
}
keystore := &AccountsKeystoreRepresentation{}
if err := json.Unmarshal(encoded, keystore); err != nil {
return err
}
disabledKeysStrings := make([]string, 0)
for pk := range dr.disabledPublicKeys {
disabledKeysStrings = append(disabledKeysStrings, fmt.Sprintf("%x", pk))
}
keystore.DisabledPublicKeys = disabledKeysStrings
encoded, err = json.MarshalIndent(keystore, "", "\t")
if err != nil {
return err
}
if err := dr.wallet.WriteFileAtPath(ctx, AccountsPath, AccountsKeystoreFileName, encoded); err != nil {
return errors.Wrap(err, "could not write keystore file for accounts")
}
return nil
}