[wallet] fix bug of empty passphrase

add export command to export the key

Signed-off-by: Leo Chen <leo@harmony.one>
pull/887/head
Leo Chen 6 years ago
parent 109922c9b2
commit 6b5d01d38e
  1. 65
      cmd/client/wallet/main.go

@ -63,7 +63,10 @@ var (
// List subcommands // List subcommands
listCommand = flag.NewFlagSet("list", flag.ExitOnError) listCommand = flag.NewFlagSet("list", flag.ExitOnError)
listCommandNoPassPtr = listCommand.Bool("nopass", false, "The account has no pass phrase")
// Export subcommands
exportCommand = flag.NewFlagSet("export", flag.ExitOnError)
exportCommandAccountPtr = exportCommand.String("account", "", "The account to be exported")
// Account subcommands // Account subcommands
accountImportCommand = flag.NewFlagSet("import", flag.ExitOnError) accountImportCommand = flag.NewFlagSet("import", flag.ExitOnError)
@ -116,7 +119,6 @@ func main() {
fmt.Println(" 1. new - Generates a new account and store the private key locally") fmt.Println(" 1. new - Generates a new account and store the private key locally")
fmt.Println(" --nopass - The private key has no passphrase (for test only)") fmt.Println(" --nopass - The private key has no passphrase (for test only)")
fmt.Println(" 2. list - Lists all accounts in local keystore") fmt.Println(" 2. list - Lists all accounts in local keystore")
fmt.Println(" --nopass - The private key has no passphrase (for test only)")
fmt.Println(" 3. removeAll - Removes all accounts in local keystore") fmt.Println(" 3. removeAll - Removes all accounts in local keystore")
fmt.Println(" 4. import - Imports a new account by private key") fmt.Println(" 4. import - Imports a new account by private key")
fmt.Println(" --pass - The passphrase of the private key to import") fmt.Println(" --pass - The passphrase of the private key to import")
@ -132,6 +134,8 @@ func main() {
fmt.Println(" --shardID - The shard Id for the transfer") fmt.Println(" --shardID - The shard Id for the transfer")
fmt.Println(" --inputData - Base64-encoded input data to embed in the transaction") fmt.Println(" --inputData - Base64-encoded input data to embed in the transaction")
fmt.Println(" --pass - Passphrase of sender's private key") fmt.Println(" --pass - Passphrase of sender's private key")
fmt.Println(" 8. export - Export account key to a new file")
fmt.Println(" --account - Specify the account to export. Empty will export every key.")
os.Exit(1) os.Exit(1)
} }
@ -173,6 +177,8 @@ ARG:
processNewCommnad() processNewCommnad()
case "list": case "list":
processListCommand() processListCommand()
case "export":
processExportCommand()
case "removeAll": case "removeAll":
clearKeystore() clearKeystore()
case "import": case "import":
@ -248,7 +254,7 @@ func processNewCommnad() {
password := "" password := ""
if !noPass { if !noPass {
password := utils.AskForPassphrase("Passphrase: ") password = utils.AskForPassphrase("Passphrase: ")
password2 := utils.AskForPassphrase("Passphrase again: ") password2 := utils.AskForPassphrase("Passphrase again: ")
if password != password2 { if password != password2 {
fmt.Printf("Passphrase doesn't match. Please try again!\n") fmt.Printf("Passphrase doesn't match. Please try again!\n")
@ -264,23 +270,16 @@ func processNewCommnad() {
fmt.Printf("URL: %s\n", account.URL) fmt.Printf("URL: %s\n", account.URL)
} }
func processListCommand() { func _exportAccount(account accounts.Account) {
listCommand.Parse(os.Args[2:])
noPass := *listCommandNoPassPtr
allAccounts := ks.Accounts()
for _, account := range allAccounts {
fmt.Printf("account: %s\n", account.Address.Hex()) fmt.Printf("account: %s\n", account.Address.Hex())
fmt.Printf("URL: %s\n", account.URL) fmt.Printf("URL: %s\n", account.URL)
password := "" pass := utils.AskForPassphrase("Original Passphrase: ")
newpass := "" newpass := utils.AskForPassphrase("Export Passphrase: ")
if !noPass {
password = utils.AskForPassphrase("Passphrase: ") data, err := ks.Export(account, pass, newpass)
newpass = utils.AskForPassphrase("Export Passphrase: ")
}
data, err := ks.Export(account, password, newpass)
if err == nil { if err == nil {
f, err := os.OpenFile(fmt.Sprintf(".hmy/%s.key", account.Address.Hex()), os.O_CREATE|os.O_WRONLY, 0644) filename := fmt.Sprintf(".hmy/%s.key", account.Address.Hex())
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
panic("Failed to open keystore") panic("Failed to open keystore")
} }
@ -289,7 +288,8 @@ func processListCommand() {
panic("Failed to write to keystore") panic("Failed to write to keystore")
} }
f.Close() f.Close()
continue fmt.Printf("Exported keyfile to: %v\n", filename)
return
} }
switch err { switch err {
case accounts.ErrInvalidPassphrase: case accounts.ErrInvalidPassphrase:
@ -297,6 +297,27 @@ func processListCommand() {
default: default:
fmt.Printf("export error: %v\n", err) fmt.Printf("export error: %v\n", err)
} }
}
func processListCommand() {
listCommand.Parse(os.Args[2:])
allAccounts := ks.Accounts()
for _, account := range allAccounts {
fmt.Printf("account: %s\n", account.Address.Hex())
fmt.Printf("URL: %s\n", account.URL)
}
}
func processExportCommand() {
exportCommand.Parse(os.Args[2:])
acc := *exportCommandAccountPtr
allAccounts := ks.Accounts()
for _, account := range allAccounts {
if acc == "" || acc == account.Address.Hex() {
_exportAccount(account)
}
} }
} }
@ -432,7 +453,13 @@ func processTransferCommand() {
return return
} }
ks.Unlock(account, senderPass) err = ks.Unlock(account, senderPass)
if err != nil {
fmt.Printf("Unlock account failed! %v\n", err)
return
}
fmt.Printf("Unlock account succeeded! '%v'\n", senderPass)
tx, err = ks.SignTx(account, tx, nil) tx, err = ks.SignTx(account, tx, nil)
if err != nil { if err != nil {

Loading…
Cancel
Save