Add utility to export private key

pull/1030/head
Rongjian Lan 6 years ago
parent 9ea931f33d
commit 65bf29ce0f
  1. 15
      accounts/keystore/keystore.go
  2. 43
      cmd/client/wallet/main.go

@ -237,7 +237,7 @@ func (ks *KeyStore) Delete(a accounts.Account, passphrase string) error {
// Decrypting the key isn't really necessary, but we do
// it anyway to check the password and zero out the key
// immediately afterwards.
a, key, err := ks.getDecryptedKey(a, passphrase)
a, key, err := ks.GetDecryptedKey(a, passphrase)
if key != nil {
zeroKey(key.PrivateKey)
}
@ -291,7 +291,7 @@ func (ks *KeyStore) SignTx(a accounts.Account, tx *types.Transaction, chainID *b
// can be decrypted with the given passphrase. The produced signature is in the
// [R || S || V] format where V is 0 or 1.
func (ks *KeyStore) SignHashWithPassphrase(a accounts.Account, passphrase string, hash []byte) (signature []byte, err error) {
_, key, err := ks.getDecryptedKey(a, passphrase)
_, key, err := ks.GetDecryptedKey(a, passphrase)
if err != nil {
return nil, err
}
@ -302,7 +302,7 @@ func (ks *KeyStore) SignHashWithPassphrase(a accounts.Account, passphrase string
// SignTxWithPassphrase signs the transaction if the private key matching the
// given address can be decrypted with the given passphrase.
func (ks *KeyStore) SignTxWithPassphrase(a accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
_, key, err := ks.getDecryptedKey(a, passphrase)
_, key, err := ks.GetDecryptedKey(a, passphrase)
if err != nil {
return nil, err
}
@ -340,7 +340,7 @@ func (ks *KeyStore) Lock(addr common.Address) error {
// shortens the active unlock timeout. If the address was previously unlocked
// indefinitely the timeout is not altered.
func (ks *KeyStore) TimedUnlock(a accounts.Account, passphrase string, timeout time.Duration) error {
a, key, err := ks.getDecryptedKey(a, passphrase)
a, key, err := ks.GetDecryptedKey(a, passphrase)
if err != nil {
return err
}
@ -377,7 +377,8 @@ func (ks *KeyStore) Find(a accounts.Account) (accounts.Account, error) {
return a, err
}
func (ks *KeyStore) getDecryptedKey(a accounts.Account, auth string) (accounts.Account, *Key, error) {
// GetDecryptedKey decrypt and return the key for the account.
func (ks *KeyStore) GetDecryptedKey(a accounts.Account, auth string) (accounts.Account, *Key, error) {
a, err := ks.Find(a)
if err != nil {
return a, nil, err
@ -422,7 +423,7 @@ func (ks *KeyStore) NewAccount(passphrase string) (accounts.Account, error) {
// Export exports as a JSON key, encrypted with newPassphrase.
func (ks *KeyStore) Export(a accounts.Account, passphrase, newPassphrase string) (keyJSON []byte, err error) {
_, key, err := ks.getDecryptedKey(a, passphrase)
_, key, err := ks.GetDecryptedKey(a, passphrase)
if err != nil {
return nil, err
}
@ -468,7 +469,7 @@ func (ks *KeyStore) importKey(key *Key, passphrase string) (accounts.Account, er
// Update changes the passphrase of an existing account.
func (ks *KeyStore) Update(a accounts.Account, passphrase, newPassphrase string) error {
a, key, err := ks.getDecryptedKey(a, passphrase)
a, key, err := ks.GetDecryptedKey(a, passphrase)
if err != nil {
return err
}

@ -2,6 +2,7 @@ package main
import (
"encoding/base64"
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
@ -70,6 +71,10 @@ var (
exportCommand = flag.NewFlagSet("export", flag.ExitOnError)
exportCommandAccountPtr = exportCommand.String("account", "", "The account to be exported")
// ExportPriKey subcommands
exportPriKeyCommand = flag.NewFlagSet("exportPriKey", flag.ExitOnError)
exportPriKeyCommandAccountPtr = exportPriKeyCommand.String("account", "", "The account whose private key to be exported")
// Account subcommands
accountImportCommand = flag.NewFlagSet("import", flag.ExitOnError)
accountImportPtr = accountImportCommand.String("privateKey", "", "Specify the private keyfile to import")
@ -141,9 +146,11 @@ func main() {
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.")
fmt.Println(" 9. blsgen - Generate a bls key and store private key locally.")
fmt.Println(" 9. exportPriKey - Export account private key")
fmt.Println(" --account - Specify the account to export private key.")
fmt.Println(" 10. blsgen - Generate a bls key and store private key locally.")
fmt.Println(" --nopass - The private key has no passphrase (for test only)")
fmt.Println(" 10. format - Shows different encoding formats of specific address")
fmt.Println(" 11. format - Shows different encoding formats of specific address")
fmt.Println(" --address - The address to display the different encoding formats for")
os.Exit(1)
}
@ -188,6 +195,8 @@ ARG:
processListCommand()
case "export":
processExportCommand()
case "exportPriKey":
processExportPriKeyCommand()
case "blsgen":
processBlsgenCommand()
case "removeAll":
@ -319,6 +328,19 @@ func _exportAccount(account accounts.Account) {
}
}
func _exportPriKeyAccount(account accounts.Account) {
fmt.Printf("account: %s\n", common2.MustAddressToBech32(account.Address))
fmt.Printf("URL: %s\n", account.URL)
pass := utils.AskForPassphrase("Original Passphrase: ")
account, key, err := ks.GetDecryptedKey(account, pass)
if err != nil {
fmt.Printf("Failed to decrypt the account: %s \n", err)
} else {
fmt.Printf("Private key: %s \n", hex.EncodeToString(key.PrivateKey.D.Bytes()))
}
}
func processListCommand() {
if err := listCommand.Parse(os.Args[2:]); err != nil {
fmt.Println(ctxerror.New("failed to parse flags").WithCause(err))
@ -341,12 +363,27 @@ func processExportCommand() {
allAccounts := ks.Accounts()
for _, account := range allAccounts {
if acc == "" || acc == common2.MustAddressToBech32(account.Address) {
if acc == "" || common2.ParseAddr(acc) == account.Address {
_exportAccount(account)
}
}
}
func processExportPriKeyCommand() {
if err := exportCommand.Parse(os.Args[2:]); err != nil {
fmt.Println(ctxerror.New("failed to parse flags").WithCause(err))
return
}
acc := *exportCommandAccountPtr
allAccounts := ks.Accounts()
for _, account := range allAccounts {
if acc == "" || common2.ParseAddr(acc) == account.Address {
_exportPriKeyAccount(account)
}
}
}
func processBlsgenCommand() {
newCommand.Parse(os.Args[2:])
noPass := *newCommandNoPassPtr

Loading…
Cancel
Save