Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Trezor support #41

Closed
wants to merge 4 commits into from
Closed

Add Trezor support #41

wants to merge 4 commits into from

Conversation

wszdexdrf
Copy link

This PR aims to add Trezor functionality to this library using trezor-client crate. The following commits add an implementation of trait HWI using the above mentioned Trezor API in rust.

@darosior darosior requested a review from edouardparis August 7, 2023 07:05
@prusnak
Copy link

prusnak commented Aug 7, 2023

@wszdexdrf
Copy link
Author

I fixed the CI tests. The problem was the the CI didn't have a protobuf compiler, which is needed to build the trezor-client crate (dependency).

Copy link
Member

@edouardparis edouardparis left a comment

Choose a reason for hiding this comment

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

Thank you for the PR !

src/trezor.rs Outdated Show resolved Hide resolved
src/trezor.rs Show resolved Hide resolved
src/trezor.rs Outdated Show resolved Hide resolved
src/trezor.rs Outdated Show resolved Hide resolved
@prusnak
Copy link

prusnak commented Aug 10, 2023

https://crates.io/crates/trezor-client 0.1.2 with added Sync and Send to Transport has been released

src/trezor.rs Outdated Show resolved Hide resolved
@edouardparis
Copy link
Member

Seems good, targeting next week to release it after testing with simulator 👍

@edouardparis
Copy link
Member

Can you add an implementation of From<TrezorClient> for Box<dyn HWI + Send> please
like https://github.com/wizardsardine/async-hwi/blob/master/src/specter.rs#L145

@wszdexdrf
Copy link
Author

Can you add an implementation of From<TrezorClient> for Box<dyn HWI + Send> please like https://github.com/wizardsardine/async-hwi/blob/master/src/specter.rs#L145

Something like this?

@edouardparis
Copy link
Member

Yes perfect ! , my bad for commenting too quickly.

@matejcik
Copy link

sorry @wszdexdrf i'm having trouble following the logic but, can this actually sign transactions?

ISTM from sign_tx you get back a SignTxProgress object ... but that would require you to repeatedly call ack_psbt(tx) until you get an error or until progress.finished() is true ... or is there some magic loop that I am missing?

@wszdexdrf
Copy link
Author

sorry @wszdexdrf i'm having trouble following the logic but, can this actually sign transactions?

Yes, about that. I didn't want to make any assumptions, hence I created the sign_tx to be pretty much one-to-one implementation of the sign_tx in the trezor_client library. I was expecting the user of the library to create the loop or whatever they wish, and I returned a hopefully helpful error value of DeviceDidNotSign if the signing process was still incomplete.

I honestly don't feel strongly about either way. We could also create a while loop (with some sleeps in between, hopefully using the async nature somehow), or simply leaving it up to the user. What do you think about this @matejcik?

@matejcik
Copy link

well, sign_tx from HWI trait doesn't return anything, it expects you to modify the passed in psbt object (as far as I can tell anyway). there is no way for the caller to get ahold of the SignTxProgress object so you'll need to do the loop internally somehow.

given that the whole thing is async, it would be nice to do it in an async manner -- but trezor-client library is currently synchronous, so I'm not sure how to accomplish that

@wszdexdrf
Copy link
Author

but that would require you to repeatedly call ack_psbt(tx) until you get an error or until progress.finished() is true

@matejcik please take a look at https://github.com/wszdexdrf/async-hwi/pull/3 where I used some tests to test the functionality.

@wszdexdrf
Copy link
Author

I did some testing and now the code is able to sign transactions. If anyone needs to look at the tests, they are on branch https://github.com/wszdexdrf/async-hwi/tree/tests

@wszdexdrf
Copy link
Author

Pinging reviewers for review and merge. @matejcik @edouardparis @prusnak

src/trezor.rs Outdated Show resolved Hide resolved
src/trezor.rs Outdated Show resolved Hide resolved
))?;
for pk in input.bip32_derivation.keys() {
let fp = input.bip32_derivation.get(pk).unwrap().0;
let pk = PublicKey::from_slice(pk.serialize().as_ref()).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

It is not clear here why it is serialize and deserialize, can you not just clone the key ?

Copy link
Author

Choose a reason for hiding this comment

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

The pk from the bip32_derivation is of type bitcoin::secp256k1::PublicKey and the type needed in partial_sigs is bitcoin::PublicKey. The serializing and deserializing is used as a rudimentary type-casting.

@edouardparis
Copy link
Member

Sorry I did not forget you, I am just very busy. But I am going to a conference next week and I hope to test with a real device here.

@wszdexdrf wszdexdrf closed this by deleting the head repository Sep 17, 2023
@prusnak prusnak mentioned this pull request Sep 17, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants