-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added optional export of private key #108
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,9 +2,12 @@ | |||||
|
||||||
A library to read JSON keyfiles and sign Ethereum stuff. | ||||||
|
||||||
Library by defaults hide private key from access, | ||||||
but you can add --features export-private-key to export it. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## Usage: | ||||||
```rust | ||||||
use ethsign::{Protected, KeyFile}; | ||||||
use ethsign::{KeyFile, Protected}; | ||||||
|
||||||
fn main() { | ||||||
let file = std::fs::File::open("./res/wallet.json").unwrap(); | ||||||
|
@@ -21,6 +24,13 @@ fn main() { | |||||
let public = signature.recover(&message).unwrap(); | ||||||
println!("{:?}", public); | ||||||
|
||||||
#[cfg(feature = "export-private-key")] | ||||||
{ | ||||||
//Do not print private key in that way in production code | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
let private = secret.private(); | ||||||
println!("Extracted private key: {}", hex::encode(private)); | ||||||
} | ||||||
|
||||||
// Verify the signature | ||||||
let res = public.verify(&signature, &message).unwrap(); | ||||||
println!("{}", if res { "signature correct" } else { "invalid signature" }); | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,28 @@ | ||||||
use ethsign::{KeyFile, Protected}; | ||||||
|
||||||
fn main() { | ||||||
let file = std::fs::File::open("./res/wallet.json").unwrap(); | ||||||
let key: KeyFile = serde_json::from_reader(file).unwrap(); | ||||||
let password: Protected = "".into(); | ||||||
let secret = key.to_secret_key(&password).unwrap(); | ||||||
let message = [1_u8; 32]; | ||||||
|
||||||
// Sign the message | ||||||
let signature = secret.sign(&message).unwrap(); | ||||||
println!("{:?}", signature); | ||||||
|
||||||
// Recover the signer | ||||||
let public = signature.recover(&message).unwrap(); | ||||||
println!("{:?}", public); | ||||||
|
||||||
#[cfg(feature = "export-private-key")] | ||||||
{ | ||||||
//Do not print private key in that way in production code | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
let private = secret.private(); | ||||||
println!("Extracted private key: {}", hex::encode(private)); | ||||||
} | ||||||
|
||||||
// Verify the signature | ||||||
let res = public.verify(&signature, &message).unwrap(); | ||||||
println!("{}", if res { "signature correct" } else { "invalid signature" }); | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -122,6 +122,17 @@ impl SecretKey { | |||||||||||||
PublicKey::from_slice(&uncompressed[1..]).expect("The length of the key is correct; qed") | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Export stored, unencrypted, plain private key, use with caution | ||||||||||||||
/// Do not expose this key in logs, etc. Use only if needed | ||||||||||||||
Comment on lines
+125
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
#[cfg(feature = "export-private-key")] | ||||||||||||||
pub fn private(&self) -> [u8; 32] { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
WDYT about calling the function |
||||||||||||||
use std::convert::TryInto; | ||||||||||||||
self.secret | ||||||||||||||
.as_ref() | ||||||||||||||
.try_into() | ||||||||||||||
.expect("The length of the key is correct; qed") | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Sign given 32-byte message with the key. | ||||||||||||||
pub fn sign(&self, message: &[u8]) -> Result<Signature, ec::Error> { | ||||||||||||||
let (v, data) = ec::sign(self.secret.as_ref(), message)?; | ||||||||||||||
|
@@ -152,6 +163,15 @@ mod tests { | |||||||||||||
pub_key.address().to_hex::<String>(), | ||||||||||||||
"005b3bcf82085eededd551f50de7892471ffb272" | ||||||||||||||
); | ||||||||||||||
#[cfg(feature = "export-private-key")] | ||||||||||||||
{ | ||||||||||||||
let secret_key = key.private(); | ||||||||||||||
assert_eq!( | ||||||||||||||
secret_key.to_hex::<String>(), | ||||||||||||||
"43cd5154df157a4ec26e3a04f9db252280e0c840b6a209026accb70dc124328c".to_string() | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
assert_eq!(&pub_key.bytes().to_hex::<String>(), "782cc7dd72426893ae0d71477e41c41b03249a2b72e78eefcfe0baa9df604a8f979ab94cd23d872dac7bfa8d07d8b76b26efcbede7079f1c5cacd88fe9858f6e"); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.