diff --git a/.github/actions/build-bitcoind/action.yml b/.github/actions/build-bitcoind/action.yml index 4fc591cf3..44bc41564 100644 --- a/.github/actions/build-bitcoind/action.yml +++ b/.github/actions/build-bitcoind/action.yml @@ -1,5 +1,14 @@ name: Build bitcoind description: Build bitcoind with depends. +inputs: + repo-url: + description: Bitcoin Core repository to build + required: false + default: https://github.com/bitcoin/bitcoin.git + branch: + description: Branch of the repository to build + required: false + default: master runs: using: composite steps: @@ -19,11 +28,13 @@ runs: - shell: bash env: CCACHE_MAXSIZE: '20M' + BITCOIN_REPO_URL: ${{ inputs.repo-url }} + BITCOIN_REPO_BRANCH: ${{ inputs.branch }} run: | ccache --zero-stats cd test; ./setup_environment.sh --bitcoind; cd .. ccache --show-stats --verbose - tar -czf bitcoind.tar.gz test/work/bitcoin/build/bin/bitcoind + tar -czf bitcoind.tar.gz test/work/bitcoin/build/bin/bitcoind test/work/bitcoin/build/bin/bitcoin-node - uses: actions/cache/save@v4 if: github.event_name != 'pull_request' && steps.ccache-cache.outputs.cache-hit != 'true' diff --git a/.github/actions/test-core-ipc/action.yml b/.github/actions/test-core-ipc/action.yml new file mode 100644 index 000000000..3b15ddaec --- /dev/null +++ b/.github/actions/test-core-ipc/action.yml @@ -0,0 +1,44 @@ +name: Test Core IPC +description: Run the Bitcoin Core IPC signer test against an installed HWI wheel. +runs: + using: composite + steps: + # The install-sim action uses poetry for some devices. + - shell: bash + run: | + pip install poetry + + - uses: actions/download-artifact@v8 + with: + name: dist + path: dist/ + + # Install the wheel with the ipc extra, so the test drives the packaged + # hwi rather than the source tree. + - name: Install (Wheel) + shell: bash + run: | + wheel=$(ls dist/*.whl) + pip install "${wheel}[ipc]" + + - uses: actions/download-artifact@v8 + with: + name: bitcoind + + - shell: bash + run: | + tar -xvf bitcoind.tar.gz + + - uses: ./.github/actions/install-sim + with: + device: ${{ matrix.device }} + + - name: Run test + shell: bash + run: | + cd test; ./run_tests.py --${{ matrix.device }} --core-ipc-only --interface=cli --device-only; cd .. + + - if: failure() + shell: bash + run: | + tail -v -n +1 test/*.std* diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cb66eea51..391f561d0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -188,6 +188,11 @@ jobs: steps: - uses: actions/checkout@v7 - uses: ./.github/actions/build-bitcoind + with: + # The IPC signer test needs Bitcoin Core's external signer over IPC + # branch, which is not merged yet: bitcoin/bitcoin#TBD + repo-url: https://github.com/Sjors/bitcoin.git + branch: 2026/09/hwi-ipc test-trezor-1: uses: ./.github/workflows/device-test.yml diff --git a/.github/workflows/device-test.yml b/.github/workflows/device-test.yml index ccfd477c0..68aa2e76a 100644 --- a/.github/workflows/device-test.yml +++ b/.github/workflows/device-test.yml @@ -16,7 +16,7 @@ on: jobs: test-device: - name: Python ${{ matrix.python-version }} ${{ matrix.device }} ${{ matrix.test.script || matrix.test.interface }} + name: Python ${{ matrix.python-version }} ${{ matrix.device }} ${{ matrix.test.core_ipc && format('Core IPC ({0})', matrix.test.core_ipc) || matrix.test.script || matrix.test.interface }} runs-on: ${{ inputs.runs-on }} timeout-minutes: 45 strategy: @@ -32,6 +32,12 @@ jobs: - {script: 'Wheel'} - {script: 'Sdist'} - {script: 'Bindist'} + # The Bitcoin Core IPC signer test gets one dedicated job per device, + # on the newest Python and against the wheel. + include: + - python-version: '3.14' + device: ${{ inputs.device }} + test: {core_ipc: 'Wheel'} container: python:${{ matrix.python-version }} steps: - uses: actions/checkout@v7 @@ -41,3 +47,6 @@ jobs: - name: run dist test if: ${{ matrix.test.script }} uses: ./.github/actions/test-dist + - name: run Core IPC test + if: ${{ matrix.test.core_ipc }} + uses: ./.github/actions/test-core-ipc diff --git a/hwilib/_cli.py b/hwilib/_cli.py index 52077ac08..815af58c3 100644 --- a/hwilib/_cli.py +++ b/hwilib/_cli.py @@ -117,6 +117,14 @@ def install_udev_rules_handler(args: argparse.Namespace) -> Dict[str, bool]: def register_descriptor_handler(args: argparse.Namespace, client: HardwareWalletClient) -> Dict[str, str]: return register_descriptor(client, args.name, args.descriptor) +def ipc_signer_handler(args: argparse.Namespace) -> Dict[str, Any]: + from ._ipc import ipc_signer_handler as run_ipc_signer + result: Dict[str, Any] = {} + with handle_errors(result=result): + run_ipc_signer(args) + result = {'success': True} + return result + class HWIHelpFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter): pass @@ -254,6 +262,10 @@ def get_parser() -> HWIArgumentParser: register_descriptor_parser.add_argument("descriptor", help="The descriptor to register.") register_descriptor_parser.set_defaults(func=register_descriptor_handler) + ipc_parser = subparsers.add_parser('ipc', help='Connect to a Bitcoin Core node IPC socket and serve as its external signer until the node disconnects') + ipc_parser.add_argument('--socket-path', help='Path to the unix socket the node listens on with -ipcbind. Defaults to node.sock in the default Bitcoin Core datadir of the chain given by --chain', default=None) + ipc_parser.set_defaults(func=ipc_signer_handler) + if sys.platform.startswith("linux"): udevrules_parser = subparsers.add_parser('installudevrules', help='Install and load the udev rule files for the hardware wallet devices') udevrules_parser.add_argument('--location', help='The path where the udev rules files will be copied', default='/etc/udev/rules.d/') @@ -299,6 +311,10 @@ def process_commands(cli_args: List[str]) -> Any: if command == 'enumerate': return args.func(args) + # Serve as a Bitcoin Core external signer over IPC + if command == 'ipc': + return args.func(args) + # Install the devices udev rules for Linux if command == 'installudevrules': with handle_errors(msg="installudevrules failed:", result=result): diff --git a/hwilib/_gui.py b/hwilib/_gui.py index 389a29e21..929d088c6 100644 --- a/hwilib/_gui.py +++ b/hwilib/_gui.py @@ -4,10 +4,12 @@ import json import logging import sys +import threading import time from typing import Callable from . import commands, __version__ +from . import _ipc from ._cli import HWIArgumentParser from .errors import handle_errors, DEVICE_NOT_INITIALIZED from .common import AddressType, Chain @@ -27,7 +29,7 @@ exit(-1) from PySide2.QtGui import QRegExpValidator -from PySide2.QtWidgets import QApplication, QDialog, QDialogButtonBox, QFileDialog, QLineEdit, QMessageBox, QMainWindow, QMenu +from PySide2.QtWidgets import QApplication, QDialog, QDialogButtonBox, QFileDialog, QLabel, QLineEdit, QMessageBox, QMainWindow, QMenu from PySide2.QtCore import QCoreApplication, QRegExp, Signal, Slot def do_command(f, *args, **kwargs): @@ -321,6 +323,8 @@ def attestation_check(self, result: bool) -> None: pass class HWIQt(QMainWindow): + ipc_status_changed = Signal(str) + def __init__(self, passphrase=None, chain=Chain.MAIN, allow_emulators: bool = False): super(HWIQt, self).__init__() self.ui = Ui_MainWindow() @@ -357,6 +361,36 @@ def __init__(self, passphrase=None, chain=Chain.MAIN, allow_emulators: bool = Fa self.ui.enumerate_combobox.currentIndexChanged.connect(self.get_client_and_device_info) + self.ipc_status_label = QLabel() + self.statusBar().addPermanentWidget(self.ipc_status_label) + self.ipc_status_changed.connect(self.set_ipc_status) + self.start_ipc_signer_service() + + @Slot(str) + def set_ipc_status(self, status): + logging.info(status) + self.ipc_status_label.setText(status) + + def start_ipc_signer_service(self): + """Serve as the external signer of a Bitcoin Core node over IPC, if + one is listening on the default socket path for our chain.""" + if _ipc.capnp is None: + self.set_ipc_status('Core IPC: unavailable (install pycapnp)') + return + socket_path = _ipc.default_socket_path(self.chain) + self.set_ipc_status('Core IPC: connecting...') + + def serve(): + try: + _ipc.run_signer_service( + socket_path, self.passphrase, self.allow_emulators, + on_registered=lambda: self.ipc_status_changed.emit(f'Core IPC: connected ({socket_path})')) + self.ipc_status_changed.emit('Core IPC: disconnected') + except Exception as e: + self.ipc_status_changed.emit(f'Core IPC: not connected ({e})') + + threading.Thread(target=serve, name='ipc-signer', daemon=True).start() + def clear_info(self): self.ui.getxpub_button.setEnabled(False) self.ui.signtx_button.setEnabled(False) diff --git a/hwilib/_ipc.py b/hwilib/_ipc.py new file mode 100644 index 000000000..a1a419678 --- /dev/null +++ b/hwilib/_ipc.py @@ -0,0 +1,166 @@ +""" +Bitcoin Core IPC signer service +******************************* + +Serve HWI as a Bitcoin Core external signer over Cap'n Proto IPC. + +Bitcoin Core's ``bitcoin-node`` binary can listen on a unix socket with +``-ipcbind=unix``. This module connects to that socket and registers HWI as +the node's external signer backend, replacing the ``-signer=`` model of +spawning ``hwi`` for every operation. See Bitcoin Core's +``doc/external-signer.md`` and the schemas in ``hwilib/ipc/schema``. + +Each signer call resolves the device by fingerprint with a fresh client, +matching the behavior of one CLI invocation per operation. Expected failures +(device missing, user cancel) are reported in-band via the ``error``/ +``result`` fields; an uncaught exception here reaches the node as a +transport error and makes it drop the registration. +""" + +import argparse +import asyncio +import base64 +import logging +import os +import sys +from pathlib import Path +from typing import Any, Callable, Dict, Optional + +# Optional dependency: everything except serving works without it, so e.g. +# default_socket_path() stays importable in environments without the extra. +try: + import capnp # type: ignore +except ImportError: + capnp = None + +from . import commands +from .common import Chain +from .errors import DEVICE_CONN_ERROR, UnavailableActionError, handle_errors +from .hwwclient import HardwareWalletClient + +SCHEMA_DIR = Path(__file__).parent / "ipc" / "schema" + + +def default_socket_path(chain: Chain) -> str: + """The default path of Bitcoin Core's -ipcbind=unix socket for this + platform and chain: //node.sock.""" + if sys.platform == "win32": + datadir = Path(os.environ["APPDATA"]) / "Bitcoin" + elif sys.platform == "darwin": + datadir = Path.home() / "Library" / "Application Support" / "Bitcoin" + else: + datadir = Path.home() / ".bitcoin" + subdir = { + Chain.MAIN: None, + Chain.TEST: "testnet3", + Chain.REGTEST: "regtest", + Chain.SIGNET: "signet", + Chain.TESTNET4: "testnet4", + }[chain] + if subdir is not None: + datadir = datadir / subdir + return str(datadir / "node.sock") + + +def load_schemas() -> Any: + imports = [str(SCHEMA_DIR)] + signer = capnp.load(str(SCHEMA_DIR / "signer.capnp"), imports=imports) + init = capnp.load(str(SCHEMA_DIR / "init.capnp"), imports=imports) + return init, signer + + +def make_signer_server(signer_schema: Any, password: str, allow_emulators: bool) -> Any: + + class SignerServer(signer_schema.ExternalSignerService.Server): # type: ignore + + def _with_client(self, fingerprint: str, chain_str: str, func: Callable[[HardwareWalletClient], Dict[str, Any]]) -> Dict[str, Any]: + """Resolve the device by fingerprint, run func with a fresh + client, and close it. Mirrors one CLI invocation.""" + result: Dict[str, Any] = {} + with handle_errors(result=result): + chain = Chain.argparse(chain_str) + if not isinstance(chain, Chain): + raise Exception(f"Invalid chain: {chain_str}") + client = commands.find_device(password=password, fingerprint=fingerprint, chain=chain, allow_emulators=allow_emulators) + if client is None: + return {"error": "Could not find device with specified fingerprint", "code": DEVICE_CONN_ERROR} + try: + result = func(client) + finally: + with handle_errors(result={}): + client.close() + return result + + async def enumerate(self, chain, _context, **kwargs): + chain_enum = Chain.argparse(chain) + if not isinstance(chain_enum, Chain): + raise Exception(f"Invalid chain: {chain}") + devices = commands.enumerate(password=password, chain=chain_enum, allow_emulators=allow_emulators) + # The typed API carries no per-device error entries: skip + # devices which failed to report a fingerprint. + _context.results.result = [ + {"fingerprint": d["fingerprint"], "name": d.get("model", "")} + for d in devices if d.get("fingerprint") + ] + + async def getDescriptors(self, fingerprint, chain, account, _context, **kwargs): + res = self._with_client(fingerprint, chain, lambda client: commands.getdescriptors(client, account)) + r = _context.results + if "error" in res: + r.error = res["error"] + r.result = False + else: + r.receive = res["receive"] + r.internal = res["internal"] + r.result = True + + async def displayAddress(self, fingerprint, chain, descriptor, _context, **kwargs): + res = self._with_client(fingerprint, chain, lambda client: commands.displayaddress(client, desc=descriptor)) + r = _context.results + if "error" in res: + r.error = res["error"] + r.result = False + else: + r.address = res["address"] + r.result = True + + async def signTransaction(self, fingerprint, chain, psbt, _context, **kwargs): + psbt_b64 = base64.b64encode(bytes(psbt)).decode("ascii") + res = self._with_client(fingerprint, chain, lambda client: commands.signtx(client, psbt_b64)) + r = _context.results + if "error" in res: + r.error = res["error"] + r.result = False + else: + r.signedPsbt = base64.b64decode(res["psbt"]) + r.result = True + + return SignerServer + + +async def serve(socket_path: str, password: str, allow_emulators: bool, on_registered: Optional[Callable[[], None]] = None) -> None: + init_schema, signer_schema = load_schemas() + connection = await capnp.AsyncIoStream.create_unix_connection(socket_path) + client = capnp.TwoPartyClient(connection) + init = client.bootstrap().cast_as(init_schema.Init) + server_class = make_signer_server(signer_schema, password, allow_emulators) + await init.registerExternalSigner(server_class()) + logging.info(f"Registered as external signer with the node at {socket_path}") + if on_registered is not None: + on_registered() + # Serve signer calls until the node disconnects. + await client.on_disconnect() + logging.info("Node disconnected, exiting") + + +def run_signer_service(socket_path: str, password: str, allow_emulators: bool, on_registered: Optional[Callable[[], None]] = None) -> None: + """Connect to the node, register as its external signer and serve until + the node disconnects. Blocks the calling thread.""" + if capnp is None: + raise UnavailableActionError("IPC support requires the pycapnp package. Install it with: pip install hwi[ipc]") + asyncio.run(capnp.run(serve(socket_path, password, allow_emulators, on_registered))) + + +def ipc_signer_handler(args: argparse.Namespace) -> None: + socket_path = args.socket_path or default_socket_path(args.chain) + run_signer_service(socket_path, args.password, args.allow_emulators) diff --git a/hwilib/ipc/__init__.py b/hwilib/ipc/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/hwilib/ipc/schema/README.md b/hwilib/ipc/schema/README.md new file mode 100644 index 000000000..dfeb7b793 --- /dev/null +++ b/hwilib/ipc/schema/README.md @@ -0,0 +1,18 @@ +# Bitcoin Core IPC schemas + +These are stripped copies of Bitcoin Core's Cap'n Proto schemas +(`src/ipc/capnp/init.capnp` and `src/ipc/capnp/signer.capnp`), with the C++ +proxy generation annotations (`$Proxy.*`, `$Cxx.*`) removed and, for +`init.capnp`, all methods HWI does not call reduced to stubs. + +This works because Cap'n Proto derives an interface's type ID from the file ID +and the declaration's name, and dispatches calls on the interface ID, method +ordinal and positional parameter layout. As long as these files keep the same +file IDs, the same top-level declaration names, and the same ordinals and +field layouts for the methods actually used, they interoperate with the +schemas compiled into `bitcoin-node`. + +When updating: keep the `@0x...` file IDs identical to Bitcoin Core's, never +renumber fields or methods, and never call the stub methods. If this scheme +ever causes trouble, the fallback is to vendor Bitcoin Core's schema files +verbatim (plus libmultiprocess's `mp/proxy.capnp`, which they import). diff --git a/hwilib/ipc/schema/init.capnp b/hwilib/ipc/schema/init.capnp new file mode 100644 index 000000000..8dccd98f5 --- /dev/null +++ b/hwilib/ipc/schema/init.capnp @@ -0,0 +1,18 @@ +# Stripped copy of Bitcoin Core's src/ipc/capnp/init.capnp with only the +# method HWI calls. See README.md in this directory. + +@0xf2c5cfa319406aa6; + +using Signer = import "signer.capnp"; + +interface Init { + # Methods @0-@4 exist only to keep registerExternalSigner at its real + # ordinal; their signatures are stubbed out and they must never be called. + construct @0 () -> (); + makeEcho @1 () -> (); + makeMiningOld2 @2 () -> (); + makeMining @3 () -> (); + makeRpc @4 () -> (); + + registerExternalSigner @5 (signer :Signer.ExternalSignerService) -> (); +} diff --git a/hwilib/ipc/schema/signer.capnp b/hwilib/ipc/schema/signer.capnp new file mode 100644 index 000000000..8616372f9 --- /dev/null +++ b/hwilib/ipc/schema/signer.capnp @@ -0,0 +1,16 @@ +# Stripped copy of Bitcoin Core's src/ipc/capnp/signer.capnp, without the +# C++ proxy generation annotations. See README.md in this directory. + +@0x9d361755b28835a4; + +struct SignerInfo { + fingerprint @0 :Text; + name @1 :Text; +} + +interface ExternalSignerService { + enumerate @0 (chain :Text) -> (result :List(SignerInfo)); + getDescriptors @1 (fingerprint :Text, chain :Text, account :Int32) -> (receive :List(Text), internal :List(Text), error :Text, result :Bool); + displayAddress @2 (fingerprint :Text, chain :Text, descriptor :Text) -> (address :Text, error :Text, result :Bool); + signTransaction @3 (fingerprint :Text, chain :Text, psbt :Data) -> (signedPsbt :Data, error :Text, result :Bool); +} diff --git a/mypy.ini b/mypy.ini index 459e5c338..f3a929a76 100644 --- a/mypy.ini +++ b/mypy.ini @@ -14,6 +14,12 @@ warn_return_any = True implicit_reexport = True strict_equality = True +[mypy-hwilib._ipc] +follow_imports = skip + +[mypy-capnp] +ignore_missing_imports = True + [mypy-hwilib.devices.ckcc.*] follow_imports = skip diff --git a/poetry.lock b/poetry.lock index 2d42a2ea7..61b54dcc8 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 2.4.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 2.4.2 and should not be changed by hand. [[package]] name = "alabaster" @@ -586,11 +586,12 @@ version = "3.1.6" description = "A very fast and expressive template engine." optional = false python-versions = ">=3.7" -groups = ["dev"] +groups = ["main", "dev"] files = [ {file = "jinja2-3.1.6-py3-none-any.whl", hash = "sha256:85ece4451f492d0c13c5dd7c13a64681a86afae63a5f347908daf103ce6d2f67"}, {file = "jinja2-3.1.6.tar.gz", hash = "sha256:0137fb05990d35f1275a587e9aee6d56da821fc83491a0fb838183be43f66d6d"}, ] +markers = {main = "extra == \"ipc\""} [package.dependencies] MarkupSafe = ">=2.0" @@ -634,7 +635,7 @@ version = "3.0.3" description = "Safely add untrusted strings to HTML/XML markup." optional = false python-versions = ">=3.9" -groups = ["dev"] +groups = ["main", "dev"] files = [ {file = "markupsafe-3.0.3-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:2f981d352f04553a7171b8e44369f2af4055f888dfb147d55e42d29e29e74559"}, {file = "markupsafe-3.0.3-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:e1c1493fb6e50ab01d20a22826e57520f1284df32f2d8601fdd90b6304601419"}, @@ -726,6 +727,7 @@ files = [ {file = "markupsafe-3.0.3-cp39-cp39-win_arm64.whl", hash = "sha256:38664109c14ffc9e7437e86b4dceb442b0096dfe3541d7864d9cbe1da4cf36c8"}, {file = "markupsafe-3.0.3.tar.gz", hash = "sha256:722695808f4b6457b320fdc131280796bdceb04ab50fe1795cd540799ebe1698"}, ] +markers = {main = "extra == \"ipc\""} [[package]] name = "mccabe" @@ -883,6 +885,119 @@ files = [ {file = "pyaes-1.6.1.tar.gz", hash = "sha256:02c1b1405c38d3c370b085fb952dd8bea3fadcee6411ad99f312cc129c536d8f"}, ] +[[package]] +name = "pycapnp" +version = "2.2.4" +description = "A cython wrapping of the C++ Cap'n Proto library" +optional = true +python-versions = ">=3.9" +groups = ["main"] +markers = "extra == \"ipc\"" +files = [ + {file = "pycapnp-2.2.4-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:c4c503cbf4b9f19fe9d289201e32fa4e2d979a2b01bdcdb8b370f13a5c301f0a"}, + {file = "pycapnp-2.2.4-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:d0cb4ba94a9ac544f7db1b066402c0f00adbf77d047d19ba6a451e9f8b4dfd33"}, + {file = "pycapnp-2.2.4-cp310-cp310-manylinux_2_28_aarch64.whl", hash = "sha256:4da9b6f9739fda6ab15200970597e6d7703534aa828d68660406a15801be5aa4"}, + {file = "pycapnp-2.2.4-cp310-cp310-manylinux_2_28_i686.whl", hash = "sha256:40088d6d781d3a808ebd976f9aba711b1f7b05dcbc24cad39715062505ec1471"}, + {file = "pycapnp-2.2.4-cp310-cp310-manylinux_2_28_ppc64le.whl", hash = "sha256:f8c0954835a712f0fd464cd448669fbdd56c95d9eeb201be8111763992452405"}, + {file = "pycapnp-2.2.4-cp310-cp310-manylinux_2_28_s390x.whl", hash = "sha256:584edadcc15379fbafbe6924c3c554535884e8983403d200e2f14f13006decad"}, + {file = "pycapnp-2.2.4-cp310-cp310-manylinux_2_28_x86_64.whl", hash = "sha256:d59c387782a61af813ef8fef0e74f75b38a3bce5be54bfacd266d88667b1b07c"}, + {file = "pycapnp-2.2.4-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:e1c85bc04365cfbd5430001a3d482a1babe546f75f7e5a2cd33dd2667395b86b"}, + {file = "pycapnp-2.2.4-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:55ab10f98146368ad22b59b67811e0425e5c266ba869e2f4307c5b83be9f91c0"}, + {file = "pycapnp-2.2.4-cp310-cp310-musllinux_1_2_ppc64le.whl", hash = "sha256:4e9953c0db832567e5dc7f75be1313729bb2efa8977198701e7b70771270e528"}, + {file = "pycapnp-2.2.4-cp310-cp310-musllinux_1_2_s390x.whl", hash = "sha256:51b382fac364146b87df3f43788a44d38c791dd8e83481371e994eea4426aec1"}, + {file = "pycapnp-2.2.4-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:fa093ddcbc83ec374168c30369762725eb5fa390ac2c680753e6cea9a5611dc5"}, + {file = "pycapnp-2.2.4-cp310-cp310-win32.whl", hash = "sha256:4ad979e26dcc07e4c09f7e33ffe06cbc6e7e918f170e0e90a4448fbdda3770e9"}, + {file = "pycapnp-2.2.4-cp310-cp310-win_amd64.whl", hash = "sha256:d7caab6e296c255af89774f09fd9baacc8b02dd770a0eb32193d75d8b97e9d1c"}, + {file = "pycapnp-2.2.4-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:b246680666335e95086489ad34c71a53436dd6e70b00a042377192176acb5ff4"}, + {file = "pycapnp-2.2.4-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:7d7df5d9a3c0ef0cbd4ca98453ea497a621fd362e6114299dd494193b26c8733"}, + {file = "pycapnp-2.2.4-cp311-cp311-manylinux_2_28_aarch64.whl", hash = "sha256:780cd9207ac5eb1baad5013d4bc0e4ba67235cdc054e01ec4d6ba395de42ce6a"}, + {file = "pycapnp-2.2.4-cp311-cp311-manylinux_2_28_i686.whl", hash = "sha256:0d7691b608c22fb3e6b737d1853efdba7bb86eeef06f2e025f4f6105d9c004a6"}, + {file = "pycapnp-2.2.4-cp311-cp311-manylinux_2_28_ppc64le.whl", hash = "sha256:e63373c00b7c47d0b6be332d5fa0ea12f03d8fc53e3c1b5c54f23e739854b5cd"}, + {file = "pycapnp-2.2.4-cp311-cp311-manylinux_2_28_s390x.whl", hash = "sha256:d4ce157c11877ad7dd23f71af47a66136eb848b562787bf62125227a15052389"}, + {file = "pycapnp-2.2.4-cp311-cp311-manylinux_2_28_x86_64.whl", hash = "sha256:7055771afef9fdfabedcabcde0c11938c016779cd512d93e3bc04d6ac6dd65d2"}, + {file = "pycapnp-2.2.4-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:767ce1fcf433c2e56dff5af538048ad75122971576bfd89c7c2566d12504d0ab"}, + {file = "pycapnp-2.2.4-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:69531f93a7a2e4206caad035ce20958f38cd2c21c12ccd65cce690cbe2edc061"}, + {file = "pycapnp-2.2.4-cp311-cp311-musllinux_1_2_ppc64le.whl", hash = "sha256:514dbfb866a1dcbac3efc22296b44977aebaf43cb71be8b6ea3bc70b19b5c6cd"}, + {file = "pycapnp-2.2.4-cp311-cp311-musllinux_1_2_s390x.whl", hash = "sha256:796828ce481bdbe41603ccc9bf68ca837925f0ba132e426ae69f8af1158061e5"}, + {file = "pycapnp-2.2.4-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:31c3a9bf6a35b0d640eabdb58f804253fd6037658d19f3cd1c4437c25a574267"}, + {file = "pycapnp-2.2.4-cp311-cp311-win32.whl", hash = "sha256:f892295724bb979115de689876c181dabc6b719b01d896af5d3269e710ccb526"}, + {file = "pycapnp-2.2.4-cp311-cp311-win_amd64.whl", hash = "sha256:7a2e55f73ed816c0d42454f4a68fc2fc7f9d1598afaf53625e0a592932e96b83"}, + {file = "pycapnp-2.2.4-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:5ce72f99b089966b27e43a95911f2f448c2167ed9e56355b626423ab197c729e"}, + {file = "pycapnp-2.2.4-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:e54b87f5e2ecae92a6288fd0c25efff3bf3655c65cda710045bf02dd0226975d"}, + {file = "pycapnp-2.2.4-cp312-cp312-manylinux_2_28_aarch64.whl", hash = "sha256:ef67d39da054f0b2ff4b69a4c6e20e993246a33c57f45f2b2f323df2ea4c7d5a"}, + {file = "pycapnp-2.2.4-cp312-cp312-manylinux_2_28_i686.whl", hash = "sha256:ad8d314b92301c76839a09c6924d949d65686938250aef9569081ece3776ac04"}, + {file = "pycapnp-2.2.4-cp312-cp312-manylinux_2_28_ppc64le.whl", hash = "sha256:7f65558d2da2df1bff585f204007c9bf9c8492bf9d47041f31041f41991cf242"}, + {file = "pycapnp-2.2.4-cp312-cp312-manylinux_2_28_s390x.whl", hash = "sha256:3e78f352005b731d475e0589d70dc6e0070d0e8cc5cd5e2d8b45222286413145"}, + {file = "pycapnp-2.2.4-cp312-cp312-manylinux_2_28_x86_64.whl", hash = "sha256:80e12e4713dca8683188aae7dd883b25d9175c1b41455af2b5d5a35dd0d3c42a"}, + {file = "pycapnp-2.2.4-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:f76823b5ce17ab91d3c69849d906ad872b2615167c21e436fa432f84373d3f46"}, + {file = "pycapnp-2.2.4-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:a0ecb2636231c27f0551a4b7df92384fdf0637ca5b2fad996e9450aedc4f2447"}, + {file = "pycapnp-2.2.4-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:a22d39d36c6c07cd0be934b7c82999faa8d38e16a6808d5ee7d88e4a84c7ef95"}, + {file = "pycapnp-2.2.4-cp312-cp312-musllinux_1_2_s390x.whl", hash = "sha256:70a6bc1951ca1b7579bc053bef9d97ee72902329ee9eccf21533881a3b051b9d"}, + {file = "pycapnp-2.2.4-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:24003799334310d7760d0bd5896773051afa93c28b17c5e5fd2ec2c9f6df50cc"}, + {file = "pycapnp-2.2.4-cp312-cp312-win32.whl", hash = "sha256:4d50c983c2564e4fee209a9de44fee30009e15cf1af788d38ed0caf27b5c8587"}, + {file = "pycapnp-2.2.4-cp312-cp312-win_amd64.whl", hash = "sha256:bdcf2c04bbf92b075581c8325708cce3093d073945057c9ab5bb6a80f74b06bb"}, + {file = "pycapnp-2.2.4-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:f001ec86321ee9e6a8f86ff03fbd76e0de2d1d0518e83f3901cb5b7111d9460c"}, + {file = "pycapnp-2.2.4-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:b513e7ba63b8d2a4832d3dc1d683bfbb28df1e34d79eabbbbaf08ba058fb9afb"}, + {file = "pycapnp-2.2.4-cp313-cp313-manylinux_2_28_aarch64.whl", hash = "sha256:27db8ec75f252337612db30af95a0e12a0a20afdb9ae4f51c7086dea1e626f77"}, + {file = "pycapnp-2.2.4-cp313-cp313-manylinux_2_28_i686.whl", hash = "sha256:b6f2f09684b96dbd909423b83b3623c61bad40f88ab834470c7347e444f143bc"}, + {file = "pycapnp-2.2.4-cp313-cp313-manylinux_2_28_ppc64le.whl", hash = "sha256:c270456d650c7bd19654b6edd171bbda0e4de76d3c243be1661e6ef643c3278a"}, + {file = "pycapnp-2.2.4-cp313-cp313-manylinux_2_28_s390x.whl", hash = "sha256:ddb3a39941af3bb23ca598f1f9e43246e7e212937c53ba3cc4b5dcfb7419fe56"}, + {file = "pycapnp-2.2.4-cp313-cp313-manylinux_2_28_x86_64.whl", hash = "sha256:3d5444575a2f9c2f1cf50c99ef08d9a76dde47a3a13fdf02909fe6c6e1b69901"}, + {file = "pycapnp-2.2.4-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:181326bf648a65398eeb9144aa8c55dbc0b8c58c7991d3f91fa60275591648e1"}, + {file = "pycapnp-2.2.4-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:11eb14153884d19771aa7e92e8601d084e6655cacfcae1441c165953d2b8ce3b"}, + {file = "pycapnp-2.2.4-cp313-cp313-musllinux_1_2_ppc64le.whl", hash = "sha256:517d812d53d07e800c8ce9fb156b027f208a0b0a62e725bf85a2f3d051057dfb"}, + {file = "pycapnp-2.2.4-cp313-cp313-musllinux_1_2_s390x.whl", hash = "sha256:2a32cc049ccf59f4c7390197e66609f87a4b979fe922fca8328a6dabb8c644d9"}, + {file = "pycapnp-2.2.4-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:6724b5665f6a1adb8f7bc574f3b223a1f21c07294040fbd4e08885b754f24c45"}, + {file = "pycapnp-2.2.4-cp313-cp313-win32.whl", hash = "sha256:5a90625741062a28d859b1694903b4c84f3e70023661839a3c73635ff67a5e47"}, + {file = "pycapnp-2.2.4-cp313-cp313-win_amd64.whl", hash = "sha256:99fbd0d096cafd63c63edf0be6f82214c1b390f9f61d686146518c971e59eefc"}, + {file = "pycapnp-2.2.4-cp314-cp314-macosx_10_15_x86_64.whl", hash = "sha256:eae0d12142e529bdca858ae29cdc3969314a17e64e7256fb88e82c684846ac1a"}, + {file = "pycapnp-2.2.4-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:986ec18eeeff3ede2a0d82a4b77ba18c318a26514f49d705491d4342b0d04d3d"}, + {file = "pycapnp-2.2.4-cp314-cp314-manylinux_2_28_aarch64.whl", hash = "sha256:35d9ac936938783cc4723c1e0a7fa2bdbe5d76803f2f2ba167d1bb070108830c"}, + {file = "pycapnp-2.2.4-cp314-cp314-manylinux_2_28_i686.whl", hash = "sha256:38af0a432c5deaf9ad5efb9ec1d2c496eba78cc96a70f896c2baf76de2a1d7b8"}, + {file = "pycapnp-2.2.4-cp314-cp314-manylinux_2_28_ppc64le.whl", hash = "sha256:40066e7ea9c3d388e2c75d65b10fdde50d69a49eed18ece9044f9cf8fdacf54c"}, + {file = "pycapnp-2.2.4-cp314-cp314-manylinux_2_28_s390x.whl", hash = "sha256:411ada8107419076409ffa9d176891f08611550b17e2b6c684eef3ff31e092fc"}, + {file = "pycapnp-2.2.4-cp314-cp314-manylinux_2_28_x86_64.whl", hash = "sha256:45bd2a2eba74f94c96a532af1f8b944fafc5ab74483ed66c2d88bf6606e5a126"}, + {file = "pycapnp-2.2.4-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:d4221a2abd7bd73e3b6c9fe9484bcdb658d79d2b6174b6ce45cd0cfc6980adfe"}, + {file = "pycapnp-2.2.4-cp314-cp314-musllinux_1_2_i686.whl", hash = "sha256:aa40eb5f44b9f62ad99e7df200a3a024ac3272cdc7d2c3ac3bb74c64f89178ba"}, + {file = "pycapnp-2.2.4-cp314-cp314-musllinux_1_2_ppc64le.whl", hash = "sha256:88971e8a4f0c433872ad156ec2af2ea104727708aed9007a3e4100ae47ca2538"}, + {file = "pycapnp-2.2.4-cp314-cp314-musllinux_1_2_s390x.whl", hash = "sha256:64373985fa3a4660e14ec81671e927b422cc4b21ff6f1790287fa1b7620e86a9"}, + {file = "pycapnp-2.2.4-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:6e9212cd6ec0976071c252608b8f96966f7ac33f9a29af72c594ace662514c9f"}, + {file = "pycapnp-2.2.4-cp314-cp314-win32.whl", hash = "sha256:7608f8b737fe23b6bf72d06917c10f40b85a835581f98f62cefa5332791a3336"}, + {file = "pycapnp-2.2.4-cp314-cp314-win_amd64.whl", hash = "sha256:a3eb38d9b5e81b8ac0b21ee3626c5146cbf675580842cd07039f74d53f4156a4"}, + {file = "pycapnp-2.2.4-cp314-cp314t-macosx_10_15_x86_64.whl", hash = "sha256:1c0fa292ef30ef429bbf12cae3cacae9750f5dca23a1e81f802bf511add50d49"}, + {file = "pycapnp-2.2.4-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:13297222f4a71ef776a5897c9c84881fd1f16d8cb864e9673e05562cae67166d"}, + {file = "pycapnp-2.2.4-cp314-cp314t-manylinux_2_28_aarch64.whl", hash = "sha256:03ff197e3e6e737dbf3748d4d735e4ae778cf4fd16f80ef0bf4f63007903e13a"}, + {file = "pycapnp-2.2.4-cp314-cp314t-manylinux_2_28_i686.whl", hash = "sha256:ddf8d60b9e698690bca03ffd0c839853151640d7eb3f6728da458e40c2a0c894"}, + {file = "pycapnp-2.2.4-cp314-cp314t-manylinux_2_28_ppc64le.whl", hash = "sha256:8164b020646fb9a366d4976232326fae42c01834bfecf2c5251db23aa9c21695"}, + {file = "pycapnp-2.2.4-cp314-cp314t-manylinux_2_28_s390x.whl", hash = "sha256:bd84c96509cadfea3c127c9928bf008dc21124f53aed8aaae7f16e08f28d79d5"}, + {file = "pycapnp-2.2.4-cp314-cp314t-manylinux_2_28_x86_64.whl", hash = "sha256:b5e635e356c68b91381561753c46cf23c59388d13f784e5588685def16ab99ba"}, + {file = "pycapnp-2.2.4-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:96f1566ef6ff0f756c413ad4d9b6e2d49c4d1c1d60c310e14fb0143ba9d23a6f"}, + {file = "pycapnp-2.2.4-cp314-cp314t-musllinux_1_2_i686.whl", hash = "sha256:4b86c6f92a63ec07e7a365d8ebfacec180c7f862a77ed25409f13f852e681578"}, + {file = "pycapnp-2.2.4-cp314-cp314t-musllinux_1_2_ppc64le.whl", hash = "sha256:a4e4e091cf578f0c4b60059c8c181f30e34e35b6b74cb67af4ec49c906f52746"}, + {file = "pycapnp-2.2.4-cp314-cp314t-musllinux_1_2_s390x.whl", hash = "sha256:3b52d5fbdf82a56e9fabe84ec3c9574b5efacf9206c1d6f32397f39ad14d8038"}, + {file = "pycapnp-2.2.4-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:f66d61e9c5a97a1d2902b81d5d002fd9e052387e5355e1d988e28e00c3de8223"}, + {file = "pycapnp-2.2.4-cp314-cp314t-win32.whl", hash = "sha256:87ec866daf3368e424c911b571faae720e02ac7ac7ac4c9d32d4937a48d5b22e"}, + {file = "pycapnp-2.2.4-cp314-cp314t-win_amd64.whl", hash = "sha256:fbf5e9b3fb43300569a04d015355a3462becb421275a6bd9bf19cb304198e4ad"}, + {file = "pycapnp-2.2.4-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:026074658a2b5da82bfed604e5d3e59979b43c4bac11197d1f3bc8758416e07c"}, + {file = "pycapnp-2.2.4-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:6f05ea55c89ad197ba39e62da36145a907bdbba03711e0101b118f26986a8375"}, + {file = "pycapnp-2.2.4-cp39-cp39-manylinux_2_28_aarch64.whl", hash = "sha256:31d1fcebd926c280378000d46f16c7637ce0fdb9c74425c074f6ad532f957e18"}, + {file = "pycapnp-2.2.4-cp39-cp39-manylinux_2_28_i686.whl", hash = "sha256:3c92225f7be587b483cab72206e78f0a5a70884e25de1dd1cd9d46be75e53aeb"}, + {file = "pycapnp-2.2.4-cp39-cp39-manylinux_2_28_ppc64le.whl", hash = "sha256:ba0b2fb98426e67b14199c42efc7cf8f1b5f240800d3e6f0874e9be08ec95aae"}, + {file = "pycapnp-2.2.4-cp39-cp39-manylinux_2_28_s390x.whl", hash = "sha256:93ea4c25e61ee9a4308efefb1baf692ba25085038c17864b3ae4eb29c30f6568"}, + {file = "pycapnp-2.2.4-cp39-cp39-manylinux_2_28_x86_64.whl", hash = "sha256:0a8e1e18d5e7e323876c82d793b67d4e6fc6d79ff8e71c40a083dc346a1d897f"}, + {file = "pycapnp-2.2.4-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:675f38a9b55bb69763755d844c196ebccba311872b800cd2f0428912b7a31415"}, + {file = "pycapnp-2.2.4-cp39-cp39-musllinux_1_2_i686.whl", hash = "sha256:da740b16208730c14941fe83a317e601e4f301694155bd6d1462a76f60040dfc"}, + {file = "pycapnp-2.2.4-cp39-cp39-musllinux_1_2_ppc64le.whl", hash = "sha256:d496a669999fae0fabc1a3f1d851606fabb3dd5d858fa6204ddcb402ad2ba18b"}, + {file = "pycapnp-2.2.4-cp39-cp39-musllinux_1_2_s390x.whl", hash = "sha256:d9aacfff1391c95ec0d07f5e23f46ce935d3a669452948b0d33d3a5549a6e54f"}, + {file = "pycapnp-2.2.4-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:8d1afe4f5005404e4a57f1379771e48b9a2d6a4ea7ab5b6e5a19cd8f6a6964b9"}, + {file = "pycapnp-2.2.4-cp39-cp39-win32.whl", hash = "sha256:b72a6f2e23b8c8bb4ea5e7e122cb0d3b80565a4dadbc88d3b34642a2ddb7e75e"}, + {file = "pycapnp-2.2.4-cp39-cp39-win_amd64.whl", hash = "sha256:7440087acc945977388b6328819f43c85ead4eeff033739cdbf717b043846838"}, + {file = "pycapnp-2.2.4.tar.gz", hash = "sha256:a598bd3952de76a803a53b845c8c300828338b949946d49f8af716328cda0a1a"}, +] + +[package.dependencies] +jinja2 = "*" + [[package]] name = "pycodestyle" version = "2.14.0" @@ -1416,9 +1531,10 @@ socks = ["pysocks (>=1.5.6,!=1.5.7,<2.0)"] zstd = ["backports-zstd (>=1.0.0) ; python_version < \"3.14\""] [extras] +ipc = ["pycapnp"] qt = ["pyside2"] [metadata] lock-version = "2.1" python-versions = ">=3.10,<3.16" -content-hash = "b8f79f8e4f03ac26913c912846f0c5eb6c2e70535cee0a2cc769082d5de66895" +content-hash = "c73db63661748e5e35748016ab8a96e7a85617260519a7799a4bdd26bce81079" diff --git a/pyproject.toml b/pyproject.toml index b41592b44..2480a331c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,9 @@ dependencies = [ qt = [ "pyside2 (>=5.15.2.1,<6.0) ; python_version < '3.11'", ] +ipc = [ + "pycapnp (>=2.0.0,<3.0.0)", +] [dependency-groups] dev = [ diff --git a/test/run_tests.py b/test/run_tests.py index 28d8f7a95..f9dde462b 100755 --- a/test/run_tests.py +++ b/test/run_tests.py @@ -5,6 +5,7 @@ import unittest from test_base58 import TestBase58 +from test_ipc import TestDefaultSocketPath from test_bech32 import TestSegwitAddress from test_bip32 import TestBIP32 from test_coldcard import coldcard_test_suite, TestColdcardFirmware @@ -73,6 +74,8 @@ parser.add_argument('--all', help='Run tests on all existing simulators', default=False, action='store_true') parser.add_argument('--bitcoind', help='Path to bitcoind', default='work/bitcoin/build/bin/bitcoind') parser.add_argument('--interface', help='Which interface to send commands over', choices=['library', 'cli', 'bindist', 'stdin'], default='library') +parser.add_argument('--core-ipc', dest='core_ipc', help='Also run the Bitcoin Core IPC signer test for each enabled device (needs the bitcoin-node binary next to bitcoind and pycapnp installed)', default=False, action='store_true') +parser.add_argument('--core-ipc-only', dest='core_ipc_only', help='Run only the Bitcoin Core IPC signer test, skipping the other device tests. Implies --core-ipc', default=False, action='store_true') parser.add_argument("--device-only", help="Only run device tests", action="store_true") @@ -80,6 +83,9 @@ args = parser.parse_args() +if args.core_ipc_only: + args.core_ipc = True + # Run tests success = True suite = unittest.TestSuite() @@ -89,6 +95,7 @@ suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestPSBT)) suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestBase58)) suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestBIP32)) + suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestDefaultSocketPath)) suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestColdcardFirmware)) if sys.platform.startswith("linux"): suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(TestUdevRulesInstaller)) @@ -120,28 +127,72 @@ args.bitbox02 = False if args.bitbox02 is None else args.bitbox02 if args.trezor_1 or args.trezor_t or args.coldcard or args.coldcard_edge or args.ledger or args.ledger_legacy or args.keepkey or args.bitbox01 or args.jade or args.bitbox02: - # Start bitcoind - bitcoind = Bitcoind.create(args.bitcoind) - - if success and args.bitbox01: - success &= digitalbitbox_test_suite(args.bitbox01_path, bitcoind, args.interface) - if success and args.coldcard: - success &= coldcard_test_suite(args.coldcard_path, bitcoind, args.interface) - if success and args.coldcard_edge: - success &= coldcard_test_suite(args.coldcard_edge_path, bitcoind, args.interface, is_edge=True) - if success and args.trezor_1: - success &= trezor_test_suite(args.trezor_1_path, bitcoind, args.interface, '1') - if success and args.trezor_t: - success &= trezor_test_suite(args.trezor_t_path, bitcoind, args.interface, 't') - if success and args.keepkey: - success &= keepkey_test_suite(args.keepkey_path, bitcoind, args.interface) - if success and args.ledger: - success &= ledger_test_suite(args.ledger_path, bitcoind, args.interface, False) - if success and args.ledger_legacy: - success &= ledger_test_suite(args.ledger_path, bitcoind, args.interface, True) - if success and args.jade: - success &= jade_test_suite(args.jade_path, bitcoind, args.interface) - if success and args.bitbox02: - success &= bitbox02_test_suite(args.bitbox02_path, bitcoind, args.interface) + # The Core IPC signer test starts its own bitcoin-node, so with + # --core-ipc-only the shared bitcoind and the regular device test + # suites are skipped. + if not args.core_ipc_only: + # Start bitcoind + bitcoind = Bitcoind.create(args.bitcoind) + + if success and args.bitbox01: + success &= digitalbitbox_test_suite(args.bitbox01_path, bitcoind, args.interface) + if success and args.coldcard: + success &= coldcard_test_suite(args.coldcard_path, bitcoind, args.interface) + if success and args.coldcard_edge: + success &= coldcard_test_suite(args.coldcard_edge_path, bitcoind, args.interface, is_edge=True) + if success and args.trezor_1: + success &= trezor_test_suite(args.trezor_1_path, bitcoind, args.interface, '1') + if success and args.trezor_t: + success &= trezor_test_suite(args.trezor_t_path, bitcoind, args.interface, 't') + if success and args.keepkey: + success &= keepkey_test_suite(args.keepkey_path, bitcoind, args.interface) + if success and args.ledger: + success &= ledger_test_suite(args.ledger_path, bitcoind, args.interface, False) + if success and args.ledger_legacy: + success &= ledger_test_suite(args.ledger_path, bitcoind, args.interface, True) + if success and args.jade: + success &= jade_test_suite(args.jade_path, bitcoind, args.interface) + if success and args.bitbox02: + success &= bitbox02_test_suite(args.bitbox02_path, bitcoind, args.interface) + + if success and args.core_ipc: + import os + from test_core_ipc import core_ipc_test_suite + from test_coldcard import ColdcardSimulator + from test_trezor import TrezorEmulator + from test_ledger import LedgerEmulator + from test_keepkey import KeepkeyEmulator + from test_jade import JadeEmulator + from test_bitbox02 import BitBox02Emulator + from test_digitalbitbox import BitBox01Emulator + + bitcoin_node_path = os.path.join(os.path.dirname(args.bitcoind), 'bitcoin-node') + + core_ipc_emulators = [] + if args.bitbox01: + core_ipc_emulators.append(BitBox01Emulator(args.bitbox01_path)) + if args.coldcard: + core_ipc_emulators.append(ColdcardSimulator(args.coldcard_path, is_edge=False)) + if args.coldcard_edge: + core_ipc_emulators.append(ColdcardSimulator(args.coldcard_edge_path, is_edge=True)) + if args.trezor_1: + core_ipc_emulators.append(TrezorEmulator(args.trezor_1_path, '1')) + if args.trezor_t: + core_ipc_emulators.append(TrezorEmulator(args.trezor_t_path, 't')) + if args.keepkey: + core_ipc_emulators.append(KeepkeyEmulator(args.keepkey_path)) + if args.ledger: + core_ipc_emulators.append(LedgerEmulator(args.ledger_path, False)) + if args.ledger_legacy: + core_ipc_emulators.append(LedgerEmulator(args.ledger_path, True)) + if args.jade: + core_ipc_emulators.append(JadeEmulator(args.jade_path)) + if args.bitbox02: + core_ipc_emulators.append(BitBox02Emulator(args.bitbox02_path)) + + for emulator in core_ipc_emulators: + if not success: + break + success &= core_ipc_test_suite(emulator, bitcoin_node_path, args.interface) sys.exit(not success) diff --git a/test/setup_environment.sh b/test/setup_environment.sh index bec5755b3..8fbd32346 100755 --- a/test/setup_environment.sh +++ b/test/setup_environment.sh @@ -452,15 +452,20 @@ if [[ -n ${build_bitbox02} ]]; then fi if [[ -n ${build_bitcoind} ]]; then + # Repository and branch can be overridden, e.g. to test against a Bitcoin + # Core branch that is not merged yet (like the IPC signer support). + BITCOIN_REPO_URL=${BITCOIN_REPO_URL:-https://github.com/bitcoin/bitcoin.git} + BITCOIN_REPO_BRANCH=${BITCOIN_REPO_BRANCH:-master} + # Clone bitcoind if it doesn't exist, or update it if it does bitcoind_setup_needed=false if [ ! -d "bitcoin" ]; then - git clone https://github.com/bitcoin/bitcoin.git + git clone --branch "${BITCOIN_REPO_BRANCH}" "${BITCOIN_REPO_URL}" cd bitcoin bitcoind_setup_needed=true else cd bitcoin - git reset --hard origin/master + git reset --hard "origin/${BITCOIN_REPO_BRANCH}" git fetch # Determine if we need to pull. From https://stackoverflow.com/a/3278427 @@ -484,5 +489,6 @@ if [[ -n ${build_bitcoind} ]]; then # Do the build cmake -B build --toolchain depends/x86_64-pc-linux-gnu/toolchain.cmake -DBUILD_TESTS=OFF -DBUILD_BENCH=OFF - cmake --build build -j $(nproc) --target bitcoind + # bitcoin-node is the multiprocess variant, used by the Core IPC signer test + cmake --build build -j $(nproc) --target bitcoind bitcoin-node fi diff --git a/test/test_core_ipc.py b/test/test_core_ipc.py new file mode 100644 index 000000000..0cd2e7e88 --- /dev/null +++ b/test/test_core_ipc.py @@ -0,0 +1,211 @@ +#! /usr/bin/env python3 +"""Bitcoin Core integration test for the IPC signer service. + +Starts a multiprocess bitcoin-node with an IPC socket, runs `hwi ipc` as a +long-lived process registered as the node's external signer, and drives the +device entirely through Bitcoin Core RPCs: enumeratesigners, wallet creation +with external_signer=true (getdescriptors), walletdisplayaddress and send +(signtx). See Bitcoin Core's doc/external-signer.md. +""" + +import atexit +import os +import re +import subprocess +import sys +import time +import unittest + +from test_device import Bitcoind, DeviceTestCase + +from authproxy import AuthServiceProxy, JSONRPCException +from hwilib import _bech32 as bech32 + +# Address type per descriptor prefix, in HWI getdescriptors output order +DESC_ADDRESS_TYPES = [ + ("sh(wpkh(", "p2sh-segwit"), + ("pkh(", "legacy"), + ("wpkh(", "bech32"), + ("tr(", "bech32m"), +] + + +class BitcoinNode(Bitcoind): + """A regtest bitcoin-node with an IPC socket for signer registration.""" + + def __init__(self, path): + super().__init__(path) + self.ipc_socket_path = os.path.join(self.datadir, "regtest", "node.sock") + self.extra_args = [f"-ipcbind=unix:{self.ipc_socket_path}"] + + +class TestCoreSignerIPC(DeviceTestCase): + def daemon_command(self): + """How to launch the signer daemon, following the same interface + convention as DeviceTestCase.do_command, so this test can run + against an installed or frozen HWI rather than the source tree.""" + if self.interface == "cli": + return ["hwi"] + if self.interface == "bindist": + return [os.path.join("..", "dist", "hwi")] + return [sys.executable, os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "hwi.py")] + + def setUp(self): + super().setUp() # starts the emulator + daemon_args = self.daemon_command() + ["--emulators", "--debug"] + daemon_args.extend(self.get_password_args()) + daemon_args.extend(["ipc", "--socket-path", self.bitcoind.ipc_socket_path]) + self.daemon_stdout = open("core-ipc-hwi.stdout", "a") + self.daemon_stderr = open("core-ipc-hwi.stderr", "a") + self.daemon = subprocess.Popen(daemon_args, stdout=self.daemon_stdout, stderr=self.daemon_stderr) + atexit.register(self.stop_daemon) + self.wait_for_registration() + + def stop_daemon(self): + if self.daemon.poll() is None: + self.daemon.kill() + self.daemon.wait() + self.daemon_stdout.close() + self.daemon_stderr.close() + + def tearDown(self): + self.stop_daemon() + super().tearDown() # stops the emulator + + def wait_for_registration(self, timeout=30): + """Wait until the daemon has registered and reports our device.""" + deadline = time.time() + timeout + while time.time() < deadline: + if self.daemon.poll() is not None: + raise RuntimeError(f"hwi ipc exited with {self.daemon.poll()}, see core-ipc-hwi.stderr") + try: + signers = self.rpc.enumeratesigners()["signers"] + if any(signer["fingerprint"] == self.emulator.fingerprint for signer in signers): + return + except JSONRPCException: + pass + time.sleep(0.5) + raise RuntimeError("hwi ipc did not register with the node in time") + + def get_wallet_rpc(self, wallet): + # Signing waits for the device, so use a generous timeout to fail + # instead of hanging if a simulator gets stuck. + url = self.bitcoind.rpc_url + f"/wallet/{wallet}" + return AuthServiceProxy(url, timeout=120) + + def device_descriptors(self): + """The device's receive descriptors for regtest, with the address + type Bitcoin Core maps each of them to.""" + result = self.do_command(self.dev_args_regtest() + ["getdescriptors"]) + self.assertNotIn("error", result) + descriptors = [] + for descriptor in result["receive"]: + for prefix, address_type in DESC_ADDRESS_TYPES: + if descriptor.startswith(prefix): + descriptors.append((descriptor, address_type)) + break + return descriptors + + def dev_args_regtest(self): + # Self-contained variant of self.dev_args: the node runs regtest, so + # compare against descriptors for the same chain. + args = ["-t", self.emulator.type, "-d", self.emulator.path, "--chain", "regtest", "--emulators"] + args.extend(self.get_password_args()) + return args + + def create_signer_wallet(self): + wallet_name = f"{self.emulator.type}_{self.id()}" + self.rpc.createwallet(wallet_name=wallet_name, disable_private_keys=True, external_signer=True) + wrpc = self.get_wallet_rpc(wallet_name) + self.assertTrue(wrpc.getwalletinfo()["external_signer"]) + return wrpc + + def test_enumerate(self): + signers = self.rpc.enumeratesigners()["signers"] + self.assertTrue(any(signer["fingerprint"] == self.emulator.fingerprint for signer in signers)) + + def test_create_wallet_and_addresses(self): + wrpc = self.create_signer_wallet() + for descriptor, address_type in self.device_descriptors(): + normalized = self.rpc.getdescriptorinfo(descriptor)["descriptor"] + expected = self.rpc.deriveaddresses(normalized, [0, 0])[0] + self.assertEqual(wrpc.getnewaddress(address_type=address_type), expected) + + def supports_regtest(self): + """The legacy Ledger app (and its HWI client) does not handle the + regtest chain Bitcoin Core reports over IPC: it renders mainnet + addresses and rejects signing with "Bad argument".""" + return not getattr(self.emulator, "legacy", False) + + def supports_display_address(self): + """The original Digital Bitbox has no screen on which to display an + address.""" + return self.emulator.type != "digitalbitbox" + + def test_display_address(self): + if not self.supports_regtest(): + raise unittest.SkipTest("device client does not support regtest") + if not self.supports_display_address(): + raise unittest.SkipTest("device does not have a screen") + wrpc = self.create_signer_wallet() + address = wrpc.getnewaddress(address_type="bech32") + try: + result = wrpc.walletdisplayaddress(address) + self.assertEqual(result, {"address": address}) + except JSONRPCException as e: + # Some devices (e.g. Coldcard) render regtest addresses with the + # testnet HRP, which Core's echo check rejects. Verify the device + # still displayed the same witness program. + match = re.search(r"Signer echoed unexpected address (\S+)", e.error["message"]) + self.assertIsNotNone(match, f"unexpected error: {e}") + echoed = match.group(1) + self.assertEqual(bech32.decode("tb", echoed), bech32.decode("bcrt", address)) + + def test_send(self): + if not self.supports_regtest(): + raise unittest.SkipTest("device client does not support regtest") + wrpc = self.create_signer_wallet() + supply = self.bitcoind.wrpc + address_types = [address_type for _, address_type in self.device_descriptors()] + + # Some devices can derive descriptors for address types they cannot + # sign (the BitBox02 cannot sign legacy inputs). Taproot inputs are + # spent in their own transaction: the Coldcard + # Edge simulator rejects PSBTs mixing taproot with other segwit + # inputs, and when an input remains unsigned the Core wallet + # re-invokes the signer with a partially finalized PSBT, which + # strict devices reject. This mirrors the input type combinations + # the existing HWI signing tests cover. + groups = [] + non_tap = [ + t for t in address_types + if t != "bech32m" and (t != "legacy" or self.emulator.supports_legacy) + ] + if non_tap: + groups.append(non_tap) + if "bech32m" in address_types: + groups.append(["bech32m"]) + + dest = supply.getnewaddress() + for group in groups: + addresses = [wrpc.getnewaddress(address_type=address_type) for address_type in group] + for address in addresses: + supply.sendtoaddress(address, 1) + supply.generatetoaddress(1, supply.getnewaddress()) + utxos = [u for u in wrpc.listunspent() if u["address"] in addresses] + self.assertEqual(len(utxos), len(group)) + inputs = [{"txid": u["txid"], "vout": u["vout"]} for u in utxos] + res = wrpc.send(outputs={dest: len(group) - 0.5}, inputs=inputs, add_inputs=False, add_to_wallet=False) + self.assertTrue(res["complete"]) + self.assertEqual(len(wrpc.decoderawtransaction(res["hex"])["vin"]), len(group)) + self.assertTrue(wrpc.testmempoolaccept([res["hex"]])[0]["allowed"]) + + +def core_ipc_test_suite(emulator, bitcoin_node_path, interface): + node = BitcoinNode.create(bitcoin_node_path) + suite = unittest.TestSuite() + suite.addTest(DeviceTestCase.parameterize(TestCoreSignerIPC, node, emulator=emulator, interface=interface)) + result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(suite) + node.cleanup() + atexit.unregister(node.cleanup) + return result.wasSuccessful() diff --git a/test/test_device.py b/test/test_device.py index 4c09c548a..f4f734a4b 100644 --- a/test/test_device.py +++ b/test/test_device.py @@ -59,8 +59,9 @@ def stop(self): # Class for bitcoind control and RPC class Bitcoind(): - def __init__(self, bitcoind_path): + def __init__(self, bitcoind_path, extra_args=None): self.bitcoind_path = bitcoind_path + self.extra_args = extra_args if extra_args is not None else [] self.datadir = tempfile.mkdtemp() self.rpc = None self.bitcoind_proc = None @@ -89,7 +90,7 @@ def get_free_port(): "-keypool=1", f"-port={self.p2p_port}", f"-rpcport={self.rpc_port}" - ] + ] + self.extra_args ) atexit.register(self.cleanup) @@ -132,9 +133,9 @@ def cleanup(self): self.bitcoind_proc.kill() shutil.rmtree(self.datadir) - @staticmethod - def create(*args, **kwargs): - c = Bitcoind(*args, **kwargs) + @classmethod + def create(cls, *args, **kwargs): + c = cls(*args, **kwargs) c.start() return c diff --git a/test/test_ipc.py b/test/test_ipc.py new file mode 100644 index 000000000..ab0ff773c --- /dev/null +++ b/test/test_ipc.py @@ -0,0 +1,36 @@ +#! /usr/bin/env python3 +"""Unit tests for the Bitcoin Core IPC signer service helpers. + +These need no device and no pycapnp; the device integration test lives in +test_core_ipc.py. +""" + +import unittest +from pathlib import Path +from unittest.mock import patch + +from hwilib import _ipc +from hwilib.common import Chain + + +class TestDefaultSocketPath(unittest.TestCase): + def test_linux(self): + with patch("sys.platform", "linux"), patch.object(Path, "home", return_value=Path("/home/user")): + self.assertEqual(_ipc.default_socket_path(Chain.MAIN), "/home/user/.bitcoin/node.sock") + self.assertEqual(_ipc.default_socket_path(Chain.TEST), "/home/user/.bitcoin/testnet3/node.sock") + self.assertEqual(_ipc.default_socket_path(Chain.REGTEST), "/home/user/.bitcoin/regtest/node.sock") + self.assertEqual(_ipc.default_socket_path(Chain.SIGNET), "/home/user/.bitcoin/signet/node.sock") + self.assertEqual(_ipc.default_socket_path(Chain.TESTNET4), "/home/user/.bitcoin/testnet4/node.sock") + + def test_macos(self): + with patch("sys.platform", "darwin"), patch.object(Path, "home", return_value=Path("/Users/satoshi")): + self.assertEqual(_ipc.default_socket_path(Chain.MAIN), "/Users/satoshi/Library/Application Support/Bitcoin/node.sock") + self.assertEqual(_ipc.default_socket_path(Chain.REGTEST), "/Users/satoshi/Library/Application Support/Bitcoin/regtest/node.sock") + + def test_windows(self): + with patch("sys.platform", "win32"), patch.dict(_ipc.os.environ, {"APPDATA": "C:/Users/satoshi/AppData/Roaming"}): + self.assertEqual(_ipc.default_socket_path(Chain.MAIN), str(Path("C:/Users/satoshi/AppData/Roaming") / "Bitcoin" / "node.sock")) + + +if __name__ == "__main__": + unittest.main()