|
27 | 27 | import textwrap
|
28 | 28 | import json
|
29 | 29 |
|
30 |
| -import requests # type: ignore |
| 30 | +import requests |
31 | 31 | import hid
|
32 | 32 | import semver
|
33 |
| -from tzlocal import get_localzone # type: ignore |
| 33 | +from tzlocal import get_localzone |
34 | 34 |
|
35 | 35 | from bitbox02 import util
|
36 | 36 | from bitbox02 import bitbox02
|
@@ -58,11 +58,11 @@ def eprint(*args: Any, **kwargs: Any) -> None:
|
58 | 58 |
|
59 | 59 |
|
60 | 60 | def ask_user(
|
61 |
| - choices: Sequence[Tuple[str, Callable[[], None]]] |
| 61 | + choices: Sequence[Tuple[str, Callable[[], None]]], |
62 | 62 | ) -> Union[Callable[[], None], bool, None]:
|
63 | 63 | """Ask user to choose one of the choices, q quits"""
|
64 | 64 | print("What would you like to do?")
|
65 |
| - for (idx, choice) in enumerate(choices): |
| 65 | + for idx, choice in enumerate(choices): |
66 | 66 | print(f"- ({idx+1}) {choice[0]}")
|
67 | 67 | print("- (q) Quit")
|
68 | 68 | ans_str = input("")
|
@@ -253,7 +253,7 @@ def _print_backups(self, backups: Optional[Sequence[bitbox02.Backup]] = None) ->
|
253 | 253 | print("No backups found.")
|
254 | 254 | return
|
255 | 255 | fmt = "%Y-%m-%d %H:%M:%S %z"
|
256 |
| - for (i, (backup_id, backup_name, date)) in enumerate(backups): |
| 256 | + for i, (backup_id, backup_name, date) in enumerate(backups): |
257 | 257 | date = local_timezone.localize(date)
|
258 | 258 | date_str = date.strftime(fmt)
|
259 | 259 | print(f"[{i+1}] Backup Name: {backup_name}, Time: {date_str}, ID: {backup_id}")
|
@@ -758,7 +758,7 @@ def _sign_btc_policy(self) -> None:
|
758 | 758 | bip44_account: int = 0 + HARDENED
|
759 | 759 | account_keypath = [48 + HARDENED, 1 + HARDENED, bip44_account, 3 + HARDENED]
|
760 | 760 | inputs, outputs = _btc_demo_inputs_outputs(bip44_account)
|
761 |
| - for (i, inp) in enumerate(inputs): |
| 761 | + for i, inp in enumerate(inputs): |
762 | 762 | inp["keypath"] = account_keypath + [0, i]
|
763 | 763 | inp["script_config_index"] = 0
|
764 | 764 | assert isinstance(outputs[0], bitbox02.BTCOutputInternal)
|
@@ -789,7 +789,10 @@ def _sign_btc_tx_from_raw(self) -> None:
|
789 | 789 |
|
790 | 790 | def get(tx_id: str) -> Any:
|
791 | 791 | return requests.get(
|
792 |
| - "https://api.blockchair.com/bitcoin/testnet/dashboards/transaction/{}".format(tx_id) |
| 792 | + "https://api.blockchair.com/bitcoin/testnet/dashboards/transaction/{}".format( |
| 793 | + tx_id |
| 794 | + ), |
| 795 | + timeout=30, |
793 | 796 | ).json()["data"][tx_id]
|
794 | 797 |
|
795 | 798 | tx_id = input("Paste a btc testnet tx ID: ").strip()
|
@@ -1711,6 +1714,9 @@ def read(self, size: int, timeout_ms: int) -> bytes:
|
1711 | 1714 | print(f"Read from the simulator:\n{res.hex()}")
|
1712 | 1715 | return res
|
1713 | 1716 |
|
| 1717 | + def close(self) -> None: |
| 1718 | + return None |
| 1719 | + |
1714 | 1720 | def __del__(self) -> None:
|
1715 | 1721 | print("Simulator quit")
|
1716 | 1722 | self.client_socket.close()
|
@@ -1749,67 +1755,65 @@ def connect_to_usb_bitbox(debug: bool, use_cache: bool) -> int:
|
1749 | 1755 | except devices.NoneFoundException:
|
1750 | 1756 | print("Neither bitbox nor bootloader found.")
|
1751 | 1757 | return 1
|
1752 |
| - else: |
1753 |
| - hid_device = hid.device() |
1754 |
| - hid_device.open_path(bootloader["path"]) |
1755 |
| - bootloader_connection = bitbox02.Bootloader(u2fhid.U2FHid(hid_device), bootloader) |
1756 |
| - boot_app = SendMessageBootloader(bootloader_connection) |
1757 |
| - return boot_app.run() |
1758 |
| - else: |
| 1758 | + hid_device = hid.device() |
| 1759 | + hid_device.open_path(bootloader["path"]) |
| 1760 | + bootloader_connection = bitbox02.Bootloader(u2fhid.U2FHid(hid_device), bootloader) |
| 1761 | + boot_app = SendMessageBootloader(bootloader_connection) |
| 1762 | + return boot_app.run() |
1759 | 1763 |
|
1760 |
| - def show_pairing(code: str, device_response: Callable[[], bool]) -> bool: |
1761 |
| - print("Please compare and confirm the pairing code on your BitBox02:") |
1762 |
| - print(code) |
1763 |
| - if not device_response(): |
1764 |
| - return False |
1765 |
| - return input("Accept pairing? [y]/n: ").strip() != "n" |
| 1764 | + def show_pairing(code: str, device_response: Callable[[], bool]) -> bool: |
| 1765 | + print("Please compare and confirm the pairing code on your BitBox02:") |
| 1766 | + print(code) |
| 1767 | + if not device_response(): |
| 1768 | + return False |
| 1769 | + return input("Accept pairing? [y]/n: ").strip() != "n" |
1766 | 1770 |
|
1767 |
| - class NoiseConfig(util.NoiseConfigUserCache): |
1768 |
| - """NoiseConfig extends NoiseConfigUserCache""" |
| 1771 | + class NoiseConfig(util.NoiseConfigUserCache): |
| 1772 | + """NoiseConfig extends NoiseConfigUserCache""" |
1769 | 1773 |
|
1770 |
| - def __init__(self) -> None: |
1771 |
| - super().__init__("shift/send_message") |
| 1774 | + def __init__(self) -> None: |
| 1775 | + super().__init__("shift/send_message") |
1772 | 1776 |
|
1773 |
| - def show_pairing(self, code: str, device_response: Callable[[], bool]) -> bool: |
1774 |
| - return show_pairing(code, device_response) |
| 1777 | + def show_pairing(self, code: str, device_response: Callable[[], bool]) -> bool: |
| 1778 | + return show_pairing(code, device_response) |
1775 | 1779 |
|
1776 |
| - def attestation_check(self, result: bool) -> None: |
1777 |
| - if result: |
1778 |
| - print("Device attestation PASSED") |
1779 |
| - else: |
1780 |
| - print("Device attestation FAILED") |
| 1780 | + def attestation_check(self, result: bool) -> None: |
| 1781 | + if result: |
| 1782 | + print("Device attestation PASSED") |
| 1783 | + else: |
| 1784 | + print("Device attestation FAILED") |
1781 | 1785 |
|
1782 |
| - class NoiseConfigNoCache(bitbox_api_protocol.BitBoxNoiseConfig): |
1783 |
| - """NoiseConfig extends BitBoxNoiseConfig""" |
| 1786 | + class NoiseConfigNoCache(bitbox_api_protocol.BitBoxNoiseConfig): |
| 1787 | + """NoiseConfig extends BitBoxNoiseConfig""" |
1784 | 1788 |
|
1785 |
| - def show_pairing(self, code: str, device_response: Callable[[], bool]) -> bool: |
1786 |
| - return show_pairing(code, device_response) |
| 1789 | + def show_pairing(self, code: str, device_response: Callable[[], bool]) -> bool: |
| 1790 | + return show_pairing(code, device_response) |
1787 | 1791 |
|
1788 |
| - def attestation_check(self, result: bool) -> None: |
1789 |
| - if result: |
1790 |
| - print("Device attestation PASSED") |
1791 |
| - else: |
1792 |
| - print("Device attestation FAILED") |
| 1792 | + def attestation_check(self, result: bool) -> None: |
| 1793 | + if result: |
| 1794 | + print("Device attestation PASSED") |
| 1795 | + else: |
| 1796 | + print("Device attestation FAILED") |
1793 | 1797 |
|
1794 |
| - if use_cache: |
1795 |
| - config: bitbox_api_protocol.BitBoxNoiseConfig = NoiseConfig() |
1796 |
| - else: |
1797 |
| - config = NoiseConfigNoCache() |
| 1798 | + if use_cache: |
| 1799 | + config: bitbox_api_protocol.BitBoxNoiseConfig = NoiseConfig() |
| 1800 | + else: |
| 1801 | + config = NoiseConfigNoCache() |
1798 | 1802 |
|
1799 |
| - hid_device = hid.device() |
1800 |
| - hid_device.open_path(bitbox["path"]) |
1801 |
| - bitbox_connection = bitbox02.BitBox02( |
1802 |
| - transport=u2fhid.U2FHid(hid_device), device_info=bitbox, noise_config=config |
1803 |
| - ) |
1804 |
| - try: |
1805 |
| - bitbox_connection.check_min_version() |
1806 |
| - except FirmwareVersionOutdatedException as exc: |
1807 |
| - print("WARNING: ", exc) |
| 1803 | + hid_device = hid.device() |
| 1804 | + hid_device.open_path(bitbox["path"]) |
| 1805 | + bitbox_connection = bitbox02.BitBox02( |
| 1806 | + transport=u2fhid.U2FHid(hid_device), device_info=bitbox, noise_config=config |
| 1807 | + ) |
| 1808 | + try: |
| 1809 | + bitbox_connection.check_min_version() |
| 1810 | + except FirmwareVersionOutdatedException as exc: |
| 1811 | + print("WARNING: ", exc) |
1808 | 1812 |
|
1809 |
| - if debug: |
1810 |
| - print("Device Info:") |
1811 |
| - pprint.pprint(bitbox) |
1812 |
| - return SendMessage(bitbox_connection, debug).run() |
| 1813 | + if debug: |
| 1814 | + print("Device Info:") |
| 1815 | + pprint.pprint(bitbox) |
| 1816 | + return SendMessage(bitbox_connection, debug).run() |
1813 | 1817 |
|
1814 | 1818 |
|
1815 | 1819 | def main() -> int:
|
|
0 commit comments