prysm-pulse/validator/accounts/v2/testing/mock.go
Raul Jordan 8c258278d0
Direct Keymanager: Implement FetchValidatingPublicKeys() (#6475)
* begin on fetch direct
* fetch validating public keys impl
* test for fetch validating keys
* fetch validating public keys done
* helper function and benchmark
* rename package
* viz
* Update validator/accounts/v2/testing/BUILD.bazel

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
* gaz
* add lock
* comment
* Merge refs/heads/master into fetch-direct-keys
2020-07-03 20:26:00 +00:00

82 lines
1.8 KiB
Go

package mock
import (
"context"
"errors"
"sync"
petname "github.com/dustinkirkland/golang-petname"
)
// Wallet contains an in-memory, simulated wallet implementation.
type Wallet struct {
Files map[string]map[string][]byte
AccountPasswords map[string]string
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 ""
}
// WriteAccountToDisk --
func (m *Wallet) WriteAccountToDisk(ctx context.Context, password string) (string, error) {
m.lock.Lock()
defer m.lock.Unlock()
accountName := petname.Generate(3, "-")
m.AccountPasswords[accountName] = password
return accountName, nil
}
// WriteFileForAccount --
func (m *Wallet) WriteFileForAccount(
ctx context.Context,
accountName string,
fileName string,
data []byte,
) error {
m.lock.Lock()
defer m.lock.Unlock()
if m.Files[accountName] == nil {
m.Files[accountName] = make(map[string][]byte)
}
m.Files[accountName][fileName] = data
return nil
}
// ReadPasswordForAccount --
func (m *Wallet) ReadPasswordForAccount(accountName string) (string, error) {
m.lock.RLock()
defer m.lock.RUnlock()
for name, password := range m.AccountPasswords {
if name == accountName {
return password, nil
}
}
return "", errors.New("account not found")
}
// ReadFileForAccount --
func (m *Wallet) ReadFileForAccount(accountName string, fileName string) ([]byte, error) {
m.lock.RLock()
defer m.lock.RUnlock()
for f, v := range m.Files[accountName] {
if f == fileName {
return v, nil
}
}
return nil, errors.New("file not found")
}