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