-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from initia-labs/feat/keyfile
feat: revamp mnemonic and key file logic
- Loading branch information
Showing
17 changed files
with
261 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type KeyFile map[string]string | ||
|
||
func NewKeyFile() *KeyFile { | ||
kf := make(KeyFile) | ||
return &kf | ||
} | ||
|
||
func (k *KeyFile) AddMnemonic(name, mnemonic string) { | ||
(*k)[name] = mnemonic | ||
} | ||
|
||
func (k *KeyFile) GetMnemonic(name string) string { | ||
return (*k)[name] | ||
} | ||
|
||
func (k *KeyFile) Write(filePath string) error { | ||
data, err := json.MarshalIndent(k, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("error marshaling KeyFile to JSON: %w", err) | ||
} | ||
|
||
return os.WriteFile(filePath, data, 0644) | ||
} | ||
|
||
// Load tries to load an existing key file into the struct if the file exists | ||
func (k *KeyFile) Load(filePath string) error { | ||
if _, err := os.Stat(filePath); os.IsNotExist(err) { | ||
return nil | ||
} | ||
|
||
data, err := os.ReadFile(filePath) | ||
if err != nil { | ||
return fmt.Errorf("error reading file: %w", err) | ||
} | ||
|
||
err = json.Unmarshal(data, k) | ||
if err != nil { | ||
return fmt.Errorf("error unmarshaling JSON: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.