|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Small client for the Trezor User Env websocket controller. |
| 3 | +
|
| 4 | +The shell helper runs this file inside the User Env container so the host does |
| 5 | +not need a Python websocket package installed. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import asyncio |
| 11 | +import json |
| 12 | +import os |
| 13 | +import sys |
| 14 | +from typing import Any |
| 15 | + |
| 16 | +import websockets |
| 17 | + |
| 18 | + |
| 19 | +CONTROLLER_WS = os.environ.get("TREZOR_CONTROLLER_WS", "ws://127.0.0.1:9001") |
| 20 | +DEFAULT_MNEMONIC = "all all all all all all all all all all all all" |
| 21 | + |
| 22 | + |
| 23 | +def next_id() -> int: |
| 24 | + next_id.value += 1 |
| 25 | + return next_id.value |
| 26 | + |
| 27 | + |
| 28 | +next_id.value = 0 |
| 29 | + |
| 30 | + |
| 31 | +async def send(payload: dict[str, Any], *, allow_failure: bool = False) -> dict[str, Any]: |
| 32 | + async with websockets.connect(CONTROLLER_WS) as websocket: |
| 33 | + await websocket.recv() |
| 34 | + await websocket.send(json.dumps(payload)) |
| 35 | + raw_response = await websocket.recv() |
| 36 | + |
| 37 | + response = json.loads(raw_response) |
| 38 | + print(json.dumps(response, indent=2, sort_keys=True)) |
| 39 | + |
| 40 | + if not allow_failure and not response.get("success", False): |
| 41 | + raise RuntimeError(response.get("error", response)) |
| 42 | + |
| 43 | + return response |
| 44 | + |
| 45 | + |
| 46 | +async def setup() -> None: |
| 47 | + await send( |
| 48 | + { |
| 49 | + "type": "bridge-start", |
| 50 | + "version": os.environ.get("TREZOR_BRIDGE_VERSION", "node-bridge"), |
| 51 | + "id": next_id(), |
| 52 | + } |
| 53 | + ) |
| 54 | + await send( |
| 55 | + { |
| 56 | + "type": "emulator-start", |
| 57 | + "model": os.environ.get("TREZOR_MODEL", "T2T1"), |
| 58 | + "version": os.environ.get("TREZOR_FIRMWARE", "2-main"), |
| 59 | + "wipe": os.environ.get("TREZOR_WIPE", "true").lower() != "false", |
| 60 | + "id": next_id(), |
| 61 | + } |
| 62 | + ) |
| 63 | + await send( |
| 64 | + { |
| 65 | + "type": "emulator-setup", |
| 66 | + "mnemonic": os.environ.get("TREZOR_MNEMONIC", DEFAULT_MNEMONIC), |
| 67 | + "pin": os.environ.get("TREZOR_PIN", ""), |
| 68 | + "passphrase_protection": os.environ.get( |
| 69 | + "TREZOR_PASSPHRASE_PROTECTION", "false" |
| 70 | + ).lower() |
| 71 | + == "true", |
| 72 | + "label": os.environ.get("TREZOR_LABEL", "Bitkit Test Trezor"), |
| 73 | + "needs_backup": os.environ.get("TREZOR_NEEDS_BACKUP", "false").lower() |
| 74 | + == "true", |
| 75 | + "id": next_id(), |
| 76 | + } |
| 77 | + ) |
| 78 | + await status() |
| 79 | + |
| 80 | + |
| 81 | +async def status() -> None: |
| 82 | + await send({"type": "background-check", "id": next_id()}) |
| 83 | + |
| 84 | + |
| 85 | +async def stop() -> None: |
| 86 | + await send({"type": "emulator-stop", "id": next_id()}, allow_failure=True) |
| 87 | + await send({"type": "bridge-stop", "id": next_id()}, allow_failure=True) |
| 88 | + await status() |
| 89 | + |
| 90 | + |
| 91 | +async def raw(payload: str) -> None: |
| 92 | + parsed = json.loads(payload) |
| 93 | + parsed.setdefault("id", next_id()) |
| 94 | + await send(parsed) |
| 95 | + |
| 96 | + |
| 97 | +async def main() -> None: |
| 98 | + command = sys.argv[1] if len(sys.argv) > 1 else "setup" |
| 99 | + |
| 100 | + if command == "ping": |
| 101 | + await send({"type": "ping", "id": next_id()}) |
| 102 | + elif command == "setup": |
| 103 | + await setup() |
| 104 | + elif command == "status": |
| 105 | + await status() |
| 106 | + elif command == "stop": |
| 107 | + await stop() |
| 108 | + elif command == "send-json": |
| 109 | + if len(sys.argv) != 3: |
| 110 | + raise SystemExit("send-json expects one JSON payload argument") |
| 111 | + await raw(sys.argv[2]) |
| 112 | + else: |
| 113 | + raise SystemExit(f"Unknown controller command: {command}") |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + asyncio.run(main()) |
0 commit comments