0

I am trying to use the function here https://docs.rs/ethkey/0.2.5/ethkey/ to write a keyfile for ethereum :

let key = EthAccount::load_or_generate("Users/Documents/Code/Thor/thor/parity/keys", "passwd")
        .expect("should load or generate new eth key");

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

unfortunately, it doesnt work and it 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.

Update

load_or_generate works when i enter the ~ as the first argument but not the file path where i actually want my keys i.e Users/Documents/Code/Thor/thor/parity/keys

Update

I am now using the full path with a slash infront but still doenst work. i.e.

let key = EthAccount::load_or_generate("./Users/samueldare/Documents/Code/Thor/thor/parity/keys", "passwd")
        .expect("should load or generate new eth key");

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

I will apprciate pointers on this

Community
  • 1
  • 1
0xsegfault
  • 2,899
  • 6
  • 28
  • 58
  • How does this differ from [your other question](https://stackoverflow.com/questions/57201043/crate-cannot-find-path)? – hellow Jul 25 '19 at 12:14
  • Not using the home now, using a file path works with home i.e. ` ~ ` but not file path. i.e. `"Users/Documents/Code/Thor/thor/parity/keys"` – 0xsegfault Jul 25 '19 at 12:15
  • Are using vscode? If so, right click on the function (`load_or_generate`) and go to the source for that function and start reading to determine what that function is requiring. That will help you understand the error message that is being generated by the Serde crate which is being included to parse this. – Gardener Jul 25 '19 at 12:17
  • But if I understand the error, ethkey complains that `.../parity/keys` is a directory, instead of a file. The documentaion also says: `pub fn load_or_generate

    (file_path: P, password: W) ` so it should be a file, instead of a directory?

    – hellow Jul 25 '19 at 12:18
  • thats for the ok(()) leg .. on err it is meant to create a new key file let (secret, log_msg) = match File::open(&file_path) { Ok(file) => { let key_file: KeyFile = serde_json::from_reader(file)?; let secret = SecretKey::from_crypto(&key_file.crypto, &pwd)?; (secret, "loaded") } Err(_e) => { let secret = SecretKey::from_raw(&random_bytes())?; save_key(&secret, &file_path, pwd)?; (secret, "generated and saved") – 0xsegfault Jul 25 '19 at 12:19

1 Answers1

0

Add a '/' to the beginnning of your file path.

https://doc.rust-lang.org/std/path/struct.Path.html

e.g.:

let key = EthAccount::load_or_generate("/Users/Documents/Code/Thor/thor/parity/keys", "passwd")
        .expect("should load or generate new eth key");

In your first question, this was not necessary because you were using '~' as your path, which is a valid path by itself. This time, you are starting with '/Users...' which requires an initial slash '/'.

Gardener
  • 2,591
  • 1
  • 13
  • 22
  • dont think it works.. i have even gone into the directory to `pwd` i.e. `"/Users/samueldare/Documents/Code/Thor/thor/parity/keys"` i get the foillowing 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. ` – 0xsegfault Jul 25 '19 at 12:22
  • 1
    `SerdeJsonError(Error("Is a directory (os error 21))` is the underlying error message that you should google now. The first problem was the missing opening slash. This is a new error message that relates to passing a directory when a filename was expected (or vice versa). Of course the function wants a directory when no key exists and a filename when the key file does exist, so you'll have to figure out if the directory exists and why it is confused. – Gardener Jul 25 '19 at 12:30
  • if you saw my last comment i take that back and thank you. Figured it out – 0xsegfault Jul 25 '19 at 13:12