code.vegaprotocol.io/vega@v0.79.0/wallet/service/handler.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 service 17 18 import ( 19 "fmt" 20 21 v1 "code.vegaprotocol.io/vega/wallet/service/v1" 22 ) 23 24 //go:generate go run github.com/golang/mock/mockgen -destination mocks/store_mock.go -package mocks code.vegaprotocol.io/vega/wallet/service Store 25 type Store interface { 26 GetRsaKeys() (*v1.RSAKeys, error) 27 RSAKeysExists() (bool, error) 28 SaveRSAKeys(*v1.RSAKeys) error 29 ConfigExists() (bool, error) 30 SaveConfig(*Config) error 31 GetConfig() (*Config, error) 32 } 33 34 func InitialiseService(store Store, overwrite bool) error { 35 rsaKeysExists, err := store.RSAKeysExists() 36 if err != nil { 37 return fmt.Errorf("could not verify the RSA keys existence: %w", err) 38 } 39 if !rsaKeysExists || overwrite { 40 keys, err := v1.GenerateRSAKeys() 41 if err != nil { 42 return fmt.Errorf("could not generate the RSA keys: %w", err) 43 } 44 45 if err := store.SaveRSAKeys(keys); err != nil { 46 return fmt.Errorf("could not save the RSA keys: %w", err) 47 } 48 } 49 50 configExists, err := store.ConfigExists() 51 if err != nil { 52 return fmt.Errorf("could not verify the service configuration existence: %w", err) 53 } 54 if !configExists || overwrite { 55 if err := store.SaveConfig(DefaultConfig()); err != nil { 56 return fmt.Errorf("could not save the default service configuration: %w", err) 57 } 58 } 59 60 return nil 61 } 62 63 func IsInitialised(store Store) (bool, error) { 64 rsaExists, err := store.RSAKeysExists() 65 if err != nil { 66 return false, fmt.Errorf("could not verify the RSA keys existence: %w", err) 67 } 68 69 configExist, err := store.ConfigExists() 70 if err != nil { 71 return false, fmt.Errorf("could not verify the service configuration existence: %w", err) 72 } 73 74 return rsaExists && configExist, nil 75 } 76 77 func UpdateConfig(store Store, cfg *Config) error { 78 if err := cfg.Validate(); err != nil { 79 return fmt.Errorf("the service configuration is invalid: %w", err) 80 } 81 82 if err := store.SaveConfig(cfg); err != nil { 83 return fmt.Errorf("could not save the service configuration: %w", err) 84 } 85 86 return nil 87 }