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