@trezor/connect 9.4.3-beta.1 → 9.4.3-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. package/CHANGELOG.md +43 -4
  2. package/README.md +1 -1
  3. package/lib/api/applyFlags.js +1 -1
  4. package/lib/api/applySettings.js +1 -1
  5. package/lib/api/authenticateDevice.js +1 -1
  6. package/lib/api/authorizeCoinjoin.js +1 -1
  7. package/lib/api/backupDevice.js +1 -1
  8. package/lib/api/binance/api/binanceGetAddress.js +2 -1
  9. package/lib/api/binance/api/binanceGetPublicKey.js +2 -1
  10. package/lib/api/binance/api/binanceSignTransaction.js +2 -1
  11. package/lib/api/binance/binanceSignTx.js +1 -1
  12. package/lib/api/bitcoin/Fees.js +2 -2
  13. package/lib/api/bitcoin/TransactionComposer.d.ts +3 -3
  14. package/lib/api/bitcoin/TransactionComposer.js +2 -2
  15. package/lib/api/bitcoin/refTx.js +1 -1
  16. package/lib/api/blockchainDisconnect.js +1 -1
  17. package/lib/api/blockchainEvmRpcCall.d.ts +16 -0
  18. package/lib/api/blockchainEvmRpcCall.js +45 -0
  19. package/lib/api/cancelCoinjoinAuthorization.js +1 -1
  20. package/lib/api/cardano/api/cardanoGetAddress.js +2 -1
  21. package/lib/api/cardano/api/cardanoGetNativeScriptHash.js +2 -1
  22. package/lib/api/cardano/api/cardanoGetPublicKey.js +2 -1
  23. package/lib/api/cardano/api/cardanoSignTransaction.js +2 -1
  24. package/lib/api/cardano/cardanoAddressParameters.js +1 -1
  25. package/lib/api/cardano/cardanoAuxiliaryData.js +1 -1
  26. package/lib/api/cardano/cardanoCertificate.js +1 -1
  27. package/lib/api/cardano/cardanoInputs.d.ts +8 -8
  28. package/lib/api/cardano/cardanoInputs.js +1 -1
  29. package/lib/api/cardano/cardanoOutputs.d.ts +22 -22
  30. package/lib/api/cardano/cardanoOutputs.js +1 -1
  31. package/lib/api/cardano/cardanoTokenBundle.d.ts +6 -6
  32. package/lib/api/cardano/cardanoTokenBundle.js +1 -1
  33. package/lib/api/changeLanguage.js +1 -1
  34. package/lib/api/changePin.js +1 -1
  35. package/lib/api/cipherKeyValue.js +1 -1
  36. package/lib/api/common/paramsValidator.js +1 -1
  37. package/lib/api/composeTransaction.d.ts +9 -3
  38. package/lib/api/composeTransaction.js +7 -4
  39. package/lib/api/eos/api/eosGetPublicKey.js +2 -1
  40. package/lib/api/eos/api/eosSignTransaction.js +2 -1
  41. package/lib/api/eos/eosSignTx.d.ts +3 -3
  42. package/lib/api/ethereum/api/ethereumGetAddress.js +2 -1
  43. package/lib/api/ethereum/api/ethereumGetPublicKey.js +2 -1
  44. package/lib/api/ethereum/api/ethereumSignMessage.d.ts +1 -1
  45. package/lib/api/ethereum/api/ethereumSignMessage.js +2 -1
  46. package/lib/api/ethereum/api/ethereumSignTransaction.d.ts +1 -1
  47. package/lib/api/ethereum/api/ethereumSignTransaction.js +13 -7
  48. package/lib/api/ethereum/api/ethereumSignTypedData.d.ts +58 -57
  49. package/lib/api/ethereum/api/ethereumSignTypedData.js +3 -2
  50. package/lib/api/ethereum/api/ethereumVerifyMessage.js +2 -1
  51. package/lib/api/ethereum/ethereumDefinitions.d.ts +21 -21
  52. package/lib/api/ethereum/ethereumDefinitions.js +6 -5
  53. package/lib/api/firmware/getBinary.js +1 -1
  54. package/lib/api/firmware/x509certificate.js +3 -5
  55. package/lib/api/getAccountDescriptor.js +1 -1
  56. package/lib/api/getAddress.js +1 -1
  57. package/lib/api/getCoinInfo.d.ts +3 -0
  58. package/lib/api/getCoinInfo.js +1 -1
  59. package/lib/api/getFirmwareHash.js +1 -1
  60. package/lib/api/getOwnershipId.js +1 -1
  61. package/lib/api/getOwnershipProof.js +1 -1
  62. package/lib/api/getPublicKey.js +1 -1
  63. package/lib/api/index.d.ts +2 -1
  64. package/lib/api/index.js +5 -3
  65. package/lib/api/loadDevice.d.ts +14 -0
  66. package/lib/api/loadDevice.js +44 -0
  67. package/lib/api/nem/api/nemGetAddress.js +2 -1
  68. package/lib/api/nem/api/nemSignTransaction.js +2 -1
  69. package/lib/api/pushTransaction.js +1 -1
  70. package/lib/api/rebootToBootloader.js +1 -1
  71. package/lib/api/recoveryDevice.js +1 -1
  72. package/lib/api/requestLogin.js +1 -1
  73. package/lib/api/resetDevice.js +1 -1
  74. package/lib/api/ripple/api/rippleGetAddress.js +2 -1
  75. package/lib/api/ripple/api/rippleSignTransaction.js +2 -1
  76. package/lib/api/setBrightness.js +1 -1
  77. package/lib/api/setBusy.js +1 -1
  78. package/lib/api/signMessage.js +1 -1
  79. package/lib/api/solana/api/solanaGetAddress.js +2 -1
  80. package/lib/api/solana/api/solanaGetPublicKey.js +2 -1
  81. package/lib/api/solana/api/solanaSignTransaction.js +2 -1
  82. package/lib/api/stellar/api/stellarGetAddress.js +2 -1
  83. package/lib/api/stellar/api/stellarSignTransaction.js +2 -1
  84. package/lib/api/stellar/stellarSignTx.js +1 -1
  85. package/lib/api/tezos/api/tezosGetAddress.js +2 -1
  86. package/lib/api/tezos/api/tezosGetPublicKey.js +2 -1
  87. package/lib/api/tezos/api/tezosSignTransaction.js +2 -1
  88. package/lib/api/tezos/tezosSignTx.js +1 -1
  89. package/lib/api/unlockPath.js +1 -1
  90. package/lib/api/verifyMessage.js +1 -1
  91. package/lib/backend/Blockchain.d.ts +3 -0
  92. package/lib/backend/Blockchain.js +3 -0
  93. package/lib/constants/errors.d.ts +3 -0
  94. package/lib/constants/errors.js +3 -0
  95. package/lib/constants/index.d.ts +1 -0
  96. package/lib/constants/index.js +3 -1
  97. package/lib/constants/nem.d.ts +3 -3
  98. package/lib/constants/utxo.d.ts +3 -0
  99. package/lib/constants/utxo.js +5 -0
  100. package/lib/core/AbstractMethod.d.ts +13 -5
  101. package/lib/core/AbstractMethod.js +50 -24
  102. package/lib/core/index.js +41 -30
  103. package/lib/core/onCallFirmwareUpdate.d.ts +2 -2
  104. package/lib/core/onCallFirmwareUpdate.js +5 -5
  105. package/lib/data/DataManager.d.ts +0 -181
  106. package/lib/data/DataManager.js +40 -6
  107. package/lib/data/analyticsInfo.js +2 -1
  108. package/lib/data/coinInfo.d.ts +14 -0
  109. package/lib/data/config.d.ts +20 -22
  110. package/lib/data/config.js +15 -40
  111. package/lib/data/connectSettings.js +5 -4
  112. package/lib/data/deviceAuthenticityConfig.js +17 -0
  113. package/lib/data/deviceAuthenticityConfigTypes.d.ts +32 -23
  114. package/lib/data/firmwareInfo.d.ts +1 -1
  115. package/lib/data/models.d.ts +9 -0
  116. package/lib/data/models.js +9 -0
  117. package/lib/data/version.d.ts +1 -1
  118. package/lib/data/version.js +1 -1
  119. package/lib/device/Device.d.ts +143 -36
  120. package/lib/device/Device.js +226 -119
  121. package/lib/device/DeviceCommands.js +4 -4
  122. package/lib/device/DeviceList.d.ts +13 -21
  123. package/lib/device/DeviceList.js +54 -168
  124. package/lib/device/prompts.js +3 -2
  125. package/lib/device/resolveDescriptorForTaproot.d.ts +1 -1
  126. package/lib/events/call.d.ts +5 -5
  127. package/lib/events/call.js +2 -2
  128. package/lib/events/device.d.ts +0 -6
  129. package/lib/events/device.js +0 -6
  130. package/lib/events/transport.d.ts +1 -1
  131. package/lib/factory.d.ts +6 -6
  132. package/lib/factory.js +132 -136
  133. package/lib/impl/dynamic.d.ts +13 -19
  134. package/lib/impl/dynamic.js +0 -9
  135. package/lib/index-browser.d.ts +3 -1
  136. package/lib/index-browser.js +0 -3
  137. package/lib/index.d.ts +3 -1
  138. package/lib/index.js +1 -13
  139. package/lib/types/api/applySettings.d.ts +13 -13
  140. package/lib/types/api/authenticateDevice.d.ts +33 -24
  141. package/lib/types/api/authorizeCoinjoin.d.ts +10 -10
  142. package/lib/types/api/binance/index.d.ts +143 -143
  143. package/lib/types/api/binance/index.js +1 -1
  144. package/lib/types/api/bitcoin/index.d.ts +11 -11
  145. package/lib/types/api/bitcoin/index.js +1 -1
  146. package/lib/types/api/blockchainEvmRpcCall.d.ts +4 -0
  147. package/lib/types/api/{checkFirmwareAuthenticity.js → blockchainEvmRpcCall.js} +1 -1
  148. package/lib/types/api/cancelCoinjoinAuthorization.d.ts +1 -1
  149. package/lib/types/api/cardano/index.d.ts +507 -507
  150. package/lib/types/api/changeLanguage.d.ts +6 -6
  151. package/lib/types/api/cipherKeyValue.d.ts +8 -8
  152. package/lib/types/api/cipherKeyValue.js +1 -1
  153. package/lib/types/api/composeTransaction.d.ts +13 -7
  154. package/lib/types/api/eos/index.d.ts +525 -525
  155. package/lib/types/api/eos/index.js +1 -1
  156. package/lib/types/api/ethereum/index.d.ts +100 -100
  157. package/lib/types/api/firmwareUpdate.d.ts +5 -5
  158. package/lib/types/api/getAccountDescriptor.d.ts +4 -4
  159. package/lib/types/api/getAddress.d.ts +32 -32
  160. package/lib/types/api/getOwnershipId.d.ts +24 -24
  161. package/lib/types/api/getOwnershipProof.d.ts +28 -28
  162. package/lib/types/api/getPublicKey.d.ts +24 -24
  163. package/lib/types/api/getPublicKey.js +1 -1
  164. package/lib/types/api/index.d.ts +4 -8
  165. package/lib/types/api/init.d.ts +4 -2
  166. package/lib/types/api/loadDevice.d.ts +4 -0
  167. package/lib/types/api/loadDevice.js +3 -0
  168. package/lib/types/api/nem/index.d.ts +612 -612
  169. package/lib/types/api/nem/index.js +1 -1
  170. package/lib/types/api/nemGetAddress.d.ts +6 -6
  171. package/lib/types/api/pushTransaction.d.ts +3 -3
  172. package/lib/types/api/recoveryDevice.d.ts +9 -9
  173. package/lib/types/api/requestLogin.d.ts +25 -25
  174. package/lib/types/api/ripple/index.d.ts +21 -21
  175. package/lib/types/api/ripple/index.js +1 -1
  176. package/lib/types/api/solana/index.d.ts +23 -23
  177. package/lib/types/api/solana/index.js +1 -1
  178. package/lib/types/api/stellar/index.d.ts +582 -582
  179. package/lib/types/api/stellar/index.js +1 -1
  180. package/lib/types/api/tezos/index.d.ts +120 -120
  181. package/lib/types/api/tezos/index.js +1 -1
  182. package/lib/types/api/unlockPath.d.ts +2 -2
  183. package/lib/types/coinInfo.d.ts +231 -224
  184. package/lib/types/device.d.ts +13 -1
  185. package/lib/types/device.js +3 -1
  186. package/lib/types/fees.d.ts +19 -19
  187. package/lib/types/firmware.d.ts +1 -1
  188. package/lib/types/params.d.ts +21 -18
  189. package/lib/types/settings.d.ts +15 -9
  190. package/lib/types/utils.d.ts +1 -0
  191. package/lib/types/utils.js +3 -0
  192. package/lib/utils/addressUtils.js +2 -2
  193. package/lib/utils/assetUtils.js +3 -3
  194. package/lib/utils/deviceFeaturesUtils.js +1 -1
  195. package/lib/utils/uiPromiseManager.d.ts +2 -1
  196. package/lib/utils/uiPromiseManager.js +3 -3
  197. package/package.json +12 -12
  198. package/lib/api/checkFirmwareAuthenticity.d.ts +0 -11
  199. package/lib/api/checkFirmwareAuthenticity.js +0 -63
  200. package/lib/types/api/checkFirmwareAuthenticity.d.ts +0 -11
@@ -2,7 +2,7 @@ import { TypedEmitter } from '@trezor/utils';
2
2
  import { Transport, TRANSPORT } from '@trezor/transport';
3
3
  import { DEVICE, TransportInfo } from '../events';
4
4
  import { Device } from './Device';
5
- import type { ConnectSettings, Device as DeviceTyped } from '../types';
5
+ import { ConnectSettings, DeviceUniquePath, Device as DeviceTyped } from '../types';
6
6
  interface DeviceListEvents {
7
7
  [TRANSPORT.START]: TransportInfo;
8
8
  [TRANSPORT.ERROR]: string;
@@ -10,8 +10,6 @@ interface DeviceListEvents {
10
10
  [DEVICE.CONNECT_UNACQUIRED]: DeviceTyped;
11
11
  [DEVICE.DISCONNECT]: DeviceTyped;
12
12
  [DEVICE.CHANGED]: DeviceTyped;
13
- [DEVICE.RELEASED]: DeviceTyped;
14
- [DEVICE.ACQUIRED]: DeviceTyped;
15
13
  }
16
14
  export interface IDeviceList {
17
15
  isConnected(): this is DeviceList;
@@ -25,7 +23,7 @@ export interface IDeviceList {
25
23
  dispose: DeviceList['dispose'];
26
24
  }
27
25
  export declare const assertDeviceListConnected: (deviceList: IDeviceList) => asserts deviceList is DeviceList;
28
- type ConstructorParams = Pick<ConnectSettings, 'priority' | 'debug' | '_sessionsBackgroundUrl'> & {
26
+ type ConstructorParams = Pick<ConnectSettings, 'priority' | 'debug' | '_sessionsBackgroundUrl' | 'manifest'> & {
29
27
  messages: Record<string, any>;
30
28
  };
31
29
  type InitParams = Pick<ConnectSettings, 'pendingTransportEvent' | 'transportReconnect'>;
@@ -33,34 +31,31 @@ export declare class DeviceList extends TypedEmitter<DeviceListEvents> implement
33
31
  private transport;
34
32
  private transports;
35
33
  private devices;
36
- private creatingDevicesDescriptors;
37
- private createDevicesQueue;
34
+ private deviceCounter;
35
+ private readonly handshakeLock;
38
36
  private readonly authPenaltyManager;
39
37
  private initPromise?;
40
38
  private rejectPending?;
41
39
  private transportCommonArgs;
42
40
  isConnected(): this is DeviceList;
43
41
  pendingConnection(): Promise<void> | undefined;
44
- constructor({ messages, priority, debug, _sessionsBackgroundUrl }: ConstructorParams);
42
+ constructor({ messages, priority, debug, _sessionsBackgroundUrl, manifest, }: ConstructorParams);
45
43
  private createTransport;
46
44
  setTransports(transports: ConnectSettings['transports']): void;
47
- private onTransportUpdate;
48
- private waitForCreateDevicesQueue;
49
- private removeFromCreateDevicesQueue;
45
+ private onDeviceConnected;
46
+ private onDeviceDisconnected;
47
+ private onDeviceSessionChanged;
48
+ private onDeviceRequestRelease;
50
49
  init(initParams?: InitParams): Promise<void>;
51
50
  private createInitPromise;
52
51
  private createReconnectPromise;
53
52
  private selectTransport;
54
53
  private initializeTransport;
55
54
  private waitForDevices;
56
- private _createAndSaveDevice;
57
- private _createUnacquiredDevice;
58
- private _createUnreadableDevice;
59
- getDevice(path: string): Device;
60
- getFirstDevicePath(): string;
61
- asArray(): DeviceTyped[];
62
- allDevices(): Device[];
63
- length(): number;
55
+ getDeviceCount(): number;
56
+ getAllDevices(): Device[];
57
+ getOnlyDevice(): Device | undefined;
58
+ getDeviceByPath(path: DeviceUniquePath): Device | undefined;
64
59
  transportType(): "BridgeTransport" | "NodeUsbTransport" | "WebUsbTransport" | "UdpTransport";
65
60
  getTransportInfo(): TransportInfo;
66
61
  dispose(): Promise<void>;
@@ -68,9 +63,6 @@ export declare class DeviceList extends TypedEmitter<DeviceListEvents> implement
68
63
  enumerate(transport?: Transport): Promise<void>;
69
64
  addAuthPenalty(device: Device): void;
70
65
  removeAuthPenalty(device: Device): void;
71
- private handle;
72
- private _takeAndCreateDevice;
73
- private _handleUsedElsewhere;
74
66
  }
75
67
  export {};
76
68
  //# sourceMappingURL=DeviceList.d.ts.map
@@ -6,9 +6,11 @@ const transport_1 = require("@trezor/transport");
6
6
  const constants_1 = require("../constants");
7
7
  const events_1 = require("../events");
8
8
  const Device_1 = require("./Device");
9
+ const types_1 = require("../types");
9
10
  const transportInfo_1 = require("../data/transportInfo");
10
11
  const debug_1 = require("../utils/debug");
11
12
  const promiseUtils_1 = require("../utils/promiseUtils");
13
+ const utils_2 = require("../types/utils");
12
14
  const _log = (0, debug_1.initLog)('DeviceList');
13
15
  const createAuthPenaltyManager = (priority) => {
14
16
  const penalizedDevices = {};
@@ -42,17 +44,18 @@ class DeviceList extends utils_1.TypedEmitter {
42
44
  pendingConnection() {
43
45
  return this.initPromise;
44
46
  }
45
- constructor({ messages, priority, debug, _sessionsBackgroundUrl }) {
47
+ constructor({ messages, priority, debug, _sessionsBackgroundUrl, manifest, }) {
46
48
  super();
47
49
  this.devices = {};
48
- this.creatingDevicesDescriptors = {};
49
- this.createDevicesQueue = [];
50
+ this.deviceCounter = Date.now();
50
51
  const transportLogger = (0, debug_1.initLog)('@trezor/transport', debug);
52
+ this.handshakeLock = (0, utils_1.getSynchronize)();
51
53
  this.authPenaltyManager = createAuthPenaltyManager(priority);
52
54
  this.transportCommonArgs = {
53
55
  messages,
54
56
  logger: transportLogger,
55
57
  sessionsBackgroundUrl: _sessionsBackgroundUrl,
58
+ id: (manifest === null || manifest === void 0 ? void 0 : manifest.appUrl) || 'unknown app',
56
59
  };
57
60
  this.transports = [
58
61
  new transport_1.BridgeTransport({
@@ -96,91 +99,41 @@ class DeviceList extends utils_1.TypedEmitter {
96
99
  const transportTypes = (transports === null || transports === void 0 ? void 0 : transports.length) ? transports : ['BridgeTransport'];
97
100
  this.transports = transportTypes.map(this.createTransport.bind(this));
98
101
  }
99
- onTransportUpdate(diff, transport) {
100
- diff.forEach(async ({ descriptor, ...category }) => {
101
- var _a;
102
- const path = descriptor.path.toString();
103
- const device = this.devices[path];
104
- this.creatingDevicesDescriptors[path] = descriptor;
105
- switch (category.type) {
106
- case 'disconnected':
107
- this.removeFromCreateDevicesQueue(path);
108
- if (!device)
109
- break;
110
- device.disconnect();
111
- delete this.devices[path];
112
- this.emit(events_1.DEVICE.DISCONNECT, device.toMessageObject());
113
- break;
114
- case 'connected':
115
- if (!(await this.waitForCreateDevicesQueue(path))) {
116
- break;
117
- }
118
- const penalty = this.authPenaltyManager.get();
119
- if (penalty) {
120
- await (0, promiseUtils_1.resolveAfter)(501 + penalty, null).promise;
121
- }
122
- if (this.creatingDevicesDescriptors[path].session == null) {
123
- await this._createAndSaveDevice(descriptor, transport);
124
- }
125
- else {
126
- const device = this._createUnacquiredDevice(descriptor, transport);
127
- this.devices[path] = device;
128
- this.emit(events_1.DEVICE.CONNECT_UNACQUIRED, device.toMessageObject());
129
- }
130
- this.removeFromCreateDevicesQueue(path);
131
- break;
132
- case 'acquired':
133
- if (category.subtype === 'elsewhere') {
134
- this.removeFromCreateDevicesQueue(path);
135
- if (device) {
136
- device.featuresNeedsReload = true;
137
- device.interruptionFromOutside();
138
- }
139
- }
140
- if (!device)
141
- break;
142
- _log.debug('Event', events_1.DEVICE.CHANGED, device.toMessageObject());
143
- this.emit(events_1.DEVICE.CHANGED, device.toMessageObject());
144
- _log.debug('Event', events_1.DEVICE.ACQUIRED, device.toMessageObject());
145
- this.emit(events_1.DEVICE.ACQUIRED, device.toMessageObject());
146
- break;
147
- case 'released':
148
- if (!device)
149
- break;
150
- const methodStillRunning = !((_a = device.commands) === null || _a === void 0 ? void 0 : _a.isDisposed());
151
- if (methodStillRunning) {
152
- device.keepTransportSession = false;
153
- }
154
- _log.debug('Event', events_1.DEVICE.CHANGED, device.toMessageObject());
155
- this.emit(events_1.DEVICE.CHANGED, device.toMessageObject());
156
- _log.debug('Event', events_1.DEVICE.RELEASED, device.toMessageObject());
157
- this.emit(events_1.DEVICE.RELEASED, device.toMessageObject());
158
- if (category.subtype === 'elsewhere') {
159
- await (0, promiseUtils_1.resolveAfter)(1000, null).promise;
160
- if (!device.isUsed() && device.isUnacquired() && !device.isInconsistent()) {
161
- _log.debug('Create device from unacquired', device.toMessageObject());
162
- await this._createAndSaveDevice(descriptor, transport);
163
- }
164
- }
165
- break;
102
+ onDeviceConnected(descriptor, transport) {
103
+ const { path } = descriptor;
104
+ const id = (this.deviceCounter++).toString(16).slice(-8);
105
+ const device = new Device_1.Device({
106
+ id: (0, types_1.DeviceUniquePath)(id),
107
+ transport,
108
+ descriptor,
109
+ listener: lifecycle => this.emit(lifecycle, device.toMessageObject()),
110
+ });
111
+ this.devices[path] = device;
112
+ const penalty = this.authPenaltyManager.get();
113
+ this.handshakeLock(async () => {
114
+ if (this.devices[path]) {
115
+ await device.handshake(penalty);
166
116
  }
167
- device === null || device === void 0 ? void 0 : device.updateDescriptor(descriptor);
168
117
  });
169
118
  }
170
- async waitForCreateDevicesQueue(path) {
171
- const dfd = (0, utils_1.createDeferred)(path);
172
- const prevQueue = this.createDevicesQueue.slice();
173
- this.createDevicesQueue.push(dfd);
174
- await (0, utils_1.promiseAllSequence)(prevQueue.map(pr => () => pr.promise));
175
- return this.createDevicesQueue.includes(dfd);
119
+ onDeviceDisconnected(descriptor) {
120
+ const { path } = descriptor;
121
+ const device = this.devices[path];
122
+ if (device) {
123
+ device.disconnect();
124
+ delete this.devices[path];
125
+ }
176
126
  }
177
- removeFromCreateDevicesQueue(path) {
178
- const index = this.createDevicesQueue.findIndex(dfd => dfd.id === path);
179
- if (index >= 0) {
180
- const [dfd] = this.createDevicesQueue.splice(index, 1);
181
- dfd.resolve();
127
+ onDeviceSessionChanged(descriptor) {
128
+ const device = this.devices[descriptor.path];
129
+ if (device) {
130
+ device.updateDescriptor(descriptor);
182
131
  }
183
132
  }
133
+ onDeviceRequestRelease(descriptor) {
134
+ var _a;
135
+ (_a = this.devices[descriptor.path]) === null || _a === void 0 ? void 0 : _a.usedElsewhere();
136
+ }
184
137
  init(initParams = {}) {
185
138
  if (!this.initPromise) {
186
139
  _log.debug('Initializing transports');
@@ -207,7 +160,9 @@ class DeviceList extends utils_1.TypedEmitter {
207
160
  createReconnectPromise(initParams) {
208
161
  const { promise, reject } = (0, promiseUtils_1.resolveAfter)(1000, initParams);
209
162
  this.rejectPending = reject;
210
- return promise.then(this.createInitPromise.bind(this));
163
+ return promise.then(this.createInitPromise.bind(this)).finally(() => {
164
+ this.rejectPending = undefined;
165
+ });
211
166
  }
212
167
  async selectTransport([transport, ...rest]) {
213
168
  const result = await transport.init();
@@ -219,7 +174,10 @@ class DeviceList extends utils_1.TypedEmitter {
219
174
  throw new Error(result.error);
220
175
  }
221
176
  async initializeTransport(transport, initParams) {
222
- transport.on(transport_1.TRANSPORT.UPDATE, diff => this.onTransportUpdate(diff, transport));
177
+ transport.on(transport_1.TRANSPORT.DEVICE_CONNECTED, d => this.onDeviceConnected(d, transport));
178
+ transport.on(transport_1.TRANSPORT.DEVICE_DISCONNECTED, this.onDeviceDisconnected.bind(this));
179
+ transport.on(transport_1.TRANSPORT.DEVICE_SESSION_CHANGED, this.onDeviceSessionChanged.bind(this));
180
+ transport.on(transport_1.TRANSPORT.DEVICE_REQUEST_RELEASE, this.onDeviceRequestRelease.bind(this));
223
181
  transport.on(transport_1.TRANSPORT.ERROR, error => {
224
182
  this.cleanup();
225
183
  this.emit(transport_1.TRANSPORT.ERROR, error);
@@ -260,36 +218,17 @@ class DeviceList extends utils_1.TypedEmitter {
260
218
  this.off(events_1.DEVICE.CONNECT_UNACQUIRED, onDeviceConnect);
261
219
  });
262
220
  }
263
- async _createAndSaveDevice(descriptor, transport) {
264
- _log.debug('Creating Device', descriptor);
265
- await this.handle(descriptor, transport);
266
- }
267
- _createUnacquiredDevice(descriptor, transport) {
268
- _log.debug('Creating Unacquired Device', descriptor);
269
- const device = Device_1.Device.createUnacquired(transport, descriptor);
270
- device.once(events_1.DEVICE.ACQUIRED, () => {
271
- this.emit(events_1.DEVICE.CONNECT, device.toMessageObject());
272
- });
273
- return device;
274
- }
275
- _createUnreadableDevice(descriptor, transport, unreadableError) {
276
- _log.debug('Creating Unreadable Device', descriptor, unreadableError);
277
- return Device_1.Device.createUnacquired(transport, descriptor, unreadableError);
221
+ getDeviceCount() {
222
+ return Object.keys(this.devices).length;
278
223
  }
279
- getDevice(path) {
280
- return this.devices[path];
224
+ getAllDevices() {
225
+ return (0, utils_2.typedObjectKeys)(this.devices).map(key => this.devices[key]);
281
226
  }
282
- getFirstDevicePath() {
283
- return this.asArray()[0].path;
227
+ getOnlyDevice() {
228
+ return this.getDeviceCount() === 1 ? Object.values(this.devices)[0] : undefined;
284
229
  }
285
- asArray() {
286
- return this.allDevices().map(device => device.toMessageObject());
287
- }
288
- allDevices() {
289
- return Object.keys(this.devices).map(key => this.devices[key]);
290
- }
291
- length() {
292
- return this.asArray().length;
230
+ getDeviceByPath(path) {
231
+ return this.getAllDevices().find(d => d.getUniquePath() === path);
293
232
  }
294
233
  transportType() {
295
234
  return this.transport.name;
@@ -308,16 +247,14 @@ class DeviceList extends utils_1.TypedEmitter {
308
247
  async cleanup() {
309
248
  var _a;
310
249
  const { transport } = this;
311
- const devices = this.allDevices();
250
+ const devices = this.getAllDevices();
312
251
  this.transport = undefined;
313
252
  this.authPenaltyManager.clear();
314
- Object.keys(this.devices).forEach(key => delete this.devices[key]);
253
+ (0, utils_2.typedObjectKeys)(this.devices).forEach(key => delete this.devices[key]);
315
254
  (_a = this.rejectPending) === null || _a === void 0 ? void 0 : _a.call(this, new Error('Disposed'));
316
255
  devices.forEach(device => {
317
256
  this.emit(events_1.DEVICE.DISCONNECT, device.toMessageObject());
318
257
  });
319
- this.createDevicesQueue.forEach(dfd => dfd.resolve());
320
- this.createDevicesQueue = [];
321
258
  await Promise.all(devices.map(device => device.dispose()));
322
259
  transport === null || transport === void 0 ? void 0 : transport.stop();
323
260
  }
@@ -327,9 +264,8 @@ class DeviceList extends utils_1.TypedEmitter {
327
264
  return;
328
265
  }
329
266
  res.payload.forEach(d => {
330
- if (this.devices[d.path]) {
331
- this.devices[d.path].updateDescriptor(d);
332
- }
267
+ var _a;
268
+ (_a = this.devices[d.path]) === null || _a === void 0 ? void 0 : _a.updateDescriptor(d);
333
269
  });
334
270
  }
335
271
  addAuthPenalty(device) {
@@ -338,56 +274,6 @@ class DeviceList extends utils_1.TypedEmitter {
338
274
  removeAuthPenalty(device) {
339
275
  return this.authPenaltyManager.remove(device);
340
276
  }
341
- async handle(descriptor, transport) {
342
- var _a;
343
- const path = descriptor.path.toString();
344
- try {
345
- await this._takeAndCreateDevice(descriptor, transport);
346
- }
347
- catch (error) {
348
- _log.debug('Cannot create device', error);
349
- if (error.code === 'Device_NotFound' ||
350
- error.message === transport_1.TRANSPORT_ERROR.DEVICE_NOT_FOUND ||
351
- error.message === transport_1.TRANSPORT_ERROR.DEVICE_DISCONNECTED_DURING_ACTION ||
352
- error.message === transport_1.TRANSPORT_ERROR.UNEXPECTED_ERROR ||
353
- error.message === transport_1.TRANSPORT_ERROR.DESCRIPTOR_NOT_FOUND ||
354
- error.message === transport_1.TRANSPORT_ERROR.HTTP_ERROR) {
355
- delete this.devices[path];
356
- }
357
- else if (error.message === transport_1.TRANSPORT_ERROR.SESSION_WRONG_PREVIOUS) {
358
- this.enumerate(transport);
359
- this._handleUsedElsewhere(descriptor, transport);
360
- }
361
- else if (error.message === transport_1.TRANSPORT_ERROR.INTERFACE_UNABLE_TO_OPEN_DEVICE ||
362
- ((_a = error.message) === null || _a === void 0 ? void 0 : _a.indexOf(constants_1.ERRORS.LIBUSB_ERROR_MESSAGE)) >= 0 ||
363
- error.code === 'Device_InitializeFailed') {
364
- const device = this._createUnreadableDevice(this.creatingDevicesDescriptors[path], transport, error.message);
365
- this.devices[path] = device;
366
- this.emit(events_1.DEVICE.CONNECT_UNACQUIRED, device.toMessageObject());
367
- }
368
- else if (error.code === 'Device_UsedElsewhere') {
369
- this._handleUsedElsewhere(descriptor, transport);
370
- }
371
- else {
372
- await (0, promiseUtils_1.resolveAfter)(501, null).promise;
373
- await this.handle(descriptor, transport);
374
- }
375
- }
376
- }
377
- async _takeAndCreateDevice(descriptor, transport) {
378
- const device = Device_1.Device.fromDescriptor(transport, descriptor);
379
- const path = descriptor.path.toString();
380
- this.devices[path] = device;
381
- const promise = device.run();
382
- await promise;
383
- this.emit(events_1.DEVICE.CONNECT, device.toMessageObject());
384
- }
385
- _handleUsedElsewhere(descriptor, transport) {
386
- const path = descriptor.path.toString();
387
- const device = this._createUnacquiredDevice(this.creatingDevicesDescriptors[path], transport);
388
- this.devices[path] = device;
389
- this.emit(events_1.DEVICE.CONNECT_UNACQUIRED, device.toMessageObject());
390
- }
391
277
  }
392
278
  exports.DeviceList = DeviceList;
393
279
  //# sourceMappingURL=DeviceList.js.map
@@ -5,14 +5,15 @@ const transport_1 = require("@trezor/transport");
5
5
  const constants_1 = require("../constants");
6
6
  const events_1 = require("../events");
7
7
  const cancelPrompt = (device, expectResponse = true) => {
8
- if (!device.transportSession) {
8
+ const session = device.getLocalSession();
9
+ if (!session) {
9
10
  return Promise.resolve({
10
11
  success: false,
11
12
  error: transport_1.TRANSPORT_ERROR.SESSION_NOT_FOUND,
12
13
  });
13
14
  }
14
15
  const cancelArgs = {
15
- session: device.transportSession,
16
+ session,
16
17
  name: 'Cancel',
17
18
  data: {},
18
19
  protocol: device.protocol,
@@ -1,5 +1,5 @@
1
- import { HDNodeResponse } from '../types/api/getPublicKey';
2
1
  import { MessagesSchema as Messages } from '@trezor/protobuf';
2
+ import { HDNodeResponse } from '../types/api/getPublicKey';
3
3
  interface Params {
4
4
  response: HDNodeResponse;
5
5
  publicKey: Messages.PublicKey;
@@ -2,15 +2,15 @@ import type { IFRAME } from './iframe';
2
2
  import type { TrezorConnect } from '../types/api';
3
3
  import type { CommonParams, DeviceIdentity } from '../types/params';
4
4
  import { Device } from '../device/Device';
5
- type UnwrappedResponse<T> = T extends Promise<infer R> ? (R extends {
5
+ type UnwrappedResponse<Response> = Response extends Promise<infer R> ? R extends {
6
6
  success: true;
7
7
  payload: infer P;
8
- } ? P : never) : void;
9
- type OverloadedMethod<T, E extends Record<string, string>> = T extends {
8
+ } ? P : never : void;
9
+ type OverloadedMethod<Method, Params extends Record<string, string>> = Method extends {
10
10
  (params: infer P1): infer R1;
11
11
  (params: infer P2): infer R2;
12
- } ? ((params: P1 & E) => R1) | ((params: P2 & E) => R2) : T extends (...args: infer P) => infer R ? (params: E & P[0]) => R : never;
13
- type UnwrappedMethod<T, M extends Record<string, string>> = T extends () => infer R ? (params: M & CommonParams) => R : OverloadedMethod<T, M>;
12
+ } ? ((params: P1 & Params) => R1) | ((params: P2 & Params) => R2) : Method extends (...args: infer P) => infer R ? (params: Params & P[0]) => R : never;
13
+ type UnwrappedMethod<Method, Params extends Record<string, string>> = Method extends () => infer R ? (params: Params & CommonParams) => R : OverloadedMethod<Method, Params>;
14
14
  type IsMethodCallable<T> = T extends (...args: any[]) => infer R ? R extends Promise<{
15
15
  success: boolean;
16
16
  }> ? R : never : never;
@@ -11,9 +11,9 @@ const createResponseMessage = (id, success, payload, device) => ({
11
11
  payload: success ? payload : (0, errors_1.serializeError)(payload),
12
12
  device: device
13
13
  ? {
14
- path: device === null || device === void 0 ? void 0 : device.originalDescriptor.path,
14
+ path: device === null || device === void 0 ? void 0 : device.getUniquePath(),
15
15
  state: device === null || device === void 0 ? void 0 : device.getState(),
16
- instance: device === null || device === void 0 ? void 0 : device.instance,
16
+ instance: device === null || device === void 0 ? void 0 : device.getInstance(),
17
17
  }
18
18
  : undefined,
19
19
  });
@@ -7,12 +7,6 @@ export declare const DEVICE: {
7
7
  readonly CONNECT_UNACQUIRED: "device-connect_unacquired";
8
8
  readonly DISCONNECT: "device-disconnect";
9
9
  readonly CHANGED: "device-changed";
10
- readonly ACQUIRE: "device-acquire";
11
- readonly RELEASE: "device-release";
12
- readonly ACQUIRED: "device-acquired";
13
- readonly RELEASED: "device-released";
14
- readonly USED_ELSEWHERE: "device-used_elsewhere";
15
- readonly LOADING: "device-loading";
16
10
  readonly BUTTON: "button";
17
11
  readonly PIN: "pin";
18
12
  readonly PASSPHRASE: "passphrase";
@@ -7,12 +7,6 @@ exports.DEVICE = {
7
7
  CONNECT_UNACQUIRED: 'device-connect_unacquired',
8
8
  DISCONNECT: 'device-disconnect',
9
9
  CHANGED: 'device-changed',
10
- ACQUIRE: 'device-acquire',
11
- RELEASE: 'device-release',
12
- ACQUIRED: 'device-acquired',
13
- RELEASED: 'device-released',
14
- USED_ELSEWHERE: 'device-used_elsewhere',
15
- LOADING: 'device-loading',
16
10
  BUTTON: 'button',
17
11
  PIN: 'pin',
18
12
  PASSPHRASE: 'passphrase',
@@ -1,6 +1,6 @@
1
- import type { MessageFactoryFn } from '../types/utils';
2
1
  import type { Transport } from '@trezor/transport';
3
2
  import { TRANSPORT } from '@trezor/transport/lib/constants';
3
+ import type { MessageFactoryFn } from '../types/utils';
4
4
  export { TRANSPORT } from '@trezor/transport/lib/constants';
5
5
  export declare const TRANSPORT_EVENT = "TRANSPORT_EVENT";
6
6
  export interface BridgeInfo {
package/lib/factory.d.ts CHANGED
@@ -1,18 +1,18 @@
1
1
  import type { EventEmitter } from 'events';
2
2
  import type { TrezorConnect } from './types';
3
3
  import type { CallMethod } from './events/call';
4
- export interface ConnectFactoryDependencies {
4
+ import type { InitType } from './types/api/init';
5
+ export interface ConnectFactoryDependencies<SettingsType extends Record<string, any>> {
6
+ init: InitType<SettingsType>;
5
7
  call: CallMethod;
6
8
  eventEmitter: EventEmitter;
7
9
  manifest: TrezorConnect['manifest'];
8
- init: TrezorConnect['init'];
9
10
  requestLogin: TrezorConnect['requestLogin'];
10
11
  uiResponse: TrezorConnect['uiResponse'];
11
- renderWebUSBButton: TrezorConnect['renderWebUSBButton'];
12
- disableWebUSB: TrezorConnect['disableWebUSB'];
13
- requestWebUSBDevice: TrezorConnect['requestWebUSBDevice'];
14
12
  cancel: TrezorConnect['cancel'];
15
13
  dispose: TrezorConnect['dispose'];
16
14
  }
17
- export declare const factory: ({ eventEmitter, manifest, init, call, requestLogin, uiResponse, renderWebUSBButton, disableWebUSB, requestWebUSBDevice, cancel, dispose, }: ConnectFactoryDependencies) => TrezorConnect;
15
+ export declare const factory: <SettingsType extends Record<string, any>, ExtraMethodsType extends Record<string, any>>({ eventEmitter, manifest, init, call, requestLogin, uiResponse, cancel, dispose, }: ConnectFactoryDependencies<SettingsType>, extraMethods?: ExtraMethodsType) => Omit<TrezorConnect, "init"> & {
16
+ init: InitType<SettingsType>;
17
+ } & ExtraMethodsType;
18
18
  //# sourceMappingURL=factory.d.ts.map