Buy a Trezor Here

Trezor.Net

The Trezor is a crypto currency hardwarewallet. Use my library Trezor.Net for cross-platform .NET projects. You can use the Trezor via C# or VB.Net on Android, UWP, Windows, and potentially mcaOS and Linux. The code is in this repo. Contribution is needed to keep this library up to date and finish implementing the public interface.

Intro

Developing for the Trezor wallet is very straight forward. If you have a basic understanding of USB development or request/response development, you will be able to get your head around programming for the Trezor device. This article  is only a introductory article and does not give detail about how to write your first Trezor application. Instead, this article will point you to the relevant resources and give you some idea on how to get started.

The first thing to know about the Trezor is that it is a HID USB device. HID devices are very simple by nature. You simply send and receive data from the device. All data is sent in binary format by default which is unlike how most web transfer protocols like REST work. But, like REST, Trezor works on a request/response basis. You send the Trezor a message, and it will send you one back.

Trezor’s programming interface uses a technology called Protocol Buffers. It is a Google technology and you can read about it here . Protocol Buffers is a technology that allows for the programming interface to be defined in a markup language which is programming language agnostic. In Protocol Buffers the markup language defines the message types that can be sent and from the device. The Protocol Buffers (protobuf for short) technology has been implemented on many platforms such as Java, JavaScript, C# (.NET, Xamarin, etc.) , Python, and Go. If you paste some protobuf markup in to a language generator, it will pump out code in your language that can be used to talk to protobuf compliant devices. I used this online generator by Marc Gravell to generate code for the Trezor in C#.

Here is a starting point for reading about Trezor’s programming interface: Trezor Developers Guide

The above documentation does not give a lot of detail. So, the first step is for you to pick the language that you wish to build your application on. Here is the Trezor Github Page. It appears that Satoshi labs has combined all the firmware etc. in to a single repo. There are several libraries here which would allow you to get started. There is a Python, Java, JavaScript and Go library already floating around. If no library already exists, once you have chosen your technology, you will need to generate the code for that technology. The protobuf definitions (markup) can be copied and pasted in to a generator to create the code. In order to use Marc Gravell’s online generator, I needed to merge the two definition files together and then the C# generated correctly. Edit: you do not need to do this if you use Marc’s full command line generator. I am currently working on Trezor.Net which will be available at some point in the future with an as yet unknown license structure.

I cracked the system without any documentation. I trawled through the existing code and examples which give ample information on how to get information. If you do not use one of the existing libraries, you will also need a HID library. I have posted a C# library called Hid.Net on NuGet.org which you may be able to use currently. You will need to convert messages generated in your language of choice in to protobuf binary messages using a protobuf library, and then, you will need to send those messages to the Trezor via the HID library. The Trezor will then respond with its own message.

Here is the documentation about Trezor workflow.

A typical conversation will go like this:

App->Trezor: GetAddress (Request an address for a coin)

Trezor->App: PinMatrixRequest (Request pin in encrypted format)

App->Trezor: PinMatrixAck (Send pin in encrypted format)

Trezor->App: Address (Get address information)

To summarize, I recommend using on of the existing libraries or sample apps to get started, but it’s not impossible to figure all this stuff out without doing so. The next article will have more details and have some code samples.

Generate the Protobuf Messages

You will need to generate the protobuf messages in the language of your choice. You can get the definitions by cloning this repo and then grabbing the .proto files from this folder. I used the app protogen from the protobuf-net C# repo. These were the steps I took for C#

  • Clone Trezor Firmware repo
  • Clone protobuf-net
  • Build the protogen project with Visual Studio
  • Drop the proto files from the Trezor Firmware repo in the bin folder of the protogen app in a folder called proto.
  • Lastly, run this at the command line. It generates the code in the C# Code folder

protogen *.proto –proto_path=Proto –csharp_out=Code

1 Comment

  1. “In order to use Marc Gravell’s generator, I needed to merge the two definition files together and then the C# generated correctly.” – note: the command-line version of the tool supports multiple input files, but the online version currently doesn’t. At the time, the command-line version wasn’t readily available (you had to build it yourself), but I now make this available as a download with the tool.

Leave a Reply