|
1 | 1 | # Trezor interaction script |
2 | 2 |
|
3 | 3 | from ..hwwclient import HardwareWalletClient |
4 | | -from ckcc.client import ColdcardDevice |
| 4 | +from ckcc.client import ColdcardDevice, COINKITE_VID, CKCC_PID |
5 | 5 | from ckcc.protocol import CCProtocolPacker |
6 | 6 | from ckcc.constants import MAX_BLK_LEN |
7 | | -from ..base58 import xpub_main_2_test |
| 7 | +from ..base58 import xpub_main_2_test, get_xpub_fingerprint_hex |
8 | 8 | from hashlib import sha256 |
9 | 9 |
|
10 | 10 | import base64 |
|
13 | 13 | import io |
14 | 14 | import time |
15 | 15 |
|
| 16 | +CC_SIMULATOR_SOCK = '/tmp/ckcc-simulator.sock' |
| 17 | +# Using the simulator: https://github.com/Coldcard/firmware/blob/master/unix/README.md |
| 18 | + |
16 | 19 | # This class extends the HardwareWalletClient for ColdCard specific things |
17 | 20 | class ColdCardClient(HardwareWalletClient): |
18 | 21 |
|
19 | 22 | def __init__(self, path, password=''): |
20 | 23 | super(ColdCardClient, self).__init__(path, password) |
21 | | - device = hid.device() |
22 | | - device.open_path(path.encode()) |
23 | | - self.device = ColdcardDevice(dev=device) |
| 24 | + # Simulator hard coded pipe socket |
| 25 | + if path == CC_SIMULATOR_SOCK: |
| 26 | + self.device = ColdcardDevice(sn=path) |
| 27 | + else: |
| 28 | + device = hid.device() |
| 29 | + device.open_path(path.encode()) |
| 30 | + self.device = ColdcardDevice(dev=device) |
24 | 31 |
|
25 | 32 | # Must return a dict with the xpub |
26 | 33 | # Retrieves the public key at the specified BIP 32 derivation path |
@@ -108,3 +115,39 @@ def wipe_device(self): |
108 | 115 | # Close the device |
109 | 116 | def close(self): |
110 | 117 | self.device.close() |
| 118 | + |
| 119 | +def enumerate(password=None): |
| 120 | + results = [] |
| 121 | + for d in hid.enumerate(COINKITE_VID, CKCC_PID): |
| 122 | + d_data = {} |
| 123 | + |
| 124 | + path = d['path'].decode() |
| 125 | + d_data['type'] = 'coldcard' |
| 126 | + d_data['path'] = path |
| 127 | + |
| 128 | + try: |
| 129 | + client = ColdCardClient(path) |
| 130 | + master_xpub = client.get_pubkey_at_path('m/0h')['xpub'] |
| 131 | + d_data['fingerprint'] = get_xpub_fingerprint_hex(master_xpub) |
| 132 | + client.close() |
| 133 | + except Exception as e: |
| 134 | + d_data['error'] = "Could not open client or get fingerprint information: " + str(e) |
| 135 | + |
| 136 | + results.append(d_data) |
| 137 | + # Check if the simulator is there |
| 138 | + try: |
| 139 | + client = ColdCardClient(CC_SIMULATOR_SOCK) |
| 140 | + master_xpub = client.get_pubkey_at_path('m/0h')['xpub'] |
| 141 | + |
| 142 | + d_data = {} |
| 143 | + d_data['fingerprint'] = get_xpub_fingerprint_hex(master_xpub) |
| 144 | + d_data['type'] = 'coldcard' |
| 145 | + d_data['path'] = CC_SIMULATOR_SOCK |
| 146 | + results.append(d_data) |
| 147 | + client.close() |
| 148 | + except RuntimeError as e: |
| 149 | + if str(e) == 'Cannot connect to simulator. Is it running?': |
| 150 | + pass |
| 151 | + else: |
| 152 | + raise e |
| 153 | + return results |
0 commit comments