@injectivelabs/wallet-trezor 1.16.38 → 1.16.39-alpha.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.
Files changed (49) hide show
  1. package/dist/cjs/index.cjs +680 -0
  2. package/dist/cjs/index.d.cts +79 -0
  3. package/dist/cjs/package.json +2 -2
  4. package/dist/esm/index.d.ts +79 -3
  5. package/dist/esm/index.js +678 -3
  6. package/dist/esm/package.json +2 -2
  7. package/package.json +43 -45
  8. package/dist/cjs/index.d.ts +0 -3
  9. package/dist/cjs/index.js +0 -22
  10. package/dist/cjs/strategy/Base.d.ts +0 -44
  11. package/dist/cjs/strategy/Base.js +0 -299
  12. package/dist/cjs/strategy/TrezorBip32.d.ts +0 -5
  13. package/dist/cjs/strategy/TrezorBip32.js +0 -17
  14. package/dist/cjs/strategy/TrezorBip44.d.ts +0 -5
  15. package/dist/cjs/strategy/TrezorBip44.js +0 -17
  16. package/dist/cjs/strategy/hw/AccountManager.d.ts +0 -20
  17. package/dist/cjs/strategy/hw/AccountManager.js +0 -104
  18. package/dist/cjs/strategy/hw/index.d.ts +0 -3
  19. package/dist/cjs/strategy/hw/index.js +0 -10
  20. package/dist/cjs/strategy/hw/transport/base.d.ts +0 -6
  21. package/dist/cjs/strategy/hw/transport/base.js +0 -34
  22. package/dist/cjs/strategy/hw/transport/transport-no-init.d.ts +0 -4
  23. package/dist/cjs/strategy/hw/transport/transport-no-init.js +0 -12
  24. package/dist/cjs/strategy/lib.d.ts +0 -41
  25. package/dist/cjs/strategy/lib.js +0 -44
  26. package/dist/cjs/types.d.ts +0 -15
  27. package/dist/cjs/types.js +0 -8
  28. package/dist/cjs/utils.d.ts +0 -16
  29. package/dist/cjs/utils.js +0 -36
  30. package/dist/esm/strategy/Base.d.ts +0 -44
  31. package/dist/esm/strategy/Base.js +0 -296
  32. package/dist/esm/strategy/TrezorBip32.d.ts +0 -5
  33. package/dist/esm/strategy/TrezorBip32.js +0 -10
  34. package/dist/esm/strategy/TrezorBip44.d.ts +0 -5
  35. package/dist/esm/strategy/TrezorBip44.js +0 -10
  36. package/dist/esm/strategy/hw/AccountManager.d.ts +0 -20
  37. package/dist/esm/strategy/hw/AccountManager.js +0 -101
  38. package/dist/esm/strategy/hw/index.d.ts +0 -3
  39. package/dist/esm/strategy/hw/index.js +0 -3
  40. package/dist/esm/strategy/hw/transport/base.d.ts +0 -6
  41. package/dist/esm/strategy/hw/transport/base.js +0 -28
  42. package/dist/esm/strategy/hw/transport/transport-no-init.d.ts +0 -4
  43. package/dist/esm/strategy/hw/transport/transport-no-init.js +0 -6
  44. package/dist/esm/strategy/lib.d.ts +0 -41
  45. package/dist/esm/strategy/lib.js +0 -8
  46. package/dist/esm/types.d.ts +0 -15
  47. package/dist/esm/types.js +0 -5
  48. package/dist/esm/utils.d.ts +0 -16
  49. package/dist/esm/utils.js +0 -32
@@ -0,0 +1,680 @@
1
+ let viem = require("viem");
2
+ let __injectivelabs_sdk_ts_core_tx = require("@injectivelabs/sdk-ts/core/tx");
3
+ let __injectivelabs_wallet_base = require("@injectivelabs/wallet-base");
4
+ let __injectivelabs_sdk_ts_utils = require("@injectivelabs/sdk-ts/utils");
5
+ let __injectivelabs_exceptions = require("@injectivelabs/exceptions");
6
+
7
+ //#region src/types.ts
8
+ const TrezorDerivationPathType = {
9
+ Bip32: "bip32",
10
+ Bip44: "bip44",
11
+ Legacy: "legacy"
12
+ };
13
+
14
+ //#endregion
15
+ //#region src/strategy/lib.ts
16
+ async function loadTrezorConnect() {
17
+ var _default;
18
+ const module$1 = await import("@trezor/connect-web");
19
+ return (module$1 === null || module$1 === void 0 || (_default = module$1.default) === null || _default === void 0 ? void 0 : _default.default) || module$1.default;
20
+ }
21
+
22
+ //#endregion
23
+ //#region src/utils.ts
24
+ /**
25
+ * Calculates the domain_separator_hash and message_hash from an EIP-712 Typed Data object.
26
+ *
27
+ * The Trezor Model 1 does not currently support constructing the hash on the device,
28
+ * so this function pre-computes them.
29
+ *
30
+ * @template {TypedMessage} T
31
+ * @param {T} data - The EIP-712 Typed Data object.
32
+ * @param {boolean} metamask_v4_compat - Set to `true` for compatibility with Metamask's signTypedData_v4 function.
33
+ * @returns {{domain_separator_hash: string, message_hash?: string} & T} The hashes.
34
+ */
35
+ const transformTypedData = (data, metamask_v4_compat = true) => {
36
+ if (!metamask_v4_compat) throw new __injectivelabs_exceptions.TrezorException(/* @__PURE__ */ new Error("Trezor: Only version 4 of typed data signing is supported"));
37
+ const version = __injectivelabs_sdk_ts_utils.SignTypedDataVersionV4;
38
+ const { types, primaryType, domain, message } = (0, __injectivelabs_sdk_ts_utils.TypedDataUtilsSanitizeData)(data);
39
+ const domainSeparatorHash = (0, __injectivelabs_sdk_ts_utils.TypedDataUtilsHashStruct)("EIP712Domain", (0, __injectivelabs_sdk_ts_utils.sanitizeTypedData)(domain), types, version).toString("hex");
40
+ let messageHash = null;
41
+ if (primaryType !== "EIP712Domain") messageHash = (0, __injectivelabs_sdk_ts_utils.TypedDataUtilsHashStruct)(primaryType, (0, __injectivelabs_sdk_ts_utils.sanitizeTypedData)(message), {
42
+ ...types,
43
+ domain: (0, __injectivelabs_sdk_ts_utils.sanitizeTypedData)(domain)
44
+ }, version).toString("hex");
45
+ return {
46
+ domain_separator_hash: domainSeparatorHash,
47
+ message_hash: messageHash,
48
+ ...data
49
+ };
50
+ };
51
+
52
+ //#endregion
53
+ //#region \0@oxc-project+runtime@0.98.0/helpers/typeof.js
54
+ function _typeof(o) {
55
+ "@babel/helpers - typeof";
56
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o$1) {
57
+ return typeof o$1;
58
+ } : function(o$1) {
59
+ return o$1 && "function" == typeof Symbol && o$1.constructor === Symbol && o$1 !== Symbol.prototype ? "symbol" : typeof o$1;
60
+ }, _typeof(o);
61
+ }
62
+
63
+ //#endregion
64
+ //#region \0@oxc-project+runtime@0.98.0/helpers/toPrimitive.js
65
+ function toPrimitive(t, r) {
66
+ if ("object" != _typeof(t) || !t) return t;
67
+ var e = t[Symbol.toPrimitive];
68
+ if (void 0 !== e) {
69
+ var i = e.call(t, r || "default");
70
+ if ("object" != _typeof(i)) return i;
71
+ throw new TypeError("@@toPrimitive must return a primitive value.");
72
+ }
73
+ return ("string" === r ? String : Number)(t);
74
+ }
75
+
76
+ //#endregion
77
+ //#region \0@oxc-project+runtime@0.98.0/helpers/toPropertyKey.js
78
+ function toPropertyKey(t) {
79
+ var i = toPrimitive(t, "string");
80
+ return "symbol" == _typeof(i) ? i : i + "";
81
+ }
82
+
83
+ //#endregion
84
+ //#region \0@oxc-project+runtime@0.98.0/helpers/defineProperty.js
85
+ function _defineProperty(e, r, t) {
86
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
87
+ value: t,
88
+ enumerable: !0,
89
+ configurable: !0,
90
+ writable: !0
91
+ }) : e[r] = t, e;
92
+ }
93
+
94
+ //#endregion
95
+ //#region src/strategy/hw/AccountManager.ts
96
+ const addressOfHDKey = (hdKey) => {
97
+ const shouldSanitizePublicKey = true;
98
+ const derivedPublicKey = hdKey.publicKey;
99
+ return (0, __injectivelabs_sdk_ts_utils.addHexPrefix)((0, __injectivelabs_sdk_ts_utils.uint8ArrayToHex)((0, __injectivelabs_sdk_ts_utils.publicKeyToAddress)(derivedPublicKey, shouldSanitizePublicKey)));
100
+ };
101
+ var AccountManager = class {
102
+ constructor() {
103
+ _defineProperty(this, "wallets", []);
104
+ _defineProperty(this, "getTrezorDerivationPathBasedOnType", ({ fullBaseDerivationPath, derivationPathType, index }) => {
105
+ if (derivationPathType === TrezorDerivationPathType.Bip44) return `${fullBaseDerivationPath}/${index}'/0/0`;
106
+ if (derivationPathType === TrezorDerivationPathType.Legacy) return `m/${index}`;
107
+ return `${fullBaseDerivationPath}/0'/0/${index}`;
108
+ });
109
+ this.wallets = [];
110
+ }
111
+ async getWallets(baseDerivationPath, derivationPathType) {
112
+ const { start, end } = this.getOffset();
113
+ /**
114
+ * 1. Wallets are not yet fetched at all,
115
+ * 2. Wallets are not yet fetched for that offset
116
+ */
117
+ if (!this.hasWallets() || !this.hasWalletsInOffset(start)) await this.getWalletsBasedOnIndex({
118
+ start,
119
+ end,
120
+ baseDerivationPath,
121
+ derivationPathType
122
+ });
123
+ return this.wallets.slice(start, end);
124
+ }
125
+ async getWalletsBasedOnIndex({ start, end, baseDerivationPath, derivationPathType }) {
126
+ const TrezorConnect = await loadTrezorConnect();
127
+ const pathsToFetch = [];
128
+ for (let index = start; index < end; index += 1) {
129
+ const path = this.getTrezorDerivationPathBasedOnType({
130
+ fullBaseDerivationPath: baseDerivationPath,
131
+ derivationPathType,
132
+ index
133
+ });
134
+ pathsToFetch.push({
135
+ path,
136
+ showOnTrezor: false
137
+ });
138
+ }
139
+ const result = await TrezorConnect.ethereumGetPublicKey({ bundle: pathsToFetch });
140
+ if (!result.success) throw new __injectivelabs_exceptions.TrezorException(new Error(result.payload && result.payload.error || "Please make sure your Trezor is connected and unlocked"));
141
+ for (const item of result.payload) {
142
+ const hdKey = {
143
+ publicKey: (0, __injectivelabs_sdk_ts_utils.hexToUint8Array)(item.publicKey),
144
+ chainCode: (0, __injectivelabs_sdk_ts_utils.hexToUint8Array)(item.chainCode)
145
+ };
146
+ const address = addressOfHDKey(hdKey);
147
+ this.wallets.push({
148
+ hdKey,
149
+ derivationPath: item.serializedPath,
150
+ address: address.toLowerCase()
151
+ });
152
+ }
153
+ }
154
+ hasWallets() {
155
+ return this.wallets.length > 0;
156
+ }
157
+ hasWalletsInOffset(offset) {
158
+ return this.wallets.length > offset;
159
+ }
160
+ getOffset() {
161
+ const totalWallets = this.wallets.length;
162
+ return {
163
+ start: totalWallets,
164
+ end: totalWallets + __injectivelabs_wallet_base.DEFAULT_NUM_ADDRESSES_TO_FETCH
165
+ };
166
+ }
167
+ hasWalletForAddress(address) {
168
+ return this.wallets.find((wallet) => wallet.address.toLowerCase() === address.toLowerCase()) !== void 0;
169
+ }
170
+ async getWalletForAddress(address) {
171
+ return this.wallets.find((wallet) => wallet.address.toLowerCase() === address.toLowerCase());
172
+ }
173
+ reset() {
174
+ this.wallets = [];
175
+ }
176
+ };
177
+
178
+ //#endregion
179
+ //#region src/strategy/hw/transport/base.ts
180
+ const TREZOR_CONNECT_MANIFEST = {
181
+ email: "contact@injectivelabs.org",
182
+ appUrl: "https://injectivelabs.org",
183
+ appName: "Injective Labs"
184
+ };
185
+ var BaseTrezorTransport = class {
186
+ constructor() {
187
+ _defineProperty(this, "accountManager", null);
188
+ }
189
+ async connect() {
190
+ const TrezorConnect = await loadTrezorConnect();
191
+ if (!(await TrezorConnect.getSettings()).success) await TrezorConnect.init({
192
+ lazyLoad: true,
193
+ coreMode: "popup",
194
+ manifest: TREZOR_CONNECT_MANIFEST
195
+ });
196
+ return Promise.resolve();
197
+ }
198
+ async getAccountManager() {
199
+ if (!this.accountManager) this.accountManager = new AccountManager();
200
+ return this.accountManager;
201
+ }
202
+ };
203
+
204
+ //#endregion
205
+ //#region src/strategy/Eip1193Provider.ts
206
+ var TrezorEip1193Provider = class {
207
+ constructor(trezor, params) {
208
+ _defineProperty(this, "trezor", void 0);
209
+ _defineProperty(this, "derivationPath", void 0);
210
+ _defineProperty(this, "address", void 0);
211
+ _defineProperty(this, "chainId", void 0);
212
+ this.trezor = trezor;
213
+ this.derivationPath = params.derivationPath || "m/44'/60'/0'/0/0";
214
+ this.chainId = parseInt(params.chainId || "1");
215
+ }
216
+ async getClient() {
217
+ return (0, __injectivelabs_wallet_base.getViemWalletClient)({
218
+ chainId: this.chainId,
219
+ account: await this.getAddress()
220
+ });
221
+ }
222
+ async setChainId(chainId) {
223
+ this.chainId = parseInt(chainId.replace("0x", ""), 16);
224
+ }
225
+ async getAddress() {
226
+ if (!this.address) {
227
+ const TrezorConnect = await loadTrezorConnect();
228
+ await this.trezor.connect();
229
+ const response = await TrezorConnect.ethereumGetAddress({
230
+ path: this.derivationPath,
231
+ showOnTrezor: false
232
+ });
233
+ if (!response.success) throw new Error(response.payload && response.payload.error || "Failed to get address from Trezor");
234
+ this.address = response.payload.address;
235
+ }
236
+ return this.address;
237
+ }
238
+ async signTypedData(data) {
239
+ const TrezorConnect = await loadTrezorConnect();
240
+ await this.trezor.connect();
241
+ const object = JSON.parse(data);
242
+ const { types: { EIP712Domain = [], ...otherTypes } = {}, message = {}, domain = {}, primaryType, domain_separator_hash, message_hash } = transformTypedData({
243
+ ...object,
244
+ domain: {
245
+ ...object.domain,
246
+ chainId: object.domain.chainId,
247
+ salt: "0"
248
+ }
249
+ });
250
+ const response = await TrezorConnect.ethereumSignTypedData({
251
+ path: this.derivationPath,
252
+ data: {
253
+ types: {
254
+ EIP712Domain,
255
+ ...otherTypes
256
+ },
257
+ message,
258
+ domain,
259
+ primaryType
260
+ },
261
+ message_hash,
262
+ domain_separator_hash,
263
+ metamask_v4_compat: true
264
+ });
265
+ if ("code" in response.payload && response.payload.code === "Failure_ActionCancelled") throw new Error("Request rejected");
266
+ if (!response.success) throw new Error(response.payload && response.payload.error || "Unknown error");
267
+ return response.payload.signature;
268
+ }
269
+ async signTransaction(txData) {
270
+ const TrezorConnect = await loadTrezorConnect();
271
+ await this.trezor.connect();
272
+ const parseHexValue = (value) => {
273
+ if (typeof value === "string") {
274
+ const hexValue = value.startsWith("0x") ? value : `0x${value}`;
275
+ return BigInt(hexValue);
276
+ }
277
+ return BigInt(value);
278
+ };
279
+ const chainId = txData.chainId || this.chainId;
280
+ const valueBigInt = parseHexValue(txData.value || "0x0");
281
+ const gasBigInt = parseHexValue(txData.gas || txData.gasLimit);
282
+ const maxFeePerGasBigInt = parseHexValue(txData.maxFeePerGas);
283
+ const maxPriorityFeePerGasBigInt = parseHexValue(txData.maxPriorityFeePerGas);
284
+ const trezorTxData = {
285
+ to: txData.to,
286
+ value: (0, viem.toHex)(valueBigInt),
287
+ gasLimit: (0, viem.toHex)(gasBigInt),
288
+ nonce: (0, viem.toHex)(txData.nonce),
289
+ data: txData.data || "0x",
290
+ chainId,
291
+ maxFeePerGas: (0, viem.toHex)(maxFeePerGasBigInt),
292
+ maxPriorityFeePerGas: (0, viem.toHex)(maxPriorityFeePerGasBigInt)
293
+ };
294
+ const response = await TrezorConnect.ethereumSignTransaction({
295
+ path: this.derivationPath,
296
+ transaction: trezorTxData
297
+ });
298
+ if (!response.success) throw new Error(response.payload && response.payload.error || "Something happened while signing with Trezor");
299
+ return (0, viem.serializeTransaction)({
300
+ type: "eip1559",
301
+ chainId,
302
+ nonce: txData.nonce,
303
+ to: txData.to,
304
+ value: valueBigInt,
305
+ data: txData.data || "0x",
306
+ gas: gasBigInt,
307
+ maxFeePerGas: maxFeePerGasBigInt,
308
+ maxPriorityFeePerGas: maxPriorityFeePerGasBigInt,
309
+ v: BigInt(response.payload.v),
310
+ r: response.payload.r,
311
+ s: response.payload.s
312
+ });
313
+ }
314
+ async signMessage(message) {
315
+ const TrezorConnect = await loadTrezorConnect();
316
+ await this.trezor.connect();
317
+ const response = await TrezorConnect.ethereumSignMessage({
318
+ path: this.derivationPath,
319
+ message
320
+ });
321
+ if (!response.success) throw new Error(response.payload && response.payload.error || "Unknown error");
322
+ const signature = response.payload.signature;
323
+ return signature.startsWith("0x") ? signature : `0x${signature}`;
324
+ }
325
+ getChain() {
326
+ return (0, __injectivelabs_wallet_base.getEvmChainConfig)(this.chainId);
327
+ }
328
+ async request(args) {
329
+ if (args.method === "eth_requestAccounts") return [await this.getAddress()];
330
+ if (args.method === "eth_sign") {
331
+ if (!args.params[0]) throw new Error("Missing parameter for eth_sign");
332
+ return this.signMessage(args.params[0]);
333
+ }
334
+ if (args.method === "eth_signTransaction") {
335
+ if (!args.params[0]) throw new Error("Missing parameter for eth_signTransaction");
336
+ return this.signTransaction(args.params[0]);
337
+ }
338
+ if (args.method === "eth_signTypedData") {
339
+ if (!args.params[0]) throw new Error("Missing parameter for eth_signTypedData");
340
+ return this.signTypedData(args.params[0]);
341
+ }
342
+ if (args.method === "eth_chainId") return `0x${this.chainId.toString(16)}`;
343
+ if (args.method === "wallet_switchEthereumChain") {
344
+ var _args$params$;
345
+ return this.setChainId(((_args$params$ = args.params[0]) === null || _args$params$ === void 0 ? void 0 : _args$params$.chainId) || "0x1");
346
+ }
347
+ if (args.method === "eth_estimateGas") {
348
+ const client = (0, __injectivelabs_wallet_base.getViemPublicClient)(this.chainId);
349
+ const data = {
350
+ to: args.params[0].to,
351
+ value: args.params[0].value,
352
+ data: args.params[0].data,
353
+ account: await this.getAddress()
354
+ };
355
+ return `0x${(await client.estimateGas(data)).toString(16)}`;
356
+ }
357
+ if (args.method === "eth_getTransactionCount") {
358
+ if (!args.params) throw new Error("params is required");
359
+ return `0x${(await (0, __injectivelabs_wallet_base.getViemPublicClient)(this.chainId).getTransactionCount({
360
+ address: await this.getAddress(),
361
+ blockTag: "pending"
362
+ })).toString(16)}`;
363
+ }
364
+ if (args.method === "eth_sendTransaction") {
365
+ const address = await this.getAddress();
366
+ const walletClient = (0, __injectivelabs_wallet_base.getViemWalletClient)({
367
+ chainId: this.chainId,
368
+ account: address
369
+ });
370
+ const preparedTransaction = await walletClient.prepareTransactionRequest({ ...args.params[0] });
371
+ const signedTransaction = await this.signTransaction(preparedTransaction);
372
+ return await walletClient.sendRawTransaction({ serializedTransaction: signedTransaction });
373
+ }
374
+ return (await this.getClient()).request({
375
+ method: args.method,
376
+ params: args.params
377
+ });
378
+ }
379
+ on(_event, _listener) {
380
+ throw new Error("Method not implemented.");
381
+ }
382
+ once(_event, _listener) {
383
+ throw new Error("Method not implemented.");
384
+ }
385
+ removeListener(_event, _listener) {
386
+ throw new Error("Method not implemented.");
387
+ }
388
+ off(_event, _listener) {
389
+ throw new Error("Method not implemented.");
390
+ }
391
+ };
392
+
393
+ //#endregion
394
+ //#region src/strategy/Base.ts
395
+ var TrezorBase = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
396
+ constructor(args) {
397
+ super(args);
398
+ _defineProperty(this, "baseDerivationPath", void 0);
399
+ _defineProperty(this, "trezor", void 0);
400
+ _defineProperty(this, "evmOptions", void 0);
401
+ _defineProperty(this, "publicClient", void 0);
402
+ _defineProperty(this, "derivationPathType", void 0);
403
+ this.evmOptions = args.evmOptions;
404
+ this.trezor = new BaseTrezorTransport();
405
+ this.derivationPathType = args.derivationPathType;
406
+ this.baseDerivationPath = __injectivelabs_wallet_base.DEFAULT_BASE_DERIVATION_PATH;
407
+ }
408
+ async getWalletDeviceType() {
409
+ return Promise.resolve(__injectivelabs_wallet_base.WalletDeviceType.Hardware);
410
+ }
411
+ async enable() {
412
+ return Promise.resolve(true);
413
+ }
414
+ async disconnect() {
415
+ return Promise.resolve();
416
+ }
417
+ async getAddresses() {
418
+ const { baseDerivationPath, derivationPathType } = this;
419
+ try {
420
+ await this.trezor.connect();
421
+ return (await (await this.trezor.getAccountManager()).getWallets(baseDerivationPath, derivationPathType)).map((k) => k.address);
422
+ } catch (e) {
423
+ throw new __injectivelabs_exceptions.TrezorException(new Error(e.message), {
424
+ code: __injectivelabs_exceptions.UnspecifiedErrorCode,
425
+ type: __injectivelabs_exceptions.ErrorType.WalletError,
426
+ contextModule: __injectivelabs_wallet_base.WalletAction.GetAccounts
427
+ });
428
+ }
429
+ }
430
+ async getAddressesInfo() {
431
+ const { baseDerivationPath, derivationPathType } = this;
432
+ try {
433
+ await this.trezor.connect();
434
+ return (await (await this.trezor.getAccountManager()).getWallets(baseDerivationPath, derivationPathType)).map((k) => ({
435
+ address: k.address,
436
+ derivationPath: k.derivationPath,
437
+ baseDerivationPath: derivationPathType
438
+ }));
439
+ } catch (e) {
440
+ throw new __injectivelabs_exceptions.TrezorException(new Error(e.message), {
441
+ code: __injectivelabs_exceptions.UnspecifiedErrorCode,
442
+ type: __injectivelabs_exceptions.ErrorType.WalletError,
443
+ contextModule: __injectivelabs_wallet_base.WalletAction.GetAccounts
444
+ });
445
+ }
446
+ }
447
+ async getSessionOrConfirm(address) {
448
+ return Promise.resolve(`0x${(0, __injectivelabs_sdk_ts_utils.uint8ArrayToHex)((0, __injectivelabs_sdk_ts_utils.stringToUint8Array)(`Confirmation for ${address} at time: ${Date.now()}`))}`);
449
+ }
450
+ async sendEvmTransaction(txData, args) {
451
+ const signedTransaction = await this.signEvmTransaction(txData, args);
452
+ try {
453
+ return await (await this.getPublicClient(args.evmChainId)).sendRawTransaction({ serializedTransaction: signedTransaction });
454
+ } catch (e) {
455
+ throw new __injectivelabs_exceptions.TrezorException(new Error(e.message), {
456
+ code: __injectivelabs_exceptions.UnspecifiedErrorCode,
457
+ type: __injectivelabs_exceptions.ErrorType.WalletError,
458
+ contextModule: __injectivelabs_wallet_base.WalletAction.SendEvmTransaction
459
+ });
460
+ }
461
+ }
462
+ async sendTransaction(transaction, options) {
463
+ const { endpoints, txTimeout } = options;
464
+ if (!endpoints) throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error("You have to pass endpoints.grpc within the options for using Ethereum native wallets"));
465
+ const response = await new __injectivelabs_sdk_ts_core_tx.TxGrpcApi(endpoints.grpc).broadcast(transaction, { txTimeout });
466
+ if (response.code !== 0) throw new __injectivelabs_exceptions.TransactionException(new Error(response.rawLog), {
467
+ code: __injectivelabs_exceptions.UnspecifiedErrorCode,
468
+ contextCode: response.code,
469
+ contextModule: response.codespace
470
+ });
471
+ return response;
472
+ }
473
+ async signEip712TypedData(eip712json, address) {
474
+ const TrezorConnect = await loadTrezorConnect();
475
+ const object = JSON.parse(eip712json);
476
+ const { types: { EIP712Domain = [], ...otherTypes } = {}, message = {}, domain = {}, primaryType, domain_separator_hash, message_hash } = transformTypedData({
477
+ ...object,
478
+ domain: {
479
+ ...object.domain,
480
+ chainId: object.domain.chainId,
481
+ salt: "0"
482
+ }
483
+ });
484
+ try {
485
+ await this.trezor.connect();
486
+ const { derivationPath } = await this.getWalletForAddress(address);
487
+ const response = await TrezorConnect.ethereumSignTypedData({
488
+ path: derivationPath,
489
+ data: {
490
+ types: {
491
+ EIP712Domain,
492
+ ...otherTypes
493
+ },
494
+ message,
495
+ domain,
496
+ primaryType
497
+ },
498
+ message_hash,
499
+ domain_separator_hash,
500
+ metamask_v4_compat: true
501
+ });
502
+ if ("code" in response.payload && response.payload.code === "Failure_ActionCancelled") throw new Error("Request rejected");
503
+ if (!response.success) throw new Error(response.payload && response.payload.error || "Unknown error");
504
+ return response.payload.signature;
505
+ } catch (e) {
506
+ throw new __injectivelabs_exceptions.TrezorException(new Error(e.message), {
507
+ code: __injectivelabs_exceptions.UnspecifiedErrorCode,
508
+ type: __injectivelabs_exceptions.ErrorType.WalletError,
509
+ contextModule: __injectivelabs_wallet_base.WalletAction.SignTransaction
510
+ });
511
+ }
512
+ }
513
+ async signAminoCosmosTransaction(_transaction) {
514
+ throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error("This wallet does not support signing Cosmos transactions"), {
515
+ code: __injectivelabs_exceptions.UnspecifiedErrorCode,
516
+ type: __injectivelabs_exceptions.ErrorType.WalletError,
517
+ contextModule: __injectivelabs_wallet_base.WalletAction.SendTransaction
518
+ });
519
+ }
520
+ async signCosmosTransaction(_transaction) {
521
+ throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error("This wallet does not support signing Cosmos transactions"), {
522
+ code: __injectivelabs_exceptions.UnspecifiedErrorCode,
523
+ type: __injectivelabs_exceptions.ErrorType.WalletError,
524
+ contextModule: __injectivelabs_wallet_base.WalletAction.SendTransaction
525
+ });
526
+ }
527
+ async signArbitrary(signer, data) {
528
+ const TrezorConnect = await loadTrezorConnect();
529
+ try {
530
+ await this.trezor.connect();
531
+ const { derivationPath } = await this.getWalletForAddress(signer);
532
+ const response = await TrezorConnect.ethereumSignMessage({
533
+ path: derivationPath,
534
+ message: (0, __injectivelabs_sdk_ts_utils.toUtf8)(data)
535
+ });
536
+ if (!response.success) throw new Error(response.payload && response.payload.error || "Unknown error");
537
+ return response.payload.signature;
538
+ } catch (e) {
539
+ throw new __injectivelabs_exceptions.TrezorException(new Error(e.message), {
540
+ code: __injectivelabs_exceptions.UnspecifiedErrorCode,
541
+ type: __injectivelabs_exceptions.ErrorType.WalletError,
542
+ contextModule: __injectivelabs_wallet_base.WalletAction.SignTransaction
543
+ });
544
+ }
545
+ }
546
+ async getEthereumChainId() {
547
+ return (await (await this.getPublicClient()).getChainId()).toString();
548
+ }
549
+ async getEvmTransactionReceipt(txHash, evmChainId) {
550
+ const publicClient = await this.getPublicClient(evmChainId);
551
+ try {
552
+ await publicClient.waitForTransactionReceipt({
553
+ hash: txHash,
554
+ timeout: 3e4,
555
+ pollingInterval: 3e3
556
+ });
557
+ return txHash;
558
+ } catch (_unused) {
559
+ throw new Error(`Failed to retrieve transaction receipt for txHash: ${txHash}`);
560
+ }
561
+ }
562
+ async getPubKey() {
563
+ throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error("You can only fetch PubKey from Cosmos native wallets"));
564
+ }
565
+ async signEvmTransaction(txData, args) {
566
+ const TrezorConnect = await loadTrezorConnect();
567
+ const chainId = parseInt(args.evmChainId.toString(), 10);
568
+ const publicClient = await this.getPublicClient(args.evmChainId);
569
+ const address = args.address.startsWith("0x") ? args.address : `0x${args.address}`;
570
+ const nonce = await publicClient.getTransactionCount({ address });
571
+ const parseHexValue = (value) => {
572
+ if (typeof value === "string") {
573
+ const hexValue = value.startsWith("0x") ? value : `0x${value}`;
574
+ return BigInt(hexValue);
575
+ }
576
+ return BigInt(value);
577
+ };
578
+ const valueBigInt = parseHexValue(txData.value || "0x0");
579
+ const gasBigInt = parseHexValue(txData.gas);
580
+ const maxFeePerGasBigInt = parseHexValue(txData.maxFeePerGas);
581
+ const maxPriorityFeePerGasBigInt = parseHexValue(txData.maxPriorityFeePerGas);
582
+ const trezorTxData = {
583
+ to: txData.to,
584
+ value: (0, viem.toHex)(valueBigInt),
585
+ gasLimit: (0, viem.toHex)(gasBigInt),
586
+ nonce: (0, viem.toHex)(nonce),
587
+ data: txData.data || "0x",
588
+ chainId,
589
+ maxFeePerGas: (0, viem.toHex)(maxFeePerGasBigInt),
590
+ maxPriorityFeePerGas: (0, viem.toHex)(maxPriorityFeePerGasBigInt)
591
+ };
592
+ try {
593
+ await this.trezor.connect();
594
+ const { derivationPath } = await this.getWalletForAddress(args.address);
595
+ const response = await TrezorConnect.ethereumSignTransaction({
596
+ path: derivationPath,
597
+ transaction: trezorTxData
598
+ });
599
+ if (!response.success) throw new __injectivelabs_exceptions.TrezorException(new Error(response.payload && response.payload.error || "Something happened while signing with Trezor"), {
600
+ code: __injectivelabs_exceptions.UnspecifiedErrorCode,
601
+ type: __injectivelabs_exceptions.ErrorType.WalletError,
602
+ contextModule: __injectivelabs_wallet_base.WalletAction.SignEvmTransaction
603
+ });
604
+ return (0, viem.serializeTransaction)({
605
+ type: "eip1559",
606
+ chainId,
607
+ nonce,
608
+ to: txData.to,
609
+ value: valueBigInt,
610
+ data: txData.data || "0x",
611
+ gas: gasBigInt,
612
+ maxFeePerGas: maxFeePerGasBigInt,
613
+ maxPriorityFeePerGas: maxPriorityFeePerGasBigInt,
614
+ v: BigInt(response.payload.v),
615
+ r: response.payload.r,
616
+ s: response.payload.s
617
+ });
618
+ } catch (e) {
619
+ if (e instanceof __injectivelabs_exceptions.TrezorException) throw e;
620
+ throw new __injectivelabs_exceptions.TrezorException(new Error(e.message), {
621
+ code: __injectivelabs_exceptions.UnspecifiedErrorCode,
622
+ type: __injectivelabs_exceptions.ErrorType.WalletError,
623
+ contextModule: __injectivelabs_wallet_base.WalletAction.SignEvmTransaction
624
+ });
625
+ }
626
+ }
627
+ async getWalletForAddress(address) {
628
+ const { baseDerivationPath, derivationPathType } = this;
629
+ const accountManager = await this.trezor.getAccountManager();
630
+ if (!accountManager.hasWalletForAddress(address)) for (let i = 0; i < __injectivelabs_wallet_base.DEFAULT_ADDRESS_SEARCH_LIMIT / __injectivelabs_wallet_base.DEFAULT_NUM_ADDRESSES_TO_FETCH; i += 1) {
631
+ await accountManager.getWallets(baseDerivationPath, derivationPathType);
632
+ if (accountManager.hasWalletForAddress(address)) return await accountManager.getWalletForAddress(address);
633
+ }
634
+ return await accountManager.getWalletForAddress(address);
635
+ }
636
+ async getEip1193Provider() {
637
+ var _this$metadata;
638
+ return new TrezorEip1193Provider(this.trezor, {
639
+ chainId: this.evmOptions.evmChainId.toString(),
640
+ derivationPath: (_this$metadata = this.metadata) === null || _this$metadata === void 0 ? void 0 : _this$metadata.derivationPath
641
+ });
642
+ }
643
+ async getPublicClient(evmChainId) {
644
+ var _options$rpcUrls;
645
+ if (this.publicClient) return this.publicClient;
646
+ const options = this.evmOptions;
647
+ const chainId = evmChainId || options.evmChainId;
648
+ const url = options.rpcUrl || ((_options$rpcUrls = options.rpcUrls) === null || _options$rpcUrls === void 0 ? void 0 : _options$rpcUrls[chainId]);
649
+ if (!url) throw new __injectivelabs_exceptions.GeneralException(/* @__PURE__ */ new Error("Please pass rpcUrl within the ethereumOptions"));
650
+ this.publicClient = (0, __injectivelabs_wallet_base.getViemPublicClient)(chainId, url);
651
+ return this.publicClient;
652
+ }
653
+ };
654
+
655
+ //#endregion
656
+ //#region src/strategy/TrezorBip32.ts
657
+ var TrezorBip32 = class extends TrezorBase {
658
+ constructor(args) {
659
+ super({
660
+ ...args,
661
+ derivationPathType: TrezorDerivationPathType.Bip32
662
+ });
663
+ }
664
+ };
665
+
666
+ //#endregion
667
+ //#region src/strategy/TrezorBip44.ts
668
+ var TrezorBip44 = class extends TrezorBase {
669
+ constructor(args) {
670
+ super({
671
+ ...args,
672
+ derivationPathType: TrezorDerivationPathType.Bip44
673
+ });
674
+ }
675
+ };
676
+
677
+ //#endregion
678
+ exports.TrezorBip32Strategy = TrezorBip32;
679
+ exports.TrezorBip44Strategy = TrezorBip44;
680
+ exports.TrezorDerivationPathType = TrezorDerivationPathType;