Merge pull request #1030 from rlan35/rj_fork

Add address format and exportPriKey utility in wallet
pull/1035/head
Rongjian Lan 6 years ago committed by GitHub
commit 74b7b27789
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      accounts/keystore/keystore.go
  2. 64
      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")
@ -89,6 +94,9 @@ var (
balanceCommand = flag.NewFlagSet("balances", flag.ExitOnError)
balanceAddressPtr = balanceCommand.String("address", "", "Specify the account address to check balance for")
formatCommand = flag.NewFlagSet("format", flag.ExitOnError)
formatAddressPtr = formatCommand.String("address", "", "Specify the account address to display different encoding formats")
)
var (
@ -138,8 +146,12 @@ 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(" 11. format - Shows different encoding formats of specific address")
fmt.Println(" --address - The address to display the different encoding formats for")
os.Exit(1)
}
@ -183,6 +195,8 @@ ARG:
processListCommand()
case "export":
processExportCommand()
case "exportPriKey":
processExportPriKeyCommand()
case "blsgen":
processBlsgenCommand()
case "removeAll":
@ -198,6 +212,8 @@ ARG:
case "transfer":
readProfile(profile)
processTransferCommand()
case "format":
formatAddressCommand()
default:
fmt.Printf("Unknown action: %s\n", os.Args[1])
flag.PrintDefaults()
@ -312,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))
@ -334,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
@ -421,6 +465,22 @@ func processBalancesCommand() {
}
}
func formatAddressCommand() {
if err := formatCommand.Parse(os.Args[2:]); err != nil {
fmt.Println(ctxerror.New("failed to parse flags").WithCause(err))
return
}
if *formatAddressPtr == "" {
fmt.Println("Please specify the --address to show formats for.")
} else {
address := common2.ParseAddr(*formatAddressPtr)
fmt.Printf("account address in Bech32: %s\n", common2.MustAddressToBech32(address))
fmt.Printf("account address in Base16 (deprecated): %s\n", address.Hex())
}
}
func processGetFreeToken() {
if err := freeTokenCommand.Parse(os.Args[2:]); err != nil {
fmt.Println(ctxerror.New("Failed to parse flags").WithCause(err))

Loading…
Cancel
Save