@sidhujag/sysweb3-keyring 2.1.8 → 2.1.9

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.
@@ -15,6 +15,7 @@ import { deriveEvmAccountFromMnemonic, parsePersonalMessage as parseLocalPersona
15
15
  import { captureEvmSubmission, submitSignedEvmTransaction, submitSignedHardwareEvmTransaction, evmSubmissionFailure, copyEvmSubmissionFailure, } from './evm-submission';
16
16
  import { signLedgerEvmTransaction } from './ledger-signing';
17
17
  import { signPreparedEvmEnvelope, snapshotPreparedEvmRequest, snapshotEvmSigningIdentity, preparedEvmGuard, } from './prepared-evm';
18
+ import { signTrezorEvmTransaction } from './trezor-signing';
18
19
  import { getAddressDerivationPath } from '../utils/derivation-paths';
19
20
  const hardwareSignerUnavailable = (hardware) => Object.assign(new Error(`${hardware} signer is unavailable in this host`), {
20
21
  code: 'HARDWARE_SIGNER_UNAVAILABLE',
@@ -32,26 +33,6 @@ const stripHexPrefix = (value) => {
32
33
  ? value.slice(2)
33
34
  : value;
34
35
  };
35
- // Connect returns hexadecimal signature fields; normalize their encoding
36
- // without mutating the response or reparsing an already numeric recovery v.
37
- const trezorSignature = (signature) => {
38
- const { r, s, v } = signature;
39
- if (![r, s].every((value) => /^(?:0x)?[0-9a-f]{64}$/i.test(value))) {
40
- throw new Error('Invalid Trezor signature scalar encoding');
41
- }
42
- if (typeof v === 'string' && !/^(?:0x)?[0-9a-f]+$/i.test(v)) {
43
- throw new Error('Invalid Trezor signature recovery value');
44
- }
45
- const recovery = typeof v === 'number' ? v : Number.parseInt(v, 16);
46
- if (!Number.isSafeInteger(recovery) || recovery < 0) {
47
- throw new Error('Invalid Trezor signature recovery value');
48
- }
49
- return {
50
- r: `0x${stripHexPrefix(r)}`,
51
- s: `0x${stripHexPrefix(s)}`,
52
- v: recovery,
53
- };
54
- };
55
36
  const normalizeEthersOverrideValue = (value) => normalizeTxValue(value);
56
37
  // The same protocol bound applies to software and external-device envelopes.
57
38
  // Keep this at serialization as well as before handing a request to a device.
@@ -1061,29 +1042,20 @@ export class EthereumTransactions {
1061
1042
  const expectedUnsignedTransaction = serializeTransaction(txFormattedForTrezor);
1062
1043
  if (retained)
1063
1044
  assertCancellationEnvelope(expectedUnsignedTransaction);
1064
- const signature = await this.requireTrezorSigner().signEthTransaction({
1045
+ const { rawTransaction: signedTx } = await signTrezorEvmTransaction(this.requireTrezorSigner(), {
1046
+ accountIndex: activeAccountId,
1065
1047
  coin: trezorCoin,
1066
- tx: txFormattedForTrezor,
1067
- index: activeAccountId.toString(),
1048
+ expectedAddress: activeAccount.address,
1068
1049
  slip44: activeNetwork.slip44,
1050
+ unsignedTransaction: expectedUnsignedTransaction,
1069
1051
  }, await this.authorizeHardwareTransaction(provider, assertCurrent));
1070
1052
  assertCurrent();
1071
- if (signature.success) {
1072
- const signedTx = serializeTransaction(txFormattedForTrezor, trezorSignature(signature.payload));
1073
- assertCurrent();
1074
- assertHardwareTransactionSender(signedTx, activeAccount.address);
1075
- const transactionResponse = await this.submitSignedHardwareTransaction(provider, signedTx, ({ rawTransaction, admit }) => admit(() => provider.sendTransaction(rawTransaction)), submission, undefined, expectedUnsignedTransaction);
1076
- return {
1077
- isCanceled: true,
1078
- transaction: transactionResponse,
1079
- };
1080
- }
1081
- else {
1082
- return {
1083
- isCanceled: false,
1084
- error: true,
1085
- };
1086
- }
1053
+ assertHardwareTransactionSender(signedTx, activeAccount.address);
1054
+ const transactionResponse = await this.submitSignedHardwareTransaction(provider, signedTx, ({ rawTransaction, admit }) => admit(() => provider.sendTransaction(rawTransaction)), submission, undefined, expectedUnsignedTransaction);
1055
+ return {
1056
+ isCanceled: true,
1057
+ transaction: transactionResponse,
1058
+ };
1087
1059
  }
1088
1060
  catch (error) {
1089
1061
  return {
@@ -1498,40 +1470,17 @@ export class EthereumTransactions {
1498
1470
  ...txFormattedForTrezor,
1499
1471
  type: isLegacy ? 0 : 2,
1500
1472
  });
1501
- const signature = await this.requireTrezorSigner().signEthTransaction({
1502
- index: `${activeAccountId}`,
1503
- tx: txFormattedForTrezor,
1473
+ const { rawTransaction: signedTx } = await signTrezorEvmTransaction(this.requireTrezorSigner(), {
1474
+ accountIndex: activeAccountId,
1504
1475
  coin: activeNetwork.currency,
1476
+ expectedAddress: activeAccount.address,
1505
1477
  slip44: activeNetwork.slip44,
1478
+ unsignedTransaction: expectedUnsignedTransaction,
1506
1479
  }, await this.authorizeHardwareTransaction(provider, assertCurrent));
1507
1480
  assertCurrent();
1508
- if (signature.success) {
1509
- try {
1510
- const txFormattedForEthers = isLegacy
1511
- ? {
1512
- ...formatParams,
1513
- nonce: transactionNonce,
1514
- chainId: activeNetwork.chainId,
1515
- type: 0, // Force Type 0 for legacy
1516
- }
1517
- : {
1518
- ...formatParams,
1519
- nonce: transactionNonce,
1520
- chainId: activeNetwork.chainId,
1521
- type: 2, // Need explicit type for hardware wallet serialization
1522
- };
1523
- const signedTx = serializeTransaction(txFormattedForEthers, trezorSignature(signature.payload));
1524
- assertHardwareTransactionSender(signedTx, activeAccount.address);
1525
- const finalTx = await this.submitSignedHardwareTransaction(provider, signedTx, ({ rawTransaction, admit }) => admit(() => provider.sendTransaction(rawTransaction)), submission, assertBeforeBroadcast, expectedUnsignedTransaction);
1526
- return finalTx;
1527
- }
1528
- catch (error) {
1529
- throw error;
1530
- }
1531
- }
1532
- else {
1533
- throw new Error(`Transaction Signature Failed. Error: ${signature}`);
1534
- }
1481
+ assertHardwareTransactionSender(signedTx, activeAccount.address);
1482
+ const finalTx = await this.submitSignedHardwareTransaction(provider, signedTx, ({ rawTransaction, admit }) => admit(() => provider.sendTransaction(rawTransaction)), submission, assertBeforeBroadcast, expectedUnsignedTransaction);
1483
+ return finalTx;
1535
1484
  };
1536
1485
  const sendEVMTransaction = async () => {
1537
1486
  assertCurrent();
@@ -1863,28 +1812,20 @@ export class EthereumTransactions {
1863
1812
  assertCurrent();
1864
1813
  assertTransactionGasLimit(txFormattedForTrezor);
1865
1814
  const expectedUnsignedTransaction = serializeTransaction(txFormattedForTrezor);
1866
- const signature = await this.requireTrezorSigner().signEthTransaction({
1815
+ const { rawTransaction: signedTx } = await signTrezorEvmTransaction(this.requireTrezorSigner(), {
1816
+ accountIndex: activeAccountId,
1867
1817
  coin: trezorCoin,
1868
- tx: txFormattedForTrezor,
1869
- index: activeAccountId.toString(),
1818
+ expectedAddress: activeAccount.address,
1870
1819
  slip44: activeNetwork.slip44,
1820
+ unsignedTransaction: expectedUnsignedTransaction,
1871
1821
  }, await this.authorizeHardwareTransaction(provider, assertCurrent));
1872
- if (signature.success) {
1873
- const signedTx = serializeTransaction(txFormattedForTrezor, trezorSignature(signature.payload));
1874
- assertSignedSender(signedTx);
1875
- assertHardwareTransactionSender(signedTx, activeAccount.address);
1876
- const transactionResponse = await this.submitSignedHardwareTransaction(provider, signedTx, ({ rawTransaction, admit }) => admit(() => provider.sendTransaction(rawTransaction)), submission, assertBeforeBroadcast, expectedUnsignedTransaction);
1877
- return {
1878
- isSpeedUp: true,
1879
- transaction: transactionResponse,
1880
- };
1881
- }
1882
- else {
1883
- return {
1884
- isSpeedUp: false,
1885
- error: true,
1886
- };
1887
- }
1822
+ assertSignedSender(signedTx);
1823
+ assertHardwareTransactionSender(signedTx, activeAccount.address);
1824
+ const transactionResponse = await this.submitSignedHardwareTransaction(provider, signedTx, ({ rawTransaction, admit }) => admit(() => provider.sendTransaction(rawTransaction)), submission, assertBeforeBroadcast, expectedUnsignedTransaction);
1825
+ return {
1826
+ isSpeedUp: true,
1827
+ transaction: transactionResponse,
1828
+ };
1888
1829
  }
1889
1830
  catch (error) {
1890
1831
  console.error('[SpeedUp] Failed to send replacement transaction with Trezor:', error);
@@ -2099,58 +2040,21 @@ export class EthereumTransactions {
2099
2040
  ...txToBeSignedByTrezor,
2100
2041
  type: isLegacy ? 0 : 2,
2101
2042
  });
2102
- const signature = await this.requireTrezorSigner().signEthTransaction({
2103
- index: `${activeAccountId}`,
2104
- tx: txToBeSignedByTrezor,
2043
+ const { rawTransaction: signedTx } = await signTrezorEvmTransaction(this.requireTrezorSigner(), {
2044
+ accountIndex: activeAccountId,
2105
2045
  coin: activeNetwork.currency,
2046
+ expectedAddress: activeAccount.address,
2106
2047
  slip44: activeNetwork.slip44,
2048
+ unsignedTransaction: expectedUnsignedTransaction,
2107
2049
  }, await this.authorizeHardwareTransaction(provider, assertCurrent, signal));
2108
2050
  assertCurrent();
2109
- if (signature.success) {
2110
- try {
2111
- let txFormattedForEthers;
2112
- if (isLegacy) {
2113
- txFormattedForEthers = {
2114
- to: tokenAddress,
2115
- value: '0x0',
2116
- gasLimit: gasLimit,
2117
- gasPrice,
2118
- data: txData,
2119
- nonce,
2120
- chainId: activeNetwork.chainId,
2121
- type: 0,
2122
- };
2123
- }
2124
- else {
2125
- txFormattedForEthers = {
2126
- to: tokenAddress,
2127
- value: '0x0',
2128
- gasLimit: gasLimit,
2129
- maxFeePerGas,
2130
- maxPriorityFeePerGas,
2131
- data: txData,
2132
- nonce,
2133
- chainId: activeNetwork.chainId,
2134
- type: 2, // Need explicit type for hardware wallet serialization
2135
- };
2136
- }
2137
- const signedTx = serializeTransaction(txFormattedForEthers, trezorSignature(signature.payload));
2138
- assertCurrent();
2139
- assertHardwareTransactionSender(signedTx, activeAccount.address);
2140
- const finalTx = await this.submitSignedHardwareTransaction(provider, signedTx, async ({ rawTransaction, admit }) => {
2141
- const response = await admit(() => provider.sendTransaction(rawTransaction));
2142
- saveTrezorTx && saveTrezorTx(response);
2143
- return response;
2144
- }, submission, undefined, expectedUnsignedTransaction);
2145
- return finalTx;
2146
- }
2147
- catch (error) {
2148
- throw error;
2149
- }
2150
- }
2151
- else {
2152
- throw new Error(`Transaction Signature Failed. Error: ${signature}`);
2153
- }
2051
+ assertHardwareTransactionSender(signedTx, activeAccount.address);
2052
+ const finalTx = await this.submitSignedHardwareTransaction(provider, signedTx, async ({ rawTransaction, admit }) => {
2053
+ const response = await admit(() => provider.sendTransaction(rawTransaction));
2054
+ saveTrezorTx && saveTrezorTx(response);
2055
+ return response;
2056
+ }, submission, undefined, expectedUnsignedTransaction);
2057
+ return finalTx;
2154
2058
  }
2155
2059
  catch (error) {
2156
2060
  throw error;
@@ -2305,55 +2209,17 @@ export class EthereumTransactions {
2305
2209
  ...txToBeSignedByTrezor,
2306
2210
  type: isLegacy ? 0 : 2,
2307
2211
  });
2308
- const signature = await this.requireTrezorSigner().signEthTransaction({
2309
- index: `${activeAccountId}`,
2310
- tx: txToBeSignedByTrezor,
2212
+ const { rawTransaction: signedTx } = await signTrezorEvmTransaction(this.requireTrezorSigner(), {
2213
+ accountIndex: activeAccountId,
2311
2214
  coin: trezorCoin,
2215
+ expectedAddress: activeAccount.address,
2312
2216
  slip44: activeNetwork.slip44,
2217
+ unsignedTransaction: expectedUnsignedTransaction,
2313
2218
  }, await this.authorizeHardwareTransaction(provider, assertCurrent, signal));
2314
2219
  assertCurrent();
2315
- if (signature.success) {
2316
- try {
2317
- let txFormattedForEthers;
2318
- if (isLegacy) {
2319
- txFormattedForEthers = {
2320
- to: tokenAddress,
2321
- value: '0x0',
2322
- gasLimit,
2323
- gasPrice,
2324
- data: txData,
2325
- nonce,
2326
- chainId: activeNetwork.chainId,
2327
- type: 0,
2328
- };
2329
- }
2330
- else {
2331
- txFormattedForEthers = {
2332
- to: tokenAddress,
2333
- value: '0x0',
2334
- gasLimit,
2335
- maxFeePerGas,
2336
- maxPriorityFeePerGas,
2337
- data: txData,
2338
- nonce,
2339
- chainId: activeNetwork.chainId,
2340
- type: 2, // Need explicit type for hardware wallet serialization
2341
- };
2342
- }
2343
- const signedTx = serializeTransaction(txFormattedForEthers, trezorSignature(signature.payload));
2344
- assertCurrent();
2345
- assertHardwareTransactionSender(signedTx, activeAccount.address);
2346
- const finalTx = await this.submitSignedHardwareTransaction(provider, signedTx, ({ rawTransaction, admit }) => admit(() => provider.sendTransaction(rawTransaction)), submission, undefined, expectedUnsignedTransaction);
2347
- return finalTx;
2348
- }
2349
- catch (error) {
2350
- console.log({ error });
2351
- throw error;
2352
- }
2353
- }
2354
- else {
2355
- throw new Error(`Transaction Signature Failed. Error: ${signature}`);
2356
- }
2220
+ assertHardwareTransactionSender(signedTx, activeAccount.address);
2221
+ const finalTx = await this.submitSignedHardwareTransaction(provider, signedTx, ({ rawTransaction, admit }) => admit(() => provider.sendTransaction(rawTransaction)), submission, undefined, expectedUnsignedTransaction);
2222
+ return finalTx;
2357
2223
  }
2358
2224
  catch (error) {
2359
2225
  console.log({ errorDois: error });
@@ -2502,55 +2368,17 @@ export class EthereumTransactions {
2502
2368
  ...txToBeSignedByTrezor,
2503
2369
  type: isLegacy ? 0 : 2,
2504
2370
  });
2505
- const signature = await this.requireTrezorSigner().signEthTransaction({
2506
- index: `${activeAccountId}`,
2507
- tx: txToBeSignedByTrezor,
2371
+ const { rawTransaction: signedTx } = await signTrezorEvmTransaction(this.requireTrezorSigner(), {
2372
+ accountIndex: activeAccountId,
2508
2373
  coin: activeNetwork.currency,
2374
+ expectedAddress: activeAccount.address,
2509
2375
  slip44: activeNetwork.slip44,
2376
+ unsignedTransaction: expectedUnsignedTransaction,
2510
2377
  }, await this.authorizeHardwareTransaction(provider, assertCurrent, signal));
2511
2378
  assertCurrent();
2512
- if (signature.success) {
2513
- try {
2514
- let txFormattedForEthers;
2515
- if (isLegacy) {
2516
- txFormattedForEthers = {
2517
- to: tokenAddress,
2518
- value: '0x0',
2519
- gasLimit,
2520
- gasPrice,
2521
- data: txData,
2522
- nonce,
2523
- chainId: activeNetwork.chainId,
2524
- type: 0,
2525
- };
2526
- }
2527
- else {
2528
- txFormattedForEthers = {
2529
- to: tokenAddress,
2530
- value: '0x0',
2531
- gasLimit,
2532
- maxFeePerGas,
2533
- maxPriorityFeePerGas,
2534
- data: txData,
2535
- nonce,
2536
- chainId: activeNetwork.chainId,
2537
- type: 2, // Need explicit type for hardware wallet serialization
2538
- };
2539
- }
2540
- const signedTx = serializeTransaction(txFormattedForEthers, trezorSignature(signature.payload));
2541
- assertCurrent();
2542
- assertHardwareTransactionSender(signedTx, activeAccount.address);
2543
- const finalTx = await this.submitSignedHardwareTransaction(provider, signedTx, ({ rawTransaction, admit }) => admit(() => provider.sendTransaction(rawTransaction)), submission, undefined, expectedUnsignedTransaction);
2544
- return finalTx;
2545
- }
2546
- catch (error) {
2547
- console.log({ error });
2548
- throw error;
2549
- }
2550
- }
2551
- else {
2552
- throw new Error(`Transaction Signature Failed. Error: ${signature}`);
2553
- }
2379
+ assertHardwareTransactionSender(signedTx, activeAccount.address);
2380
+ const finalTx = await this.submitSignedHardwareTransaction(provider, signedTx, ({ rawTransaction, admit }) => admit(() => provider.sendTransaction(rawTransaction)), submission, undefined, expectedUnsignedTransaction);
2381
+ return finalTx;
2554
2382
  }
2555
2383
  catch (error) {
2556
2384
  console.log({ error });