Skip to content

Commit

Permalink
Reworking cli help string for the 'import-unarmored' sub-command
Browse files Browse the repository at this point in the history
  • Loading branch information
pbukva committed Oct 31, 2024
1 parent fab9223 commit 3262b1f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
65 changes: 48 additions & 17 deletions client/keys/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,40 @@ func ImportKeyCommand() *cobra.Command {
func ImportUnarmoredKeyCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "import-unarmored <name> [keyfile]",
Short: "Import unarmored private key into the local keybase",
Long: `Import hex encoded unarmored private key into the local keybase
Key must be hex encoded, and can be passed in either via file, or via
user password prompt.
If the 2nd positional argument [keyfile] has been provided, private key
will be read from that file. The keyfile must contain hex encoded
unarmored raw private key on the very 1st line, and that line must
contain only the private key.
Otherwise, if the [keyfile] is not provided, the private key will be
requested via password prompt where it will be read from stdin.
At the moment, only the secp256k1 curve/algo is supported.`,
Short: "Imports unarmored private key into the local keybase",
Long: `Imports hex encoded raw unarmored private key into the local keybase
[keyfile] - Path to the file containing unarmored hex encoded private key.
=> *IF* this non-mandatory 2nd positional argument has been
*PROVIDED*, then private key will be read from that file.
=> *ELSE* If this positional argument has been *OMITTED*, then
user will be prompted on terminal to provide password in
the SECURE PROMPT = passed in characters of the key hex
value will *not* be displayed on the screen.
File format: The only condition for the file format is, that
the unarmored key must be on the first line (the file can also
contain further lines, though they are ignored).
The 1st line must contain only hex encoded unarmored raw value,
serialised *exactly* as it is expected by given cryptographic
algorithm specified by the '--unarmored-key-algo <algo>' flag
(see the description of that flag).
Hex key value can be preceded & followed by any number of any
whitespace characters, they will be ignored.
Key value:
As mentioned above, key is expected to be hex encoded. Hex encoding can be
lowercase, uppercase or mixed case, it does not matter, and it can (but
does NOT need to) contain the '0x' or just 'x' prefix at the beginning of
the hex encoded value.
Output:
The command will print key info after the import, the same way as the
'keys add ...' command does.
This is quite useful, since user will immediately see the address (and pub
key value) derived from the imported private key.`,
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
Expand Down Expand Up @@ -99,10 +121,12 @@ At the moment, only the secp256k1 curve/algo is supported.`,
algo, _ := cmd.Flags().GetString(flagUnarmoredKeyAlgo)

privKeyHexLC := strings.ToLower(privKeyHex)
if strings.HasPrefix(privKeyHexLC, "0x") {
privKeyHexLC = privKeyHexLC[2:]
} else if strings.HasPrefix(privKeyHexLC, "x") { //nolint:gosimple,S1017 false positive, see https://github.com/dominikh/go-tools/issues/1447
privKeyHexLC = privKeyHexLC[1:]
acceptedHexValPrefixes := []string{"0x", "x"}
for _, prefix := range acceptedHexValPrefixes {
if strings.HasPrefix(privKeyHexLC, prefix) {
privKeyHexLC = privKeyHexLC[len(prefix):]
break
}
}

privKeyRaw, err := hex.DecodeString(privKeyHexLC)
Expand All @@ -123,7 +147,14 @@ At the moment, only the secp256k1 curve/algo is supported.`,
},
}

cmd.Flags().String(flagUnarmoredKeyAlgo, string(hd.Secp256k1Type), fmt.Sprintf("defines cryptographic scheme algorithm of the private key (\"%s\", \"%s\"). At the moment *ONLY* the \"%s\" is supported. Defaults to \"%s\".", hd.Secp256k1Type, hd.Ed25519Type, hd.Secp256k1Type, hd.Secp256k1Type))
cmd.Flags().String(flagUnarmoredKeyAlgo, string(hd.Secp256k1Type), fmt.Sprintf(
`Defines cryptographic scheme algorithm of the provided unarmored private key.
At the moment *ONLY* the "%s" and "%s" algorithms are supported.
Expecdted serialisation format:
* for "%s": 32 bytes raw private key (hex encoded)
* for "%s": 32 bytes raw public key immediatelly followed by 32 bytes

Check failure on line 155 in client/keys/import.go

View workflow job for this annotation

GitHub Actions / golangci-lint

`immediatelly` is a misspelling of `immediately` (misspell)
private key = 64 bytes alltogether (hex encoded)

Check failure on line 156 in client/keys/import.go

View workflow job for this annotation

GitHub Actions / golangci-lint

`alltogether` is a misspelling of `altogether` (misspell)
`, hd.Secp256k1Type, hd.Ed25519Type, hd.Secp256k1Type, hd.Ed25519Type))

return cmd
}
Expand Down
5 changes: 3 additions & 2 deletions crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"encoding/hex"
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"

Check failure on line 7 in crypto/keyring/keyring.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -319,13 +320,13 @@ func (ks keystore) ImportUnarmoredPrivKey(uid string, unarmoredPrivKeyRaw []byte
case hd.Secp256k1Type:
privKey = &secp256k1.PrivKey{Key: unarmoredPrivKeyRaw}
case hd.Ed25519Type:
fallthrough
privKey = &ed25519.PrivKey{Key: unarmoredPrivKeyRaw}
case hd.Sr25519Type:
fallthrough
case hd.MultiType:
fallthrough
default:
return nil, fmt.Errorf("only the \"%s\" algo is supported at the moment", hd.Secp256k1Type)
return nil, fmt.Errorf("only the \"%s\" and \"%s\" algos are supported at the moment", hd.Secp256k1Type, hd.Ed25519Type)
}

// privKey, err := legacy.PrivKeyFromBytes(privKeyRaw)
Expand Down

0 comments on commit 3262b1f

Please sign in to comment.