Delving in to the world of crypto hardwarewallets has led me deep in to the rabbit hole of USB and Hid programming. I didn’t want or expect to be doing any low level programming, yet here I am – screwing around with USB sticks and and cables for hours on end. When I first started writing Hardfolio, I thought that there would be libraries out there to make USB access easy. I was dead wrong. Fortunately, for you, I’ve done all the hard work on Android, Windows, and UWP, so you can just reap the benefits and integrate devices in to your software.
USB.Net and Hid.Net
These libraries started their life inside Hardfolio as the communication layer to the hardwarewallets. As I realized that there really wasn’t any other C# libraries out there that make cross platform communication easy, I started developing these libraries. The code is all open source, and can be found here. While most Hid devices are USB devices, not all USB devices support Hid. So, it’s necessary to communicate with them with different API calls. USB.Net, and Hid.Net hide all this nastiness from you.
Here are the API calls for Hid on Windows as an example.
Here are the equivalent USB calls:
You might ask why these two things are so different… You’d be completely justified in thinking that this is madness. But luckily, here is some code that automatically switched between Hid, or USB and hides all the stuff from you:
The code above can be found here.
I’ll save the boring details about the underlying for each of the platforms. If you’re interested in understanding more about how each of the platforms handle USB and Hid access, you can clone the repo and inspect each part. However, if you’re anything like me, you’ll just want a library that works. I have gone to great detail about how to use the libraries in the documentation here. Please have a read through it there and let me know if you have any problems.