-
Notifications
You must be signed in to change notification settings - Fork 2
bhwi-cli: command to enumerate devices #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,4 +21,5 @@ reqwest = "0.13.2" | |
| serde = "1.0.228" | ||
| serde_json = "1.0.149" | ||
| serde_cbor = "0.11.2" | ||
| thiserror = "1" | ||
| tokio = "1.0" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| pub use bhwi::coldcard::DEFAULT_CKCC_SOCKET; | ||
| pub mod hid; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| pub use bhwi::jade::JADE_DEVICE_IDS; | ||
|
|
||
| #[cfg(feature = "emulators")] | ||
| pub mod tcp; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,109 +1,51 @@ | ||
| use std::fmt::{Debug, Display}; | ||
| use std::fmt::Debug; | ||
|
|
||
| use async_trait::async_trait; | ||
| use hex::FromHexError; | ||
| use serde::{Deserialize, Serialize, de::DeserializeOwned}; | ||
|
|
||
| use crate::Transport; | ||
| use crate::{Transport, transport::Channel}; | ||
|
|
||
| pub struct SpeculosTransport<C> { | ||
| pub client: C, | ||
| pub struct LedgerTransportTcp<C: Channel> { | ||
| channel: C, | ||
| } | ||
|
|
||
| impl<C> SpeculosTransport<C> { | ||
| pub fn new(client: C) -> Self { | ||
| Self { client } | ||
| } | ||
| } | ||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum LedgerTcpError { | ||
| #[error("ledger io error: {0}")] | ||
| Io(#[from] std::io::Error), | ||
|
|
||
| #[derive(Serialize, Deserialize)] | ||
| /// Apdu response from speculos emulator | ||
| pub struct Apdu { | ||
| /// hex encoded apdu data | ||
| data: String, | ||
| #[error("ledger invalid response")] | ||
| InvalidResponse, | ||
| } | ||
|
|
||
| #[async_trait(?Send)] | ||
| pub trait SpeculosClient { | ||
| type Error: Debug + From<FromHexError>; | ||
|
|
||
| /// The endpoint that the speculos emulator is listening on. | ||
| /// Ex. "localhost:5000" | ||
| fn url(&self) -> &str; | ||
|
|
||
| async fn post<Req: Serialize>(&self, endpoint: &str, json_req: Req) -> Result<(), Self::Error>; | ||
|
|
||
| async fn post_json<Req: Serialize, Res: DeserializeOwned>( | ||
| &self, | ||
| endpoint: &str, | ||
| json_req: Req, | ||
| ) -> Result<Res, Self::Error>; | ||
|
|
||
| async fn button_press(&self, button: Button) -> Result<(), Self::Error> { | ||
| Self::post( | ||
| self, | ||
| &format!("{}/button/{button}", Self::url(self)), | ||
| &ButtonRequest { | ||
| action: "press-and-release".into(), | ||
| }, | ||
| ) | ||
| .await | ||
| } | ||
|
|
||
| async fn set_automation<T: Serialize>(&self, automation_json: &T) -> Result<(), Self::Error> { | ||
| Self::post( | ||
| self, | ||
| &format!("{}/automation", Self::url(self)), | ||
| automation_json, | ||
| ) | ||
| .await | ||
| impl<C: Channel> LedgerTransportTcp<C> { | ||
| pub fn new(channel: C) -> Self { | ||
| Self { channel } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait(?Send)] | ||
| impl<C: SpeculosClient> Transport for SpeculosTransport<C> { | ||
| type Error = <C as SpeculosClient>::Error; | ||
| impl<C: Channel> Transport for LedgerTransportTcp<C> { | ||
| type Error = LedgerTcpError; | ||
|
|
||
| async fn exchange( | ||
| &mut self, | ||
| apdu_command: &[u8], | ||
| _encrypted: bool, | ||
| ) -> Result<Vec<u8>, Self::Error> { | ||
| let Apdu { data } = self | ||
| .client | ||
| .post_json( | ||
| &format!("{}/apdu", self.client.url()), | ||
| &Apdu { | ||
| data: hex::encode(apdu_command), | ||
| }, | ||
| ) | ||
| .await?; | ||
| Ok(hex::decode(data)?) | ||
| } | ||
| } | ||
| let mut tx = Vec::with_capacity(4 + apdu_command.len()); | ||
| tx.extend_from_slice(&(apdu_command.len() as u32).to_be_bytes()); | ||
| tx.extend_from_slice(apdu_command); | ||
|
|
||
| #[derive(Debug, Clone, Copy)] | ||
| pub enum Button { | ||
| Left, | ||
| Right, | ||
| Both, | ||
| } | ||
| self.channel.send(&tx).await?; | ||
|
|
||
| impl Display for Button { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| write!( | ||
| f, | ||
| "{}", | ||
| match self { | ||
| Button::Left => "left", | ||
| Button::Right => "right", | ||
| Button::Both => "both", | ||
| } | ||
| ) | ||
| } | ||
| } | ||
| let mut len_buf = [0u8; 4]; | ||
| self.channel.receive(&mut len_buf).await?; | ||
|
|
||
| let resp_len = u32::from_be_bytes(len_buf) as usize; | ||
|
|
||
| #[derive(Serialize)] | ||
| struct ButtonRequest { | ||
| action: String, | ||
| let mut resp = vec![0u8; resp_len + 2]; | ||
| self.channel.receive(&mut resp).await?; | ||
|
|
||
| Ok(resp) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to come up with a better solution to this before attempting to write a proc macro. Duplicating all the methods isn't so awful, but not very nice IMO