Why is/was the Bitcoin Core HWI written in Python?
The most important issue was that, on the time, each the foremost {hardware} pockets vendor offered a Python library to work together with them. Over time, this has remained true – as new {hardware} wallets have been created, they normally include Python libraries (e.g. Bitbox02, Coldcard), however that is probably attributable to the truth that HWI exists.
Moreover, HWI began out as some scripts for experimentation and largely as a toy venture. On condition that I’m acquainted and comfy with Python, and Python may be very handy for experimentation, it made sense to make use of it
Nevertheless, as with every open supply venture, some selections made firstly for the only creator’s comfort can come again to chunk us.
What have the challenges been of getting the HWI written in Python?
One of many targets for HWI is to have standalone binaries that may be referred to as by different software program. That is truly somewhat tough to do in Python. It requires the usage of different tooling like PyInstaller to supply these binaries. These binaries are somewhat inefficient as properly since they’re self extracting archives that comprise a full Python interpreter. This has been problematic on some programs, and a few downstream customers of HWI have opted to repackage the binaries otherwise to be able to higher swimsuit their customers.
Moreover, not one of the tooling to create standalone Python binaries can “cross compile”. They will solely create binaries focused for the OS and CPU structure that this system is being run on. Whereas for Home windows this may be labored round with WINE, it’s not attainable to take action for MacOS, thus requiring the builder(s) to spend a big sum of cash in buying {hardware} to create these binaries.
One other main subject has additionally been reproducibility. One of many aims for HWI was to make the binaries reproducibly constructed. This has been somewhat tough due to how PyInstaller repackages the working Python interpreter, so Python itself must be rebuilt reproducibly on every system that we need to help. That is majorly annoying, provided that Python itself is just not reproducible on all programs.
Python additionally has some points round dependencies. A person could also be lacking a dependency and by no means discover it till they determine to run some a part of the code that requires a dependency. Then it is going to begin mysteriously failing. This could generate a number of pointless help tickets.
With dependencies are additionally points with the dependency graph. Because it’s very easy to put in a dependency with pip, it is very simple to run into points the place a minimal dependency truly is not so minimal because it pulls in a ton of different dependencies. This makes it arduous to audit everything of the code that is truly being run. Nevertheless this isn’t distinctive to Python, as NodeJS and rust can have an identical drawback.
Lastly, since Python is an interpreted language that’s weakly typed, it is very easy to by chance write bugs associated to sorts or incorrectly assuming {that a} specific worth will probably be of some kind when it’s not. For the reason that code could also be syntactically appropriate, Python won’t give an error till the buggy code is executed, which can not all the time occur. An instance of that is utilizing a[key] for a listing. It will increase a KeyError when it’s executed, however in any other case Python will not inform you that it is improper.
There appears to be curiosity and rationale(s) to jot down one other HWI in Rust. Would there be a powerful rationale to proceed to take care of the Python HWI if there was a Rust HWI? MicroPython is a well-liked Python implementation (optimized to run on microcontrollers) for {hardware} wallets. Does Rust have an equal and is it extensively used?
No, there wouldn’t be a powerful cause to proceed to take care of a Python HWI if a compiled language model sufficiently replicated all of its performance. This does not even require issues that use MicroPython or different Python interpreters to essentially change (additionally I do not assume HWI works in MicroPython).
Compiled languages produce machine code binaries. These will be libraries which have an exterior API which have bindings in each language. This permits such a venture for use with every other language. These will be mechanically generated, and there are a number of initiatives that may do that fairly properly. They may also be handwritten. If a rust HWI reproduced HWI’s Python APIs precisely, it may very well be used as a drop in substitute. There does not must be a rust equal for MicroPython, there simply must be Python bindings for the rust library.
Nevertheless, it additionally would not make sense to have HWI run in MicroPython. It is software program for the host, not the {hardware} pockets.
