Skip to content

Commit

Permalink
214 separate created wallets from thyra binary directory (#217)
Browse files Browse the repository at this point in the history
adds $HOME/.conf/thyra as preferred directory
  • Loading branch information
thomas-senechal authored Oct 28, 2022
1 parent b888e35 commit 44873af
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion int/api/wallet/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c *walletCreate) Handle(params operations.MgmtWalletCreateParams) middlewa
})
}

err = os.WriteFile("wallet_"+*params.Body.Nickname+".json", bytesOutput, fileModeUserRW)
err = os.WriteFile(wallet.GetWalletDirectory()+"wallet_"+*params.Body.Nickname+".json", bytesOutput, fileModeUserRW)
if err != nil {
return operations.NewMgmtWalletCreateInternalServerError().WithPayload(
&models.Error{
Expand Down
2 changes: 1 addition & 1 deletion int/api/wallet/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (c *wImport) Handle(params operations.MgmtWalletImportParams) middleware.Re
})
}

err = os.WriteFile("wallet_"+*params.Body.Nickname+".json", bytesOutput, fileModeUserRW)
err = os.WriteFile(wallet.GetWalletDirectory()+"wallet_"+*params.Body.Nickname+".json", bytesOutput, fileModeUserRW)
if err != nil {
return operations.NewMgmtWalletCreateInternalServerError().WithPayload(
&models.Error{
Expand Down
29 changes: 18 additions & 11 deletions pkg/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
SecretKeyLength = 32
PBKDF2NbRound = 10000
FileModeUserReadWriteOnly = 0o600
WalletDirectoryPermission = 0o755
MinAddressLength = 49
)

Expand Down Expand Up @@ -115,24 +116,30 @@ func FromYAML(raw []byte) (w Wallet, err error) {
return
}

func GetWalletDirectory() string {
walletDirectory := os.Getenv("HOME") + "/.config/thyra/"
if _, err := os.Stat(walletDirectory); os.IsNotExist(err) {
if os.MkdirAll(walletDirectory, WalletDirectoryPermission) != nil {
walletDirectory = ""
}
}

return walletDirectory
}

func LoadAll() (wallets []Wallet, e error) {
wallets = []Wallet{}

workingDir, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("returning working directory: %w", err)
}

files, err := os.ReadDir(workingDir)
files, err := os.ReadDir(GetWalletDirectory())
if err != nil {
return nil, fmt.Errorf("reading working directory '%s': %w", workingDir, err)
return nil, fmt.Errorf("reading wallet directory '%s': %w", GetWalletDirectory(), err)
}

for _, f := range files {
fileName := f.Name()

if strings.HasPrefix(fileName, "wallet_") && strings.HasSuffix(fileName, ".json") {
bytesInput, err := os.ReadFile(fileName)
bytesInput, err := os.ReadFile(GetWalletDirectory() + fileName)
if err != nil {
return nil, fmt.Errorf("reading file '%s': %w", fileName, err)
}
Expand All @@ -152,7 +159,7 @@ func LoadAll() (wallets []Wallet, e error) {
}

func Load(nickname string) (*Wallet, error) {
bytesInput, err := os.ReadFile("wallet_" + nickname + ".json")
bytesInput, err := os.ReadFile(GetWalletDirectory() + "wallet_" + nickname + ".json")
if err != nil {
return nil, fmt.Errorf("reading file 'wallet_%s.json': %w", nickname, err)
}
Expand Down Expand Up @@ -206,7 +213,7 @@ func New(nickname string) (*Wallet, error) {
return nil, fmt.Errorf("marshalling wallet: %w", err)
}

err = os.WriteFile("wallet_"+nickname+".json", bytesOutput, FileModeUserReadWriteOnly)
err = os.WriteFile(GetWalletDirectory()+"wallet_"+nickname+".json", bytesOutput, FileModeUserReadWriteOnly)
if err != nil {
return nil, fmt.Errorf("writing wallet to 'wallet_%s.json': %w", nickname, err)
}
Expand All @@ -215,7 +222,7 @@ func New(nickname string) (*Wallet, error) {
}

func Delete(nickname string) (err error) {
err = os.Remove("wallet_" + nickname + ".json")
err = os.Remove(GetWalletDirectory() + "wallet_" + nickname + ".json")
if err != nil {
return fmt.Errorf("deleting wallet 'wallet_%s.json': %w", nickname, err)
}
Expand Down

0 comments on commit 44873af

Please sign in to comment.