@injectivelabs/wallet-ledger 1.17.2-alpha.9 → 1.17.2

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.
@@ -165,25 +165,6 @@ var LedgerTransport$1 = class LedgerTransport$1 {
165
165
  }
166
166
  };
167
167
 
168
- //#endregion
169
- //#region src/strategy/Ledger/utils.ts
170
- /**
171
- * Used mainly for Ledger Nano S
172
- */
173
- const domainHash = (message) => {
174
- return (0, viem.hashDomain)({
175
- domain: message.domain,
176
- types: message.types
177
- });
178
- };
179
- const messageHash = (message) => {
180
- return (0, viem.hashStruct)({
181
- data: message.message,
182
- types: message.types,
183
- primaryType: message.primaryType
184
- });
185
- };
186
-
187
168
  //#endregion
188
169
  //#region src/strategy/Ledger/Eip1193Provider.ts
189
170
  var LedgerEip1193Provider = class {
@@ -303,6 +284,79 @@ var LedgerEip1193Provider = class {
303
284
  }
304
285
  };
305
286
 
287
+ //#endregion
288
+ //#region src/strategy/Ledger/utils.ts
289
+ /**
290
+ * Used mainly for Ledger Nano S
291
+ */
292
+ const domainHash = (message) => {
293
+ return (0, viem.hashDomain)({
294
+ domain: message.domain,
295
+ types: message.types
296
+ });
297
+ };
298
+ const messageHash = (message) => {
299
+ return (0, viem.hashStruct)({
300
+ data: message.message,
301
+ types: message.types,
302
+ primaryType: message.primaryType
303
+ });
304
+ };
305
+ /**
306
+ * Checks if an EIP-712 payload is too large for Ledger hardware wallet signing
307
+ * @param {Object} eip712Payload - The EIP-712 typed data object
308
+ * @returns {boolean} - Returns true if payload is too big, false otherwise
309
+ */
310
+ function isEIP712PayloadTooBig(eip712Payload) {
311
+ const LIMITS = {
312
+ MAX_TOTAL_SIZE: 8e3,
313
+ MAX_MSGS_SIZE: 4e3,
314
+ MAX_CONTEXT_SIZE: 2e3,
315
+ MAX_NESTING_DEPTH: 3,
316
+ MAX_MESSAGE_COUNT: 3
317
+ };
318
+ try {
319
+ const totalSize = JSON.stringify(eip712Payload).length;
320
+ if (totalSize > LIMITS.MAX_TOTAL_SIZE) {
321
+ console.log(`❌ Total payload size (${totalSize} bytes) exceeds limit (${LIMITS.MAX_TOTAL_SIZE} bytes)`);
322
+ return true;
323
+ }
324
+ if (eip712Payload.message?.msgs) {
325
+ const msgsSize = eip712Payload.message.msgs.length;
326
+ if (msgsSize > LIMITS.MAX_MSGS_SIZE) {
327
+ console.log(`❌ msgs field size (${msgsSize} bytes) exceeds limit (${LIMITS.MAX_MSGS_SIZE} bytes)`);
328
+ return true;
329
+ }
330
+ try {
331
+ const msgsArray = JSON.parse(eip712Payload.message.msgs);
332
+ if (Array.isArray(msgsArray) && msgsArray.length > LIMITS.MAX_MESSAGE_COUNT) {
333
+ console.log(`❌ Message count (${msgsArray.length}) exceeds limit (${LIMITS.MAX_MESSAGE_COUNT})`);
334
+ return true;
335
+ }
336
+ for (const msg of msgsArray) if (msg.msg && typeof msg.msg === "string") {
337
+ const escapeCount = (msg.msg.match(/\\\\/g) || []).length;
338
+ if (escapeCount > 10) {
339
+ console.log(`❌ Detected deeply nested/escaped JSON (${escapeCount} escape sequences)`);
340
+ return true;
341
+ }
342
+ }
343
+ } catch (e) {
344
+ console.warn("⚠️ Could not parse msgs field:", e.message);
345
+ }
346
+ }
347
+ if (eip712Payload.message?.context) {
348
+ const contextSize = eip712Payload.message.context.length;
349
+ if (contextSize > LIMITS.MAX_CONTEXT_SIZE) {
350
+ console.log(`❌ context field size (${contextSize} bytes) exceeds limit (${LIMITS.MAX_CONTEXT_SIZE} bytes)`);
351
+ return true;
352
+ }
353
+ }
354
+ return false;
355
+ } catch (_error) {
356
+ return true;
357
+ }
358
+ }
359
+
306
360
  //#endregion
307
361
  //#region src/strategy/Ledger/Base.ts
308
362
  var LedgerBase = class extends __injectivelabs_wallet_base.BaseConcreteStrategy {
@@ -390,32 +444,42 @@ var LedgerBase = class extends __injectivelabs_wallet_base.BaseConcreteStrategy
390
444
  async signEip712TypedData(eip712json, address) {
391
445
  const derivationPath = await this.getDerivationPath(address);
392
446
  const object = JSON.parse(eip712json);
447
+ if (isEIP712PayloadTooBig(object)) {
448
+ console.log("Payload is too big, signing with hashed message");
449
+ return this.signEIP712HashedMessage(derivationPath, object);
450
+ }
393
451
  try {
452
+ console.log("Payload is not too big, signing with message");
394
453
  const result = await (await this.ledger.getInstance()).signEIP712Message(derivationPath, object);
395
- const v = result.v.toString(16).padStart(2, "0");
396
- const combined = `${result.r}${result.s}${v}`;
397
- return combined.startsWith("0x") ? combined : `0x${combined}`;
454
+ return this.formatSignatureResult(result);
398
455
  } catch (e) {
456
+ console.log("Error signing EIP712 message:", e);
399
457
  const errorMessage = e.message;
400
458
  if (!(errorMessage.includes("instruction not supported") || errorMessage.includes("invalid status") || errorMessage.includes("not supported") || errorMessage.includes("INS_NOT_SUPPORTED"))) throw new __injectivelabs_exceptions.LedgerException(new Error(errorMessage), {
401
459
  code: __injectivelabs_exceptions.UnspecifiedErrorCode,
402
460
  type: __injectivelabs_exceptions.ErrorType.WalletError,
403
461
  contextModule: __injectivelabs_wallet_base.WalletAction.SignTransaction
404
462
  });
405
- try {
406
- const result = await (await this.ledger.getInstance()).signEIP712HashedMessage(derivationPath, domainHash(object), messageHash(object));
407
- const v = result.v.toString(16).padStart(2, "0");
408
- const combined = `${result.r}${result.s}${v}`;
409
- return combined.startsWith("0x") ? combined : `0x${combined}`;
410
- } catch (e$1) {
411
- throw new __injectivelabs_exceptions.LedgerException(new Error(e$1.message), {
412
- code: __injectivelabs_exceptions.UnspecifiedErrorCode,
413
- type: __injectivelabs_exceptions.ErrorType.WalletError,
414
- contextModule: __injectivelabs_wallet_base.WalletAction.SignTransaction
415
- });
416
- }
463
+ return this.signEIP712HashedMessage(derivationPath, object);
417
464
  }
418
465
  }
466
+ async signEIP712HashedMessage(derivationPath, object) {
467
+ try {
468
+ const result = await (await this.ledger.getInstance()).signEIP712HashedMessage(derivationPath, domainHash(object), messageHash(object));
469
+ return this.formatSignatureResult(result);
470
+ } catch (e) {
471
+ throw new __injectivelabs_exceptions.LedgerException(new Error(e.message), {
472
+ code: __injectivelabs_exceptions.UnspecifiedErrorCode,
473
+ type: __injectivelabs_exceptions.ErrorType.WalletError,
474
+ contextModule: __injectivelabs_wallet_base.WalletAction.SignTransaction
475
+ });
476
+ }
477
+ }
478
+ formatSignatureResult(result) {
479
+ const v = result.v.toString(16).padStart(2, "0");
480
+ const combined = `${result.r}${result.s}${v}`;
481
+ return combined.startsWith("0x") ? combined : `0x${combined}`;
482
+ }
419
483
  async signAminoCosmosTransaction(_transaction) {
420
484
  throw new __injectivelabs_exceptions.WalletException(/* @__PURE__ */ new Error("This wallet does not support signing Cosmos transactions"), {
421
485
  code: __injectivelabs_exceptions.UnspecifiedErrorCode,
@@ -434,9 +498,7 @@ var LedgerBase = class extends __injectivelabs_wallet_base.BaseConcreteStrategy
434
498
  try {
435
499
  const derivationPath = await this.getDerivationPath(signer);
436
500
  const result = await (await this.ledger.getInstance()).signPersonalMessage(derivationPath, (0, __injectivelabs_sdk_ts_utils.uint8ArrayToHex)((0, __injectivelabs_sdk_ts_utils.stringToUint8Array)((0, __injectivelabs_sdk_ts_utils.toUtf8)(data))));
437
- const v = result.v.toString(16).padStart(2, "0");
438
- const combined = `${result.r}${result.s}${v}`;
439
- return combined.startsWith("0x") ? combined : `0x${combined}`;
501
+ return this.formatSignatureResult(result);
440
502
  } catch (e) {
441
503
  throw new __injectivelabs_exceptions.LedgerException(new Error(e.message), {
442
504
  code: __injectivelabs_exceptions.UnspecifiedErrorCode,
@@ -55,6 +55,8 @@ declare class LedgerBase extends BaseConcreteStrategy implements ConcreteWalletS
55
55
  }): Promise<string>;
56
56
  sendTransaction(transaction: TxRaw, options: SendTransactionOptions): Promise<TxResponse>;
57
57
  signEip712TypedData(eip712json: string, address: AccountAddress): Promise<string>;
58
+ private signEIP712HashedMessage;
59
+ private formatSignatureResult;
58
60
  signAminoCosmosTransaction(_transaction: {
59
61
  address: string;
60
62
  signDoc: StdSignDoc;
@@ -55,6 +55,8 @@ declare class LedgerBase extends BaseConcreteStrategy implements ConcreteWalletS
55
55
  }): Promise<string>;
56
56
  sendTransaction(transaction: TxRaw, options: SendTransactionOptions): Promise<TxResponse>;
57
57
  signEip712TypedData(eip712json: string, address: AccountAddress): Promise<string>;
58
+ private signEIP712HashedMessage;
59
+ private formatSignatureResult;
58
60
  signAminoCosmosTransaction(_transaction: {
59
61
  address: string;
60
62
  signDoc: StdSignDoc;
package/dist/esm/index.js CHANGED
@@ -164,25 +164,6 @@ var LedgerTransport$1 = class LedgerTransport$1 {
164
164
  }
165
165
  };
166
166
 
167
- //#endregion
168
- //#region src/strategy/Ledger/utils.ts
169
- /**
170
- * Used mainly for Ledger Nano S
171
- */
172
- const domainHash = (message) => {
173
- return hashDomain({
174
- domain: message.domain,
175
- types: message.types
176
- });
177
- };
178
- const messageHash = (message) => {
179
- return hashStruct({
180
- data: message.message,
181
- types: message.types,
182
- primaryType: message.primaryType
183
- });
184
- };
185
-
186
167
  //#endregion
187
168
  //#region src/strategy/Ledger/Eip1193Provider.ts
188
169
  var LedgerEip1193Provider = class {
@@ -302,6 +283,79 @@ var LedgerEip1193Provider = class {
302
283
  }
303
284
  };
304
285
 
286
+ //#endregion
287
+ //#region src/strategy/Ledger/utils.ts
288
+ /**
289
+ * Used mainly for Ledger Nano S
290
+ */
291
+ const domainHash = (message) => {
292
+ return hashDomain({
293
+ domain: message.domain,
294
+ types: message.types
295
+ });
296
+ };
297
+ const messageHash = (message) => {
298
+ return hashStruct({
299
+ data: message.message,
300
+ types: message.types,
301
+ primaryType: message.primaryType
302
+ });
303
+ };
304
+ /**
305
+ * Checks if an EIP-712 payload is too large for Ledger hardware wallet signing
306
+ * @param {Object} eip712Payload - The EIP-712 typed data object
307
+ * @returns {boolean} - Returns true if payload is too big, false otherwise
308
+ */
309
+ function isEIP712PayloadTooBig(eip712Payload) {
310
+ const LIMITS = {
311
+ MAX_TOTAL_SIZE: 8e3,
312
+ MAX_MSGS_SIZE: 4e3,
313
+ MAX_CONTEXT_SIZE: 2e3,
314
+ MAX_NESTING_DEPTH: 3,
315
+ MAX_MESSAGE_COUNT: 3
316
+ };
317
+ try {
318
+ const totalSize = JSON.stringify(eip712Payload).length;
319
+ if (totalSize > LIMITS.MAX_TOTAL_SIZE) {
320
+ console.log(`❌ Total payload size (${totalSize} bytes) exceeds limit (${LIMITS.MAX_TOTAL_SIZE} bytes)`);
321
+ return true;
322
+ }
323
+ if (eip712Payload.message?.msgs) {
324
+ const msgsSize = eip712Payload.message.msgs.length;
325
+ if (msgsSize > LIMITS.MAX_MSGS_SIZE) {
326
+ console.log(`❌ msgs field size (${msgsSize} bytes) exceeds limit (${LIMITS.MAX_MSGS_SIZE} bytes)`);
327
+ return true;
328
+ }
329
+ try {
330
+ const msgsArray = JSON.parse(eip712Payload.message.msgs);
331
+ if (Array.isArray(msgsArray) && msgsArray.length > LIMITS.MAX_MESSAGE_COUNT) {
332
+ console.log(`❌ Message count (${msgsArray.length}) exceeds limit (${LIMITS.MAX_MESSAGE_COUNT})`);
333
+ return true;
334
+ }
335
+ for (const msg of msgsArray) if (msg.msg && typeof msg.msg === "string") {
336
+ const escapeCount = (msg.msg.match(/\\\\/g) || []).length;
337
+ if (escapeCount > 10) {
338
+ console.log(`❌ Detected deeply nested/escaped JSON (${escapeCount} escape sequences)`);
339
+ return true;
340
+ }
341
+ }
342
+ } catch (e) {
343
+ console.warn("⚠️ Could not parse msgs field:", e.message);
344
+ }
345
+ }
346
+ if (eip712Payload.message?.context) {
347
+ const contextSize = eip712Payload.message.context.length;
348
+ if (contextSize > LIMITS.MAX_CONTEXT_SIZE) {
349
+ console.log(`❌ context field size (${contextSize} bytes) exceeds limit (${LIMITS.MAX_CONTEXT_SIZE} bytes)`);
350
+ return true;
351
+ }
352
+ }
353
+ return false;
354
+ } catch (_error) {
355
+ return true;
356
+ }
357
+ }
358
+
305
359
  //#endregion
306
360
  //#region src/strategy/Ledger/Base.ts
307
361
  var LedgerBase = class extends BaseConcreteStrategy {
@@ -389,32 +443,42 @@ var LedgerBase = class extends BaseConcreteStrategy {
389
443
  async signEip712TypedData(eip712json, address) {
390
444
  const derivationPath = await this.getDerivationPath(address);
391
445
  const object = JSON.parse(eip712json);
446
+ if (isEIP712PayloadTooBig(object)) {
447
+ console.log("Payload is too big, signing with hashed message");
448
+ return this.signEIP712HashedMessage(derivationPath, object);
449
+ }
392
450
  try {
451
+ console.log("Payload is not too big, signing with message");
393
452
  const result = await (await this.ledger.getInstance()).signEIP712Message(derivationPath, object);
394
- const v = result.v.toString(16).padStart(2, "0");
395
- const combined = `${result.r}${result.s}${v}`;
396
- return combined.startsWith("0x") ? combined : `0x${combined}`;
453
+ return this.formatSignatureResult(result);
397
454
  } catch (e) {
455
+ console.log("Error signing EIP712 message:", e);
398
456
  const errorMessage = e.message;
399
457
  if (!(errorMessage.includes("instruction not supported") || errorMessage.includes("invalid status") || errorMessage.includes("not supported") || errorMessage.includes("INS_NOT_SUPPORTED"))) throw new LedgerException(new Error(errorMessage), {
400
458
  code: UnspecifiedErrorCode,
401
459
  type: ErrorType.WalletError,
402
460
  contextModule: WalletAction.SignTransaction
403
461
  });
404
- try {
405
- const result = await (await this.ledger.getInstance()).signEIP712HashedMessage(derivationPath, domainHash(object), messageHash(object));
406
- const v = result.v.toString(16).padStart(2, "0");
407
- const combined = `${result.r}${result.s}${v}`;
408
- return combined.startsWith("0x") ? combined : `0x${combined}`;
409
- } catch (e$1) {
410
- throw new LedgerException(new Error(e$1.message), {
411
- code: UnspecifiedErrorCode,
412
- type: ErrorType.WalletError,
413
- contextModule: WalletAction.SignTransaction
414
- });
415
- }
462
+ return this.signEIP712HashedMessage(derivationPath, object);
416
463
  }
417
464
  }
465
+ async signEIP712HashedMessage(derivationPath, object) {
466
+ try {
467
+ const result = await (await this.ledger.getInstance()).signEIP712HashedMessage(derivationPath, domainHash(object), messageHash(object));
468
+ return this.formatSignatureResult(result);
469
+ } catch (e) {
470
+ throw new LedgerException(new Error(e.message), {
471
+ code: UnspecifiedErrorCode,
472
+ type: ErrorType.WalletError,
473
+ contextModule: WalletAction.SignTransaction
474
+ });
475
+ }
476
+ }
477
+ formatSignatureResult(result) {
478
+ const v = result.v.toString(16).padStart(2, "0");
479
+ const combined = `${result.r}${result.s}${v}`;
480
+ return combined.startsWith("0x") ? combined : `0x${combined}`;
481
+ }
418
482
  async signAminoCosmosTransaction(_transaction) {
419
483
  throw new WalletException(/* @__PURE__ */ new Error("This wallet does not support signing Cosmos transactions"), {
420
484
  code: UnspecifiedErrorCode,
@@ -433,9 +497,7 @@ var LedgerBase = class extends BaseConcreteStrategy {
433
497
  try {
434
498
  const derivationPath = await this.getDerivationPath(signer);
435
499
  const result = await (await this.ledger.getInstance()).signPersonalMessage(derivationPath, uint8ArrayToHex(stringToUint8Array(toUtf8(data))));
436
- const v = result.v.toString(16).padStart(2, "0");
437
- const combined = `${result.r}${result.s}${v}`;
438
- return combined.startsWith("0x") ? combined : `0x${combined}`;
500
+ return this.formatSignatureResult(result);
439
501
  } catch (e) {
440
502
  throw new LedgerException(new Error(e.message), {
441
503
  code: UnspecifiedErrorCode,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@injectivelabs/wallet-ledger",
3
- "version": "1.17.2-alpha.9",
3
+ "version": "1.17.2",
4
4
  "description": "Ledger wallet strategy for use with @injectivelabs/wallet-core.",
5
5
  "license": "Apache-2.0",
6
6
  "author": {
@@ -49,10 +49,10 @@
49
49
  "@ledgerhq/hw-transport-webusb": "^6.29.13",
50
50
  "buffer": "^6.0.3",
51
51
  "viem": "^2.41.2",
52
- "@injectivelabs/exceptions": "1.17.2-alpha.9",
53
- "@injectivelabs/ts-types": "1.17.2-alpha.9",
54
- "@injectivelabs/sdk-ts": "1.17.2-alpha.9",
55
- "@injectivelabs/wallet-base": "1.17.2-alpha.9"
52
+ "@injectivelabs/exceptions": "1.17.2",
53
+ "@injectivelabs/wallet-base": "1.17.2",
54
+ "@injectivelabs/sdk-ts": "1.17.2",
55
+ "@injectivelabs/ts-types": "1.17.2"
56
56
  },
57
57
  "peerDependencies": {
58
58
  "@ethersproject/abi": "^5.7.0",