Tuesday, March 21, 2023
HomeBitcoinWhy is the Bitcoin Core HWI written in Python? If it was...

Why is the Bitcoin Core HWI written in Python? If it was rewritten in Rust ought to the Python HWI nonetheless be maintained?


Why is/was the Bitcoin Core HWI written in Python?

The largest 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 often 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 principally as a toy undertaking. Provided that I’m acquainted and comfy with Python, and Python could be very handy for experimentation, it made sense to make use of it

Nonetheless, as with all open supply undertaking, some choices made firstly for the only real 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 slightly tough to do in Python. It requires the usage of different tooling like PyInstaller to supply these binaries. These binaries are slightly inefficient as effectively since they’re self extracting archives that comprise a full Python interpreter. This has been problematic on some techniques, and a few downstream shoppers of HWI have opted to repackage the binaries otherwise with a view to higher go well with 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 isn’t potential 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 difficulty has additionally been reproducibility. One of many goals for HWI was to make the binaries reproducibly constructed. This has been slightly 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 isn’t reproducible on all techniques.

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 can begin mysteriously failing. This may 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 onerous to audit everything of the code that is truly being run. Nonetheless this isn’t distinctive to Python, as NodeJS and rust can have an identical downside.

Lastly, since Python is an interpreted language that’s weakly typed, it is very easy to unintentionally write bugs associated to varieties or incorrectly assuming {that a} specific worth can be of some sort when it isn’t. For the reason that code could also be syntactically right, Python won’t give an error till the buggy code is executed, which can not at all times occur. An instance of that is utilizing a[key] for a listing. This can increase a KeyError when it’s executed, however in any other case Python will not let you know that it is improper.

There appears to be curiosity and rationale(s) to jot down one other HWI in Rust. Would there be a robust 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 robust motive 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 undertaking for use with another language. These will be routinely generated, and there are a number of tasks that may do that fairly effectively. They will 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 would not must be a rust equal for MicroPython, there simply must be Python bindings for the rust library.

Nonetheless, it additionally would not make sense to have HWI run in MicroPython. It is software program for the host, not the {hardware} pockets.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments