@bytezhang/hardware-trezor-adapter 0.0.7 → 0.0.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytezhang/hardware-trezor-adapter",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Trezor hardware wallet adapter using IConnector abstraction",
5
5
  "author": "OneKey",
6
6
  "license": "MIT",
package/dist/index.d.mts DELETED
@@ -1,198 +0,0 @@
1
- import { IHardwareWallet, IConnector, TransportType, IUiHandler, DeviceInfo, Response, ChainCapability, HardwareEventMap, DeviceEventListener, ChainForFingerprint, EvmGetAddressParams, EvmAddress, ProgressCallback, EvmGetPublicKeyParams, EvmPublicKey, EvmSignTxParams, EvmSignedTx, EvmSignMsgParams, EvmSignature, EvmSignTypedDataParams, BtcGetAddressParams, BtcAddress, BtcGetPublicKeyParams, BtcPublicKey, BtcSignTxParams, BtcSignedTx, BtcSignMsgParams, BtcSignature, SolGetAddressParams, SolAddress, SolGetPublicKeyParams, SolPublicKey, SolSignTxParams, SolSignedTx, SolSignMsgParams, SolSignature } from '@bytezhang/hardware-wallet-core';
2
- import { AbstractProxyClient } from '@bytezhang/hardware-transport-core';
3
-
4
- /**
5
- * Trezor hardware wallet adapter that delegates to an IConnector.
6
- *
7
- * This is a thin translation layer that:
8
- * - Accepts a pre-configured IConnector (transport decisions are made at connector creation time)
9
- * - Translates IHardwareWallet method calls to connector.call() invocations
10
- * - Maps connector results/errors to our Response<T> format with enriched error messages
11
- * - Translates connector events to HardwareEventMap events
12
- * - Integrates with IUiHandler for interactive flows (PIN, passphrase)
13
- */
14
- declare class TrezorAdapter implements IHardwareWallet {
15
- readonly vendor: "trezor";
16
- private readonly connector;
17
- private readonly emitter;
18
- private _uiHandler;
19
- private _discoveredDevices;
20
- private _sessions;
21
- constructor(connector: IConnector);
22
- get activeTransport(): TransportType | null;
23
- getAvailableTransports(): TransportType[];
24
- switchTransport(_type: TransportType): Promise<void>;
25
- setUiHandler(handler: Partial<IUiHandler>): void;
26
- init(_config?: unknown): Promise<void>;
27
- dispose(): Promise<void>;
28
- searchDevices(): Promise<DeviceInfo[]>;
29
- connectDevice(connectId: string): Promise<Response<string>>;
30
- disconnectDevice(connectId: string): Promise<void>;
31
- getDeviceInfo(connectId: string, deviceId: string): Promise<Response<DeviceInfo>>;
32
- getSupportedChains(): ChainCapability[];
33
- on<K extends keyof HardwareEventMap>(event: K, listener: (event: HardwareEventMap[K]) => void): void;
34
- on(event: string, listener: DeviceEventListener): void;
35
- off<K extends keyof HardwareEventMap>(event: K, listener: (event: HardwareEventMap[K]) => void): void;
36
- off(event: string, listener: DeviceEventListener): void;
37
- cancel(connectId: string): void;
38
- /**
39
- * For Trezor, the chain fingerprint is the hardware device_id from firmware.
40
- * Trezor has a persistent device identity, so no address derivation is needed.
41
- */
42
- getChainFingerprint(connectId: string, deviceId: string, _chain: ChainForFingerprint): Promise<Response<string>>;
43
- evmGetAddress(connectId: string, _deviceId: string, params: EvmGetAddressParams): Promise<Response<EvmAddress>>;
44
- evmGetAddresses(connectId: string, deviceId: string, params: EvmGetAddressParams[], onProgress?: ProgressCallback): Promise<Response<EvmAddress[]>>;
45
- evmGetPublicKey(connectId: string, _deviceId: string, params: EvmGetPublicKeyParams): Promise<Response<EvmPublicKey>>;
46
- evmSignTransaction(connectId: string, _deviceId: string, params: EvmSignTxParams): Promise<Response<EvmSignedTx>>;
47
- evmSignMessage(connectId: string, _deviceId: string, params: EvmSignMsgParams): Promise<Response<EvmSignature>>;
48
- evmSignTypedData(connectId: string, _deviceId: string, params: EvmSignTypedDataParams): Promise<Response<EvmSignature>>;
49
- btcGetAddress(connectId: string, _deviceId: string, params: BtcGetAddressParams): Promise<Response<BtcAddress>>;
50
- btcGetAddresses(connectId: string, deviceId: string, params: BtcGetAddressParams[], onProgress?: ProgressCallback): Promise<Response<BtcAddress[]>>;
51
- btcGetPublicKey(connectId: string, _deviceId: string, params: BtcGetPublicKeyParams): Promise<Response<BtcPublicKey>>;
52
- btcSignTransaction(connectId: string, _deviceId: string, params: BtcSignTxParams): Promise<Response<BtcSignedTx>>;
53
- btcSignMessage(connectId: string, _deviceId: string, params: BtcSignMsgParams): Promise<Response<BtcSignature>>;
54
- btcGetMasterFingerprint(connectId: string, _deviceId: string): Promise<Response<{
55
- masterFingerprint: string;
56
- }>>;
57
- solGetAddress(connectId: string, _deviceId: string, params: SolGetAddressParams): Promise<Response<SolAddress>>;
58
- solGetAddresses(connectId: string, deviceId: string, params: SolGetAddressParams[], onProgress?: ProgressCallback): Promise<Response<SolAddress[]>>;
59
- solGetPublicKey(connectId: string, _deviceId: string, params: SolGetPublicKeyParams): Promise<Response<SolPublicKey>>;
60
- solSignTransaction(connectId: string, _deviceId: string, params: SolSignTxParams): Promise<Response<SolSignedTx>>;
61
- solSignMessage(_connectId: string, _deviceId: string, _params: SolSignMsgParams): Promise<Response<SolSignature>>;
62
- /**
63
- * Call the connector with session resolution.
64
- * Looks up sessionId from connectId, falls back to connectId itself.
65
- */
66
- private connectorCall;
67
- /**
68
- * Ensure device permission before proceeding.
69
- * - No connectId (searchDevices): check environment-level permission
70
- * - With connectId (business methods): check device-level permission
71
- * If not granted, calls onDevicePermission so the consumer can request access.
72
- */
73
- private _ensureDevicePermission;
74
- /**
75
- * Convert a thrown error to a Response failure.
76
- * Parses TrezorConnect error strings to map to HardwareErrorCode values.
77
- */
78
- private errorToFailure;
79
- /**
80
- * Parse TrezorConnect error codes from error message strings.
81
- * The connector throws errors with messages like "Trezor ethereumGetAddress failed: <error>".
82
- * We also check for embedded code patterns.
83
- */
84
- private parseErrorCode;
85
- /**
86
- * Enrich error messages with actionable recovery info for the caller.
87
- */
88
- private enrichErrorMessage;
89
- /**
90
- * Generic batch call with progress reporting.
91
- * If any single call fails, returns the failure immediately.
92
- */
93
- private batchCall;
94
- /** Ensure a hex string has the `0x` prefix. */
95
- private ensure0x;
96
- /** Ensure a hex string is `0x`-prefixed and zero-padded to 64 hex chars (32 bytes). */
97
- private padHex64;
98
- private deviceConnectHandler;
99
- private deviceDisconnectHandler;
100
- private uiRequestHandler;
101
- private uiEventHandler;
102
- private registerEventListeners;
103
- private unregisterEventListeners;
104
- private handleUiEvent;
105
- private connectorDeviceToDeviceInfo;
106
- private extractDeviceInfoFromPayload;
107
- private unknownDevice;
108
- }
109
-
110
- /**
111
- * Response shape returned by connector.call() results that follow
112
- * the TrezorConnect success/failure pattern.
113
- *
114
- * The connector may either:
115
- * 1. Return the result directly (success case from TrezorDirectConnector)
116
- * 2. Throw an error (failure case from TrezorDirectConnector)
117
- *
118
- * These types are kept for error parsing when the connector propagates
119
- * TrezorConnect error strings.
120
- */
121
- interface ConnectorCallError {
122
- message: string;
123
- code?: string;
124
- }
125
- /**
126
- * Abstraction over @trezor/connect API surface.
127
- *
128
- * Does NOT include `init()` — the consumer calls init() inside createConnect().
129
- * Adapter calls dispose() when switching or tearing down.
130
- */
131
- interface ITrezorConnect {
132
- dispose(): void;
133
- cancel(reason?: string): void;
134
- on(event: string, handler: (...args: unknown[]) => void): void;
135
- off(event: string, handler: (...args: unknown[]) => void): void;
136
- removeAllListeners?(): void;
137
- uiResponse(response: {
138
- type: string;
139
- payload: unknown;
140
- }): void;
141
- enumerate?(): Promise<Array<Record<string, unknown>>>;
142
- ethereumGetAddress(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
143
- ethereumGetPublicKey(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
144
- ethereumSignTransaction(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
145
- ethereumSignMessage(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
146
- ethereumSignTypedData(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
147
- getAddress(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
148
- getPublicKey(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
149
- signTransaction(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
150
- signMessage(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
151
- solanaGetAddress(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
152
- solanaSignTransaction(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
153
- }
154
- /**
155
- * Response shape returned by all @trezor/connect methods.
156
- */
157
- type TrezorConnectResponse = {
158
- success: true;
159
- payload: Record<string, unknown>;
160
- } | {
161
- success: false;
162
- payload: {
163
- error: string;
164
- code?: string;
165
- };
166
- };
167
-
168
- /**
169
- * Trezor method stubs on top of AbstractProxyClient.
170
- *
171
- * All cross-process Trezor proxy clients (Electron IPC, Extension messaging)
172
- * extend this class. They only need to implement:
173
- * - `sendCall()` — the transport channel
174
- * - `dispose()` / `cancel()` / `uiResponse()` — lifecycle
175
- *
176
- * The 11 TrezorConnect method stubs are defined here once.
177
- */
178
- declare abstract class TrezorProxyClient extends AbstractProxyClient implements ITrezorConnect {
179
- abstract dispose(): void;
180
- abstract cancel(reason?: string): void;
181
- abstract uiResponse(response: {
182
- type: string;
183
- payload: unknown;
184
- }): void;
185
- ethereumGetAddress(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
186
- ethereumGetPublicKey(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
187
- ethereumSignTransaction(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
188
- ethereumSignMessage(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
189
- ethereumSignTypedData(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
190
- getAddress(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
191
- getPublicKey(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
192
- signTransaction(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
193
- signMessage(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
194
- solanaGetAddress(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
195
- solanaSignTransaction(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
196
- }
197
-
198
- export { type ConnectorCallError, type ITrezorConnect, TrezorAdapter, type TrezorConnectResponse, TrezorProxyClient };
package/dist/index.d.ts DELETED
@@ -1,198 +0,0 @@
1
- import { IHardwareWallet, IConnector, TransportType, IUiHandler, DeviceInfo, Response, ChainCapability, HardwareEventMap, DeviceEventListener, ChainForFingerprint, EvmGetAddressParams, EvmAddress, ProgressCallback, EvmGetPublicKeyParams, EvmPublicKey, EvmSignTxParams, EvmSignedTx, EvmSignMsgParams, EvmSignature, EvmSignTypedDataParams, BtcGetAddressParams, BtcAddress, BtcGetPublicKeyParams, BtcPublicKey, BtcSignTxParams, BtcSignedTx, BtcSignMsgParams, BtcSignature, SolGetAddressParams, SolAddress, SolGetPublicKeyParams, SolPublicKey, SolSignTxParams, SolSignedTx, SolSignMsgParams, SolSignature } from '@bytezhang/hardware-wallet-core';
2
- import { AbstractProxyClient } from '@bytezhang/hardware-transport-core';
3
-
4
- /**
5
- * Trezor hardware wallet adapter that delegates to an IConnector.
6
- *
7
- * This is a thin translation layer that:
8
- * - Accepts a pre-configured IConnector (transport decisions are made at connector creation time)
9
- * - Translates IHardwareWallet method calls to connector.call() invocations
10
- * - Maps connector results/errors to our Response<T> format with enriched error messages
11
- * - Translates connector events to HardwareEventMap events
12
- * - Integrates with IUiHandler for interactive flows (PIN, passphrase)
13
- */
14
- declare class TrezorAdapter implements IHardwareWallet {
15
- readonly vendor: "trezor";
16
- private readonly connector;
17
- private readonly emitter;
18
- private _uiHandler;
19
- private _discoveredDevices;
20
- private _sessions;
21
- constructor(connector: IConnector);
22
- get activeTransport(): TransportType | null;
23
- getAvailableTransports(): TransportType[];
24
- switchTransport(_type: TransportType): Promise<void>;
25
- setUiHandler(handler: Partial<IUiHandler>): void;
26
- init(_config?: unknown): Promise<void>;
27
- dispose(): Promise<void>;
28
- searchDevices(): Promise<DeviceInfo[]>;
29
- connectDevice(connectId: string): Promise<Response<string>>;
30
- disconnectDevice(connectId: string): Promise<void>;
31
- getDeviceInfo(connectId: string, deviceId: string): Promise<Response<DeviceInfo>>;
32
- getSupportedChains(): ChainCapability[];
33
- on<K extends keyof HardwareEventMap>(event: K, listener: (event: HardwareEventMap[K]) => void): void;
34
- on(event: string, listener: DeviceEventListener): void;
35
- off<K extends keyof HardwareEventMap>(event: K, listener: (event: HardwareEventMap[K]) => void): void;
36
- off(event: string, listener: DeviceEventListener): void;
37
- cancel(connectId: string): void;
38
- /**
39
- * For Trezor, the chain fingerprint is the hardware device_id from firmware.
40
- * Trezor has a persistent device identity, so no address derivation is needed.
41
- */
42
- getChainFingerprint(connectId: string, deviceId: string, _chain: ChainForFingerprint): Promise<Response<string>>;
43
- evmGetAddress(connectId: string, _deviceId: string, params: EvmGetAddressParams): Promise<Response<EvmAddress>>;
44
- evmGetAddresses(connectId: string, deviceId: string, params: EvmGetAddressParams[], onProgress?: ProgressCallback): Promise<Response<EvmAddress[]>>;
45
- evmGetPublicKey(connectId: string, _deviceId: string, params: EvmGetPublicKeyParams): Promise<Response<EvmPublicKey>>;
46
- evmSignTransaction(connectId: string, _deviceId: string, params: EvmSignTxParams): Promise<Response<EvmSignedTx>>;
47
- evmSignMessage(connectId: string, _deviceId: string, params: EvmSignMsgParams): Promise<Response<EvmSignature>>;
48
- evmSignTypedData(connectId: string, _deviceId: string, params: EvmSignTypedDataParams): Promise<Response<EvmSignature>>;
49
- btcGetAddress(connectId: string, _deviceId: string, params: BtcGetAddressParams): Promise<Response<BtcAddress>>;
50
- btcGetAddresses(connectId: string, deviceId: string, params: BtcGetAddressParams[], onProgress?: ProgressCallback): Promise<Response<BtcAddress[]>>;
51
- btcGetPublicKey(connectId: string, _deviceId: string, params: BtcGetPublicKeyParams): Promise<Response<BtcPublicKey>>;
52
- btcSignTransaction(connectId: string, _deviceId: string, params: BtcSignTxParams): Promise<Response<BtcSignedTx>>;
53
- btcSignMessage(connectId: string, _deviceId: string, params: BtcSignMsgParams): Promise<Response<BtcSignature>>;
54
- btcGetMasterFingerprint(connectId: string, _deviceId: string): Promise<Response<{
55
- masterFingerprint: string;
56
- }>>;
57
- solGetAddress(connectId: string, _deviceId: string, params: SolGetAddressParams): Promise<Response<SolAddress>>;
58
- solGetAddresses(connectId: string, deviceId: string, params: SolGetAddressParams[], onProgress?: ProgressCallback): Promise<Response<SolAddress[]>>;
59
- solGetPublicKey(connectId: string, _deviceId: string, params: SolGetPublicKeyParams): Promise<Response<SolPublicKey>>;
60
- solSignTransaction(connectId: string, _deviceId: string, params: SolSignTxParams): Promise<Response<SolSignedTx>>;
61
- solSignMessage(_connectId: string, _deviceId: string, _params: SolSignMsgParams): Promise<Response<SolSignature>>;
62
- /**
63
- * Call the connector with session resolution.
64
- * Looks up sessionId from connectId, falls back to connectId itself.
65
- */
66
- private connectorCall;
67
- /**
68
- * Ensure device permission before proceeding.
69
- * - No connectId (searchDevices): check environment-level permission
70
- * - With connectId (business methods): check device-level permission
71
- * If not granted, calls onDevicePermission so the consumer can request access.
72
- */
73
- private _ensureDevicePermission;
74
- /**
75
- * Convert a thrown error to a Response failure.
76
- * Parses TrezorConnect error strings to map to HardwareErrorCode values.
77
- */
78
- private errorToFailure;
79
- /**
80
- * Parse TrezorConnect error codes from error message strings.
81
- * The connector throws errors with messages like "Trezor ethereumGetAddress failed: <error>".
82
- * We also check for embedded code patterns.
83
- */
84
- private parseErrorCode;
85
- /**
86
- * Enrich error messages with actionable recovery info for the caller.
87
- */
88
- private enrichErrorMessage;
89
- /**
90
- * Generic batch call with progress reporting.
91
- * If any single call fails, returns the failure immediately.
92
- */
93
- private batchCall;
94
- /** Ensure a hex string has the `0x` prefix. */
95
- private ensure0x;
96
- /** Ensure a hex string is `0x`-prefixed and zero-padded to 64 hex chars (32 bytes). */
97
- private padHex64;
98
- private deviceConnectHandler;
99
- private deviceDisconnectHandler;
100
- private uiRequestHandler;
101
- private uiEventHandler;
102
- private registerEventListeners;
103
- private unregisterEventListeners;
104
- private handleUiEvent;
105
- private connectorDeviceToDeviceInfo;
106
- private extractDeviceInfoFromPayload;
107
- private unknownDevice;
108
- }
109
-
110
- /**
111
- * Response shape returned by connector.call() results that follow
112
- * the TrezorConnect success/failure pattern.
113
- *
114
- * The connector may either:
115
- * 1. Return the result directly (success case from TrezorDirectConnector)
116
- * 2. Throw an error (failure case from TrezorDirectConnector)
117
- *
118
- * These types are kept for error parsing when the connector propagates
119
- * TrezorConnect error strings.
120
- */
121
- interface ConnectorCallError {
122
- message: string;
123
- code?: string;
124
- }
125
- /**
126
- * Abstraction over @trezor/connect API surface.
127
- *
128
- * Does NOT include `init()` — the consumer calls init() inside createConnect().
129
- * Adapter calls dispose() when switching or tearing down.
130
- */
131
- interface ITrezorConnect {
132
- dispose(): void;
133
- cancel(reason?: string): void;
134
- on(event: string, handler: (...args: unknown[]) => void): void;
135
- off(event: string, handler: (...args: unknown[]) => void): void;
136
- removeAllListeners?(): void;
137
- uiResponse(response: {
138
- type: string;
139
- payload: unknown;
140
- }): void;
141
- enumerate?(): Promise<Array<Record<string, unknown>>>;
142
- ethereumGetAddress(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
143
- ethereumGetPublicKey(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
144
- ethereumSignTransaction(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
145
- ethereumSignMessage(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
146
- ethereumSignTypedData(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
147
- getAddress(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
148
- getPublicKey(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
149
- signTransaction(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
150
- signMessage(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
151
- solanaGetAddress(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
152
- solanaSignTransaction(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
153
- }
154
- /**
155
- * Response shape returned by all @trezor/connect methods.
156
- */
157
- type TrezorConnectResponse = {
158
- success: true;
159
- payload: Record<string, unknown>;
160
- } | {
161
- success: false;
162
- payload: {
163
- error: string;
164
- code?: string;
165
- };
166
- };
167
-
168
- /**
169
- * Trezor method stubs on top of AbstractProxyClient.
170
- *
171
- * All cross-process Trezor proxy clients (Electron IPC, Extension messaging)
172
- * extend this class. They only need to implement:
173
- * - `sendCall()` — the transport channel
174
- * - `dispose()` / `cancel()` / `uiResponse()` — lifecycle
175
- *
176
- * The 11 TrezorConnect method stubs are defined here once.
177
- */
178
- declare abstract class TrezorProxyClient extends AbstractProxyClient implements ITrezorConnect {
179
- abstract dispose(): void;
180
- abstract cancel(reason?: string): void;
181
- abstract uiResponse(response: {
182
- type: string;
183
- payload: unknown;
184
- }): void;
185
- ethereumGetAddress(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
186
- ethereumGetPublicKey(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
187
- ethereumSignTransaction(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
188
- ethereumSignMessage(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
189
- ethereumSignTypedData(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
190
- getAddress(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
191
- getPublicKey(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
192
- signTransaction(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
193
- signMessage(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
194
- solanaGetAddress(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
195
- solanaSignTransaction(params: Record<string, unknown>): Promise<TrezorConnectResponse>;
196
- }
197
-
198
- export { type ConnectorCallError, type ITrezorConnect, TrezorAdapter, type TrezorConnectResponse, TrezorProxyClient };