prysm-pulse/validator/accounts/v2/testing/mock.go
Raul Jordan 60558b7970
Refactor Accounts and Keymanager Methods to Not Rely on CLI (#7135)
* keymanagers no longer use cli ctx
* rename important values to keymanageropts
* further refactor accounts methods to reduce cli dependency
* separating cli vs non cli methods for various accounts functions
* recover wallet cli vs non-cli mode
* ensure half of tests build
* accounts v2 package now builds
* full revamp wallet creation or opening wallets
* everything builds
* tests pass finally
* Merge branch 'master' into no-cli-keymanagers
* ensure commands work as expected
* Merge branch 'no-cli-keymanagers' of github.com:prysmaticlabs/prysm into no-cli-keymanagers
* further fix build
* account creation comments
* fix imports and comments
* fix up failing test
* fix build
* Update derived.go
* Update direct.go
* Update remote.go
* Fixed variable
* fix up red tests
* use Cli instead of CLI, fix tests, and address Radek's feedback
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
* Merge refs/heads/master into no-cli-keymanagers
2020-08-31 19:46:45 +00:00

88 lines
1.9 KiB
Go

package mock
import (
"bytes"
"context"
"errors"
"io"
"io/ioutil"
"strings"
"sync"
)
// Wallet contains an in-memory, simulated wallet implementation.
type Wallet struct {
InnerAccountsDir string
Directories []string
Files map[string]map[string][]byte
EncryptedSeedFile []byte
AccountPasswords map[string]string
WalletPassword string
UnlockAccounts bool
lock sync.RWMutex
}
// AccountNames --
func (m *Wallet) AccountNames() ([]string, error) {
m.lock.RLock()
defer m.lock.RUnlock()
names := make([]string, 0)
for name := range m.AccountPasswords {
names = append(names, name)
}
return names, nil
}
// AccountsDir --
func (m *Wallet) AccountsDir() string {
return m.InnerAccountsDir
}
// Exists --
func (m *Wallet) Exists() (bool, error) {
return len(m.Directories) > 0, nil
}
// Password --
func (m *Wallet) Password() string {
return m.WalletPassword
}
// WriteFileAtPath --
func (m *Wallet) WriteFileAtPath(ctx context.Context, pathName string, fileName string, data []byte) error {
m.lock.Lock()
defer m.lock.Unlock()
if m.Files[pathName] == nil {
m.Files[pathName] = make(map[string][]byte)
}
m.Files[pathName][fileName] = data
return nil
}
// ReadFileAtPath --
func (m *Wallet) ReadFileAtPath(ctx context.Context, pathName string, fileName string) ([]byte, error) {
m.lock.RLock()
defer m.lock.RUnlock()
for f, v := range m.Files[pathName] {
if strings.Contains(fileName, f) {
return v, nil
}
}
return nil, errors.New("no files found")
}
// ReadEncryptedSeedFromDisk --
func (m *Wallet) ReadEncryptedSeedFromDisk(ctx context.Context) (io.ReadCloser, error) {
m.lock.Lock()
defer m.lock.Unlock()
return ioutil.NopCloser(bytes.NewReader(m.EncryptedSeedFile)), nil
}
// WriteEncryptedSeedToDisk --
func (m *Wallet) WriteEncryptedSeedToDisk(ctx context.Context, encoded []byte) error {
m.lock.Lock()
defer m.lock.Unlock()
m.EncryptedSeedFile = encoded
return nil
}