code.vegaprotocol.io/vega@v0.79.0/wallet/service/store/v1/store.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 v1 17 18 import ( 19 "fmt" 20 21 vgfs "code.vegaprotocol.io/vega/libs/fs" 22 "code.vegaprotocol.io/vega/paths" 23 "code.vegaprotocol.io/vega/wallet/service" 24 "code.vegaprotocol.io/vega/wallet/service/v1" 25 ) 26 27 type Store struct { 28 pubRsaKeyFilePath string 29 privRsaKeyFilePath string 30 configFilePath string 31 } 32 33 func InitialiseStore(vegaPaths paths.Paths) (*Store, error) { 34 pubRsaKeyFilePath, err := vegaPaths.CreateDataPathFor(paths.WalletServicePublicRSAKeyDataFile) 35 if err != nil { 36 return nil, fmt.Errorf("couldn't get data path for %s: %w", paths.WalletServicePublicRSAKeyDataFile, err) 37 } 38 39 privRsaKeyFilePath, err := vegaPaths.CreateDataPathFor(paths.WalletServicePrivateRSAKeyDataFile) 40 if err != nil { 41 return nil, fmt.Errorf("couldn't get data path for %s: %w", paths.WalletServicePrivateRSAKeyDataFile, err) 42 } 43 44 configFilePath, err := vegaPaths.CreateConfigPathFor(paths.WalletServiceDefaultConfigFile) 45 if err != nil { 46 return nil, fmt.Errorf("couldn't get config path for %s: %w", paths.WalletServiceDefaultConfigFile, err) 47 } 48 49 return &Store{ 50 pubRsaKeyFilePath: pubRsaKeyFilePath, 51 privRsaKeyFilePath: privRsaKeyFilePath, 52 configFilePath: configFilePath, 53 }, nil 54 } 55 56 func (s *Store) RSAKeysExists() (bool, error) { 57 privKeyExists, err := vgfs.FileExists(s.privRsaKeyFilePath) 58 if err != nil { 59 return false, err 60 } 61 pubKeyExists, err := vgfs.FileExists(s.pubRsaKeyFilePath) 62 if err != nil { 63 return false, err 64 } 65 return privKeyExists && pubKeyExists, nil 66 } 67 68 func (s *Store) SaveRSAKeys(keys *v1.RSAKeys) error { 69 if err := vgfs.WriteFile(s.privRsaKeyFilePath, keys.Priv); err != nil { 70 return fmt.Errorf("unable to save private key: %w", err) 71 } 72 73 if err := vgfs.WriteFile(s.pubRsaKeyFilePath, keys.Pub); err != nil { 74 return fmt.Errorf("unable to save public key: %w", err) 75 } 76 77 return nil 78 } 79 80 func (s *Store) GetRsaKeys() (*v1.RSAKeys, error) { 81 pub, err := vgfs.ReadFile(s.pubRsaKeyFilePath) 82 if err != nil { 83 return nil, err 84 } 85 86 priv, err := vgfs.ReadFile(s.privRsaKeyFilePath) 87 if err != nil { 88 return nil, err 89 } 90 91 return &v1.RSAKeys{ 92 Pub: pub, 93 Priv: priv, 94 }, nil 95 } 96 97 func (s *Store) ConfigExists() (bool, error) { 98 exists, err := vgfs.FileExists(s.configFilePath) 99 if err != nil { 100 return false, fmt.Errorf("could not verify the service configuration file existence: %w", err) 101 } 102 103 return exists, nil 104 } 105 106 func (s *Store) GetConfig() (*service.Config, error) { 107 config := service.DefaultConfig() 108 109 if exists, err := vgfs.FileExists(s.configFilePath); err != nil { 110 return nil, fmt.Errorf("could not verify the service configuration file existence: %w", err) 111 } else if !exists { 112 return config, nil 113 } 114 115 if err := paths.ReadStructuredFile(s.configFilePath, config); err != nil { 116 return nil, fmt.Errorf("could not read the service configuration file: %w", err) 117 } 118 return config, nil 119 } 120 121 func (s *Store) SaveConfig(config *service.Config) error { 122 if err := paths.WriteStructuredFile(s.configFilePath, config); err != nil { 123 return fmt.Errorf("could not write the service configuration file: %w", err) 124 } 125 return nil 126 } 127 128 func (s *Store) GetServiceConfigsPath() string { 129 return s.configFilePath 130 }