code.vegaprotocol.io/vega@v0.79.0/wallet/wallets/mocked_store_for_test.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package wallets_test 17 18 import ( 19 "context" 20 "errors" 21 "fmt" 22 23 "code.vegaprotocol.io/vega/wallet/wallet" 24 "code.vegaprotocol.io/vega/wallet/wallets" 25 ) 26 27 var errWrongPassphrase = errors.New("wrong passphrase") 28 29 type mockedStore struct { 30 passphrase string 31 wallets map[string]wallet.Wallet 32 } 33 34 func newMockedStore() *mockedStore { 35 return &mockedStore{ 36 passphrase: "", 37 wallets: map[string]wallet.Wallet{}, 38 } 39 } 40 41 func (m *mockedStore) UnlockWallet(_ context.Context, name, passphrase string) error { 42 _, ok := m.wallets[name] 43 if !ok { 44 return wallets.ErrWalletDoesNotExists 45 } 46 if passphrase != m.passphrase { 47 return errWrongPassphrase 48 } 49 return nil 50 } 51 52 func (m *mockedStore) WalletExists(_ context.Context, name string) (bool, error) { 53 _, ok := m.wallets[name] 54 return ok, nil 55 } 56 57 func (m *mockedStore) ListWallets(_ context.Context) ([]string, error) { 58 ws := make([]string, 0, len(m.wallets)) 59 for k := range m.wallets { 60 ws = append(ws, k) 61 } 62 return ws, nil 63 } 64 65 func (m *mockedStore) CreateWallet(_ context.Context, w wallet.Wallet, passphrase string) error { 66 m.passphrase = passphrase 67 m.wallets[w.Name()] = w 68 return nil 69 } 70 71 func (m *mockedStore) UpdateWallet(_ context.Context, w wallet.Wallet) error { 72 m.wallets[w.Name()] = w 73 return nil 74 } 75 76 func (m *mockedStore) GetWallet(_ context.Context, name string) (wallet.Wallet, error) { 77 w, ok := m.wallets[name] 78 if !ok { 79 return nil, wallets.ErrWalletDoesNotExists 80 } 81 return w, nil 82 } 83 84 func (m *mockedStore) GetWalletPath(name string) string { 85 return fmt.Sprintf("some/path/%v", name) 86 } 87 88 func (m *mockedStore) GetKey(name, pubKey string) wallet.PublicKey { 89 w, ok := m.wallets[name] 90 if !ok { 91 panic(fmt.Sprintf("wallet \"%v\" not found", name)) 92 } 93 for _, key := range w.ListPublicKeys() { 94 if key.Key() == pubKey { 95 return key 96 } 97 } 98 panic(fmt.Sprintf("key \"%v\" not found", pubKey)) 99 }