Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feat/trezor: cache session on filesystem #747

Merged
merged 5 commits into from
Dec 29, 2021

Conversation

joshieDo
Copy link
Contributor

@joshieDo joshieDo commented Dec 28, 2021

Motivation

Sessions are used to "keep" the passphrase on the device in-between different calls. And while it was being reused inside the same TrezorEthereum object, once we instantiated a new one, we would lose the session.

For example, different forge or cast calls would always request the user to insert their passphrase again and again. With this change, it won't.

Relevant information from sessions @ docs.trezor.io :

  • Sessions only exist in RAM and are lost when Trezor is disconnected.
  • New session is started by calling Initialize with no arguments. The response is a Features message, which contains a 32-byte session_id. All subsequent commands happen within the given session.
  • To resume a previous session (after creating a new one), call Initialize with a stored session_id as an argument. Attempt to resume an unknown session ID will transparently allocate a new session ID.

Solution

Read/write session_id to ./cache/trezor.session

PR Checklist

  • Added Tests
  • Added Documentation
  • Updated the changelog

ethers-signers/src/trezor/app.rs Outdated Show resolved Hide resolved
.join("cache")
.join("trezor.session");

if let Ok(file) = fs::File::open(path) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we return a None here if the file does not exist?
and an error if reading the file failed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

although in the beginning, i was thinking of issuing a warning, without halting the execution

Comment on lines 73 to 74
let mut file = std::io::BufReader::new(file);
file.read(&mut session).map_err(|e| TrezorError::CacheError(e.to_string()))?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't think a BufReader is necessary here for 32bytes?
also read does not guarantee to fill the buf entirely.
there is https://doc.rust-lang.org/stable/std/io/trait.Read.html#method.read_exact instead, or alternatively File::read_to_end

path = path.join("trezor.session");

let file = fs::File::create(path).map_err(|e| TrezorError::CacheError(e.to_string()))?;
let mut file = std::io::BufWriter::new(file);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, if the session id is only 32byte, probably not worth it, fs::write should do just fine I believe

Comment on lines 70 to 73
let path = env::current_dir()
.map_err(|e| TrezorError::CacheError(e.to_string()))?
.join("cache")
.join("trezor.session");
Copy link
Owner

@gakonst gakonst Dec 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should pass a cache_dir to trezor when initializing it? Or have a global ~/.ethers-rs/trezor/cache/trezor.session file? Don't think we always want the current dir to be populated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added all options

  1. accepts cache_dir as a parameter
  2. if none, then tries home_dir
  3. if none, then tries current_dir
  4. if err, exit

@gakonst gakonst merged commit c6b51e3 into gakonst:master Dec 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants