-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
114 lines (94 loc) · 3.09 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
package v1
import (
"fmt"
vgfs "github.com/zeta-protocol/zeta/libs/fs"
"github.com/zeta-protocol/zeta/paths"
"github.com/zeta-protocol/zeta/wallet/service"
"github.com/zeta-protocol/zeta/wallet/service/v1"
)
type Store struct {
pubRsaKeyFilePath string
privRsaKeyFilePath string
configFilePath string
}
func InitialiseStore(zetaPaths paths.Paths) (*Store, error) {
pubRsaKeyFilePath, err := zetaPaths.CreateDataPathFor(paths.WalletServicePublicRSAKeyDataFile)
if err != nil {
return nil, fmt.Errorf("couldn't get data path for %s: %w", paths.WalletServicePublicRSAKeyDataFile, err)
}
privRsaKeyFilePath, err := zetaPaths.CreateDataPathFor(paths.WalletServicePrivateRSAKeyDataFile)
if err != nil {
return nil, fmt.Errorf("couldn't get data path for %s: %w", paths.WalletServicePrivateRSAKeyDataFile, err)
}
configFilePath, err := zetaPaths.CreateConfigPathFor(paths.WalletServiceDefaultConfigFile)
if err != nil {
return nil, fmt.Errorf("couldn't get config path for %s: %w", paths.WalletServiceDefaultConfigFile, err)
}
return &Store{
pubRsaKeyFilePath: pubRsaKeyFilePath,
privRsaKeyFilePath: privRsaKeyFilePath,
configFilePath: configFilePath,
}, nil
}
func (s *Store) RSAKeysExists() (bool, error) {
privKeyExists, err := vgfs.FileExists(s.privRsaKeyFilePath)
if err != nil {
return false, err
}
pubKeyExists, err := vgfs.FileExists(s.pubRsaKeyFilePath)
if err != nil {
return false, err
}
return privKeyExists && pubKeyExists, nil
}
func (s *Store) SaveRSAKeys(keys *v1.RSAKeys) error {
if err := vgfs.WriteFile(s.privRsaKeyFilePath, keys.Priv); err != nil {
return fmt.Errorf("unable to save private key: %w", err)
}
if err := vgfs.WriteFile(s.pubRsaKeyFilePath, keys.Pub); err != nil {
return fmt.Errorf("unable to save public key: %w", err)
}
return nil
}
func (s *Store) GetRsaKeys() (*v1.RSAKeys, error) {
pub, err := vgfs.ReadFile(s.pubRsaKeyFilePath)
if err != nil {
return nil, err
}
priv, err := vgfs.ReadFile(s.privRsaKeyFilePath)
if err != nil {
return nil, err
}
return &v1.RSAKeys{
Pub: pub,
Priv: priv,
}, nil
}
func (s *Store) ConfigExists() (bool, error) {
exists, err := vgfs.FileExists(s.configFilePath)
if err != nil {
return false, fmt.Errorf("could not verify the service configuration file existence: %w", err)
}
return exists, nil
}
func (s *Store) GetConfig() (*service.Config, error) {
if exists, err := vgfs.FileExists(s.configFilePath); err != nil {
return nil, fmt.Errorf("could not verify the service configuration file existence: %w", err)
} else if !exists {
return service.DefaultConfig(), nil
}
config := &service.Config{}
if err := paths.ReadStructuredFile(s.configFilePath, config); err != nil {
return nil, fmt.Errorf("could not read the service configuration file: %w", err)
}
return config, nil
}
func (s *Store) SaveConfig(config *service.Config) error {
if err := paths.WriteStructuredFile(s.configFilePath, config); err != nil {
return fmt.Errorf("could not write the service configuration file: %w", err)
}
return nil
}
func (s *Store) GetServiceConfigsPath() string {
return s.configFilePath
}