0

I am trying to use this crate to generate an ethereum address: https://docs.rs/ethkey/0.2.5/ethkey/

use ethkey::prelude::*;

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

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

This is the example from the documentation and it doesnt seem to work. I get the error below:

cargo run Compiling ethkey v0.1.0 (/Users/samueldare/Documents/Code/Thor/ethkey) Finished dev [unoptimized + debuginfo] target(s) in 1.34s Running target/debug/ethkey thread 'main' panicked at 'should load or generate new eth key: Error(IoError(Os { code: 2, kind: NotFound, message: "No such file or directory" }), State { next_error: None, backtrace: InternalBacktrace { backtrace: None } })', src/libcore/result.rs:999:5 note: run with RUST_BACKTRACE=1 environment variable to display a backtrace.

Ive use ~/ as a last attempt to generate the key file in rust, but it still doesnt seem to work.

I will appreciate any pointers with this

0xsegfault
  • 2,899
  • 6
  • 28
  • 58

1 Answers1

2

The first argument to load_or_generate() takes a std::path::Path with no closing slash ( '/' ). Remove the slash:

fn main() {
    let key = EthAccount::load_or_generate("~", "passwd")
        .expect("should load or generate new eth key");

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

Sample output:

05:47 ethtest (master) ✗ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/ethtest`
Address(0x8248af6d1765e559509060b88e540a3567c42d20)
05:47 ethtest (master) ✗ 
hellow
  • 12,430
  • 7
  • 56
  • 79
Gardener
  • 2,591
  • 1
  • 13
  • 22
  • Thanks! that works for the home, but when i try to put it in a file like : `/Users/Documents/Code/Thor/thor/parity/keys` i get the following error: `thread 'main' panicked at 'should load or generate new eth key: Error(SerdeJsonError(Error("Is a directory (os error 21)", line: 0, column: 0)), State { next_error: None, backtrace: InternalBacktrace { backtrace: None } })', src/libcore/result.rs:999:5 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace` thanks for your help! – 0xsegfault Jul 25 '19 at 11:59
  • Looks like a new question :-). Your first question is a great [mcve]. The comment, is really a brand new question. Please present it with its own [mcve] in a new question, after accepting the answer in this question. – Gardener Jul 25 '19 at 12:08
  • I have written a new question here: https://stackoverflow.com/questions/57201720/cannot-write-keyfile-with-crate thanks for your help!! – 0xsegfault Jul 25 '19 at 12:13