github.com/prysmaticlabs/prysm@v1.4.4/validator/accounts/testing/mock.go (about)

     1  package mock
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"strings"
     7  	"sync"
     8  
     9  	"github.com/prysmaticlabs/prysm/validator/accounts/iface"
    10  	"github.com/prysmaticlabs/prysm/validator/keymanager"
    11  )
    12  
    13  // Wallet contains an in-memory, simulated wallet implementation.
    14  type Wallet struct {
    15  	InnerAccountsDir  string
    16  	Directories       []string
    17  	Files             map[string]map[string][]byte
    18  	EncryptedSeedFile []byte
    19  	AccountPasswords  map[string]string
    20  	WalletPassword    string
    21  	UnlockAccounts    bool
    22  	lock              sync.RWMutex
    23  }
    24  
    25  // AccountNames --
    26  func (w *Wallet) AccountNames() ([]string, error) {
    27  	w.lock.RLock()
    28  	defer w.lock.RUnlock()
    29  	names := make([]string, 0)
    30  	for name := range w.AccountPasswords {
    31  		names = append(names, name)
    32  	}
    33  	return names, nil
    34  }
    35  
    36  // AccountsDir --
    37  func (w *Wallet) AccountsDir() string {
    38  	return w.InnerAccountsDir
    39  }
    40  
    41  // Exists --
    42  func (w *Wallet) Exists() (bool, error) {
    43  	return len(w.Directories) > 0, nil
    44  }
    45  
    46  // Password --
    47  func (w *Wallet) Password() string {
    48  	return w.WalletPassword
    49  }
    50  
    51  // WriteFileAtPath --
    52  func (w *Wallet) WriteFileAtPath(_ context.Context, pathName, fileName string, data []byte) error {
    53  	w.lock.Lock()
    54  	defer w.lock.Unlock()
    55  	if w.Files[pathName] == nil {
    56  		w.Files[pathName] = make(map[string][]byte)
    57  	}
    58  	w.Files[pathName][fileName] = data
    59  	return nil
    60  }
    61  
    62  // ReadFileAtPath --
    63  func (w *Wallet) ReadFileAtPath(_ context.Context, pathName, fileName string) ([]byte, error) {
    64  	w.lock.RLock()
    65  	defer w.lock.RUnlock()
    66  	for f, v := range w.Files[pathName] {
    67  		if strings.Contains(fileName, f) {
    68  			return v, nil
    69  		}
    70  	}
    71  	return nil, errors.New("no files found")
    72  }
    73  
    74  // InitializeKeymanager --
    75  func (w *Wallet) InitializeKeymanager(_ context.Context, _ iface.InitKeymanagerConfig) (keymanager.IKeymanager, error) {
    76  	return nil, nil
    77  }