question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

import unarmored-hex private key in keychain

See original GitHub issue

Describe the bug

For now there is no supported way to import unarmored-hex private key through cli.

To Reproduce

Steps to reproduce the behavior:

  1. Create key with key add
Example

root@agoric:# ag0 keys add testkey Enter keyring passphrase:

  • name: testkey type: local address: agoric17v05q6zkms4an52cmqjh66wyua4p3mc5x5x42n pubkey: ‘{“@type”:“/cosmos.crypto.secp256k1.PubKey”,“key”:“ArBppIsQhPrugYbB2E3cBazDeImbSRfT4ersnCpX/PI5”}’ mnemonic: “”

Important write this mnemonic phrase in a safe place. It is the only way to recover your account if you ever forget your password.

donor misery hospital detect hole road sun stock bag sound surge sister young proud unhappy rubber hover limit glide hip element sign fossil blame

  1. Export created key in unarmored-hex format
Example

root@agoric:# ag0 keys export testkey --unarmored-hex --unsafe WARNING: The private key will be exported as an unarmored hexadecimal string. USE AT YOUR OWN RISK. Continue? [y/N]: y Enter keyring passphrase: b101cfc33ec9f702d2d0540cdbe34855066520d1059c1535f86f08c368a15a6c

  1. Save key in file
Example

root@agoric:# echo “b101cfc33ec9f702d2d0540cdbe34855066520d1059c1535f86f08c368a15a6c” >> key.txt root@agoric:# cat ./key.txt b101cfc33ec9f702d2d0540cdbe34855066520d1059c1535f86f08c368a15a6c

  1. Try to import key and get an error keys import ...
Example

root@agoric:# ag0 keys import testkey2 ./key.txt Enter passphrase to decrypt your key: Error: failed to decrypt private key: EOF Usage: ag0 keys import <name> <keyfile> [flags]

Flags: -h, --help help for import

Global Flags: –home string The application home directory (default “/root/.agoric”) –keyring-backend string Select keyring’s backend (os|file|test) (default “os”) –keyring-dir string The client Keyring directory; if omitted, the default ‘home’ directory will be used –log_format string The logging format (json|plain) (default “plain”) –log_level string The logging level (trace|debug|info|warn|error|fatal|panic) (default “info”) –output string Output format (text|json) (default “text”) –trace print out full stack trace on errors

Expected behavior

Key should be encrypted and imported in keychain

Platform Environment

  • Ubuntu 20.04 Go 1.17.2
  • ag0 tag agoric-3.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
dckccommented, Nov 8, 2021

keyFmt work-around

Given the raw key above b101cfc33ec9f702d2d0540cdbe34855066520d1059c1535f86f08c368a15a6c, using password for a password:

$ cd ag0/cmd

$ go build keyFmt.go # see source code below

$ ./keyFmt >keyfile
Enter 64 character raw hex private secp256k1 key:
Pick a password, at least 8 chars:

$ cat keyfile
-----BEGIN TENDERMINT PRIVATE KEY-----
kdf: bcrypt
salt: E0C8E18B626209820597AF1136C85F54

0Y4i7ZWCuAbyOYxOSkUQpHuXdofjnb0Aos2NFiC2oxFuqemctRDq+aFJIA3nWnoR
zNpdD+vcnGOMBQijgjIoEtqWMDse+zlwqNaKmdw=
=uXKy
-----END TENDERMINT PRIVATE KEY-----

$ ag0 keys import keyname keyfile
Enter passphrase to decrypt your key:

keyFmt.go

package main

import (
	"bufio"
	"encoding/hex"
	"fmt"
	"os"

	"github.com/cosmos/cosmos-sdk/client/input"

	"github.com/cosmos/cosmos-sdk/codec"
	"github.com/cosmos/cosmos-sdk/codec/legacy"
	"github.com/cosmos/cosmos-sdk/crypto"
	cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
	"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
)

func main() {
	buf := bufio.NewReader(os.Stdin)
	privKeyHex, err := input.GetPassword("Enter 64 character raw hex private secp256k1 key:", buf)
	passphrase, err := input.GetPassword("Pick a password, at least 8 chars:", buf)
	if err != nil {
		panic(err)
	}

	privKeyRaw, err := hex.DecodeString(privKeyHex)
	if err != nil {
		panic(err)
	}
	cdc := codec.NewLegacyAmino()
	cryptocodec.RegisterCrypto(cdc)
	privKeyBytes := cdc.MustMarshal(secp256k1.PrivKey{Key: privKeyRaw})
	privKey, err := legacy.PrivKeyFromBytes(privKeyBytes)
	if err != nil {
		panic(err)
	}
	text := crypto.EncryptArmorPrivKey(privKey, passphrase, "")
	fmt.Println(text)
}
1reaction
asifhjcommented, Nov 6, 2021

@dckc Thank you so much for the workaround. I confirm I am able to import my private key.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I add a private key to my keychain? - Ask Different
I exported this item in OpenRSA format from Putty. When I try to use Import Items , the file is unselectable. What do...
Read more >
Import and export keychain items using Keychain Access on ...
In Keychain Access on your Mac, you can import or export keychain items between ... Some exported items, such as public keys, don't...
Read more >
Can't import Code-signing Public or Private keys using ...
When I do this, although the command returns "1 key imported" I do not see a public key for "Roomer Inc" in my...
Read more >
Importing/Exporting certificates on Mac OS via Keychain
In the Keychain Access window, located under the Keychains sidebar, click System and then under Category, click Certificates to view the imported certificate....
Read more >
Import Private Key into Mac - certificate, FYIcenter.com
1. Review private key to ensure it is stored in an encrypted file like fyicenter-key.p12. · 2. Open "Launchpad > Other > Keychain...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found