16 releases (8 breaking)

0.9.0 May 31, 2023
0.8.0 Feb 3, 2021
0.7.3 Oct 15, 2019
0.6.1 Mar 28, 2019

#4 in #protected

Download history 228/week @ 2024-07-22 606/week @ 2024-07-29 446/week @ 2024-08-05 387/week @ 2024-08-12 325/week @ 2024-08-19 314/week @ 2024-08-26 312/week @ 2024-09-02 400/week @ 2024-09-09 617/week @ 2024-09-16 459/week @ 2024-09-23 437/week @ 2024-09-30 179/week @ 2024-10-07 247/week @ 2024-10-14 143/week @ 2024-10-21 167/week @ 2024-10-28 170/week @ 2024-11-04

740 downloads per month
Used in 15 crates (6 directly)

GPL-3.0 license

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

Dependencies

~8.5MB
~124K SLoC