Skip to content

Commit

Permalink
feat: add save with pubkeyhex as option
Browse files Browse the repository at this point in the history
  • Loading branch information
shrimalmadhur committed Sep 24, 2024
1 parent 2616785 commit c019225
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,24 @@ func (ks *Keystore) ToJSON() (string, error) {
return string(data), nil
}

// Save writes the Keystore to a file in JSON format
func (ks *Keystore) Save(fileFolder string) error {
// SaveWithUUID writes the Keystore to a file in JSON format filename as uuid.json
func (ks *Keystore) SaveWithUUID(fileFolder string) error {
cleanedFileFolder := filepath.Clean(fileFolder)
filePath := filepath.Join(cleanedFileFolder, ks.UUID+".json")

cleanedFilePath := filepath.Clean(fileFolder)
return saveFile(ks, filePath)
}

// SaveWithPubKeyHex writes the Keystore to a file in JSON format and filename as pub_key_hex.json
func (ks *Keystore) SaveWithPubKeyHex(fileFolder string) error {
cleanedFileFolder := filepath.Clean(fileFolder)
filePath := filepath.Join(cleanedFileFolder, ks.PubKey+".json")

return saveFile(ks, filePath)
}

func saveFile(ks *Keystore, path string) error {
cleanedFilePath := filepath.Clean(path)
file, err := os.Create(cleanedFilePath)
if err != nil {
return err
Expand All @@ -351,7 +363,7 @@ func (ks *Keystore) Save(fileFolder string) error {

// Set file permissions to read-only for owner and group if on Unix systems
if os.Getenv("GOOS") == "linux" || os.Getenv("GOOS") == "darwin" {
return os.Chmod(filePath, 0440)
return os.Chmod(cleanedFilePath, 0440)
}
return nil
}
Expand Down Expand Up @@ -478,7 +490,7 @@ func (k *KeyPair) Save(path string) error {
}

// Save the keystore
err = ks.Save(path)
err = ks.SaveWithPubKeyHex(path)
if err != nil {
return err
}
Expand All @@ -495,7 +507,7 @@ func ImportFromPrivateKey(pk []byte, path string, password string) error {
}

// Save the keystore
err = ks.Save(path)
err = ks.SaveWithPubKeyHex(path)
if err != nil {
return err
}
Expand All @@ -518,7 +530,7 @@ func ImportFromMnemonic(pkMnemonic string, path string, password string) error {
}

// Save the keystore
err = ks.Save(path)
err = ks.SaveWithPubKeyHex(path)
if err != nil {
return err
}
Expand Down

0 comments on commit c019225

Please sign in to comment.