Commit Graph

93 Commits

Author SHA1 Message Date
SomberNight
201309a7f0 ledger plugin: fix Ledger_Client.construct_new on very old btc app ver
related: https://github.com/spesmilo/electrum/issues/8568#issuecomment-1710162955
2023-09-07 14:47:55 +00:00
SomberNight
24980feab7 config: introduce ConfigVars
A new config API is introduced, and ~all of the codebase is adapted to it.
The old API is kept but mainly only for dynamic usage where its extra flexibility is needed.

Using examples, the old config API looked this:
```
>>> config.get("request_expiry", 86400)
604800
>>> config.set_key("request_expiry", 86400)
>>>
```

The new config API instead:
```
>>> config.WALLET_PAYREQ_EXPIRY_SECONDS
604800
>>> config.WALLET_PAYREQ_EXPIRY_SECONDS = 86400
>>>
```

The old API operated on arbitrary string keys, the new one uses
a static ~enum-like list of variables.

With the new API:
- there is a single centralised list of config variables, as opposed to
  these being scattered all over
- no more duplication of default values (in the getters)
- there is now some (minimal for now) type-validation/conversion for
  the config values

closes https://github.com/spesmilo/electrum/pull/5640
closes https://github.com/spesmilo/electrum/pull/5649

Note: there is yet a third API added here, for certain niche/abstract use-cases,
where we need a reference to the config variable itself.
It should only be used when needed:
```
>>> var = config.cv.WALLET_PAYREQ_EXPIRY_SECONDS
>>> var
<ConfigVarWithConfig key='request_expiry'>
>>> var.get()
604800
>>> var.set(3600)
>>> var.get_default_value()
86400
>>> var.is_set()
True
>>> var.is_modifiable()
True
```
2023-05-25 17:39:48 +00:00
SomberNight
9e13246be8 ledger: fix Ledger_Client_Legacy.sign_transaction()
fix signing txs when using old "bitcoin app" (pre-2.1) on the ledger device

```
 33.36 | W | transaction | heyheyhey. cp1. include_sigs=True force_legacy=False use_segwit_ser=True
 33.36 | W | transaction | heyheyhey. cp2. branch1
 33.37 | E | plugins.ledger.ledger |
Traceback (most recent call last):
  File "...\electrum\electrum\plugins\ledger\ledger.py", line 669, in sign_transaction
    rawTx = tx.serialize_to_network()
  File "...\electrum\electrum\transaction.py", line 945, in serialize_to_network
    witness = ''.join(self.serialize_witness(x, estimate_size=estimate_size) for x in inputs)
  File "...\electrum\electrum\transaction.py", line 945, in <genexpr>
    witness = ''.join(self.serialize_witness(x, estimate_size=estimate_size) for x in inputs)
  File "...\electrum\electrum\transaction.py", line 839, in serialize_witness
    sol = desc.satisfy(allow_dummy=estimate_size, sigdata=txin.part_sigs)
  File "...\electrum\electrum\descriptor.py", line 378, in satisfy
    sol = self._satisfy_inner(sigdata=sigdata, allow_dummy=allow_dummy)
  File "...\electrum\electrum\descriptor.py", line 574, in _satisfy_inner
    raise MissingSolutionPiece(f"no sig for {pubkey.hex()}")
electrum.descriptor.MissingSolutionPiece: no sig for 033e92e55923ea25809790f292ee9bd410355ee02492472d9a1ff1b364874d0679
 33.38 | I | plugins.ledger.ledger | no sig for 033e92e55923ea25809790f292ee9bd410355ee02492472d9a1ff1b364874d0679
```

fixes https://github.com/spesmilo/electrum/issues/8365
regression from https://github.com/spesmilo/electrum/pull/8230
2023-05-09 16:17:04 +00:00
SomberNight
3020499199 hww: ledger: bump required ledger-bitcoin and adapt to API change 2023-05-05 15:02:34 +00:00
SomberNight
33d394c9d7 ledger: fix scan_devices for certain devices with multiple interfaces
regression from https://github.com/spesmilo/electrum/pull/8041 (4.3.3)

maybe fixes https://github.com/spesmilo/electrum/issues/8293

-----

Testing with a "Ledger Nano S", btc app version 1.6.3.
btchip-python==0.1.32
ledgercomm==1.1.1
ledger-bitcoin==0.1.1

Trying to scan devices hangs/blocks forever on Linux (ubuntu 22.04).

```patch
diff --git a/electrum/plugins/ledger/ledger.py b/electrum/plugins/ledger/ledger.py
index 17c1caca48..ba5ae2e3ee 100644
--- a/electrum/plugins/ledger/ledger.py
+++ b/electrum/plugins/ledger/ledger.py
@@ -306,17 +306,25 @@ class Ledger_Client(HardwareClientBase, ABC):
     @staticmethod
     def construct_new(*args, device: Device, **kwargs) -> 'Ledger_Client':
         """The 'real' constructor, that automatically decides which subclass to use."""
+        _logger.info(f"xxx construct_new(). cp1. {device.path=}. {device=}")
         if LedgerPlugin.is_hw1(device.product_key):
             return Ledger_Client_Legacy_HW1(*args, **kwargs, device=device)
         # for nano S or newer hw, decide which client impl to use based on software/firmware version:
+        _logger.info(f"xxx construct_new(). cp2.")
         hid_device = HID()
         hid_device.path = device.path
+        _logger.info(f"xxx construct_new(). cp3.")
         hid_device.open()
+        _logger.info(f"xxx construct_new(). cp4.")
         transport = ledger_bitcoin.TransportClient('hid', hid=hid_device)
-        cl = ledger_bitcoin.createClient(transport, chain=get_chain())
+        _logger.info(f"xxx construct_new(). cp5.")
+        cl = ledger_bitcoin.createClient(transport, chain=get_chain(), debug=True)
+        _logger.info(f"xxx construct_new(). cp6.")
         if isinstance(cl, ledger_bitcoin.client.NewClient):
+            _logger.info(f"xxx construct_new(). cp7. trying Ledger_Client_New")
             return Ledger_Client_New(hid_device, *args, **kwargs)
         else:
+            _logger.info(f"xxx construct_new(). cp7. trying Ledger_Client_Legacy")
             return Ledger_Client_Legacy(hid_device, *args, **kwargs)

     def __init__(self, *, plugin: HW_PluginBase):
@@ -1416,7 +1424,7 @@ class LedgerPlugin(HW_PluginBase):
         try:
             return Ledger_Client.construct_new(device=device, product_key=device.product_key, plugin=self)
         except Exception as e:
-            self.logger.info(f"cannot connect at {device.path} {e}")
+            self.logger.info(f"cannot connect at {device.path} {e}")  #
         return None

     def setup_device(self, device_info, wizard, purpose):
```

scanning devices freezes... log:
```
  8.94 | I | plugin.DeviceMgr | scanning devices...
  9.18 | D | util.profiler | DeviceMgr.scan_devices 0.2357 sec
  9.18 | I | plugins.ledger.ledger | xxx construct_new(). cp1. device.path=b'0001:0008:00'. device=Device(path=b'0001:0008:00', interface_number=0, id_="b'0001:0008:00',0001,0,0", product_key=(11415, 4117), usage_page=0, transport_ui_string='hid')
  9.18 | I | plugins.ledger.ledger | xxx construct_new(). cp2.
  9.18 | I | plugins.ledger.ledger | xxx construct_new(). cp3.
heyheyhey. cp1. self.path=b'0001:0008:00'
  9.18 | I | plugins.ledger.ledger | xxx construct_new(). cp4.
  9.18 | I | plugins.ledger.ledger | xxx construct_new(). cp5.
=> b001000000
<= 010c426974636f696e205465737405312e362e3301029000
  9.22 | I | plugins.ledger.ledger | xxx construct_new(). cp6.
  9.22 | I | plugins.ledger.ledger | xxx construct_new(). cp7. trying Ledger_Client_Legacy
  9.29 | I | plugin.DeviceMgr | Registering <electrum.plugins.ledger.ledger.Ledger_Client_Legacy object at 0x7f4369e6e380>
 10.33 | I | plugins.ledger.ledger | xxx construct_new(). cp1. device.path=b'0001:0008:01'. device=Device(path=b'0001:0008:01', interface_number=1, id_="b'0001:0008:01',0001,1,0", product_key=(11415, 4117), usage_page=0, transport_ui_string='hid')
 10.33 | I | plugins.ledger.ledger | xxx construct_new(). cp2.
 10.33 | I | plugins.ledger.ledger | xxx construct_new(). cp3.
heyheyhey. cp1. self.path=b'0001:0008:01'
 10.33 | I | plugins.ledger.ledger | xxx construct_new(). cp4.
 10.33 | I | plugins.ledger.ledger | xxx construct_new(). cp5.
=> b001000000
```

in Qt console (before change):
```
>>> lp = plugins.get_plugin("ledger")
>>> plugins.device_manager.scan_devices()
[Device(path=b'0001:000a:00', interface_number=0, id_="b'0001:000a:00',0001,0,0", product_key=(11415, 4117), usage_page=0, transport_ui_string='hid'), Device(path=b'0001:000a:01', interface_number=1, id_="b'0001:000a:01',0001,1,0", product_key=(11415, 4117), usage_page=0, transport_ui_string='hid')]
```
2023-05-02 17:23:07 +00:00
SomberNight
499f51535f bip32: fix hardened char "h" vs "'" compatibility for some hw wallets
in particular, ledger: fix sign_message for some wallets

```
156.02 | E | plugins.ledger.ledger |
Traceback (most recent call last):
  File "...\electrum\electrum\plugins\ledger\ledger.py", line 1265, in sign_message
    result = base64.b64decode(self.client.sign_message(message, address_path))
  File "...\Python310\site-packages\ledger_bitcoin\client.py", line 230, in sign_message
    sw, response = self._make_request(self.builder.sign_message(message_bytes, bip32_path), client_intepreter)
  File "...\Python310\site-packages\ledger_bitcoin\command_builder.py", line 176, in sign_message
    bip32_path: List[bytes] = bip32_path_from_string(bip32_path)
  File "...\Python310\site-packages\ledger_bitcoin\common.py", line 68, in bip32_path_from_string
    return [int(p).to_bytes(4, byteorder="big") if "'" not in p
  File "...\Python310\site-packages\ledger_bitcoin\common.py", line 68, in <listcomp>
    return [int(p).to_bytes(4, byteorder="big") if "'" not in p
ValueError: invalid literal for int() with base 10: '84h'
```

Regression from df2bd61de6, where the
default hardened char was changed from "'" to "h". Note that there was
no corresponding wallet db upgrade, so some files use one char and
others use the other.
2023-04-27 17:03:16 +00:00
SomberNight
4219022c2e fix flake8-bugbear B023
B023 Function definition does not bind loop variable 'already_selected_buckets_value_sum'

in keepkey/qt.py, looks like this was an actual bug
(fixed in trezor plugin already: 52a4810752 )
2023-04-24 13:00:07 +00:00
SomberNight
312f2641e7 don't use bare except
use "except Exception", or if really needed explicitly "except BaseException"
2023-04-24 12:58:01 +00:00
Victor Forgeoux
ee61f99c22 Add support for Ledger Stax (#8308)
* Add support for Ledger Stax
2023-04-18 13:11:49 +00:00
SomberNight
7746cc8e60 bip32: (trivial) rename method strpath_to_intpath, for symmetry
Required a much higher mental load to parse the name "convert_bip32_path_to_list_of_uint32"
than to parse "convert_bip32_strpath_to_intpath".
And we already have the ~inverse: "convert_bip32_intpath_to_strpath".
2023-03-10 14:23:17 +00:00
SomberNight
0647a2cf9f transaction.py: rm PartialTxInput.{num_sig, script_type} 2023-03-03 16:40:12 +00:00
SomberNight
2242a506a9 ledger: fix sign_transaction for Ypub / sh(wsh(multi())) wallets
regression from https://github.com/spesmilo/electrum/pull/8041
2023-02-22 14:02:24 +00:00
ghost43
ac239a81b8 Merge pull request #8041 from bigspider/app-bitcoin-new
Modify Ledger plugin to support the new bitcoin app v2.1.0
2022-11-10 14:24:14 +00:00
SomberNight
e75110ec04 hw_wallet: de-dupe "message_dialog" code, make text selectable 2022-11-09 21:10:52 +00:00
SomberNight
9b82eb6d06 ledger: re-add support for HW.1, and add a deprecation warning 2022-11-09 20:42:55 +00:00
SomberNight
3cac7e9a61 ledger: minor clean-up 2022-11-09 20:11:08 +00:00
Salvatore Ingala
e47270daa5 Fix multiple signing 2022-11-09 13:21:14 +01:00
Salvatore Ingala
3c0e3eb7e2 Refactored LedgerPlugin::create_client, and handle errors when creating the client. 2022-11-09 11:34:41 +01:00
Salvatore Ingala
7c80779903 Replace print() with logger 2022-11-09 11:21:21 +01:00
Salvatore Ingala
80e214d465 - Remove code calling hw1-related features, not supported in the version of btchip embedded in ledger_bitcoin
- Fix signature of sign_message in Ledger_Client_Legacy
2022-11-09 10:59:12 +01:00
Salvatore Ingala
f3eb492dca Update Ledger website to ledger.com in error message 2022-11-08 17:44:09 +01:00
Salvatore Ingala
c401f84aa7 Avoid relying on old btchip package; improve missing library error handling 2022-11-08 17:44:06 +01:00
Salvatore Ingala
2d64dc13c9 Modify Ledger plugin to support the new bitcoin app v2.1.0 2022-11-08 17:44:05 +01:00
SomberNight
01b5e3f8e0 flake8: enable more mandatory tests 2022-10-31 16:13:22 +00:00
SomberNight
574243b897 hww ledger: call scan_devices fewer times 2022-06-03 17:14:51 +02:00
SomberNight
b5d3f1458a hww: impl get_client in Hardware_KeyStore instead of subclasses 2022-06-03 17:14:44 +02:00
SomberNight
6c50d3b0a3 hw plugins: (cleanup) rm no-op clear_client argument from keystore.give_error
The keystore does not have a "client" field.
One is supposed to use the "get_client" method instead (the generic API is `plugin.get_client(keystore)`)
Remnants of old code.
2022-05-11 19:30:14 +02:00
SomberNight
aab8e664ed hw plugins: (cleanup) Plugin objects should not have a Client field
Clients are per-connected-device, plugins are ~singletons.
These were mostly remnants of old code.
2022-05-11 19:16:18 +02:00
SomberNight
30623c3529 hw plugins: (cleanup) Plugin objects should not have a Handler field
Handlers are per-client (connected device), plugins are ~singletons.
These were mostly remnants of old code.
2022-05-11 19:13:00 +02:00
SomberNight
9599254d43 hw: rm dead code from Hardware_KeyStore subclasses
- force_watching_only is long since unused
- comment was just duplicated from the base class
2022-05-09 20:09:10 +02:00
SomberNight
ec98ef5aee hw ledger: sign_transaction: add progress indicator
related: https://github.com/spesmilo/electrum/issues/7516
2022-03-26 15:26:13 +01:00
nndiaye-ledger
7e083824b7 Add support for Ledger Nano S Plus (#7692) 2022-03-02 12:59:51 +00:00
SomberNight
376fc01b27 keystore.sign_message: add optional script_type argument
this is used by trezor
(and also by bitbox02, which was using a workaround previously)

fixes https://github.com/spesmilo/electrum/issues/7670
2022-02-22 19:20:03 +01:00
Benoit Verret
f731c38293 Minor style changes 2021-03-21 00:36:23 -04:00
SomberNight
83750a861f ledger: bump min btchip-python (0.1.30->0.1.32)
So that it has https://github.com/LedgerHQ/btchip-python/pull/42

fixes #6928
2021-02-12 05:28:01 +01:00
SomberNight
b56fe237cd ledger: give clear error that old HW.1 cannot send to bech32 address
maybe related #7022
2021-02-12 04:47:53 +01:00
Gordan Nekić
9c4807644b Fix ledger sign message (#7004)
there was an around ~1/128 chance of creating an invalid signature when signing a message with a ledger
2021-02-06 05:16:52 +00:00
SomberNight
2eb02931ae hw plugins: log exception at import time (but only if interesting)
related: https://github.com/spesmilo/electrum/issues/6928
2021-01-11 00:05:23 +01:00
SomberNight
b78cbcffd1 ledger: fix enumerating ledger devices with new bitcoin app (1.5.1)
see https://github.com/bitcoin-core/HWI/issues/402
2020-11-18 15:36:11 +01:00
ghost43
8e9d6a4c91 Merge pull request #6685 from SomberNight/202010_bitcoin_script
bitcoin/transaction: construct_script, and clean-ups
2020-10-24 23:06:55 +00:00
SomberNight
200f547a07 ledger: fix compat with hw.1 - signing flow deadlocked
broke in 8a1b46d839ac24f77bfa5e3a1eed0cb7284b59eac5b685854c517f224c98dc44
2020-10-24 23:32:18 +02:00
SomberNight
eefb68c82b transaction: change Transaction.is_segwit_input(txin) to txin.is_segwit() 2020-10-24 08:03:13 +02:00
SomberNight
e60aede77e ledger: workaround to avoid on-device warning for unusual der path
related: #6512
2020-10-06 17:55:29 +02:00
ghost43
21c3572600 hardware devices: run all device communication on dedicated thread (#6561)
hidapi/libusb etc are not thread-safe.

related: #6554
2020-09-08 15:52:53 +00:00
SomberNight
a7199696d3 json_db: exempt keystore from StoredDict conversion
The keystore logic would need to be significantly changed to nicely
interoperate with StoredDict/json_db logic.
(just see KeyStore.__init__() and KeyStore.dump())
For now we exempt the keystore from the recursive StoredDict conversion, as
it is a smaller change that is also easier to review for correctness.

fixes #6066
fixes #6401

also reverts 2d3c2eeea9 (which was an even hackier workaround for #6066)
2020-09-04 16:11:01 +02:00
SomberNight
b042c4118f ledger: speed-up sign_transaction
really slow to scan usb devices for e.g. every tx input...
if user disconnects mid-signing, we would fail anyway.
2020-07-02 12:45:42 +02:00
SomberNight
48993118ad ledger: bump min btchip-python version
and minor simplification
2020-07-02 12:45:38 +02:00
BTChip github
c1101ee258 Remove warning on Segwit inputs and newer Bitcoin application, use generic signing for P2SH inputs 2020-06-27 18:26:54 +02:00
SomberNight
2cfa3bd6c8 hww hidapi usage: try to mitigate some thread-safety issues
related: #6097
2020-04-17 19:53:39 +02:00
SomberNight
98d2ab5bd6 hww: fix HardwareClientBase not having reference to plugin
it was incorrectly documented that it did (previously only for some plugins)
2020-04-17 19:53:35 +02:00