@bytezhang/hardware-trezor-adapter 0.0.30 → 0.0.32

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.
package/dist/index.js CHANGED
@@ -20,739 +20,19 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
- TrezorAdapter: () => TrezorAdapter,
24
- TrezorProxyClient: () => TrezorProxyClient
23
+ TrezorService: () => TrezorService,
24
+ VERSION: () => VERSION
25
25
  });
26
26
  module.exports = __toCommonJS(index_exports);
27
-
28
- // src/TrezorAdapter.ts
29
- var import_hardware_wallet_core = require("@bytezhang/hardware-wallet-core");
30
- var TrezorAdapter = class {
31
- constructor(connector) {
32
- this.vendor = "trezor";
33
- this.emitter = new import_hardware_wallet_core.TypedEventEmitter();
34
- this._uiHandler = null;
35
- // Device cache: tracks discovered devices from connector events
36
- this._discoveredDevices = /* @__PURE__ */ new Map();
37
- // Session tracking: maps connectId -> sessionId
38
- this._sessions = /* @__PURE__ */ new Map();
39
- // ─── Chain capability accessors ────────────────────────────
40
- this._evmMethods = {
41
- evmGetAddress: (connectId, deviceId, params) => this._evmGetAddress(connectId, deviceId, params),
42
- evmGetAddresses: (connectId, deviceId, params, onProgress) => this._evmGetAddresses(connectId, deviceId, params, onProgress),
43
- evmGetPublicKey: (connectId, deviceId, params) => this._evmGetPublicKey(connectId, deviceId, params),
44
- evmSignTransaction: (connectId, deviceId, params) => this._evmSignTransaction(connectId, deviceId, params),
45
- evmSignMessage: (connectId, deviceId, params) => this._evmSignMessage(connectId, deviceId, params),
46
- evmSignTypedData: (connectId, deviceId, params) => this._evmSignTypedData(connectId, deviceId, params)
47
- };
48
- this._btcMethods = {
49
- btcGetAddress: (connectId, deviceId, params) => this._btcGetAddress(connectId, deviceId, params),
50
- btcGetAddresses: (connectId, deviceId, params, onProgress) => this._btcGetAddresses(connectId, deviceId, params, onProgress),
51
- btcGetPublicKey: (connectId, deviceId, params) => this._btcGetPublicKey(connectId, deviceId, params),
52
- btcSignTransaction: (connectId, deviceId, params) => this._btcSignTransaction(connectId, deviceId, params),
53
- btcSignMessage: (connectId, deviceId, params) => this._btcSignMessage(connectId, deviceId, params),
54
- btcGetMasterFingerprint: (connectId, deviceId) => this._btcGetMasterFingerprint(connectId, deviceId)
55
- };
56
- this._solMethods = {
57
- solGetAddress: (connectId, deviceId, params) => this._solGetAddress(connectId, deviceId, params),
58
- solGetAddresses: (connectId, deviceId, params, onProgress) => this._solGetAddresses(connectId, deviceId, params, onProgress),
59
- solGetPublicKey: (connectId, deviceId, params) => this._solGetPublicKey(connectId, deviceId, params),
60
- solSignTransaction: (connectId, deviceId, params) => this._solSignTransaction(connectId, deviceId, params),
61
- solSignMessage: (connectId, deviceId, params) => this._solSignMessage(connectId, deviceId, params)
62
- };
63
- // ─── Event translation ────────────────────────────────────
64
- this.deviceConnectHandler = (data) => {
65
- const deviceInfo = this.connectorDeviceToDeviceInfo(data.device);
66
- this._discoveredDevices.set(deviceInfo.connectId, deviceInfo);
67
- this.emitter.emit(import_hardware_wallet_core.DEVICE.CONNECT, {
68
- type: import_hardware_wallet_core.DEVICE.CONNECT,
69
- payload: deviceInfo
70
- });
71
- };
72
- this.deviceDisconnectHandler = (data) => {
73
- this._discoveredDevices.delete(data.connectId);
74
- this.emitter.emit(import_hardware_wallet_core.DEVICE.DISCONNECT, {
75
- type: import_hardware_wallet_core.DEVICE.DISCONNECT,
76
- payload: { connectId: data.connectId }
77
- });
78
- };
79
- this.uiRequestHandler = (data) => {
80
- this.handleUiEvent(data);
81
- };
82
- this.uiEventHandler = (data) => {
83
- this.handleUiEvent(data);
84
- };
85
- this.connector = connector;
86
- this.registerEventListeners();
87
- }
88
- // ─── Transport ──────────────────────────────────────────
89
- // Transport is decided at connector creation time. These methods
90
- // satisfy the IHardwareWallet interface with sensible defaults.
91
- get activeTransport() {
92
- return "usb";
93
- }
94
- getAvailableTransports() {
95
- return ["usb"];
96
- }
97
- async switchTransport(_type) {
98
- }
99
- // ─── UI handler ────────────────────────────────────────────
100
- setUiHandler(handler) {
101
- this._uiHandler = handler;
102
- }
103
- // ─── Lifecycle ────────────────────────────────────────────
104
- async init(_config) {
105
- }
106
- async dispose() {
107
- this.unregisterEventListeners();
108
- this.connector.reset();
109
- this._uiHandler = null;
110
- this._discoveredDevices.clear();
111
- this._sessions.clear();
112
- this.emitter.removeAllListeners();
113
- }
114
- // ─── Device management ────────────────────────────────────
115
- async searchDevices() {
116
- await this._ensureDevicePermission();
117
- const devices = await this.connector.searchDevices();
118
- for (const d of devices) {
119
- if (d.connectId && !this._discoveredDevices.has(d.connectId)) {
120
- this._discoveredDevices.set(d.connectId, this.connectorDeviceToDeviceInfo(d));
121
- }
122
- }
123
- if (this._discoveredDevices.size === 0) {
124
- await this._ensureDevicePermission();
125
- }
126
- return Array.from(this._discoveredDevices.values());
127
- }
128
- async connectDevice(connectId) {
129
- try {
130
- const session = await this.connector.connect(connectId);
131
- this._sessions.set(connectId, session.sessionId);
132
- if (session.deviceInfo) {
133
- this._discoveredDevices.set(connectId, session.deviceInfo);
134
- }
135
- return (0, import_hardware_wallet_core.success)(connectId);
136
- } catch (err) {
137
- return this.errorToFailure(err);
138
- }
139
- }
140
- async disconnectDevice(connectId) {
141
- const sessionId = this._sessions.get(connectId);
142
- if (sessionId) {
143
- await this.connector.disconnect(sessionId);
144
- this._sessions.delete(connectId);
145
- }
146
- }
147
- async getDeviceInfo(connectId, deviceId) {
148
- await this._ensureDevicePermission(connectId, deviceId);
149
- const cached = this._discoveredDevices.get(connectId) ?? Array.from(this._discoveredDevices.values()).find(
150
- (d) => d.deviceId === deviceId
151
- );
152
- if (cached) {
153
- return (0, import_hardware_wallet_core.success)(cached);
154
- }
155
- return (0, import_hardware_wallet_core.failure)(
156
- import_hardware_wallet_core.HardwareErrorCode.DeviceNotFound,
157
- "Device not found in cache. Call searchDevices() or wait for a device-connected event first."
158
- );
159
- }
160
- getSupportedChains() {
161
- return ["evm", "btc", "sol"];
162
- }
163
- on(event, listener) {
164
- this.emitter.on(event, listener);
165
- }
166
- off(event, listener) {
167
- this.emitter.off(event, listener);
168
- }
169
- cancel(connectId) {
170
- const sessionId = this._sessions.get(connectId) ?? connectId;
171
- void this.connector.cancel(sessionId);
172
- }
173
- // ─── Chain fingerprint ────────────────────────────────────
174
- /**
175
- * For Trezor, the chain fingerprint is the hardware device_id from firmware.
176
- * Trezor has a persistent device identity, so no address derivation is needed.
177
- */
178
- async getChainFingerprint(connectId, deviceId, _chain) {
179
- await this._ensureDevicePermission(connectId, deviceId);
180
- const cached = this._discoveredDevices.get(connectId) ?? Array.from(this._discoveredDevices.values()).find(
181
- (d) => d.deviceId === deviceId
182
- );
183
- if (cached?.deviceId) {
184
- return (0, import_hardware_wallet_core.success)(cached.deviceId);
185
- }
186
- try {
187
- const result = await this.connectorCall(connectId, "getFeatures", {});
188
- if (result.device_id) {
189
- return (0, import_hardware_wallet_core.success)(result.device_id);
190
- }
191
- } catch {
192
- }
193
- return (0, import_hardware_wallet_core.failure)(
194
- import_hardware_wallet_core.HardwareErrorCode.DeviceNotFound,
195
- "Could not determine Trezor device identity. Ensure the device is connected."
196
- );
197
- }
198
- evm() {
199
- return this._evmMethods;
200
- }
201
- btc() {
202
- return this._btcMethods;
203
- }
204
- sol() {
205
- return this._solMethods;
206
- }
207
- tron() {
208
- return null;
209
- }
210
- // ─── EVM methods (private) ────────────────────────────────
211
- async _evmGetAddress(connectId, _deviceId, params) {
212
- await this._ensureDevicePermission(connectId, _deviceId);
213
- try {
214
- const result = await this.connectorCall(connectId, "evmGetAddress", {
215
- path: params.path,
216
- showOnDevice: params.showOnDevice,
217
- chainId: params.chainId
218
- });
219
- return (0, import_hardware_wallet_core.success)({
220
- address: result.address,
221
- path: params.path
222
- });
223
- } catch (err) {
224
- return this.errorToFailure(err);
225
- }
226
- }
227
- async _evmGetAddresses(connectId, deviceId, params, onProgress) {
228
- return this.batchCall(
229
- params,
230
- (p) => this._evmGetAddress(connectId, deviceId, p),
231
- onProgress
232
- );
233
- }
234
- async _evmGetPublicKey(connectId, _deviceId, params) {
235
- await this._ensureDevicePermission(connectId, _deviceId);
236
- try {
237
- const result = await this.connectorCall(connectId, "evmGetPublicKey", {
238
- path: params.path,
239
- showOnDevice: params.showOnDevice
240
- });
241
- return (0, import_hardware_wallet_core.success)({
242
- publicKey: result.publicKey,
243
- path: params.path
244
- });
245
- } catch (err) {
246
- return this.errorToFailure(err);
247
- }
248
- }
249
- async _evmSignTransaction(connectId, _deviceId, params) {
250
- await this._ensureDevicePermission(connectId, _deviceId);
251
- try {
252
- const result = await this.connectorCall(connectId, "evmSignTransaction", {
253
- path: params.path,
254
- transaction: {
255
- to: params.to,
256
- value: params.value,
257
- chainId: params.chainId,
258
- nonce: params.nonce,
259
- gasLimit: params.gasLimit,
260
- gasPrice: params.gasPrice,
261
- maxFeePerGas: params.maxFeePerGas,
262
- maxPriorityFeePerGas: params.maxPriorityFeePerGas,
263
- accessList: params.accessList,
264
- data: params.data
265
- }
266
- });
267
- return (0, import_hardware_wallet_core.success)({
268
- v: this.ensure0x(result.v),
269
- r: this.padHex64(result.r),
270
- s: this.padHex64(result.s),
271
- serializedTx: result.serializedTx
272
- });
273
- } catch (err) {
274
- return this.errorToFailure(err);
275
- }
276
- }
277
- async _evmSignMessage(connectId, _deviceId, params) {
278
- await this._ensureDevicePermission(connectId, _deviceId);
279
- try {
280
- const result = await this.connectorCall(connectId, "evmSignMessage", {
281
- path: params.path,
282
- message: params.message,
283
- hex: params.hex
284
- });
285
- return (0, import_hardware_wallet_core.success)({
286
- signature: this.ensure0x(result.signature),
287
- address: result.address
288
- });
289
- } catch (err) {
290
- return this.errorToFailure(err);
291
- }
292
- }
293
- async _evmSignTypedData(connectId, _deviceId, params) {
294
- await this._ensureDevicePermission(connectId, _deviceId);
295
- try {
296
- const callParams = {
297
- path: params.path
298
- };
299
- if (params.mode !== "hash") {
300
- callParams["mode"] = params.mode ?? "full";
301
- callParams["data"] = params.data;
302
- callParams["metamaskV4Compat"] = params.metamaskV4Compat ?? true;
303
- } else {
304
- callParams["mode"] = "hash";
305
- callParams["domainSeparatorHash"] = params.domainSeparatorHash;
306
- callParams["messageHash"] = params.messageHash;
307
- }
308
- const result = await this.connectorCall(connectId, "evmSignTypedData", callParams);
309
- return (0, import_hardware_wallet_core.success)({
310
- signature: this.ensure0x(result.signature),
311
- address: result.address
312
- });
313
- } catch (err) {
314
- return this.errorToFailure(err);
315
- }
316
- }
317
- // ─── BTC methods (private) ─────────────────────────────────
318
- async _btcGetAddress(connectId, _deviceId, params) {
319
- await this._ensureDevicePermission(connectId, _deviceId);
320
- try {
321
- const result = await this.connectorCall(connectId, "btcGetAddress", {
322
- path: params.path,
323
- coin: params.coin,
324
- showOnDevice: params.showOnDevice,
325
- scriptType: params.scriptType
326
- });
327
- return (0, import_hardware_wallet_core.success)({
328
- address: result.address,
329
- path: params.path
330
- });
331
- } catch (err) {
332
- return this.errorToFailure(err);
333
- }
334
- }
335
- async _btcGetAddresses(connectId, deviceId, params, onProgress) {
336
- return this.batchCall(
337
- params,
338
- (p) => this._btcGetAddress(connectId, deviceId, p),
339
- onProgress
340
- );
341
- }
342
- async _btcGetPublicKey(connectId, _deviceId, params) {
343
- await this._ensureDevicePermission(connectId, _deviceId);
344
- try {
345
- const result = await this.connectorCall(connectId, "btcGetPublicKey", {
346
- path: params.path,
347
- coin: params.coin,
348
- showOnDevice: params.showOnDevice
349
- });
350
- return (0, import_hardware_wallet_core.success)({
351
- xpub: result.xpub,
352
- publicKey: result.publicKey,
353
- fingerprint: result.fingerprint,
354
- chainCode: result.chainCode,
355
- path: params.path,
356
- depth: result.depth
357
- });
358
- } catch (err) {
359
- return this.errorToFailure(err);
360
- }
361
- }
362
- async _btcSignTransaction(connectId, _deviceId, params) {
363
- await this._ensureDevicePermission(connectId, _deviceId);
364
- try {
365
- const result = await this.connectorCall(connectId, "btcSignTransaction", {
366
- inputs: params.inputs ?? [],
367
- outputs: params.outputs ?? [],
368
- refTxs: params.refTxs,
369
- coin: params.coin,
370
- locktime: params.locktime,
371
- version: params.version
372
- });
373
- return (0, import_hardware_wallet_core.success)({
374
- signatures: result.signatures,
375
- serializedTx: result.serializedTx,
376
- txid: result.txid
377
- });
378
- } catch (err) {
379
- return this.errorToFailure(err);
380
- }
381
- }
382
- async _btcSignMessage(connectId, _deviceId, params) {
383
- await this._ensureDevicePermission(connectId, _deviceId);
384
- try {
385
- const result = await this.connectorCall(connectId, "btcSignMessage", {
386
- path: params.path,
387
- message: params.message,
388
- coin: params.coin
389
- });
390
- return (0, import_hardware_wallet_core.success)({
391
- signature: result.signature,
392
- address: result.address
393
- });
394
- } catch (err) {
395
- return this.errorToFailure(err);
396
- }
397
- }
398
- async _btcGetMasterFingerprint(connectId, _deviceId) {
399
- await this._ensureDevicePermission(connectId, _deviceId);
400
- try {
401
- const result = await this.connectorCall(connectId, "btcGetPublicKey", {
402
- path: "m/0'"
403
- });
404
- const fp = result.fingerprint >>> 0;
405
- const hex = fp.toString(16).padStart(8, "0");
406
- return (0, import_hardware_wallet_core.success)({ masterFingerprint: hex });
407
- } catch (err) {
408
- return this.errorToFailure(err);
409
- }
410
- }
411
- // ─── Solana methods (private) ──────────────────────────────
412
- async _solGetAddress(connectId, _deviceId, params) {
413
- await this._ensureDevicePermission(connectId, _deviceId);
414
- try {
415
- const result = await this.connectorCall(connectId, "solGetAddress", {
416
- path: params.path,
417
- showOnDevice: params.showOnDevice
418
- });
419
- return (0, import_hardware_wallet_core.success)({
420
- address: result.address,
421
- path: params.path
422
- });
423
- } catch (err) {
424
- return this.errorToFailure(err);
425
- }
426
- }
427
- async _solGetAddresses(connectId, deviceId, params, onProgress) {
428
- return this.batchCall(
429
- params,
430
- (p) => this._solGetAddress(connectId, deviceId, p),
431
- onProgress
432
- );
433
- }
434
- async _solGetPublicKey(connectId, _deviceId, params) {
435
- await this._ensureDevicePermission(connectId, _deviceId);
436
- try {
437
- const result = await this.connectorCall(connectId, "solGetAddress", {
438
- path: params.path,
439
- showOnDevice: params.showOnDevice
440
- });
441
- return (0, import_hardware_wallet_core.success)({
442
- publicKey: result.address,
443
- path: params.path
444
- });
445
- } catch (err) {
446
- return this.errorToFailure(err);
447
- }
448
- }
449
- async _solSignTransaction(connectId, _deviceId, params) {
450
- await this._ensureDevicePermission(connectId, _deviceId);
451
- try {
452
- const result = await this.connectorCall(connectId, "solSignTransaction", {
453
- path: params.path,
454
- serializedTx: params.serializedTx,
455
- additionalInfo: params.additionalInfo
456
- });
457
- return (0, import_hardware_wallet_core.success)({
458
- signature: result.signature
459
- });
460
- } catch (err) {
461
- return this.errorToFailure(err);
462
- }
463
- }
464
- async _solSignMessage(_connectId, _deviceId, _params) {
465
- return (0, import_hardware_wallet_core.failure)(
466
- import_hardware_wallet_core.HardwareErrorCode.MethodNotSupported,
467
- "Solana signMessage is not supported by Trezor Connect"
468
- );
469
- }
470
- // ─── Private helpers ──────────────────────────────────────
471
- /**
472
- * Call the connector with session resolution.
473
- * Looks up sessionId from connectId, falls back to connectId itself.
474
- */
475
- async connectorCall(connectId, method, params) {
476
- const sessionId = this._sessions.get(connectId) ?? connectId;
477
- return this.connector.call(sessionId, method, params);
478
- }
479
- /**
480
- * Ensure device permission before proceeding.
481
- * - No connectId (searchDevices): check environment-level permission
482
- * - With connectId (business methods): check device-level permission
483
- * If not granted, calls onDevicePermission so the consumer can request access.
484
- */
485
- async _ensureDevicePermission(connectId, deviceId) {
486
- const transportType = "usb";
487
- let granted = false;
488
- let context;
489
- if (this._uiHandler?.checkDevicePermission) {
490
- try {
491
- const result = await this._uiHandler.checkDevicePermission({ transportType, connectId, deviceId });
492
- granted = result.granted;
493
- context = result.context;
494
- } catch {
495
- granted = false;
496
- }
497
- }
498
- if (!granted) {
499
- try {
500
- await this._uiHandler?.onDevicePermission?.({ transportType, context });
501
- } catch {
502
- }
503
- }
504
- }
505
- /**
506
- * Convert a thrown error to a Response failure.
507
- * Parses TrezorConnect error strings to map to HardwareErrorCode values.
508
- */
509
- errorToFailure(err) {
510
- const message = err instanceof Error ? err.message : String(err);
511
- const code = this.parseErrorCode(message);
512
- const enriched = this.enrichErrorMessage(code, message);
513
- return (0, import_hardware_wallet_core.failure)(code, enriched);
514
- }
515
- /**
516
- * Parse TrezorConnect error codes from error message strings.
517
- * The connector throws errors with messages like "Trezor ethereumGetAddress failed: <error>".
518
- * We also check for embedded code patterns.
519
- */
520
- parseErrorCode(message) {
521
- if (message.includes("Failure_ActionCancelled") || message.includes("Failure_Cancel")) {
522
- return import_hardware_wallet_core.HardwareErrorCode.UserRejected;
523
- }
524
- if (message.includes("Failure_PinInvalid") || message.includes("Failure_PinMismatch")) {
525
- return import_hardware_wallet_core.HardwareErrorCode.PinInvalid;
526
- }
527
- if (message.includes("Failure_PinCancelled")) {
528
- return import_hardware_wallet_core.HardwareErrorCode.PinCancelled;
529
- }
530
- if (message.includes("Failure_PassphraseRejected")) {
531
- return import_hardware_wallet_core.HardwareErrorCode.PassphraseRejected;
532
- }
533
- if (message.includes("Device_UsedElsewhere")) {
534
- return import_hardware_wallet_core.HardwareErrorCode.DeviceBusy;
535
- }
536
- if (message.includes("Device_NotFound")) {
537
- return import_hardware_wallet_core.HardwareErrorCode.DeviceNotFound;
538
- }
539
- if (message.includes("Device_InvalidState")) {
540
- return import_hardware_wallet_core.HardwareErrorCode.DeviceNotInitialized;
541
- }
542
- if (message.includes("Transport_Missing")) {
543
- return import_hardware_wallet_core.HardwareErrorCode.TransportNotAvailable;
544
- }
545
- if (message.includes("Transport_DeviceDisconnected")) {
546
- return import_hardware_wallet_core.HardwareErrorCode.DeviceDisconnected;
547
- }
548
- if (message.includes("Failure_FirmwareError")) {
549
- return import_hardware_wallet_core.HardwareErrorCode.FirmwareTooOld;
550
- }
551
- if (message.includes("Method_InvalidParameter") || message.includes("Method_InvalidParams")) {
552
- return import_hardware_wallet_core.HardwareErrorCode.InvalidParams;
553
- }
554
- if (message.includes("Method_NotAllowed")) {
555
- return import_hardware_wallet_core.HardwareErrorCode.MethodNotSupported;
556
- }
557
- return import_hardware_wallet_core.HardwareErrorCode.UnknownError;
558
- }
559
- /**
560
- * Enrich error messages with actionable recovery info for the caller.
561
- */
562
- enrichErrorMessage(code, originalMessage) {
563
- switch (code) {
564
- case import_hardware_wallet_core.HardwareErrorCode.PinInvalid:
565
- return `${originalMessage}. Please re-enter your PIN.`;
566
- case import_hardware_wallet_core.HardwareErrorCode.PinCancelled:
567
- return `${originalMessage}. PIN entry was cancelled.`;
568
- case import_hardware_wallet_core.HardwareErrorCode.DeviceBusy:
569
- return `${originalMessage}. The device is in use by another application. Close other wallet apps and try again.`;
570
- case import_hardware_wallet_core.HardwareErrorCode.DeviceDisconnected:
571
- return `${originalMessage}. Please reconnect the device and try again.`;
572
- case import_hardware_wallet_core.HardwareErrorCode.TransportNotAvailable:
573
- return `${originalMessage}. Ensure Trezor Bridge is installed and running, or connect via USB.`;
574
- case import_hardware_wallet_core.HardwareErrorCode.FirmwareTooOld:
575
- return `${originalMessage}. Please update your Trezor firmware via Trezor Suite.`;
576
- case import_hardware_wallet_core.HardwareErrorCode.DeviceNotInitialized:
577
- return `${originalMessage}. The device may need to be set up first via Trezor Suite.`;
578
- default:
579
- return originalMessage;
580
- }
581
- }
582
- /**
583
- * Generic batch call with progress reporting.
584
- * If any single call fails, returns the failure immediately.
585
- */
586
- async batchCall(params, callFn, onProgress) {
587
- const results = [];
588
- for (let i = 0; i < params.length; i++) {
589
- const result = await callFn(params[i]);
590
- if (!result.success) {
591
- return result;
592
- }
593
- results.push(result.payload);
594
- onProgress?.({ index: i, total: params.length });
595
- }
596
- return (0, import_hardware_wallet_core.success)(results);
597
- }
598
- // ─── Hex formatting ──────────────────────────────────────
599
- /** Ensure a hex string has the `0x` prefix. */
600
- ensure0x(hex) {
601
- return hex.startsWith("0x") ? hex : `0x${hex}`;
602
- }
603
- /** Ensure a hex string is `0x`-prefixed and zero-padded to 64 hex chars (32 bytes). */
604
- padHex64(hex) {
605
- const stripped = hex.startsWith("0x") ? hex.slice(2) : hex;
606
- return `0x${stripped.padStart(64, "0")}`;
607
- }
608
- registerEventListeners() {
609
- this.connector.on("device-connect", this.deviceConnectHandler);
610
- this.connector.on("device-disconnect", this.deviceDisconnectHandler);
611
- this.connector.on("ui-request", this.uiRequestHandler);
612
- this.connector.on("ui-event", this.uiEventHandler);
613
- }
614
- unregisterEventListeners() {
615
- this.connector.off("device-connect", this.deviceConnectHandler);
616
- this.connector.off("device-disconnect", this.deviceDisconnectHandler);
617
- this.connector.off("ui-request", this.uiRequestHandler);
618
- this.connector.off("ui-event", this.uiEventHandler);
619
- }
620
- handleUiEvent(event) {
621
- if (!event.type) return;
622
- const payload = event.payload;
623
- const devicePayload = payload?.["device"] ?? payload;
624
- const deviceInfo = devicePayload ? this.extractDeviceInfoFromPayload(devicePayload) : this.unknownDevice();
625
- switch (event.type) {
626
- case "ui-request_pin":
627
- this.emitter.emit(import_hardware_wallet_core.UI_REQUEST.REQUEST_PIN, {
628
- type: import_hardware_wallet_core.UI_REQUEST.REQUEST_PIN,
629
- payload: { device: deviceInfo }
630
- });
631
- if (this._uiHandler?.onPinRequest) {
632
- this._uiHandler.onPinRequest(deviceInfo).then((pin) => {
633
- this.connector.uiResponse({
634
- type: "receive-pin",
635
- payload: pin
636
- });
637
- }).catch(() => {
638
- });
639
- }
640
- break;
641
- case "ui-request_passphrase":
642
- this.emitter.emit(import_hardware_wallet_core.UI_REQUEST.REQUEST_PASSPHRASE, {
643
- type: import_hardware_wallet_core.UI_REQUEST.REQUEST_PASSPHRASE,
644
- payload: { device: deviceInfo }
645
- });
646
- if (this._uiHandler?.onPassphraseRequest) {
647
- this._uiHandler.onPassphraseRequest(deviceInfo).then((result) => {
648
- const response = typeof result === "string" ? { passphrase: result, onDevice: false } : result ?? { passphrase: "", onDevice: false };
649
- if (response.onDevice) {
650
- this.connector.uiResponse({
651
- type: "receive-passphrase",
652
- payload: {
653
- value: "",
654
- passphraseOnDevice: true,
655
- save: false
656
- }
657
- });
658
- } else {
659
- this.connector.uiResponse({
660
- type: "receive-passphrase",
661
- payload: {
662
- value: response.passphrase,
663
- passphraseOnDevice: false,
664
- save: false
665
- }
666
- });
667
- }
668
- }).catch(() => {
669
- });
670
- }
671
- break;
672
- case "ui-request_confirmation":
673
- this.emitter.emit(import_hardware_wallet_core.UI_REQUEST.REQUEST_BUTTON, {
674
- type: import_hardware_wallet_core.UI_REQUEST.REQUEST_BUTTON,
675
- payload: { device: deviceInfo }
676
- });
677
- break;
678
- }
679
- }
680
- connectorDeviceToDeviceInfo(device) {
681
- return {
682
- vendor: "trezor",
683
- model: device.model ?? "unknown",
684
- firmwareVersion: "",
685
- deviceId: device.deviceId,
686
- connectId: device.connectId,
687
- label: device.name,
688
- connectionType: "usb"
689
- };
690
- }
691
- extractDeviceInfoFromPayload(payload) {
692
- const features = payload["features"];
693
- return {
694
- vendor: "trezor",
695
- model: features?.["model"] ?? payload["model"] ?? "unknown",
696
- firmwareVersion: features ? `${features["major_version"] ?? 0}.${features["minor_version"] ?? 0}.${features["patch_version"] ?? 0}` : "",
697
- deviceId: features?.["device_id"] ?? payload["id"] ?? "",
698
- connectId: payload["path"] ?? "",
699
- label: features?.["label"] ?? payload["label"],
700
- connectionType: "usb"
701
- };
702
- }
703
- unknownDevice() {
704
- return {
705
- vendor: "trezor",
706
- model: "unknown",
707
- firmwareVersion: "",
708
- deviceId: "",
709
- connectId: "",
710
- connectionType: "usb"
711
- };
712
- }
713
- };
714
-
715
- // src/TrezorProxyClient.ts
716
- var import_hardware_transport_core = require("@bytezhang/hardware-transport-core");
717
- var TrezorProxyClient = class extends import_hardware_transport_core.AbstractProxyClient {
718
- // ─── TrezorConnect method stubs — all delegate to call() ────
719
- ethereumGetAddress(params) {
720
- return this.call("ethereumGetAddress", params);
721
- }
722
- ethereumGetPublicKey(params) {
723
- return this.call("ethereumGetPublicKey", params);
724
- }
725
- ethereumSignTransaction(params) {
726
- return this.call("ethereumSignTransaction", params);
727
- }
728
- ethereumSignMessage(params) {
729
- return this.call("ethereumSignMessage", params);
730
- }
731
- ethereumSignTypedData(params) {
732
- return this.call("ethereumSignTypedData", params);
733
- }
734
- getAddress(params) {
735
- return this.call("getAddress", params);
736
- }
737
- getPublicKey(params) {
738
- return this.call("getPublicKey", params);
739
- }
740
- signTransaction(params) {
741
- return this.call("signTransaction", params);
742
- }
743
- signMessage(params) {
744
- return this.call("signMessage", params);
745
- }
746
- solanaGetAddress(params) {
747
- return this.call("solanaGetAddress", params);
748
- }
749
- solanaSignTransaction(params) {
750
- return this.call("solanaSignTransaction", params);
27
+ var VERSION = "0.0.1";
28
+ var TrezorService = class {
29
+ constructor() {
30
+ throw new Error("TrezorService not yet implemented");
751
31
  }
752
32
  };
753
33
  // Annotate the CommonJS export names for ESM import in node:
754
34
  0 && (module.exports = {
755
- TrezorAdapter,
756
- TrezorProxyClient
35
+ TrezorService,
36
+ VERSION
757
37
  });
758
38
  //# sourceMappingURL=index.js.map