Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
108 lines
3.4 KiB
Go
108 lines
3.4 KiB
Go
package keys
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/hex"
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/client/input"
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
"github.com/cosmos/cosmos-sdk/crypto/types"
|
|
)
|
|
|
|
const (
|
|
flagUnarmoredHex = "unarmored-hex"
|
|
flagUnsafe = "unsafe"
|
|
)
|
|
|
|
// ExportKeyCommand exports private keys from the key store.
|
|
func ExportKeyCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "export <name>",
|
|
Short: "Export private keys",
|
|
Long: `Export a private key from the local keyring in ASCII-armored encrypted format.
|
|
|
|
When both the --unarmored-hex and --unsafe flags are selected, cryptographic
|
|
private key material is exported in an INSECURE fashion that is designed to
|
|
allow users to import their keys in hot wallets. This feature is for advanced
|
|
users only that are confident about how to handle private keys work and are
|
|
FULLY AWARE OF THE RISKS. If you are unsure, you may want to do some research
|
|
and export your keys in ASCII-armored encrypted format.`,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
clientCtx, err := client.GetClientQueryContext(cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
buf := bufio.NewReader(clientCtx.Input)
|
|
unarmored, _ := cmd.Flags().GetBool(flagUnarmoredHex)
|
|
unsafe, _ := cmd.Flags().GetBool(flagUnsafe)
|
|
|
|
if unarmored && unsafe {
|
|
return exportUnsafeUnarmored(cmd, args[0], buf, clientCtx.Keyring)
|
|
} else if unarmored || unsafe {
|
|
return fmt.Errorf("the flags %s and %s must be used together", flagUnsafe, flagUnarmoredHex)
|
|
}
|
|
|
|
encryptPassword, err := input.GetPassword("Enter passphrase to encrypt the exported key:", buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
armored, err := clientCtx.Keyring.ExportPrivKeyArmor(args[0], encryptPassword)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.Println(armored)
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().Bool(flagUnarmoredHex, false, "Export unarmored hex privkey. Requires --unsafe.")
|
|
cmd.Flags().Bool(flagUnsafe, false, "Enable unsafe operations. This flag must be switched on along with all unsafe operation-specific options.")
|
|
cmd.Flags().BoolP(flagYes, "y", false, "Skip confirmation prompt when export unarmored hex privkey")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func exportUnsafeUnarmored(cmd *cobra.Command, uid string, buf *bufio.Reader, kr keyring.Keyring) error {
|
|
// confirm export unarmored hex privkey, unless -y is passed
|
|
if skip, _ := cmd.Flags().GetBool(flagYes); !skip {
|
|
if yes, err := input.GetConfirmation("WARNING: The private key will be exported as an unarmored hexadecimal string. USE AT YOUR OWN RISK. Continue?", buf, cmd.ErrOrStderr()); err != nil {
|
|
return err
|
|
} else if !yes {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
hexPrivKey, err := unsafeExportPrivKeyHex(kr.(unsafeExporter), uid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.Println(hexPrivKey)
|
|
|
|
return nil
|
|
}
|
|
|
|
// unsafeExporter is implemented by key stores that support unsafe export
|
|
// of private keys' material.
|
|
type unsafeExporter interface {
|
|
// ExportPrivateKeyObject returns a private key in unarmored format.
|
|
ExportPrivateKeyObject(uid string) (types.PrivKey, error)
|
|
}
|
|
|
|
// unsafeExportPrivKeyHex exports private keys in unarmored hexadecimal format.
|
|
func unsafeExportPrivKeyHex(ks unsafeExporter, uid string) (privkey string, err error) {
|
|
priv, err := ks.ExportPrivateKeyObject(uid)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return hex.EncodeToString(priv.Bytes()), nil
|
|
}
|