2020-07-03 20:26:00 +00:00
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
2020-07-21 02:05:23 +00:00
|
|
|
"bytes"
|
2020-07-03 20:26:00 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2020-07-21 02:05:23 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2020-07-28 00:02:29 +00:00
|
|
|
"strings"
|
2020-07-03 20:26:00 +00:00
|
|
|
"sync"
|
2020-09-09 18:29:17 +00:00
|
|
|
|
2020-10-27 20:51:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/iface"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
2020-07-03 20:26:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Wallet contains an in-memory, simulated wallet implementation.
|
|
|
|
type Wallet struct {
|
2020-07-23 02:12:51 +00:00
|
|
|
InnerAccountsDir string
|
|
|
|
Directories []string
|
2020-07-21 02:05:23 +00:00
|
|
|
Files map[string]map[string][]byte
|
|
|
|
EncryptedSeedFile []byte
|
|
|
|
AccountPasswords map[string]string
|
2020-08-31 19:46:45 +00:00
|
|
|
WalletPassword string
|
2020-07-21 02:05:23 +00:00
|
|
|
UnlockAccounts bool
|
|
|
|
lock sync.RWMutex
|
2020-07-03 20:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-07-23 02:12:51 +00:00
|
|
|
return m.InnerAccountsDir
|
2020-07-03 20:26:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 16:46:26 +00:00
|
|
|
// Exists --
|
|
|
|
func (m *Wallet) Exists() (bool, error) {
|
|
|
|
return len(m.Directories) > 0, nil
|
2020-07-03 20:26:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 19:46:45 +00:00
|
|
|
// Password --
|
|
|
|
func (m *Wallet) Password() string {
|
|
|
|
return m.WalletPassword
|
|
|
|
}
|
|
|
|
|
2020-07-21 02:05:23 +00:00
|
|
|
// WriteFileAtPath --
|
2020-10-12 08:11:05 +00:00
|
|
|
func (m *Wallet) WriteFileAtPath(_ context.Context, pathName, fileName string, data []byte) error {
|
2020-07-21 02:05:23 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-21 05:06:11 +00:00
|
|
|
// ReadFileAtPath --
|
2020-10-12 08:11:05 +00:00
|
|
|
func (m *Wallet) ReadFileAtPath(_ context.Context, pathName, fileName string) ([]byte, error) {
|
2020-07-21 05:06:11 +00:00
|
|
|
m.lock.RLock()
|
|
|
|
defer m.lock.RUnlock()
|
|
|
|
for f, v := range m.Files[pathName] {
|
2020-07-28 00:02:29 +00:00
|
|
|
if strings.Contains(fileName, f) {
|
2020-07-21 05:06:11 +00:00
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
2020-08-02 20:02:04 +00:00
|
|
|
return nil, errors.New("no files found")
|
2020-07-21 05:06:11 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 02:05:23 +00:00
|
|
|
// ReadEncryptedSeedFromDisk --
|
2020-10-12 08:11:05 +00:00
|
|
|
func (m *Wallet) ReadEncryptedSeedFromDisk(_ context.Context) (io.ReadCloser, error) {
|
2020-07-21 02:05:23 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
return ioutil.NopCloser(bytes.NewReader(m.EncryptedSeedFile)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteEncryptedSeedToDisk --
|
2020-10-12 08:11:05 +00:00
|
|
|
func (m *Wallet) WriteEncryptedSeedToDisk(_ context.Context, encoded []byte) error {
|
2020-07-21 02:05:23 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
m.EncryptedSeedFile = encoded
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-09 18:29:17 +00:00
|
|
|
|
|
|
|
// InitializeKeymanager --
|
2020-10-27 20:51:29 +00:00
|
|
|
func (m *Wallet) InitializeKeymanager(_ context.Context, _ *iface.InitializeKeymanagerConfig) (keymanager.IKeymanager, error) {
|
2020-09-09 18:29:17 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|