@metamask-previews/eth-trezor-keyring 9.0.0-fd40efd → 9.1.0-8ff7ba2

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 (40) hide show
  1. package/CHANGELOG.md +21 -1
  2. package/dist/index.cjs +2 -0
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +2 -0
  5. package/dist/index.d.cts.map +1 -1
  6. package/dist/index.d.mts +2 -0
  7. package/dist/index.d.mts.map +1 -1
  8. package/dist/index.mjs +2 -0
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/trezor-error-handler.cjs +52 -0
  11. package/dist/trezor-error-handler.cjs.map +1 -0
  12. package/dist/trezor-error-handler.d.cts +9 -0
  13. package/dist/trezor-error-handler.d.cts.map +1 -0
  14. package/dist/trezor-error-handler.d.mts +9 -0
  15. package/dist/trezor-error-handler.d.mts.map +1 -0
  16. package/dist/trezor-error-handler.mjs +49 -0
  17. package/dist/trezor-error-handler.mjs.map +1 -0
  18. package/dist/trezor-errors.cjs +57 -0
  19. package/dist/trezor-errors.cjs.map +1 -0
  20. package/dist/trezor-errors.d.cts +24 -0
  21. package/dist/trezor-errors.d.cts.map +1 -0
  22. package/dist/trezor-errors.d.mts +24 -0
  23. package/dist/trezor-errors.d.mts.map +1 -0
  24. package/dist/trezor-errors.mjs +52 -0
  25. package/dist/trezor-errors.mjs.map +1 -0
  26. package/dist/trezor-keyring-v2.cjs +2 -1
  27. package/dist/trezor-keyring-v2.cjs.map +1 -1
  28. package/dist/trezor-keyring-v2.d.cts +2 -1
  29. package/dist/trezor-keyring-v2.d.cts.map +1 -1
  30. package/dist/trezor-keyring-v2.d.mts +2 -1
  31. package/dist/trezor-keyring-v2.d.mts.map +1 -1
  32. package/dist/trezor-keyring-v2.mjs +2 -1
  33. package/dist/trezor-keyring-v2.mjs.map +1 -1
  34. package/dist/trezor-keyring.cjs +88 -81
  35. package/dist/trezor-keyring.cjs.map +1 -1
  36. package/dist/trezor-keyring.d.cts.map +1 -1
  37. package/dist/trezor-keyring.d.mts.map +1 -1
  38. package/dist/trezor-keyring.mjs +88 -81
  39. package/dist/trezor-keyring.mjs.map +1 -1
  40. package/package.json +6 -4
package/CHANGELOG.md CHANGED
@@ -7,11 +7,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Changed
11
+
12
+ - Add new dependency `@metamask/keyring-sdk@1.1.0` ([#478](https://github.com/MetaMask/accounts/pull/478)), ([#482](https://github.com/MetaMask/accounts/pull/482)), ([#496](https://github.com/MetaMask/accounts/pull/496))
13
+ - This package now contains the keyring v2 wrapper helpers (`EthKeyringWrapper`).
14
+ - Bump `@metamask/hw-wallet-sdk` from `^0.6.0` to `^0.7.0` ([#482](https://github.com/MetaMask/accounts/pull/482))
15
+ - Bump `@metamask/keyring-api` from `^21.6.0` to `^22.0.0` ([#482](https://github.com/MetaMask/accounts/pull/482))
16
+ - Bump `@metamask/account-api` from `^1.0.0` to `^1.0.1` ([#487](https://github.com/MetaMask/accounts/pull/487))
17
+ - Bump `@metamask/utils` from `^11.1.0` to `^11.10.0` ([#489](https://github.com/MetaMask/accounts/pull/489))
18
+ - Bump `@metamask/hw-wallet-sdk` from `^0.7.0` to `^0.8.0` ([#497](https://github.com/MetaMask/accounts/pull/497))
19
+
20
+ ## [9.1.0]
21
+
10
22
  ### Added
11
23
 
12
24
  - Add `TrezorKeyringV2` and `OneKeyKeyringV2` classes implementing `KeyringV2` interface ([#412](https://github.com/MetaMask/accounts/pull/412)), ([#451](https://github.com/MetaMask/accounts/pull/451)), ([#453](https://github.com/MetaMask/accounts/pull/453))
13
25
  - Wraps legacy `TrezorKeyring` and `OneKeyKeyring` to expose accounts via the unified `KeyringV2` API and the `KeyringAccount` type.
14
26
  - Extends `EthKeyringWrapper` for common Ethereum logic.
27
+ - Add `createTrezorError` and `getTrezorErrorIdentifier` error helpers ([#471](https://github.com/MetaMask/accounts/pull/471))
28
+
29
+ ### Changed
30
+
31
+ - Integrate `@metamask/hw-wallet-sdk` for standardized Trezor error handling ([#471](https://github.com/MetaMask/accounts/pull/471))
32
+ - Replace custom transport and user-action error handling with typed `HardwareWalletError` instances.
33
+ - Move Trezor error mappings and utilities to `@metamask/hw-wallet-sdk` for reuse across packages.
15
34
 
16
35
  ## [9.0.0]
17
36
 
@@ -217,7 +236,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
217
236
 
218
237
  - Support new versions of ethereumjs/tx ([#88](https://github.com/metamask/eth-trezor-keyring/pull/88))
219
238
 
220
- [Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-trezor-keyring@9.0.0...HEAD
239
+ [Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/eth-trezor-keyring@9.1.0...HEAD
240
+ [9.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-trezor-keyring@9.0.0...@metamask/eth-trezor-keyring@9.1.0
221
241
  [9.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-trezor-keyring@8.0.0...@metamask/eth-trezor-keyring@9.0.0
222
242
  [8.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-trezor-keyring@7.0.0...@metamask/eth-trezor-keyring@8.0.0
223
243
  [7.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/eth-trezor-keyring@6.1.1...@metamask/eth-trezor-keyring@7.0.0
package/dist/index.cjs CHANGED
@@ -18,5 +18,7 @@ __exportStar(require("./trezor-keyring.cjs"), exports);
18
18
  __exportStar(require("./trezor-keyring-v2.cjs"), exports);
19
19
  __exportStar(require("./onekey-keyring.cjs"), exports);
20
20
  __exportStar(require("./onekey-keyring-v2.cjs"), exports);
21
+ __exportStar(require("./trezor-error-handler.cjs"), exports);
22
+ __exportStar(require("./trezor-errors.cjs"), exports);
21
23
  __exportStar(require("./trezor-connect-bridge.cjs"), exports);
22
24
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAiC;AACjC,0DAAoC;AACpC,uDAAiC;AACjC,0DAAoC;AAEpC,8DAAwC","sourcesContent":["export * from './trezor-keyring';\nexport * from './trezor-keyring-v2';\nexport * from './onekey-keyring';\nexport * from './onekey-keyring-v2';\nexport type * from './trezor-bridge';\nexport * from './trezor-connect-bridge';\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAiC;AACjC,0DAAoC;AACpC,uDAAiC;AACjC,0DAAoC;AACpC,6DAAuC;AACvC,sDAAgC;AAEhC,8DAAwC","sourcesContent":["export * from './trezor-keyring';\nexport * from './trezor-keyring-v2';\nexport * from './onekey-keyring';\nexport * from './onekey-keyring-v2';\nexport * from './trezor-error-handler';\nexport * from './trezor-errors';\nexport type * from './trezor-bridge';\nexport * from './trezor-connect-bridge';\n"]}
package/dist/index.d.cts CHANGED
@@ -2,6 +2,8 @@ export * from "./trezor-keyring.cjs";
2
2
  export * from "./trezor-keyring-v2.cjs";
3
3
  export * from "./onekey-keyring.cjs";
4
4
  export * from "./onekey-keyring-v2.cjs";
5
+ export * from "./trezor-error-handler.cjs";
6
+ export * from "./trezor-errors.cjs";
5
7
  export type * from "./trezor-bridge.cjs";
6
8
  export * from "./trezor-connect-bridge.cjs";
7
9
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qCAAiC;AACjC,wCAAoC;AACpC,qCAAiC;AACjC,wCAAoC;AACpC,yCAAqC;AACrC,4CAAwC"}
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qCAAiC;AACjC,wCAAoC;AACpC,qCAAiC;AACjC,wCAAoC;AACpC,2CAAuC;AACvC,oCAAgC;AAChC,yCAAqC;AACrC,4CAAwC"}
package/dist/index.d.mts CHANGED
@@ -2,6 +2,8 @@ export * from "./trezor-keyring.mjs";
2
2
  export * from "./trezor-keyring-v2.mjs";
3
3
  export * from "./onekey-keyring.mjs";
4
4
  export * from "./onekey-keyring-v2.mjs";
5
+ export * from "./trezor-error-handler.mjs";
6
+ export * from "./trezor-errors.mjs";
5
7
  export type * from "./trezor-bridge.mjs";
6
8
  export * from "./trezor-connect-bridge.mjs";
7
9
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qCAAiC;AACjC,wCAAoC;AACpC,qCAAiC;AACjC,wCAAoC;AACpC,yCAAqC;AACrC,4CAAwC"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qCAAiC;AACjC,wCAAoC;AACpC,qCAAiC;AACjC,wCAAoC;AACpC,2CAAuC;AACvC,oCAAgC;AAChC,yCAAqC;AACrC,4CAAwC"}
package/dist/index.mjs CHANGED
@@ -2,5 +2,7 @@ export * from "./trezor-keyring.mjs";
2
2
  export * from "./trezor-keyring-v2.mjs";
3
3
  export * from "./onekey-keyring.mjs";
4
4
  export * from "./onekey-keyring-v2.mjs";
5
+ export * from "./trezor-error-handler.mjs";
6
+ export * from "./trezor-errors.mjs";
5
7
  export * from "./trezor-connect-bridge.mjs";
6
8
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qCAAiC;AACjC,wCAAoC;AACpC,qCAAiC;AACjC,wCAAoC;AAEpC,4CAAwC","sourcesContent":["export * from './trezor-keyring';\nexport * from './trezor-keyring-v2';\nexport * from './onekey-keyring';\nexport * from './onekey-keyring-v2';\nexport type * from './trezor-bridge';\nexport * from './trezor-connect-bridge';\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qCAAiC;AACjC,wCAAoC;AACpC,qCAAiC;AACjC,wCAAoC;AACpC,2CAAuC;AACvC,oCAAgC;AAEhC,4CAAwC","sourcesContent":["export * from './trezor-keyring';\nexport * from './trezor-keyring-v2';\nexport * from './onekey-keyring';\nexport * from './onekey-keyring-v2';\nexport * from './trezor-error-handler';\nexport * from './trezor-errors';\nexport type * from './trezor-bridge';\nexport * from './trezor-connect-bridge';\n"]}
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleTrezorTransportError = handleTrezorTransportError;
4
+ const hw_wallet_sdk_1 = require("@metamask/hw-wallet-sdk");
5
+ const trezor_errors_1 = require("./trezor-errors.cjs");
6
+ function getErrorDetails(error) {
7
+ const details = {
8
+ message: error.message,
9
+ name: error.name,
10
+ };
11
+ if ('code' in error) {
12
+ const { code } = error;
13
+ if (typeof code === 'string') {
14
+ details.code = code;
15
+ }
16
+ }
17
+ return details;
18
+ }
19
+ /**
20
+ * Converts unknown Trezor errors into typed HardwareWalletError instances.
21
+ *
22
+ * @param error - Error thrown from Trezor bridge or keyring flow.
23
+ * @param fallbackMessage - Default message for unknown non-Error inputs.
24
+ * @throws HardwareWalletError Always throws typed errors.
25
+ */
26
+ function handleTrezorTransportError(error, fallbackMessage) {
27
+ var _a, _b;
28
+ if (error instanceof hw_wallet_sdk_1.HardwareWalletError) {
29
+ throw error;
30
+ }
31
+ if (error instanceof Error) {
32
+ const details = getErrorDetails(error);
33
+ const identifier = [details.code, details.name, details.message].find((value) => value !== undefined && (0, trezor_errors_1.isKnownTrezorError)(value));
34
+ if (identifier) {
35
+ throw (0, trezor_errors_1.createTrezorError)(identifier, details.message);
36
+ }
37
+ throw new hw_wallet_sdk_1.HardwareWalletError((_a = details.message) !== null && _a !== void 0 ? _a : fallbackMessage, {
38
+ code: hw_wallet_sdk_1.ErrorCode.Unknown,
39
+ severity: hw_wallet_sdk_1.Severity.Err,
40
+ category: hw_wallet_sdk_1.Category.Unknown,
41
+ userMessage: (_b = details.message) !== null && _b !== void 0 ? _b : fallbackMessage,
42
+ cause: error,
43
+ });
44
+ }
45
+ throw new hw_wallet_sdk_1.HardwareWalletError(fallbackMessage, {
46
+ code: hw_wallet_sdk_1.ErrorCode.Unknown,
47
+ severity: hw_wallet_sdk_1.Severity.Err,
48
+ category: hw_wallet_sdk_1.Category.Unknown,
49
+ userMessage: fallbackMessage,
50
+ });
51
+ }
52
+ //# sourceMappingURL=trezor-error-handler.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trezor-error-handler.cjs","sourceRoot":"","sources":["../src/trezor-error-handler.ts"],"names":[],"mappings":";;AAsCA,gEAkCC;AAxED,2DAKiC;AAEjC,uDAAwE;AAQxE,SAAS,eAAe,CAAC,KAAY;IACnC,MAAM,OAAO,GAAiB;QAC5B,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,IAAI,EAAE,KAAK,CAAC,IAAI;KACjB,CAAC;IAEF,IAAI,MAAM,IAAI,KAAK,EAAE,CAAC;QACpB,MAAM,EAAE,IAAI,EAAE,GAAG,KAAmC,CAAC;QACrD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;QACtB,CAAC;IACH,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,0BAA0B,CACxC,KAAc,EACd,eAAuB;;IAEvB,IAAI,KAAK,YAAY,mCAAmB,EAAE,CAAC;QACzC,MAAM,KAAK,CAAC;IACd,CAAC;IAED,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,MAAM,OAAO,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;QACvC,MAAM,UAAU,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,IAAI,CACnE,CAAC,KAAK,EAAmB,EAAE,CACzB,KAAK,KAAK,SAAS,IAAI,IAAA,kCAAkB,EAAC,KAAK,CAAC,CACnD,CAAC;QAEF,IAAI,UAAU,EAAE,CAAC;YACf,MAAM,IAAA,iCAAiB,EAAC,UAAU,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;QACvD,CAAC;QAED,MAAM,IAAI,mCAAmB,CAAC,MAAA,OAAO,CAAC,OAAO,mCAAI,eAAe,EAAE;YAChE,IAAI,EAAE,yBAAS,CAAC,OAAO;YACvB,QAAQ,EAAE,wBAAQ,CAAC,GAAG;YACtB,QAAQ,EAAE,wBAAQ,CAAC,OAAO;YAC1B,WAAW,EAAE,MAAA,OAAO,CAAC,OAAO,mCAAI,eAAe;YAC/C,KAAK,EAAE,KAAK;SACb,CAAC,CAAC;IACL,CAAC;IAED,MAAM,IAAI,mCAAmB,CAAC,eAAe,EAAE;QAC7C,IAAI,EAAE,yBAAS,CAAC,OAAO;QACvB,QAAQ,EAAE,wBAAQ,CAAC,GAAG;QACtB,QAAQ,EAAE,wBAAQ,CAAC,OAAO;QAC1B,WAAW,EAAE,eAAe;KAC7B,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n ErrorCode,\n Severity,\n Category,\n HardwareWalletError,\n} from '@metamask/hw-wallet-sdk';\n\nimport { createTrezorError, isKnownTrezorError } from './trezor-errors';\n\ntype ErrorDetails = {\n message?: string;\n code?: string;\n name?: string;\n};\n\nfunction getErrorDetails(error: Error): ErrorDetails {\n const details: ErrorDetails = {\n message: error.message,\n name: error.name,\n };\n\n if ('code' in error) {\n const { code } = error as Error & { code?: unknown };\n if (typeof code === 'string') {\n details.code = code;\n }\n }\n\n return details;\n}\n\n/**\n * Converts unknown Trezor errors into typed HardwareWalletError instances.\n *\n * @param error - Error thrown from Trezor bridge or keyring flow.\n * @param fallbackMessage - Default message for unknown non-Error inputs.\n * @throws HardwareWalletError Always throws typed errors.\n */\nexport function handleTrezorTransportError(\n error: unknown,\n fallbackMessage: string,\n): never {\n if (error instanceof HardwareWalletError) {\n throw error;\n }\n\n if (error instanceof Error) {\n const details = getErrorDetails(error);\n const identifier = [details.code, details.name, details.message].find(\n (value): value is string =>\n value !== undefined && isKnownTrezorError(value),\n );\n\n if (identifier) {\n throw createTrezorError(identifier, details.message);\n }\n\n throw new HardwareWalletError(details.message ?? fallbackMessage, {\n code: ErrorCode.Unknown,\n severity: Severity.Err,\n category: Category.Unknown,\n userMessage: details.message ?? fallbackMessage,\n cause: error,\n });\n }\n\n throw new HardwareWalletError(fallbackMessage, {\n code: ErrorCode.Unknown,\n severity: Severity.Err,\n category: Category.Unknown,\n userMessage: fallbackMessage,\n });\n}\n"]}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Converts unknown Trezor errors into typed HardwareWalletError instances.
3
+ *
4
+ * @param error - Error thrown from Trezor bridge or keyring flow.
5
+ * @param fallbackMessage - Default message for unknown non-Error inputs.
6
+ * @throws HardwareWalletError Always throws typed errors.
7
+ */
8
+ export declare function handleTrezorTransportError(error: unknown, fallbackMessage: string): never;
9
+ //# sourceMappingURL=trezor-error-handler.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trezor-error-handler.d.cts","sourceRoot":"","sources":["../src/trezor-error-handler.ts"],"names":[],"mappings":"AA+BA;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CACxC,KAAK,EAAE,OAAO,EACd,eAAe,EAAE,MAAM,GACtB,KAAK,CA+BP"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Converts unknown Trezor errors into typed HardwareWalletError instances.
3
+ *
4
+ * @param error - Error thrown from Trezor bridge or keyring flow.
5
+ * @param fallbackMessage - Default message for unknown non-Error inputs.
6
+ * @throws HardwareWalletError Always throws typed errors.
7
+ */
8
+ export declare function handleTrezorTransportError(error: unknown, fallbackMessage: string): never;
9
+ //# sourceMappingURL=trezor-error-handler.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trezor-error-handler.d.mts","sourceRoot":"","sources":["../src/trezor-error-handler.ts"],"names":[],"mappings":"AA+BA;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CACxC,KAAK,EAAE,OAAO,EACd,eAAe,EAAE,MAAM,GACtB,KAAK,CA+BP"}
@@ -0,0 +1,49 @@
1
+ import { ErrorCode, Severity, Category, HardwareWalletError } from "@metamask/hw-wallet-sdk";
2
+ import { createTrezorError, isKnownTrezorError } from "./trezor-errors.mjs";
3
+ function getErrorDetails(error) {
4
+ const details = {
5
+ message: error.message,
6
+ name: error.name,
7
+ };
8
+ if ('code' in error) {
9
+ const { code } = error;
10
+ if (typeof code === 'string') {
11
+ details.code = code;
12
+ }
13
+ }
14
+ return details;
15
+ }
16
+ /**
17
+ * Converts unknown Trezor errors into typed HardwareWalletError instances.
18
+ *
19
+ * @param error - Error thrown from Trezor bridge or keyring flow.
20
+ * @param fallbackMessage - Default message for unknown non-Error inputs.
21
+ * @throws HardwareWalletError Always throws typed errors.
22
+ */
23
+ export function handleTrezorTransportError(error, fallbackMessage) {
24
+ var _a, _b;
25
+ if (error instanceof HardwareWalletError) {
26
+ throw error;
27
+ }
28
+ if (error instanceof Error) {
29
+ const details = getErrorDetails(error);
30
+ const identifier = [details.code, details.name, details.message].find((value) => value !== undefined && isKnownTrezorError(value));
31
+ if (identifier) {
32
+ throw createTrezorError(identifier, details.message);
33
+ }
34
+ throw new HardwareWalletError((_a = details.message) !== null && _a !== void 0 ? _a : fallbackMessage, {
35
+ code: ErrorCode.Unknown,
36
+ severity: Severity.Err,
37
+ category: Category.Unknown,
38
+ userMessage: (_b = details.message) !== null && _b !== void 0 ? _b : fallbackMessage,
39
+ cause: error,
40
+ });
41
+ }
42
+ throw new HardwareWalletError(fallbackMessage, {
43
+ code: ErrorCode.Unknown,
44
+ severity: Severity.Err,
45
+ category: Category.Unknown,
46
+ userMessage: fallbackMessage,
47
+ });
48
+ }
49
+ //# sourceMappingURL=trezor-error-handler.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trezor-error-handler.mjs","sourceRoot":"","sources":["../src/trezor-error-handler.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,mBAAmB,EACpB,gCAAgC;AAEjC,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,4BAAwB;AAQxE,SAAS,eAAe,CAAC,KAAY;IACnC,MAAM,OAAO,GAAiB;QAC5B,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,IAAI,EAAE,KAAK,CAAC,IAAI;KACjB,CAAC;IAEF,IAAI,MAAM,IAAI,KAAK,EAAE,CAAC;QACpB,MAAM,EAAE,IAAI,EAAE,GAAG,KAAmC,CAAC;QACrD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;QACtB,CAAC;IACH,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,0BAA0B,CACxC,KAAc,EACd,eAAuB;;IAEvB,IAAI,KAAK,YAAY,mBAAmB,EAAE,CAAC;QACzC,MAAM,KAAK,CAAC;IACd,CAAC;IAED,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,MAAM,OAAO,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;QACvC,MAAM,UAAU,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,IAAI,CACnE,CAAC,KAAK,EAAmB,EAAE,CACzB,KAAK,KAAK,SAAS,IAAI,kBAAkB,CAAC,KAAK,CAAC,CACnD,CAAC;QAEF,IAAI,UAAU,EAAE,CAAC;YACf,MAAM,iBAAiB,CAAC,UAAU,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;QACvD,CAAC;QAED,MAAM,IAAI,mBAAmB,CAAC,MAAA,OAAO,CAAC,OAAO,mCAAI,eAAe,EAAE;YAChE,IAAI,EAAE,SAAS,CAAC,OAAO;YACvB,QAAQ,EAAE,QAAQ,CAAC,GAAG;YACtB,QAAQ,EAAE,QAAQ,CAAC,OAAO;YAC1B,WAAW,EAAE,MAAA,OAAO,CAAC,OAAO,mCAAI,eAAe;YAC/C,KAAK,EAAE,KAAK;SACb,CAAC,CAAC;IACL,CAAC;IAED,MAAM,IAAI,mBAAmB,CAAC,eAAe,EAAE;QAC7C,IAAI,EAAE,SAAS,CAAC,OAAO;QACvB,QAAQ,EAAE,QAAQ,CAAC,GAAG;QACtB,QAAQ,EAAE,QAAQ,CAAC,OAAO;QAC1B,WAAW,EAAE,eAAe;KAC7B,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n ErrorCode,\n Severity,\n Category,\n HardwareWalletError,\n} from '@metamask/hw-wallet-sdk';\n\nimport { createTrezorError, isKnownTrezorError } from './trezor-errors';\n\ntype ErrorDetails = {\n message?: string;\n code?: string;\n name?: string;\n};\n\nfunction getErrorDetails(error: Error): ErrorDetails {\n const details: ErrorDetails = {\n message: error.message,\n name: error.name,\n };\n\n if ('code' in error) {\n const { code } = error as Error & { code?: unknown };\n if (typeof code === 'string') {\n details.code = code;\n }\n }\n\n return details;\n}\n\n/**\n * Converts unknown Trezor errors into typed HardwareWalletError instances.\n *\n * @param error - Error thrown from Trezor bridge or keyring flow.\n * @param fallbackMessage - Default message for unknown non-Error inputs.\n * @throws HardwareWalletError Always throws typed errors.\n */\nexport function handleTrezorTransportError(\n error: unknown,\n fallbackMessage: string,\n): never {\n if (error instanceof HardwareWalletError) {\n throw error;\n }\n\n if (error instanceof Error) {\n const details = getErrorDetails(error);\n const identifier = [details.code, details.name, details.message].find(\n (value): value is string =>\n value !== undefined && isKnownTrezorError(value),\n );\n\n if (identifier) {\n throw createTrezorError(identifier, details.message);\n }\n\n throw new HardwareWalletError(details.message ?? fallbackMessage, {\n code: ErrorCode.Unknown,\n severity: Severity.Err,\n category: Category.Unknown,\n userMessage: details.message ?? fallbackMessage,\n cause: error,\n });\n }\n\n throw new HardwareWalletError(fallbackMessage, {\n code: ErrorCode.Unknown,\n severity: Severity.Err,\n category: Category.Unknown,\n userMessage: fallbackMessage,\n });\n}\n"]}
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTrezorError = createTrezorError;
4
+ exports.isKnownTrezorError = isKnownTrezorError;
5
+ exports.getTrezorErrorMapping = getTrezorErrorMapping;
6
+ const hw_wallet_sdk_1 = require("@metamask/hw-wallet-sdk");
7
+ /**
8
+ * Factory function to create a HardwareWalletError from a Trezor error identifier.
9
+ *
10
+ * @param trezorErrorIdentifier - The Trezor error identifier (e.g., 'Device_Disconnected', 'Method_Cancel')
11
+ * @param context - Optional additional context to append to the error message
12
+ * @returns A HardwareWalletError instance with mapped error details
13
+ */
14
+ function createTrezorError(trezorErrorIdentifier, context) {
15
+ var _a;
16
+ const errorMapping = getTrezorErrorMapping(trezorErrorIdentifier);
17
+ if (errorMapping) {
18
+ const message = context
19
+ ? `${errorMapping.message} (${context})`
20
+ : errorMapping.message;
21
+ return new hw_wallet_sdk_1.HardwareWalletError(message, {
22
+ code: errorMapping.code,
23
+ severity: errorMapping.severity,
24
+ category: errorMapping.category,
25
+ userMessage: (_a = errorMapping.userMessage) !== null && _a !== void 0 ? _a : message,
26
+ });
27
+ }
28
+ // Fallback for unknown error codes
29
+ const fallbackMessage = context
30
+ ? `Unknown Trezor error: ${trezorErrorIdentifier} (${context})`
31
+ : `Unknown Trezor error: ${trezorErrorIdentifier}`;
32
+ return new hw_wallet_sdk_1.HardwareWalletError(fallbackMessage, {
33
+ code: hw_wallet_sdk_1.ErrorCode.Unknown,
34
+ severity: hw_wallet_sdk_1.Severity.Err,
35
+ category: hw_wallet_sdk_1.Category.Unknown,
36
+ userMessage: fallbackMessage,
37
+ });
38
+ }
39
+ /**
40
+ * Checks if a Trezor error identifier exists in the error mappings.
41
+ *
42
+ * @param trezorErrorIdentifier - The Trezor error identifier to check
43
+ * @returns True if the error identifier is mapped, false otherwise
44
+ */
45
+ function isKnownTrezorError(trezorErrorIdentifier) {
46
+ return trezorErrorIdentifier in hw_wallet_sdk_1.TREZOR_ERROR_MAPPINGS;
47
+ }
48
+ /**
49
+ * Gets the error mapping details for a Trezor error identifier without creating an error instance.
50
+ *
51
+ * @param trezorErrorIdentifier - The Trezor error identifier to look up
52
+ * @returns The error mapping details or undefined if not found
53
+ */
54
+ function getTrezorErrorMapping(trezorErrorIdentifier) {
55
+ return hw_wallet_sdk_1.TREZOR_ERROR_MAPPINGS[trezorErrorIdentifier];
56
+ }
57
+ //# sourceMappingURL=trezor-errors.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trezor-errors.cjs","sourceRoot":"","sources":["../src/trezor-errors.ts"],"names":[],"mappings":";;AAgBA,8CA8BC;AAQD,gDAEC;AAQD,sDAIC;AApED,2DAOiC;AAEjC;;;;;;GAMG;AACH,SAAgB,iBAAiB,CAC/B,qBAA6B,EAC7B,OAAgB;;IAEhB,MAAM,YAAY,GAAG,qBAAqB,CAAC,qBAAqB,CAAC,CAAC;IAElE,IAAI,YAAY,EAAE,CAAC;QACjB,MAAM,OAAO,GAAG,OAAO;YACrB,CAAC,CAAC,GAAG,YAAY,CAAC,OAAO,KAAK,OAAO,GAAG;YACxC,CAAC,CAAC,YAAY,CAAC,OAAO,CAAC;QAEzB,OAAO,IAAI,mCAAmB,CAAC,OAAO,EAAE;YACtC,IAAI,EAAE,YAAY,CAAC,IAAI;YACvB,QAAQ,EAAE,YAAY,CAAC,QAAQ;YAC/B,QAAQ,EAAE,YAAY,CAAC,QAAQ;YAC/B,WAAW,EAAE,MAAA,YAAY,CAAC,WAAW,mCAAI,OAAO;SACjD,CAAC,CAAC;IACL,CAAC;IAED,mCAAmC;IACnC,MAAM,eAAe,GAAG,OAAO;QAC7B,CAAC,CAAC,yBAAyB,qBAAqB,KAAK,OAAO,GAAG;QAC/D,CAAC,CAAC,yBAAyB,qBAAqB,EAAE,CAAC;IAErD,OAAO,IAAI,mCAAmB,CAAC,eAAe,EAAE;QAC9C,IAAI,EAAE,yBAAS,CAAC,OAAO;QACvB,QAAQ,EAAE,wBAAQ,CAAC,GAAG;QACtB,QAAQ,EAAE,wBAAQ,CAAC,OAAO;QAC1B,WAAW,EAAE,eAAe;KAC7B,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,SAAgB,kBAAkB,CAAC,qBAA6B;IAC9D,OAAO,qBAAqB,IAAI,qCAAqB,CAAC;AACxD,CAAC;AAED;;;;;GAKG;AACH,SAAgB,qBAAqB,CACnC,qBAA6B;IAE7B,OAAO,qCAAqB,CAAC,qBAAqB,CAAC,CAAC;AACtD,CAAC","sourcesContent":["import {\n type ErrorMapping,\n TREZOR_ERROR_MAPPINGS,\n ErrorCode,\n Severity,\n Category,\n HardwareWalletError,\n} from '@metamask/hw-wallet-sdk';\n\n/**\n * Factory function to create a HardwareWalletError from a Trezor error identifier.\n *\n * @param trezorErrorIdentifier - The Trezor error identifier (e.g., 'Device_Disconnected', 'Method_Cancel')\n * @param context - Optional additional context to append to the error message\n * @returns A HardwareWalletError instance with mapped error details\n */\nexport function createTrezorError(\n trezorErrorIdentifier: string,\n context?: string,\n): HardwareWalletError {\n const errorMapping = getTrezorErrorMapping(trezorErrorIdentifier);\n\n if (errorMapping) {\n const message = context\n ? `${errorMapping.message} (${context})`\n : errorMapping.message;\n\n return new HardwareWalletError(message, {\n code: errorMapping.code,\n severity: errorMapping.severity,\n category: errorMapping.category,\n userMessage: errorMapping.userMessage ?? message,\n });\n }\n\n // Fallback for unknown error codes\n const fallbackMessage = context\n ? `Unknown Trezor error: ${trezorErrorIdentifier} (${context})`\n : `Unknown Trezor error: ${trezorErrorIdentifier}`;\n\n return new HardwareWalletError(fallbackMessage, {\n code: ErrorCode.Unknown,\n severity: Severity.Err,\n category: Category.Unknown,\n userMessage: fallbackMessage,\n });\n}\n\n/**\n * Checks if a Trezor error identifier exists in the error mappings.\n *\n * @param trezorErrorIdentifier - The Trezor error identifier to check\n * @returns True if the error identifier is mapped, false otherwise\n */\nexport function isKnownTrezorError(trezorErrorIdentifier: string): boolean {\n return trezorErrorIdentifier in TREZOR_ERROR_MAPPINGS;\n}\n\n/**\n * Gets the error mapping details for a Trezor error identifier without creating an error instance.\n *\n * @param trezorErrorIdentifier - The Trezor error identifier to look up\n * @returns The error mapping details or undefined if not found\n */\nexport function getTrezorErrorMapping(\n trezorErrorIdentifier: string,\n): ErrorMapping | undefined {\n return TREZOR_ERROR_MAPPINGS[trezorErrorIdentifier];\n}\n"]}
@@ -0,0 +1,24 @@
1
+ import { type ErrorMapping, HardwareWalletError } from "@metamask/hw-wallet-sdk";
2
+ /**
3
+ * Factory function to create a HardwareWalletError from a Trezor error identifier.
4
+ *
5
+ * @param trezorErrorIdentifier - The Trezor error identifier (e.g., 'Device_Disconnected', 'Method_Cancel')
6
+ * @param context - Optional additional context to append to the error message
7
+ * @returns A HardwareWalletError instance with mapped error details
8
+ */
9
+ export declare function createTrezorError(trezorErrorIdentifier: string, context?: string): HardwareWalletError;
10
+ /**
11
+ * Checks if a Trezor error identifier exists in the error mappings.
12
+ *
13
+ * @param trezorErrorIdentifier - The Trezor error identifier to check
14
+ * @returns True if the error identifier is mapped, false otherwise
15
+ */
16
+ export declare function isKnownTrezorError(trezorErrorIdentifier: string): boolean;
17
+ /**
18
+ * Gets the error mapping details for a Trezor error identifier without creating an error instance.
19
+ *
20
+ * @param trezorErrorIdentifier - The Trezor error identifier to look up
21
+ * @returns The error mapping details or undefined if not found
22
+ */
23
+ export declare function getTrezorErrorMapping(trezorErrorIdentifier: string): ErrorMapping | undefined;
24
+ //# sourceMappingURL=trezor-errors.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trezor-errors.d.cts","sourceRoot":"","sources":["../src/trezor-errors.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EAKjB,mBAAmB,EACpB,gCAAgC;AAEjC;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,qBAAqB,EAAE,MAAM,EAC7B,OAAO,CAAC,EAAE,MAAM,GACf,mBAAmB,CA2BrB;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,qBAAqB,EAAE,MAAM,GAAG,OAAO,CAEzE;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,qBAAqB,EAAE,MAAM,GAC5B,YAAY,GAAG,SAAS,CAE1B"}
@@ -0,0 +1,24 @@
1
+ import { type ErrorMapping, HardwareWalletError } from "@metamask/hw-wallet-sdk";
2
+ /**
3
+ * Factory function to create a HardwareWalletError from a Trezor error identifier.
4
+ *
5
+ * @param trezorErrorIdentifier - The Trezor error identifier (e.g., 'Device_Disconnected', 'Method_Cancel')
6
+ * @param context - Optional additional context to append to the error message
7
+ * @returns A HardwareWalletError instance with mapped error details
8
+ */
9
+ export declare function createTrezorError(trezorErrorIdentifier: string, context?: string): HardwareWalletError;
10
+ /**
11
+ * Checks if a Trezor error identifier exists in the error mappings.
12
+ *
13
+ * @param trezorErrorIdentifier - The Trezor error identifier to check
14
+ * @returns True if the error identifier is mapped, false otherwise
15
+ */
16
+ export declare function isKnownTrezorError(trezorErrorIdentifier: string): boolean;
17
+ /**
18
+ * Gets the error mapping details for a Trezor error identifier without creating an error instance.
19
+ *
20
+ * @param trezorErrorIdentifier - The Trezor error identifier to look up
21
+ * @returns The error mapping details or undefined if not found
22
+ */
23
+ export declare function getTrezorErrorMapping(trezorErrorIdentifier: string): ErrorMapping | undefined;
24
+ //# sourceMappingURL=trezor-errors.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trezor-errors.d.mts","sourceRoot":"","sources":["../src/trezor-errors.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EAKjB,mBAAmB,EACpB,gCAAgC;AAEjC;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,qBAAqB,EAAE,MAAM,EAC7B,OAAO,CAAC,EAAE,MAAM,GACf,mBAAmB,CA2BrB;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,qBAAqB,EAAE,MAAM,GAAG,OAAO,CAEzE;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,qBAAqB,EAAE,MAAM,GAC5B,YAAY,GAAG,SAAS,CAE1B"}
@@ -0,0 +1,52 @@
1
+ import { TREZOR_ERROR_MAPPINGS, ErrorCode, Severity, Category, HardwareWalletError } from "@metamask/hw-wallet-sdk";
2
+ /**
3
+ * Factory function to create a HardwareWalletError from a Trezor error identifier.
4
+ *
5
+ * @param trezorErrorIdentifier - The Trezor error identifier (e.g., 'Device_Disconnected', 'Method_Cancel')
6
+ * @param context - Optional additional context to append to the error message
7
+ * @returns A HardwareWalletError instance with mapped error details
8
+ */
9
+ export function createTrezorError(trezorErrorIdentifier, context) {
10
+ var _a;
11
+ const errorMapping = getTrezorErrorMapping(trezorErrorIdentifier);
12
+ if (errorMapping) {
13
+ const message = context
14
+ ? `${errorMapping.message} (${context})`
15
+ : errorMapping.message;
16
+ return new HardwareWalletError(message, {
17
+ code: errorMapping.code,
18
+ severity: errorMapping.severity,
19
+ category: errorMapping.category,
20
+ userMessage: (_a = errorMapping.userMessage) !== null && _a !== void 0 ? _a : message,
21
+ });
22
+ }
23
+ // Fallback for unknown error codes
24
+ const fallbackMessage = context
25
+ ? `Unknown Trezor error: ${trezorErrorIdentifier} (${context})`
26
+ : `Unknown Trezor error: ${trezorErrorIdentifier}`;
27
+ return new HardwareWalletError(fallbackMessage, {
28
+ code: ErrorCode.Unknown,
29
+ severity: Severity.Err,
30
+ category: Category.Unknown,
31
+ userMessage: fallbackMessage,
32
+ });
33
+ }
34
+ /**
35
+ * Checks if a Trezor error identifier exists in the error mappings.
36
+ *
37
+ * @param trezorErrorIdentifier - The Trezor error identifier to check
38
+ * @returns True if the error identifier is mapped, false otherwise
39
+ */
40
+ export function isKnownTrezorError(trezorErrorIdentifier) {
41
+ return trezorErrorIdentifier in TREZOR_ERROR_MAPPINGS;
42
+ }
43
+ /**
44
+ * Gets the error mapping details for a Trezor error identifier without creating an error instance.
45
+ *
46
+ * @param trezorErrorIdentifier - The Trezor error identifier to look up
47
+ * @returns The error mapping details or undefined if not found
48
+ */
49
+ export function getTrezorErrorMapping(trezorErrorIdentifier) {
50
+ return TREZOR_ERROR_MAPPINGS[trezorErrorIdentifier];
51
+ }
52
+ //# sourceMappingURL=trezor-errors.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trezor-errors.mjs","sourceRoot":"","sources":["../src/trezor-errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,qBAAqB,EACrB,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,mBAAmB,EACpB,gCAAgC;AAEjC;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAC/B,qBAA6B,EAC7B,OAAgB;;IAEhB,MAAM,YAAY,GAAG,qBAAqB,CAAC,qBAAqB,CAAC,CAAC;IAElE,IAAI,YAAY,EAAE,CAAC;QACjB,MAAM,OAAO,GAAG,OAAO;YACrB,CAAC,CAAC,GAAG,YAAY,CAAC,OAAO,KAAK,OAAO,GAAG;YACxC,CAAC,CAAC,YAAY,CAAC,OAAO,CAAC;QAEzB,OAAO,IAAI,mBAAmB,CAAC,OAAO,EAAE;YACtC,IAAI,EAAE,YAAY,CAAC,IAAI;YACvB,QAAQ,EAAE,YAAY,CAAC,QAAQ;YAC/B,QAAQ,EAAE,YAAY,CAAC,QAAQ;YAC/B,WAAW,EAAE,MAAA,YAAY,CAAC,WAAW,mCAAI,OAAO;SACjD,CAAC,CAAC;IACL,CAAC;IAED,mCAAmC;IACnC,MAAM,eAAe,GAAG,OAAO;QAC7B,CAAC,CAAC,yBAAyB,qBAAqB,KAAK,OAAO,GAAG;QAC/D,CAAC,CAAC,yBAAyB,qBAAqB,EAAE,CAAC;IAErD,OAAO,IAAI,mBAAmB,CAAC,eAAe,EAAE;QAC9C,IAAI,EAAE,SAAS,CAAC,OAAO;QACvB,QAAQ,EAAE,QAAQ,CAAC,GAAG;QACtB,QAAQ,EAAE,QAAQ,CAAC,OAAO;QAC1B,WAAW,EAAE,eAAe;KAC7B,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,qBAA6B;IAC9D,OAAO,qBAAqB,IAAI,qBAAqB,CAAC;AACxD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CACnC,qBAA6B;IAE7B,OAAO,qBAAqB,CAAC,qBAAqB,CAAC,CAAC;AACtD,CAAC","sourcesContent":["import {\n type ErrorMapping,\n TREZOR_ERROR_MAPPINGS,\n ErrorCode,\n Severity,\n Category,\n HardwareWalletError,\n} from '@metamask/hw-wallet-sdk';\n\n/**\n * Factory function to create a HardwareWalletError from a Trezor error identifier.\n *\n * @param trezorErrorIdentifier - The Trezor error identifier (e.g., 'Device_Disconnected', 'Method_Cancel')\n * @param context - Optional additional context to append to the error message\n * @returns A HardwareWalletError instance with mapped error details\n */\nexport function createTrezorError(\n trezorErrorIdentifier: string,\n context?: string,\n): HardwareWalletError {\n const errorMapping = getTrezorErrorMapping(trezorErrorIdentifier);\n\n if (errorMapping) {\n const message = context\n ? `${errorMapping.message} (${context})`\n : errorMapping.message;\n\n return new HardwareWalletError(message, {\n code: errorMapping.code,\n severity: errorMapping.severity,\n category: errorMapping.category,\n userMessage: errorMapping.userMessage ?? message,\n });\n }\n\n // Fallback for unknown error codes\n const fallbackMessage = context\n ? `Unknown Trezor error: ${trezorErrorIdentifier} (${context})`\n : `Unknown Trezor error: ${trezorErrorIdentifier}`;\n\n return new HardwareWalletError(fallbackMessage, {\n code: ErrorCode.Unknown,\n severity: Severity.Err,\n category: Category.Unknown,\n userMessage: fallbackMessage,\n });\n}\n\n/**\n * Checks if a Trezor error identifier exists in the error mappings.\n *\n * @param trezorErrorIdentifier - The Trezor error identifier to check\n * @returns True if the error identifier is mapped, false otherwise\n */\nexport function isKnownTrezorError(trezorErrorIdentifier: string): boolean {\n return trezorErrorIdentifier in TREZOR_ERROR_MAPPINGS;\n}\n\n/**\n * Gets the error mapping details for a Trezor error identifier without creating an error instance.\n *\n * @param trezorErrorIdentifier - The Trezor error identifier to look up\n * @returns The error mapping details or undefined if not found\n */\nexport function getTrezorErrorMapping(\n trezorErrorIdentifier: string,\n): ErrorMapping | undefined {\n return TREZOR_ERROR_MAPPINGS[trezorErrorIdentifier];\n}\n"]}
@@ -8,6 +8,7 @@ var _TrezorKeyringV2_instances, _TrezorKeyringV2_parseDerivationPath, _TrezorKey
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.TrezorKeyringV2 = exports.LEGACY_MEW_PATH_PREFIX = exports.SLIP0044_TESTNET_PATH_PREFIX = exports.BIP44_HD_PATH_PREFIX = void 0;
10
10
  const keyring_api_1 = require("@metamask/keyring-api");
11
+ const keyring_sdk_1 = require("@metamask/keyring-sdk");
11
12
  /**
12
13
  * Methods supported by Trezor keyring EOA accounts.
13
14
  * Trezor keyrings support a subset of signing methods (no encryption, app keys, or EIP-7702).
@@ -55,7 +56,7 @@ const ALLOWED_HD_PATHS = [
55
56
  * The prefix is then validated against ALLOWED_HD_PATHS.
56
57
  */
57
58
  const DERIVATION_PATH_PATTERN = /^(m\/44'\/(?:60'|1')(?:\/\d+'?)*)\/(\d+)$/u;
58
- class TrezorKeyringV2 extends keyring_api_1.EthKeyringWrapper {
59
+ class TrezorKeyringV2 extends keyring_sdk_1.EthKeyringWrapper {
59
60
  constructor(options) {
60
61
  var _a;
61
62
  super({
@@ -1 +1 @@
1
- {"version":3,"file":"trezor-keyring-v2.cjs","sourceRoot":"","sources":["../src/trezor-keyring-v2.ts"],"names":[],"mappings":";;;;;;;;;AACA,uDAY+B;AAM/B;;;GAGG;AACH,MAAM,sBAAsB,GAAG;IAC7B,uBAAS,CAAC,eAAe;IACzB,uBAAS,CAAC,YAAY;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;QACL,QAAQ,EAAE,QAAyB;QACnC,KAAK;KACN,CAAC;AACJ,CAAC,yFAUC,OAAY,EACZ,YAAoB;IAEpB,MAAM,EAAE,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IAC3C,MAAM,cAAc,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,IAAI,YAAY,EAAE,CAAC;IAE9D,MAAM,OAAO,GAAiC;QAC5C,EAAE;QACF,IAAI,EAAE,4BAAc,CAAC,GAAG;QACxB,OAAO;QACP,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC;QACrC,OAAO,EAAE,CAAC,GAAG,sBAAsB,CAAC;QACpC,OAAO,EAAE;YACP,OAAO,EAAE;gBACP,IAAI,EAAE,6CAA+B,CAAC,QAAQ;gBAC9C,EAAE,EAAE,IAAI,CAAC,aAAa;gBACtB,UAAU,EAAE,YAAY;gBACxB,cAAc;aACf;SACF;KACF,CAAC;IAEF,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC3B,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\nimport {\n type CreateAccountOptions,\n EthAccountType,\n EthKeyringWrapper,\n EthMethod,\n EthScope,\n type KeyringAccount,\n KeyringAccountEntropyTypeOption,\n type KeyringCapabilities,\n type KeyringV2,\n KeyringType,\n type EntropySourceId,\n} from '@metamask/keyring-api';\nimport type { AccountId, EthKeyring } from '@metamask/keyring-utils';\nimport type { Hex, Json } from '@metamask/utils';\n\nimport type { TrezorKeyring } from './trezor-keyring';\n\n/**\n * Methods supported by Trezor keyring EOA accounts.\n * Trezor keyrings support a subset of signing methods (no encryption, app keys, or EIP-7702).\n */\nconst TREZOR_KEYRING_METHODS = [\n EthMethod.SignTransaction,\n EthMethod.PersonalSign,\n EthMethod.SignTypedDataV3,\n EthMethod.SignTypedDataV4,\n];\n\nconst trezorKeyringV2Capabilities: KeyringCapabilities = {\n scopes: [EthScope.Eoa],\n bip44: {\n deriveIndex: true,\n derivePath: true,\n },\n};\n\n/**\n * BIP-44 standard HD path prefix constant for Ethereum.\n * Used as default for derive-index operations.\n */\nexport const BIP44_HD_PATH_PREFIX = `m/44'/60'/0'/0`;\n\n/**\n * SLIP-0044 testnet HD path prefix constant.\n */\nexport const SLIP0044_TESTNET_PATH_PREFIX = `m/44'/1'/0'/0`;\n\n/**\n * Legacy MEW (MyEtherWallet) HD path prefix constant.\n */\nexport const LEGACY_MEW_PATH_PREFIX = `m/44'/60'/0'`;\n\n/**\n * Allowed HD paths for Trezor keyring.\n * These must match the keys in ALLOWED_HD_PATHS from trezor-keyring.ts.\n */\nconst ALLOWED_HD_PATHS = [\n BIP44_HD_PATH_PREFIX,\n SLIP0044_TESTNET_PATH_PREFIX,\n LEGACY_MEW_PATH_PREFIX,\n] as const;\n\n/**\n * Type representing one of the allowed Trezor HD paths.\n * Used by inner.setHdPath which expects one of these specific paths.\n */\ntype AllowedHdPath = (typeof ALLOWED_HD_PATHS)[number];\n\n/**\n * Regex pattern for validating and parsing Trezor derivation paths.\n * Matches BIP-44 style paths: m/44'/{coin}'/{segments}/{index}\n * where coin is 60' (Ethereum) or 1' (testnet).\n * Captures: [1] = base path prefix, [2] = index\n * The prefix is then validated against ALLOWED_HD_PATHS.\n */\nconst DERIVATION_PATH_PATTERN = /^(m\\/44'\\/(?:60'|1')(?:\\/\\d+'?)*)\\/(\\d+)$/u;\n\n/**\n * Concrete {@link KeyringV2} adapter for {@link TrezorKeyring}.\n *\n * This wrapper exposes the accounts and signing capabilities of the legacy\n * Trezor keyring via the unified V2 interface.\n *\n * All Trezor keyring accounts are BIP-44 derived from the device.\n */\nexport type TrezorKeyringV2Options = {\n legacyKeyring: TrezorKeyring;\n entropySource: EntropySourceId;\n type?: KeyringType.Trezor | KeyringType.OneKey;\n};\n\n// TrezorKeyring.signTransaction returns `TypedTransaction | OldEthJsTransaction` for\n// backwards compatibility with old ethereumjs-tx, but EthKeyring expects `TypedTxData`.\n// The runtime behavior is correct - we cast the type to satisfy the constraint.\ntype TrezorKeyringAsEthKeyring = TrezorKeyring & EthKeyring;\n\nexport class TrezorKeyringV2\n extends EthKeyringWrapper<\n TrezorKeyringAsEthKeyring,\n Bip44Account<KeyringAccount>\n >\n implements KeyringV2\n{\n readonly entropySource: EntropySourceId;\n\n constructor(options: TrezorKeyringV2Options) {\n super({\n type: options.type ?? KeyringType.Trezor,\n inner: options.legacyKeyring as TrezorKeyringAsEthKeyring,\n capabilities: trezorKeyringV2Capabilities,\n });\n this.entropySource = options.entropySource;\n }\n\n /**\n * Hydrate the underlying keyring from a previously serialized state.\n *\n * Overrides the base class implementation to avoid calling `getAccounts()`\n * when the Trezor device is locked. The base class calls `getAccounts()` to\n * rebuild the registry, but for Trezor keyrings this requires the HDKey to\n * be initialized (via `unlock()`). Since the device may not be connected\n * during deserialization, we skip the registry rebuild here. The registry\n * will be populated on the first call to `getAccounts()` after the device\n * is unlocked.\n *\n * @param state - The serialized keyring state.\n */\n async deserialize(state: Json): Promise<void> {\n await this.withLock(async () => {\n // Clear the registry when deserializing\n this.registry.clear();\n\n // Deserialize the legacy keyring state only.\n // We intentionally skip calling getAccounts() here because the Trezor\n // device may be locked (HDKey not initialized). The TrezorKeyring's\n // deserialize restores the accounts array, but not the paths map, so\n // getIndexForAddress would need to derive addresses which requires an\n // initialized HDKey. The registry will be populated lazily when\n // getAccounts() is called after the device is unlocked.\n await this.inner.deserialize(state);\n });\n }\n\n /**\n * Parses a derivation path to extract the base HD path and account index.\n *\n * Supports the allowed Trezor paths:\n * - m/44'/60'/0'/0/{index} (BIP44 standard)\n * - m/44'/60'/0'/{index} (legacy MEW)\n * - m/44'/1'/0'/0/{index} (SLIP0044 testnet)\n *\n * @param derivationPath - The full derivation path (e.g., m/44'/60'/0'/0/5).\n * @returns The base HD path and account index.\n * @throws If the path format is invalid or not an allowed Trezor path.\n */\n #parseDerivationPath(derivationPath: string): {\n basePath: AllowedHdPath;\n index: number;\n } {\n const match = derivationPath.match(DERIVATION_PATH_PATTERN);\n if (!match?.[1] || !match[2]) {\n throw new Error(\n `Invalid derivation path: ${derivationPath}. ` +\n `Expected format: {base}/{index} where base is one of: ` +\n `${ALLOWED_HD_PATHS.join(', ')}.`,\n );\n }\n\n const basePath = match[1];\n const index = parseInt(match[2], 10);\n\n // Validate the base path is one of the allowed paths\n if (!ALLOWED_HD_PATHS.includes(basePath as AllowedHdPath)) {\n throw new Error(\n `Invalid derivation path: ${derivationPath}. ` +\n `Expected format: {base}/{index} where base is one of: ` +\n `${ALLOWED_HD_PATHS.join(', ')}.`,\n );\n }\n\n return {\n basePath: basePath as AllowedHdPath,\n index,\n };\n }\n\n /**\n * Creates a Bip44Account object for the given address.\n *\n * @param address - The account address.\n * @param addressIndex - The account index in the derivation path.\n * @returns The created Bip44Account.\n */\n #createKeyringAccount(\n address: Hex,\n addressIndex: number,\n ): Bip44Account<KeyringAccount> {\n const id = this.registry.register(address);\n const derivationPath = `${this.inner.hdPath}/${addressIndex}`;\n\n const account: Bip44Account<KeyringAccount> = {\n id,\n type: EthAccountType.Eoa,\n address,\n scopes: [...this.capabilities.scopes],\n methods: [...TREZOR_KEYRING_METHODS],\n options: {\n entropy: {\n type: KeyringAccountEntropyTypeOption.Mnemonic,\n id: this.entropySource,\n groupIndex: addressIndex,\n derivationPath,\n },\n },\n };\n\n this.registry.set(account);\n return account;\n }\n\n async getAccounts(): Promise<Bip44Account<KeyringAccount>[]> {\n const addresses = await this.inner.getAccounts();\n\n if (addresses.length === 0) {\n return [];\n }\n\n // If the device is locked, we cannot derive addresses to find indices.\n // Return cached accounts if available, otherwise throw a clear error.\n if (!this.inner.isUnlocked()) {\n const cachedAccounts = addresses\n .map((address) => {\n const existingId = this.registry.getAccountId(address);\n return existingId ? this.registry.get(existingId) : undefined;\n })\n .filter(\n (account): account is Bip44Account<KeyringAccount> =>\n account !== undefined,\n );\n\n // If we have all accounts cached, return them\n if (cachedAccounts.length === addresses.length) {\n return cachedAccounts;\n }\n\n // Some accounts are not cached and device is locked\n throw new Error(\n 'Trezor device is locked. Please unlock the device to access accounts.',\n );\n }\n\n return addresses.map((address) => {\n // Check if we already have this account in the registry\n const existingId = this.registry.getAccountId(address);\n if (existingId) {\n const cached = this.registry.get(existingId);\n if (cached) {\n return cached;\n }\n }\n\n const addressIndex = this.inner.getIndexForAddress(address);\n return this.#createKeyringAccount(address, addressIndex);\n });\n }\n\n async createAccounts(\n options: CreateAccountOptions,\n ): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withLock(async () => {\n if (\n options.type === 'bip44:derive-path' ||\n options.type === 'bip44:derive-index'\n ) {\n // Validate that the entropy source matches this keyring's entropy source\n if (options.entropySource !== this.entropySource) {\n throw new Error(\n `Entropy source mismatch: expected '${this.entropySource}', got '${options.entropySource}'`,\n );\n }\n } else {\n throw new Error(\n `Unsupported account creation type for TrezorKeyring: ${String(\n options.type,\n )}`,\n );\n }\n\n // Check if an account at this index already exists with the same derivation path\n const currentAccounts = await this.getAccounts();\n\n let targetIndex: number;\n let basePath: AllowedHdPath;\n let derivationPath: string;\n\n if (options.type === 'bip44:derive-path') {\n // Parse the derivation path to extract base path and index\n const parsed = this.#parseDerivationPath(options.derivationPath);\n targetIndex = parsed.index;\n basePath = parsed.basePath;\n // Use the normalized path to avoid mismatches with leading zeros\n // (e.g., \"m/44'/60'/0'/0/007\" becomes \"m/44'/60'/0'/0/7\")\n derivationPath = `${basePath}/${targetIndex}`;\n } else {\n // derive-index uses BIP-44 standard path by default\n if (options.groupIndex < 0) {\n throw new Error(\n `Invalid groupIndex: ${options.groupIndex}. Must be a non-negative integer.`,\n );\n }\n targetIndex = options.groupIndex;\n basePath = BIP44_HD_PATH_PREFIX;\n derivationPath = `${basePath}/${targetIndex}`;\n }\n\n const existingAccount = currentAccounts.find((account) => {\n return (\n account.options.entropy.groupIndex === targetIndex &&\n account.options.entropy.derivationPath === derivationPath\n );\n });\n\n if (existingAccount) {\n return [existingAccount];\n }\n\n // Derive the account at the specified index.\n // If the HD path is changing, clear the registry to avoid stale accounts.\n // The TrezorKeyring operates on a single path at a time - accounts from\n // different paths cannot coexist in the inner keyring.\n if (basePath !== this.inner.hdPath) {\n this.registry.clear();\n }\n\n this.inner.setHdPath(basePath);\n this.inner.setAccountToUnlock(targetIndex);\n const [newAddress] = await this.inner.addAccounts(1);\n\n if (!newAddress) {\n throw new Error('Failed to create new account');\n }\n\n const newAccount = this.#createKeyringAccount(newAddress, targetIndex);\n\n return [newAccount];\n });\n }\n\n /**\n * Delete an account from the keyring.\n *\n * @param accountId - The account ID to delete.\n */\n async deleteAccount(accountId: AccountId): Promise<void> {\n await this.withLock(async () => {\n const { address } = await this.getAccount(accountId);\n const hexAddress = this.toHexAddress(address);\n\n // Remove from the legacy keyring\n this.inner.removeAccount(hexAddress);\n\n // Remove from the registry\n this.registry.delete(accountId);\n });\n }\n}\n"]}
1
+ {"version":3,"file":"trezor-keyring-v2.cjs","sourceRoot":"","sources":["../src/trezor-keyring-v2.ts"],"names":[],"mappings":";;;;;;;;;AACA,uDAW+B;AAC/B,uDAA0D;AAM1D;;;GAGG;AACH,MAAM,sBAAsB,GAAG;IAC7B,uBAAS,CAAC,eAAe;IACzB,uBAAS,CAAC,YAAY;IACtB,uBAAS,CAAC,eAAe;IACzB,uBAAS,CAAC,eAAe;CAC1B,CAAC;AAEF,MAAM,2BAA2B,GAAwB;IACvD,MAAM,EAAE,CAAC,sBAAQ,CAAC,GAAG,CAAC;IACtB,KAAK,EAAE;QACL,WAAW,EAAE,IAAI;QACjB,UAAU,EAAE,IAAI;KACjB;CACF,CAAC;AAEF;;;GAGG;AACU,QAAA,oBAAoB,GAAG,gBAAgB,CAAC;AAErD;;GAEG;AACU,QAAA,4BAA4B,GAAG,eAAe,CAAC;AAE5D;;GAEG;AACU,QAAA,sBAAsB,GAAG,cAAc,CAAC;AAErD;;;GAGG;AACH,MAAM,gBAAgB,GAAG;IAC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AE,CAAC,GAAG,sBAAsB,CAAC;QACpC,OAAO,EAAE;YACP,OAAO,EAAE;gBACP,IAAI,EAAE,6CAA+B,CAAC,QAAQ;gBAC9C,EAAE,EAAE,IAAI,CAAC,aAAa;gBACtB,UAAU,EAAE,YAAY;gBACxB,cAAc;aACf;SACF;KACF,CAAC;IAEF,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC3B,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\nimport {\n type CreateAccountOptions,\n EthAccountType,\n EthMethod,\n EthScope,\n type KeyringAccount,\n KeyringAccountEntropyTypeOption,\n type KeyringCapabilities,\n type KeyringV2,\n KeyringType,\n type EntropySourceId,\n} from '@metamask/keyring-api';\nimport { EthKeyringWrapper } from '@metamask/keyring-sdk';\nimport type { AccountId, EthKeyring } from '@metamask/keyring-utils';\nimport type { Hex, Json } from '@metamask/utils';\n\nimport type { TrezorKeyring } from './trezor-keyring';\n\n/**\n * Methods supported by Trezor keyring EOA accounts.\n * Trezor keyrings support a subset of signing methods (no encryption, app keys, or EIP-7702).\n */\nconst TREZOR_KEYRING_METHODS = [\n EthMethod.SignTransaction,\n EthMethod.PersonalSign,\n EthMethod.SignTypedDataV3,\n EthMethod.SignTypedDataV4,\n];\n\nconst trezorKeyringV2Capabilities: KeyringCapabilities = {\n scopes: [EthScope.Eoa],\n bip44: {\n deriveIndex: true,\n derivePath: true,\n },\n};\n\n/**\n * BIP-44 standard HD path prefix constant for Ethereum.\n * Used as default for derive-index operations.\n */\nexport const BIP44_HD_PATH_PREFIX = `m/44'/60'/0'/0`;\n\n/**\n * SLIP-0044 testnet HD path prefix constant.\n */\nexport const SLIP0044_TESTNET_PATH_PREFIX = `m/44'/1'/0'/0`;\n\n/**\n * Legacy MEW (MyEtherWallet) HD path prefix constant.\n */\nexport const LEGACY_MEW_PATH_PREFIX = `m/44'/60'/0'`;\n\n/**\n * Allowed HD paths for Trezor keyring.\n * These must match the keys in ALLOWED_HD_PATHS from trezor-keyring.ts.\n */\nconst ALLOWED_HD_PATHS = [\n BIP44_HD_PATH_PREFIX,\n SLIP0044_TESTNET_PATH_PREFIX,\n LEGACY_MEW_PATH_PREFIX,\n] as const;\n\n/**\n * Type representing one of the allowed Trezor HD paths.\n * Used by inner.setHdPath which expects one of these specific paths.\n */\ntype AllowedHdPath = (typeof ALLOWED_HD_PATHS)[number];\n\n/**\n * Regex pattern for validating and parsing Trezor derivation paths.\n * Matches BIP-44 style paths: m/44'/{coin}'/{segments}/{index}\n * where coin is 60' (Ethereum) or 1' (testnet).\n * Captures: [1] = base path prefix, [2] = index\n * The prefix is then validated against ALLOWED_HD_PATHS.\n */\nconst DERIVATION_PATH_PATTERN = /^(m\\/44'\\/(?:60'|1')(?:\\/\\d+'?)*)\\/(\\d+)$/u;\n\n/**\n * Concrete {@link KeyringV2} adapter for {@link TrezorKeyring}.\n *\n * This wrapper exposes the accounts and signing capabilities of the legacy\n * Trezor keyring via the unified V2 interface.\n *\n * All Trezor keyring accounts are BIP-44 derived from the device.\n */\nexport type TrezorKeyringV2Options = {\n legacyKeyring: TrezorKeyring;\n entropySource: EntropySourceId;\n type?: KeyringType.Trezor | KeyringType.OneKey;\n};\n\n// TrezorKeyring.signTransaction returns `TypedTransaction | OldEthJsTransaction` for\n// backwards compatibility with old ethereumjs-tx, but EthKeyring expects `TypedTxData`.\n// The runtime behavior is correct - we cast the type to satisfy the constraint.\ntype TrezorKeyringAsEthKeyring = TrezorKeyring & EthKeyring;\n\nexport class TrezorKeyringV2\n extends EthKeyringWrapper<\n TrezorKeyringAsEthKeyring,\n Bip44Account<KeyringAccount>\n >\n implements KeyringV2\n{\n readonly entropySource: EntropySourceId;\n\n constructor(options: TrezorKeyringV2Options) {\n super({\n type: options.type ?? KeyringType.Trezor,\n inner: options.legacyKeyring as TrezorKeyringAsEthKeyring,\n capabilities: trezorKeyringV2Capabilities,\n });\n this.entropySource = options.entropySource;\n }\n\n /**\n * Hydrate the underlying keyring from a previously serialized state.\n *\n * Overrides the base class implementation to avoid calling `getAccounts()`\n * when the Trezor device is locked. The base class calls `getAccounts()` to\n * rebuild the registry, but for Trezor keyrings this requires the HDKey to\n * be initialized (via `unlock()`). Since the device may not be connected\n * during deserialization, we skip the registry rebuild here. The registry\n * will be populated on the first call to `getAccounts()` after the device\n * is unlocked.\n *\n * @param state - The serialized keyring state.\n */\n async deserialize(state: Json): Promise<void> {\n await this.withLock(async () => {\n // Clear the registry when deserializing\n this.registry.clear();\n\n // Deserialize the legacy keyring state only.\n // We intentionally skip calling getAccounts() here because the Trezor\n // device may be locked (HDKey not initialized). The TrezorKeyring's\n // deserialize restores the accounts array, but not the paths map, so\n // getIndexForAddress would need to derive addresses which requires an\n // initialized HDKey. The registry will be populated lazily when\n // getAccounts() is called after the device is unlocked.\n await this.inner.deserialize(state);\n });\n }\n\n /**\n * Parses a derivation path to extract the base HD path and account index.\n *\n * Supports the allowed Trezor paths:\n * - m/44'/60'/0'/0/{index} (BIP44 standard)\n * - m/44'/60'/0'/{index} (legacy MEW)\n * - m/44'/1'/0'/0/{index} (SLIP0044 testnet)\n *\n * @param derivationPath - The full derivation path (e.g., m/44'/60'/0'/0/5).\n * @returns The base HD path and account index.\n * @throws If the path format is invalid or not an allowed Trezor path.\n */\n #parseDerivationPath(derivationPath: string): {\n basePath: AllowedHdPath;\n index: number;\n } {\n const match = derivationPath.match(DERIVATION_PATH_PATTERN);\n if (!match?.[1] || !match[2]) {\n throw new Error(\n `Invalid derivation path: ${derivationPath}. ` +\n `Expected format: {base}/{index} where base is one of: ` +\n `${ALLOWED_HD_PATHS.join(', ')}.`,\n );\n }\n\n const basePath = match[1];\n const index = parseInt(match[2], 10);\n\n // Validate the base path is one of the allowed paths\n if (!ALLOWED_HD_PATHS.includes(basePath as AllowedHdPath)) {\n throw new Error(\n `Invalid derivation path: ${derivationPath}. ` +\n `Expected format: {base}/{index} where base is one of: ` +\n `${ALLOWED_HD_PATHS.join(', ')}.`,\n );\n }\n\n return {\n basePath: basePath as AllowedHdPath,\n index,\n };\n }\n\n /**\n * Creates a Bip44Account object for the given address.\n *\n * @param address - The account address.\n * @param addressIndex - The account index in the derivation path.\n * @returns The created Bip44Account.\n */\n #createKeyringAccount(\n address: Hex,\n addressIndex: number,\n ): Bip44Account<KeyringAccount> {\n const id = this.registry.register(address);\n const derivationPath = `${this.inner.hdPath}/${addressIndex}`;\n\n const account: Bip44Account<KeyringAccount> = {\n id,\n type: EthAccountType.Eoa,\n address,\n scopes: [...this.capabilities.scopes],\n methods: [...TREZOR_KEYRING_METHODS],\n options: {\n entropy: {\n type: KeyringAccountEntropyTypeOption.Mnemonic,\n id: this.entropySource,\n groupIndex: addressIndex,\n derivationPath,\n },\n },\n };\n\n this.registry.set(account);\n return account;\n }\n\n async getAccounts(): Promise<Bip44Account<KeyringAccount>[]> {\n const addresses = await this.inner.getAccounts();\n\n if (addresses.length === 0) {\n return [];\n }\n\n // If the device is locked, we cannot derive addresses to find indices.\n // Return cached accounts if available, otherwise throw a clear error.\n if (!this.inner.isUnlocked()) {\n const cachedAccounts = addresses\n .map((address) => {\n const existingId = this.registry.getAccountId(address);\n return existingId ? this.registry.get(existingId) : undefined;\n })\n .filter(\n (account): account is Bip44Account<KeyringAccount> =>\n account !== undefined,\n );\n\n // If we have all accounts cached, return them\n if (cachedAccounts.length === addresses.length) {\n return cachedAccounts;\n }\n\n // Some accounts are not cached and device is locked\n throw new Error(\n 'Trezor device is locked. Please unlock the device to access accounts.',\n );\n }\n\n return addresses.map((address) => {\n // Check if we already have this account in the registry\n const existingId = this.registry.getAccountId(address);\n if (existingId) {\n const cached = this.registry.get(existingId);\n if (cached) {\n return cached;\n }\n }\n\n const addressIndex = this.inner.getIndexForAddress(address);\n return this.#createKeyringAccount(address, addressIndex);\n });\n }\n\n async createAccounts(\n options: CreateAccountOptions,\n ): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withLock(async () => {\n if (\n options.type === 'bip44:derive-path' ||\n options.type === 'bip44:derive-index'\n ) {\n // Validate that the entropy source matches this keyring's entropy source\n if (options.entropySource !== this.entropySource) {\n throw new Error(\n `Entropy source mismatch: expected '${this.entropySource}', got '${options.entropySource}'`,\n );\n }\n } else {\n throw new Error(\n `Unsupported account creation type for TrezorKeyring: ${String(\n options.type,\n )}`,\n );\n }\n\n // Check if an account at this index already exists with the same derivation path\n const currentAccounts = await this.getAccounts();\n\n let targetIndex: number;\n let basePath: AllowedHdPath;\n let derivationPath: string;\n\n if (options.type === 'bip44:derive-path') {\n // Parse the derivation path to extract base path and index\n const parsed = this.#parseDerivationPath(options.derivationPath);\n targetIndex = parsed.index;\n basePath = parsed.basePath;\n // Use the normalized path to avoid mismatches with leading zeros\n // (e.g., \"m/44'/60'/0'/0/007\" becomes \"m/44'/60'/0'/0/7\")\n derivationPath = `${basePath}/${targetIndex}`;\n } else {\n // derive-index uses BIP-44 standard path by default\n if (options.groupIndex < 0) {\n throw new Error(\n `Invalid groupIndex: ${options.groupIndex}. Must be a non-negative integer.`,\n );\n }\n targetIndex = options.groupIndex;\n basePath = BIP44_HD_PATH_PREFIX;\n derivationPath = `${basePath}/${targetIndex}`;\n }\n\n const existingAccount = currentAccounts.find((account) => {\n return (\n account.options.entropy.groupIndex === targetIndex &&\n account.options.entropy.derivationPath === derivationPath\n );\n });\n\n if (existingAccount) {\n return [existingAccount];\n }\n\n // Derive the account at the specified index.\n // If the HD path is changing, clear the registry to avoid stale accounts.\n // The TrezorKeyring operates on a single path at a time - accounts from\n // different paths cannot coexist in the inner keyring.\n if (basePath !== this.inner.hdPath) {\n this.registry.clear();\n }\n\n this.inner.setHdPath(basePath);\n this.inner.setAccountToUnlock(targetIndex);\n const [newAddress] = await this.inner.addAccounts(1);\n\n if (!newAddress) {\n throw new Error('Failed to create new account');\n }\n\n const newAccount = this.#createKeyringAccount(newAddress, targetIndex);\n\n return [newAccount];\n });\n }\n\n /**\n * Delete an account from the keyring.\n *\n * @param accountId - The account ID to delete.\n */\n async deleteAccount(accountId: AccountId): Promise<void> {\n await this.withLock(async () => {\n const { address } = await this.getAccount(accountId);\n const hexAddress = this.toHexAddress(address);\n\n // Remove from the legacy keyring\n this.inner.removeAccount(hexAddress);\n\n // Remove from the registry\n this.registry.delete(accountId);\n });\n }\n}\n"]}
@@ -1,5 +1,6 @@
1
1
  import type { Bip44Account } from "@metamask/account-api";
2
- import { type CreateAccountOptions, EthKeyringWrapper, type KeyringAccount, type KeyringV2, KeyringType, type EntropySourceId } from "@metamask/keyring-api";
2
+ import { type CreateAccountOptions, type KeyringAccount, type KeyringV2, KeyringType, type EntropySourceId } from "@metamask/keyring-api";
3
+ import { EthKeyringWrapper } from "@metamask/keyring-sdk";
3
4
  import type { AccountId, EthKeyring } from "@metamask/keyring-utils";
4
5
  import type { Json } from "@metamask/utils";
5
6
  import type { TrezorKeyring } from "./trezor-keyring.cjs";
@@ -1 +1 @@
1
- {"version":3,"file":"trezor-keyring-v2.d.cts","sourceRoot":"","sources":["../src/trezor-keyring-v2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,EACL,KAAK,oBAAoB,EAEzB,iBAAiB,EAGjB,KAAK,cAAc,EAGnB,KAAK,SAAS,EACd,WAAW,EACX,KAAK,eAAe,EACrB,8BAA8B;AAC/B,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,gCAAgC;AACrE,OAAO,KAAK,EAAO,IAAI,EAAE,wBAAwB;AAEjD,OAAO,KAAK,EAAE,aAAa,EAAE,6BAAyB;AAqBtD;;;GAGG;AACH,eAAO,MAAM,oBAAoB,mBAAmB,CAAC;AAErD;;GAEG;AACH,eAAO,MAAM,4BAA4B,kBAAkB,CAAC;AAE5D;;GAEG;AACH,eAAO,MAAM,sBAAsB,iBAAiB,CAAC;AA2BrD;;;;;;;GAOG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,aAAa,EAAE,aAAa,CAAC;IAC7B,a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;GAEG;AACH,eAAO,MAAM,sBAAsB,iBAAiB,CAAC;AA2BrD;;;;;;;GAOG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,aAAa,EAAE,aAAa,CAAC;IAC7B,aAAa,EAAE,eAAe,CAAC;IAC/B,IAAI,CAAC,EAAE,WAAW,CAAC,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC;CAChD,CAAC;AAKF,KAAK,yBAAyB,GAAG,aAAa,GAAG,UAAU,CAAC;AAE5D,qBAAa,eACX,SAAQ,iBAAiB,CACvB,yBAAyB,EACzB,YAAY,CAAC,cAAc,CAAC,CAE9B,YAAW,SAAS;;IAEpB,QAAQ,CAAC,aAAa,EAAE,eAAe,CAAC;gBAE5B,OAAO,EAAE,sBAAsB;IAS3C;;;;;;;;;;;;OAYG;IACG,WAAW,CAAC,KAAK,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IA6FvC,WAAW,IAAI,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IA8CtD,cAAc,CAClB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAgF1C;;;;OAIG;IACG,aAAa,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;CAYzD"}
@@ -1,5 +1,6 @@
1
1
  import type { Bip44Account } from "@metamask/account-api";
2
- import { type CreateAccountOptions, EthKeyringWrapper, type KeyringAccount, type KeyringV2, KeyringType, type EntropySourceId } from "@metamask/keyring-api";
2
+ import { type CreateAccountOptions, type KeyringAccount, type KeyringV2, KeyringType, type EntropySourceId } from "@metamask/keyring-api";
3
+ import { EthKeyringWrapper } from "@metamask/keyring-sdk";
3
4
  import type { AccountId, EthKeyring } from "@metamask/keyring-utils";
4
5
  import type { Json } from "@metamask/utils";
5
6
  import type { TrezorKeyring } from "./trezor-keyring.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"trezor-keyring-v2.d.mts","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,cAAc,CAAC,EAAE,CAAC;IA8CtD,cAAc,CAClB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAgF1C;;;;OAIG;IACG,aAAa,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;CAYzD"}
@@ -4,7 +4,8 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
4
4
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
5
  };
6
6
  var _TrezorKeyringV2_instances, _TrezorKeyringV2_parseDerivationPath, _TrezorKeyringV2_createKeyringAccount;
7
- import { EthAccountType, EthKeyringWrapper, EthMethod, EthScope, KeyringAccountEntropyTypeOption, KeyringType } from "@metamask/keyring-api";
7
+ import { EthAccountType, EthMethod, EthScope, KeyringAccountEntropyTypeOption, KeyringType } from "@metamask/keyring-api";
8
+ import { EthKeyringWrapper } from "@metamask/keyring-sdk";
8
9
  /**
9
10
  * Methods supported by Trezor keyring EOA accounts.
10
11
  * Trezor keyrings support a subset of signing methods (no encryption, app keys, or EIP-7702).