This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
store.go
130 lines (107 loc) · 3 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package v1
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"code.vegaprotocol.io/vegawallet/wallet"
vgcrypto "code.vegaprotocol.io/shared/libs/crypto"
vgfs "code.vegaprotocol.io/shared/libs/fs"
)
var (
ErrWrongPassphrase = errors.New("wrong passphrase")
)
type Store struct {
walletsHome string
}
func InitialiseStore(walletsHome string) (*Store, error) {
if err := vgfs.EnsureDir(walletsHome); err != nil {
return nil, fmt.Errorf("couldn't ensure directories at %s: %w", walletsHome, err)
}
return &Store{
walletsHome: walletsHome,
}, nil
}
func (s *Store) WalletExists(name string) bool {
walletPath := s.walletPath(name)
exists, _ := vgfs.PathExists(walletPath)
return exists
}
func (s *Store) ListWallets() ([]string, error) {
walletsParentDir, walletsDir := filepath.Split(s.walletsHome)
entries, err := fs.ReadDir(os.DirFS(walletsParentDir), walletsDir)
if err != nil {
return nil, fmt.Errorf("couldn't read directory at %s: %w", s.walletsHome, err)
}
wallets := make([]string, len(entries))
for i, entry := range entries {
wallets[i] = entry.Name()
}
sort.Strings(wallets)
return wallets, nil
}
func (s *Store) GetWallet(name, passphrase string) (wallet.Wallet, error) {
walletPath := s.walletPath(name)
exists, err := vgfs.FileExists(walletPath)
if err != nil {
return nil, fmt.Errorf("couldn't verify file presence at %s: %w", walletPath, err)
}
if !exists {
return nil, fmt.Errorf("no wallet file at %s", walletPath)
}
buf, err := fs.ReadFile(os.DirFS(s.walletsHome), name)
if err != nil {
return nil, fmt.Errorf("couldn't read file at %s: %w", s.walletsHome, err)
}
decBuf, err := vgcrypto.Decrypt(buf, passphrase)
if err != nil {
if err.Error() == "cipher: message authentication failed" {
return nil, ErrWrongPassphrase
}
return nil, err
}
versionedWallet := &struct {
Version uint32 `json:"version"`
}{}
err = json.Unmarshal(decBuf, versionedWallet)
if err != nil {
return nil, fmt.Errorf("couldn't unmarshal wallet verion: %w", err)
}
var w wallet.Wallet
switch versionedWallet.Version {
case 1, 2:
w = &wallet.HDWallet{}
default:
return nil, fmt.Errorf("wallet with version %d isn't supported", versionedWallet.Version)
}
err = json.Unmarshal(decBuf, w)
if err != nil {
return nil, fmt.Errorf("couldn't unmarshal wallet: %w", err)
}
return w, nil
}
func (s *Store) SaveWallet(w wallet.Wallet, passphrase string) error {
buf, err := json.Marshal(w)
if err != nil {
return fmt.Errorf("couldn't marshal wallet: %w", err)
}
encBuf, err := vgcrypto.Encrypt(buf, passphrase)
if err != nil {
return fmt.Errorf("couldn't encrypt wallet: %w", err)
}
walletPath := s.walletPath(w.Name())
err = vgfs.WriteFile(walletPath, encBuf)
if err != nil {
return fmt.Errorf("couldn't write wallet file at %s: %w", walletPath, err)
}
return nil
}
func (s *Store) GetWalletPath(name string) string {
return s.walletPath(name)
}
func (s *Store) walletPath(name string) string {
return filepath.Join(s.walletsHome, name)
}