Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

solana-keygen - Poor mans keypair encryption #6259

Merged
merged 4 commits into from
Oct 10, 2019

Conversation

t-nelson
Copy link
Contributor

@t-nelson t-nelson commented Oct 7, 2019

Problem

There's no mechanism to encrypt a keypair generated by solana-keygen

Summary of Changes

  1. Support reading from stdin in read_keypair
  2. Print all non-key text output to stderr in solana-keygen

This allows a poor man's keypair encryption via tools such as gpg without the keypair plaintext ever touching disk.

# Key pair creation
$ solana-keygen new -o - | gpg -c -o key.json.gpg
# Print pubkey
$ gpg -d key.json.gpg | solana-keygen pubkey -

May require $ export GPG_TTY=$(tty) on a Mac. You'll see an error along the lines of Inappropriate ioctl for device.

Caveat

When using gpg, the phase phrase prompt gets spammed by the mnemonic output AND the mnemonic output is cleared when the pass phrase prompt updates. Be sure to copy the mnemonic before entering the passphrase.

@codecov
Copy link

codecov bot commented Oct 7, 2019

Codecov Report

Merging #6259 into master will decrease coverage by 2.2%.
The diff coverage is 78.1%.

@@           Coverage Diff            @@
##           master   #6259     +/-   ##
========================================
- Coverage    73.3%   71.1%   -2.3%     
========================================
  Files         219     219             
  Lines       44022   45403   +1381     
========================================
- Hits        32306   32289     -17     
- Misses      11716   13114   +1398

@t-nelson
Copy link
Contributor Author

t-nelson commented Oct 8, 2019

@CriesofCarrots RE the gpg passphrase prompt garbling. I tried tweaking the buffering and even reordering printing of the mnemonic and key json to no success. I believe gpg is tossing up the prompt immediately upon being launched rather than waiting for input plaintext data to show up, which makes sense. However, passing --silent to solana-keygen new suppresses the mnemonic altogether and makes for a nice experience. What do you think about forcing --slient when infile == "-" ? Having the mnemonic is probably counter to the intentions of someone interested in encrypting their keypair anyway.

@CriesofCarrots
Copy link
Contributor

Hmm, on my terminal (iTerm2), the mnemonic phrase and passphrase prompt actually seem to behave properly.

I do still like the recommendation of --silent for outfile== "-", as I agree about the intentions of encryption, but I don't think we should force it. What do you think about adding some expanded comments or a readme with usage examples and recs?

@t-nelson
Copy link
Contributor Author

t-nelson commented Oct 8, 2019

Which pinentry does that spawn, the GUI one or curses-based terminal one?

@CriesofCarrots
Copy link
Contributor

Which pinentry does that spawn, the GUI one or curses-based terminal one?

The GUI one. Ah, that's the difference?

@t-nelson
Copy link
Contributor Author

t-nelson commented Oct 8, 2019

garbled_gpg_prompt

🙃

@CriesofCarrots
Copy link
Contributor

Thanks. Ugh, gross.
I would, however, still prefer to have the mnemonic option somehow.
We could switch to having silent be the default, and an optional mnemonic arg, for outfile== "-"
Or just handle with docs/recommendations, as I mentioned above. Thoughts?

@t-nelson
Copy link
Contributor Author

t-nelson commented Oct 8, 2019

I think documenting that --silent should be used if the encryption application prompts for a passphrase on the command line is probably sufficient. That'll be the smallest change to current behavior. We can tune the defaults towards this use case if users complain.

@t-nelson t-nelson force-pushed the poor_mans_keypair_encryption branch 2 times, most recently from deff131 to f46b9a5 Compare October 9, 2019 15:02
Copy link
Contributor

@CriesofCarrots CriesofCarrots left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this much better!
Just need a little fixup to "recover"

Comment on lines 169 to 183
let serialized_keypair = write_keypair(&keypair, outfile)?;
let serialized_keypair = write_keypair_file(&keypair, outfile)?;
if outfile == "-" {
println!("{}", serialized_keypair);
} else {
println!("Wrote recovered keypair to {}", outfile);
eprintln!("Wrote recovered keypair to {}", outfile);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section needs to be rewritten to match your changes in "new"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! I must've lost that in my, "Oh just let me make history pretty..." rebase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

864c3b8 oughta do it

@t-nelson t-nelson force-pushed the poor_mans_keypair_encryption branch from f46b9a5 to 864c3b8 Compare October 10, 2019 21:36
Copy link
Contributor

@CriesofCarrots CriesofCarrots left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
r+ if CI is happy

@t-nelson t-nelson merged commit 9cde670 into solana-labs:master Oct 10, 2019
@t-nelson t-nelson deleted the poor_mans_keypair_encryption branch October 10, 2019 23:01
@t-nelson t-nelson added this to the Cloud Nine v0.20.0 milestone Oct 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants