Merge pull request #214 from harmony-one/213

[#213] Export keystore to an output directory
pull/215/head
Daniel Van Der Maden 5 years ago committed by GitHub
commit dd234e8a95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      cmd/subcommands/keys.go
  2. 19
      pkg/account/export.go
  3. 1
      pkg/keys/keys.go

@ -292,16 +292,20 @@ func keysSub() []*cobra.Command {
cmdExportPK.Flags().StringVar(&passphraseFilePath, "passphrase-file", "", "path to a file containing the passphrase")
cmdExportKS := &cobra.Command{
Use: "export-ks <ACCOUNT_ADDRESS>",
Use: "export-ks <ACCOUNT_ADDRESS> <OUTPUT_DIRECTORY>",
Short: "Export the keystore file contents",
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(2),
PreRunE: validateAddress,
RunE: func(cmd *cobra.Command, args []string) error {
passphrase, err := getPassphrase()
if err != nil {
return err
}
return account.ExportKeystore(addr.address, passphrase)
file, e := account.ExportKeystore(addr.address, args[1], passphrase)
if file != "" {
fmt.Println("Exported keystore to", file)
}
return e
},
}
cmdExportKS.Flags().BoolVar(&userProvidesPassphrase, "passphrase", false, ppPrompt)

@ -2,7 +2,8 @@ package account
import (
"fmt"
"path/filepath"
"github.com/harmony-one/go-sdk/pkg/store"
"github.com/harmony-one/harmony/accounts"
)
@ -20,15 +21,23 @@ func ExportPrivateKey(address, passphrase string) error {
return nil
}
func ExportKeystore(address, passphrase string) error {
func ExportKeystore(address, path, passphrase string) (string, error) {
ks := store.FromAddress(address)
allAccounts := ks.Accounts()
dirPath, err := filepath.Abs(path)
if err != nil {
return "", err
}
outFile := filepath.Join(dirPath, fmt.Sprintf("%s.key", address))
for _, account := range allAccounts {
keyFile, err := ks.Export(accounts.Account{Address: account.Address}, passphrase, passphrase)
if err != nil {
return err
return "", err
}
e := writeToFile(outFile, string(keyFile))
if e != nil {
return "", e
}
fmt.Printf("%s\n", keyFile)
}
return nil
return outFile, nil
}

@ -37,7 +37,6 @@ func ListKeys(keystoreDir string) {
for _, account := range allAccounts {
fmt.Printf("%s\t\t %s\n", address.ToBech32(account.Address), account.URL)
}
}
func AddNewKey(password string) {

Loading…
Cancel
Save