trezor / trezord-go

:smiling_imp: Trezor Communication Daemon (written in Go)
GNU Lesser General Public License v3.0
244 stars 146 forks source link

Make trezord-go usable as go library #147

Closed karelbilek closed 2 years ago

karelbilek commented 5 years ago

To help other devs to use trezord-go as a library, I have added an api/ folder, which can be now used as a native golang API/library to contact Trezor devices.

To "test" the API properly, I have also modified the server/ to use directly the API, and not the core/ calls.

api.New(..) now also initializes all the USB libraries, to make the use easier; that was originally in main.go

I have also cleaned up the main.go a bit by breaking it into more files, when I was already rewriting it significantly

There are some caveats.


The first commit is the addition of api/ and using it in server/ and trezord.go. The second commit is "just" moving everything except for api/ into internal/, so it cannot be linked directly by outside packages.

karelbilek commented 5 years ago

Ugh. I run gofmt -s, which simplified the code but made it incompatible with older go versions. I... don't know if that's something important or not

karelbilek commented 5 years ago

Also I broke fmt and metalinter. Hm. OK

karelbilek commented 5 years ago

github.com/conejoninja/tesoro/pb/messages has the protobuf messages, apparently

karelbilek commented 5 years ago

... github.com/conejoninja/tesoro seems to be another API for trezor, that partly uses our trezord-go code by copy-pasting it. Maybe we should look at that too and try to somehow fit trezord-go-as-a-library there

karelbilek commented 5 years ago

also CC @conejoninja , @xaionaro

conejoninja commented 5 years ago

... github.com/conejoninja/tesoro seems to be another API for trezor, that partly uses our trezord-go code by copy-pasting it. Maybe we should look at that too and try to somehow fit trezord-go-as-a-library there

Hi, it's been a while since I last touched tesoro, apart from websub, what other part seems to be copy-pasted? I'll remove them.

karelbilek commented 5 years ago

Hi, it's been a while since I last touched tesoro, apart from websub, what other part seems to be copy-pasted? I'll remove them

.Yeah I mean webusb :)

karelbilek commented 5 years ago

Hm another self-review: if this is meant to be used as an API, api/ (and therefore, internal/core/ and all the others) should not get memorywriter (which is our internal thing) as an option, just either io.Writer or *log.Logger. Which will be some rewriting too.

karelbilek commented 5 years ago

OK, now the library is actually trying to connect to bridge first and only then starts libusb. And from my short tests, it seems to be working.

Next steps: I will try to look at existing libraries/projects and try to use trezord-go/api there. Then add godoc documentation.

karelbilek commented 5 years ago

Looking at the other libraries - some of them have the ability to work in Android. That's interesting. Trezord-go probably doesn't work in Android, but I am not so sure - it's Linux under the hood - so maybe with some tiny changes, this would work as a library under Android.

karelbilek commented 5 years ago

I have tried to change Tesoro to use this new library, catched some bugs, but it seems working! (So it now works both with bridge, and if not installed, then with all 3 (~4) transports - hid, webusb, UDP emulator t2, UDP emulator for t1)

https://github.com/conejoninja/tesoro/compare/master...karel-3d:master

It will be similar to change the other libraries. (Note - android is probably not working.)

karelbilek commented 5 years ago

I have added documentation (which creates nice-ish godoc files) and also added protobuf messages, so it's all less annoying to use.

I needed to write my own code generation tool, because otherwise, it's error-prone to "by hand" put together messages.MessageType_MessageType_ButtonAck and the proto type messages.ButtonAck (because go doesn't allow you to find that).

What COULD I do is to use some more dynamic protobuf library, but that would mean losing the type safety, so... yeah I have added a lot of auto-generated code. To make using of this as a library simplier.

From me it's all here, I will also modify Tesoro and xaionaro-go to use the final version, I will NOT look into go-ethereum, which reimplements the same usb things AGAIN, but it could use this library too. If someone has mood/time :))))

karelbilek commented 5 years ago

OK, this is tesoro using the new library - not the PB serialization, since that would take just too much time (the code would be cleaner, but I don't have infinite time)

https://github.com/karel-3d/tesoro/commits/master

This is @xaionaro cryptowallet using the new library, INCLUDING the PB serialization (it was mostly deletions, as most of the code is not necessary anymore)

https://github.com/karel-3d/cryptoWallet/commit/add0f509eaaf803effad0a24312a5ddf98c2d7c4

The example for using the new library, which shows all the major functions (this is really all that is needed to integrate Trezor to golang app, no need for custom USB handling and custom protobuf magic in every integration)

https://github.com/karel-3d/trezord-go-2/blob/1699ab6f898e22460f0b73b9c8f885727f592266/trezorapi/transport_test.go

karelbilek commented 3 years ago

I am no longer updating this PR, and I would do it very differently this time (but still kept the auto-generation).

I will keep it here if someone wants to take over.