4 releases (2 breaking)

0.3.0 May 31, 2023
0.2.1 Oct 15, 2019
0.2.0 Sep 28, 2019
0.1.0 Mar 28, 2019

#13 in #drop-in

Download history 18/week @ 2024-04-07 47/week @ 2024-04-14 26/week @ 2024-04-21 9/week @ 2024-04-28 12/week @ 2024-05-05 22/week @ 2024-05-12 33/week @ 2024-05-19 23/week @ 2024-05-26 32/week @ 2024-06-02 22/week @ 2024-06-09 15/week @ 2024-06-16 31/week @ 2024-06-23 35/week @ 2024-06-30 89/week @ 2024-07-07 19/week @ 2024-07-14 8/week @ 2024-07-21

153 downloads per month
Used in ethsign

GPL-3.0 license

9KB
148 lines

ethsign

A library to read JSON keyfiles and sign Ethereum stuff.

Usage:

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

A higher-level lib ethkey facilitates managing key files and exposes ethsign:

[dependencies]
ethkey = "0.3"
use ethkey::prelude::*;

fn main() {
    let key = EthAccount::load_or_generate("/tmp/path/to/keystore", "passwd")
        .expect("should load or generate new eth key");

    println!("{:?}", key.address());

    let message = [7_u8; 32];

    // sign the message
    let signature = key.sign(&message).unwrap();

    // verify the signature
    let result = key.verify(&signature, &message).unwrap();
    println!("{}", if result {"verification ok"} else {"wrong signature"});
}


lib.rs:

Pure Rust drop-in replacement for the parity-crypto crate.

Dependencies

~2.5MB
~46K SLoC