Skip to content

Commit

Permalink
KeyFile to SecretKey (#23)
Browse files Browse the repository at this point in the history
* Add keyfile to secretkey conversion.

* Add usage example.

* Example in readme as well.

* Re-export KeyFile.
  • Loading branch information
tomusdrw authored Sep 3, 2019
1 parent ba02404 commit f665490
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# ethsign

A library to read JSON keyfiles and sign Ethereum stuff.

## Usage:
```rust
use ethsign::{Protected, KeyFile};

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);

// Verify the signature
let res = public.verify(&signature, &message).unwrap();
println!("{}", if res { "signature correct" } else { "invalid signature" });
}
```

9 changes: 8 additions & 1 deletion src/keyfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::crypto::{self, Keccak256};
use crate::error::Error;
use crate::Protected;
use crate::{SecretKey, Protected};

use rand::{thread_rng, RngCore};
use serde::{Deserialize, Serialize};
Expand All @@ -25,6 +25,13 @@ pub struct KeyFile {
pub address: Option<Bytes>,
}

impl KeyFile {
/// Attemp to convert the `KeyFile` into `SecretKey`.
pub fn to_secret_key(&self, password: &Protected) -> Result<SecretKey, Error> {
SecretKey::from_crypto(&self.crypto, password)
}
}

/// Encrypted secret
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Crypto {
Expand Down
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
//! A simple library to read JSON keyfiles and sign Ethereum stuff.
//!
//! How to use it?
//! ```rust
//! use ethsign::{Protected, KeyFile};
//!
//! 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);
//!
//! // Verify the signature
//! let res = public.verify(&signature, &message).unwrap();
//! println!("{}", if res { "signature correct" } else { "invalid signature" });
//! }
//! ```

#![warn(missing_docs)]

Expand All @@ -19,4 +44,5 @@ pub mod keyfile;

pub use self::error::Error;
pub use self::key::{PublicKey, SecretKey, Signature};
pub use self::keyfile::KeyFile;
pub use self::protected::Protected;

0 comments on commit f665490

Please sign in to comment.