-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get initial framework for exporting raw keys working
- Loading branch information
1 parent
1c53a19
commit 031185b
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright © 2023 Glif LTD | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/AlecAivazis/survey/v2" | ||
"github.com/ethereum/go-ethereum/accounts" | ||
"github.com/ethereum/go-ethereum/accounts/keystore" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/glifio/cli/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// exportAccountCmd represents the export-account command | ||
var exportAccountCmd = &cobra.Command{ | ||
Use: "export-raw-key [account-name or account-address]", | ||
Short: "(Dangerous) Export a single private key account", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
reallyDo, err := cmd.Flags().GetBool("really-do-it") | ||
if err != nil { | ||
logFatal(err) | ||
} | ||
|
||
if !reallyDo { | ||
logFatal("DANGEROUS COMMAND - are you really trying to export a raw private key from your wallet? If so, you must pass --really-do-it to complete the export") | ||
} | ||
|
||
addrNameToExport := args[0] | ||
|
||
as := util.AccountsStore() | ||
addrStr, err := as.Get(addrNameToExport) | ||
if err != nil { | ||
logFatal(err) | ||
} | ||
|
||
ks := util.KeyStore() | ||
|
||
account, err := ks.Find(accounts.Account{Address: common.HexToAddress(addrStr)}) | ||
if err != nil { | ||
logFatal(err) | ||
} | ||
|
||
var passphrase string | ||
var message = "Passphrase for account" | ||
err = ks.Unlock(account, "") | ||
if err != nil { | ||
prompt := &survey.Password{Message: message} | ||
survey.AskOne(prompt, &passphrase) | ||
if passphrase == "" { | ||
fmt.Println("Aborted") | ||
return | ||
} | ||
} | ||
|
||
keyJSON, err := ks.Export(account, passphrase, "") | ||
if err != nil { | ||
logFatal(err) | ||
} | ||
|
||
key, err := keystore.DecryptKey(keyJSON, "") | ||
if err != nil { | ||
logFatal(err) | ||
} | ||
|
||
fmt.Println("Private key: ", hex.EncodeToString(key.PrivateKey.D.Bytes())) | ||
}, | ||
} | ||
|
||
func init() { | ||
walletCmd.AddCommand(exportAccountCmd) | ||
exportAccountCmd.Flags().Bool("really-do-it", false, "really export the account") | ||
} |