@metamask/snaps-rpc-methods 11.13.1 → 12.1.0

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 (88) hide show
  1. package/CHANGELOG.md +17 -1
  2. package/dist/endowments/index.cjs +1 -0
  3. package/dist/endowments/index.cjs.map +1 -1
  4. package/dist/endowments/index.d.cts.map +1 -1
  5. package/dist/endowments/index.d.mts.map +1 -1
  6. package/dist/endowments/index.mjs +1 -0
  7. package/dist/endowments/index.mjs.map +1 -1
  8. package/dist/permitted/createInterface.d.cts +44 -44
  9. package/dist/permitted/createInterface.d.mts +44 -44
  10. package/dist/permitted/experimentalProviderRequest.d.cts +3 -3
  11. package/dist/permitted/experimentalProviderRequest.d.mts +3 -3
  12. package/dist/permitted/getFile.d.cts +2 -2
  13. package/dist/permitted/getFile.d.mts +2 -2
  14. package/dist/permitted/getState.d.cts +1 -1
  15. package/dist/permitted/getState.d.mts +1 -1
  16. package/dist/permitted/handlers.d.cts +16 -0
  17. package/dist/permitted/handlers.d.cts.map +1 -1
  18. package/dist/permitted/handlers.d.mts +16 -0
  19. package/dist/permitted/handlers.d.mts.map +1 -1
  20. package/dist/permitted/scheduleBackgroundEvent.cjs +2 -19
  21. package/dist/permitted/scheduleBackgroundEvent.cjs.map +1 -1
  22. package/dist/permitted/scheduleBackgroundEvent.d.cts +4 -4
  23. package/dist/permitted/scheduleBackgroundEvent.d.cts.map +1 -1
  24. package/dist/permitted/scheduleBackgroundEvent.d.mts +4 -4
  25. package/dist/permitted/scheduleBackgroundEvent.d.mts.map +1 -1
  26. package/dist/permitted/scheduleBackgroundEvent.mjs +4 -21
  27. package/dist/permitted/scheduleBackgroundEvent.mjs.map +1 -1
  28. package/dist/permitted/setState.d.cts +1 -1
  29. package/dist/permitted/setState.d.mts +1 -1
  30. package/dist/permitted/updateInterface.d.cts +44 -44
  31. package/dist/permitted/updateInterface.d.mts +44 -44
  32. package/dist/restricted/dialog.d.cts +91 -147
  33. package/dist/restricted/dialog.d.cts.map +1 -1
  34. package/dist/restricted/dialog.d.mts +91 -147
  35. package/dist/restricted/dialog.d.mts.map +1 -1
  36. package/dist/restricted/getBip32Entropy.cjs +16 -3
  37. package/dist/restricted/getBip32Entropy.cjs.map +1 -1
  38. package/dist/restricted/getBip32Entropy.d.cts +11 -1
  39. package/dist/restricted/getBip32Entropy.d.cts.map +1 -1
  40. package/dist/restricted/getBip32Entropy.d.mts +11 -1
  41. package/dist/restricted/getBip32Entropy.d.mts.map +1 -1
  42. package/dist/restricted/getBip32Entropy.mjs +17 -4
  43. package/dist/restricted/getBip32Entropy.mjs.map +1 -1
  44. package/dist/restricted/getBip32PublicKey.cjs +19 -3
  45. package/dist/restricted/getBip32PublicKey.cjs.map +1 -1
  46. package/dist/restricted/getBip32PublicKey.d.cts +13 -3
  47. package/dist/restricted/getBip32PublicKey.d.cts.map +1 -1
  48. package/dist/restricted/getBip32PublicKey.d.mts +13 -3
  49. package/dist/restricted/getBip32PublicKey.d.mts.map +1 -1
  50. package/dist/restricted/getBip32PublicKey.mjs +20 -4
  51. package/dist/restricted/getBip32PublicKey.mjs.map +1 -1
  52. package/dist/restricted/getBip44Entropy.cjs +8 -5
  53. package/dist/restricted/getBip44Entropy.cjs.map +1 -1
  54. package/dist/restricted/getBip44Entropy.d.cts +6 -6
  55. package/dist/restricted/getBip44Entropy.d.cts.map +1 -1
  56. package/dist/restricted/getBip44Entropy.d.mts +6 -6
  57. package/dist/restricted/getBip44Entropy.d.mts.map +1 -1
  58. package/dist/restricted/getBip44Entropy.mjs +9 -6
  59. package/dist/restricted/getBip44Entropy.mjs.map +1 -1
  60. package/dist/restricted/getEntropy.cjs +7 -7
  61. package/dist/restricted/getEntropy.cjs.map +1 -1
  62. package/dist/restricted/getEntropy.d.cts +8 -8
  63. package/dist/restricted/getEntropy.d.cts.map +1 -1
  64. package/dist/restricted/getEntropy.d.mts +8 -8
  65. package/dist/restricted/getEntropy.d.mts.map +1 -1
  66. package/dist/restricted/getEntropy.mjs +8 -8
  67. package/dist/restricted/getEntropy.mjs.map +1 -1
  68. package/dist/restricted/manageAccounts.d.cts +1 -1
  69. package/dist/restricted/manageAccounts.d.mts +1 -1
  70. package/dist/restricted/manageState.cjs +4 -4
  71. package/dist/restricted/manageState.cjs.map +1 -1
  72. package/dist/restricted/manageState.d.cts +3 -3
  73. package/dist/restricted/manageState.d.cts.map +1 -1
  74. package/dist/restricted/manageState.d.mts +3 -3
  75. package/dist/restricted/manageState.d.mts.map +1 -1
  76. package/dist/restricted/manageState.mjs +5 -5
  77. package/dist/restricted/manageState.mjs.map +1 -1
  78. package/dist/restricted/notify.d.cts +74 -74
  79. package/dist/restricted/notify.d.mts +74 -74
  80. package/dist/utils.cjs +68 -23
  81. package/dist/utils.cjs.map +1 -1
  82. package/dist/utils.d.cts +40 -16
  83. package/dist/utils.d.cts.map +1 -1
  84. package/dist/utils.d.mts +40 -16
  85. package/dist/utils.d.mts.map +1 -1
  86. package/dist/utils.mjs +63 -19
  87. package/dist/utils.mjs.map +1 -1
  88. package/package.json +7 -7
package/dist/utils.d.cts CHANGED
@@ -1,7 +1,6 @@
1
1
  import type { SupportedCurve, CryptographicFunctions } from "@metamask/key-tree";
2
2
  import { SLIP10Node } from "@metamask/key-tree";
3
3
  import type { MagicValue } from "@metamask/snaps-utils";
4
- import type { Hex } from "@metamask/utils";
5
4
  export declare const FORBIDDEN_KEYS: string[];
6
5
  /**
7
6
  * Maps an interface with method hooks to an object, using the keys of the
@@ -24,7 +23,7 @@ export type MethodHooksObject<HooksType extends Record<string, unknown>> = {
24
23
  * @template HookName - The names of the hooks to select.
25
24
  */
26
25
  export declare function selectHooks<Hooks extends Record<string, unknown>, HookName extends keyof Hooks>(hooks: Hooks, hookNames?: Record<HookName, boolean>): Pick<Hooks, HookName> | undefined;
27
- type DeriveEntropyOptions = {
26
+ type BaseDeriveEntropyOptions = {
28
27
  /**
29
28
  * The input value to derive entropy from.
30
29
  */
@@ -33,10 +32,6 @@ type DeriveEntropyOptions = {
33
32
  * An optional salt to use when deriving entropy.
34
33
  */
35
34
  salt?: string;
36
- /**
37
- * The mnemonic phrase to use for entropy derivation.
38
- */
39
- mnemonicPhrase: Uint8Array;
40
35
  /**
41
36
  * A hardened BIP-32 index, which is used to derive the root key from the
42
37
  * mnemonic phrase.
@@ -47,8 +42,14 @@ type DeriveEntropyOptions = {
47
42
  */
48
43
  cryptographicFunctions: CryptographicFunctions | undefined;
49
44
  };
45
+ type SeedDeriveEntropyOptions = BaseDeriveEntropyOptions & {
46
+ /**
47
+ * The mnemonic seed to use for entropy derivation.
48
+ */
49
+ seed: Uint8Array;
50
+ };
50
51
  /**
51
- * Derive entropy from the given mnemonic phrase and salt.
52
+ * Derive entropy from the given mnemonic seed and salt.
52
53
  *
53
54
  * This is based on the reference implementation of
54
55
  * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).
@@ -56,7 +57,7 @@ type DeriveEntropyOptions = {
56
57
  * @param options - The options for entropy derivation.
57
58
  * @param options.input - The input value to derive entropy from.
58
59
  * @param options.salt - An optional salt to use when deriving entropy.
59
- * @param options.mnemonicPhrase - The mnemonic phrase to use for entropy
60
+ * @param options.seed - The mnemonic seed to use for entropy
60
61
  * derivation.
61
62
  * @param options.magic - A hardened BIP-32 index, which is used to derive the
62
63
  * root key from the mnemonic phrase.
@@ -64,7 +65,7 @@ type DeriveEntropyOptions = {
64
65
  * for the derivation.
65
66
  * @returns The derived entropy.
66
67
  */
67
- export declare function deriveEntropy({ input, salt, mnemonicPhrase, magic, cryptographicFunctions, }: DeriveEntropyOptions): Promise<Hex>;
68
+ export declare function deriveEntropyFromSeed({ input, salt, seed, magic, cryptographicFunctions, }: SeedDeriveEntropyOptions): Promise<`0x${string}`>;
68
69
  /**
69
70
  * Get the path prefix to use for key derivation in `key-tree`. This assumes the
70
71
  * following:
@@ -81,12 +82,17 @@ export declare function deriveEntropy({ input, salt, mnemonicPhrase, magic, cryp
81
82
  * @returns The path prefix, i.e., `bip32` or `slip10`.
82
83
  */
83
84
  export declare function getPathPrefix(curve: SupportedCurve): 'bip32' | 'slip10' | 'cip3';
84
- type GetNodeArgs = {
85
+ type BaseGetNodeArgs = {
85
86
  curve: SupportedCurve;
86
- secretRecoveryPhrase: Uint8Array;
87
87
  path: string[];
88
88
  cryptographicFunctions: CryptographicFunctions | undefined;
89
89
  };
90
+ type GetNodeArgsMnemonic = BaseGetNodeArgs & {
91
+ secretRecoveryPhrase: Uint8Array;
92
+ };
93
+ type GetNodeArgsSeed = BaseGetNodeArgs & {
94
+ seed: Uint8Array;
95
+ };
90
96
  /**
91
97
  * Get a `key-tree`-compatible node.
92
98
  *
@@ -103,7 +109,24 @@ type GetNodeArgs = {
103
109
  * for the node.
104
110
  * @returns The `key-tree` SLIP-10 node.
105
111
  */
106
- export declare function getNode({ curve, secretRecoveryPhrase, path, cryptographicFunctions, }: GetNodeArgs): Promise<SLIP10Node>;
112
+ export declare function getNodeFromMnemonic({ curve, secretRecoveryPhrase, path, cryptographicFunctions, }: GetNodeArgsMnemonic): Promise<SLIP10Node>;
113
+ /**
114
+ * Get a `key-tree`-compatible node.
115
+ *
116
+ * Note: This function assumes that all the parameters have been validated
117
+ * beforehand.
118
+ *
119
+ * @param options - The derivation options.
120
+ * @param options.curve - The curve to use for derivation.
121
+ * @param options.seed - The BIP-39 to use for
122
+ * derivation.
123
+ * @param options.path - The derivation path to use as array, starting with an
124
+ * "m" as the first item.
125
+ * @param options.cryptographicFunctions - The cryptographic functions to use
126
+ * for the node.
127
+ * @returns The `key-tree` SLIP-10 node.
128
+ */
129
+ export declare function getNodeFromSeed({ curve, seed, path, cryptographicFunctions, }: GetNodeArgsSeed): Promise<SLIP10Node>;
107
130
  /**
108
131
  * Validate the key of a state object.
109
132
  *
@@ -113,13 +136,14 @@ export declare function getNode({ curve, secretRecoveryPhrase, path, cryptograph
113
136
  export declare function isValidStateKey(key: string | undefined): boolean;
114
137
  export declare const StateKeyStruct: import("@metamask/superstruct").Struct<string, null>;
115
138
  /**
116
- * Get the secret recovery phrase of the user. This calls the `getMnemonic` hook
117
- * and handles any errors that occur, throwing formatted JSON-RPC errors.
139
+ * Get a value using the entropy source hooks: getMnemonic or getMnemonicSeed.
140
+ * This function calls the passed hook and handles any errors that occur,
141
+ * throwing formatted JSON-RPC errors.
118
142
  *
119
- * @param getMnemonic - The `getMnemonic` hook.
143
+ * @param hook - The hook.
120
144
  * @param source - The entropy source to use.
121
145
  * @returns The secret recovery phrase.
122
146
  */
123
- export declare function getSecretRecoveryPhrase(getMnemonic: (source?: string | undefined) => Promise<Uint8Array>, source?: string | undefined): Promise<Uint8Array>;
147
+ export declare function getValueFromEntropySource(hook: (source?: string | undefined) => Promise<Uint8Array>, source?: string | undefined): Promise<Uint8Array>;
124
148
  export {};
125
149
  //# sourceMappingURL=utils.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,cAAc,EACd,sBAAsB,EACvB,2BAA2B;AAC5B,OAAO,EAAE,UAAU,EAAE,2BAA2B;AAEhD,OAAO,KAAK,EAAE,UAAU,EAAE,8BAA8B;AAExD,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAa3C,eAAO,MAAM,cAAc,UAA4C,CAAC;AAExE;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,CAAC,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI;KACxE,GAAG,IAAI,MAAM,SAAS,GAAG,IAAI;CAC/B,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CACzB,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACrC,QAAQ,SAAS,MAAM,KAAK,EAE5B,KAAK,EAAE,KAAK,EACZ,SAAS,CAAC,EAAE,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC,GACpC,IAAI,CAAC,KAAK,EAAE,QAAQ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AE,cAAc,GACpB,OAAO,GAAG,QAAQ,GAAG,MAAM,CAW7B;AAED,KAAK,eAAe,GAAG;IACrB,KAAK,EAAE,cAAc,CAAC;IACtB,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,sBAAsB,EAAE,sBAAsB,GAAG,SAAS,CAAC;CAC5D,CAAC;AAEF,KAAK,mBAAmB,GAAG,eAAe,GAAG;IAC3C,oBAAoB,EAAE,UAAU,CAAC;CAClC,CAAC;AAEF,KAAK,eAAe,GAAG,eAAe,GAAG;IACvC,IAAI,EAAE,UAAU,CAAC;CAClB,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,mBAAmB,CAAC,EACxC,KAAK,EACL,oBAAoB,EACpB,IAAI,EACJ,sBAAsB,GACvB,EAAE,mBAAmB,uBAerB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,eAAe,CAAC,EACpC,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,sBAAsB,GACvB,EAAE,eAAe,uBAejB;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,WAMtD;AAED,eAAO,MAAM,cAAc,sDAMzB,CAAC;AAEH;;;;;;;;GAQG;AACH,wBAAsB,yBAAyB,CAC7C,IAAI,EAAE,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,KAAK,OAAO,CAAC,UAAU,CAAC,EAC1D,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,GAC1B,OAAO,CAAC,UAAU,CAAC,CAiBrB"}
package/dist/utils.d.mts CHANGED
@@ -1,7 +1,6 @@
1
1
  import type { SupportedCurve, CryptographicFunctions } from "@metamask/key-tree";
2
2
  import { SLIP10Node } from "@metamask/key-tree";
3
3
  import type { MagicValue } from "@metamask/snaps-utils";
4
- import type { Hex } from "@metamask/utils";
5
4
  export declare const FORBIDDEN_KEYS: string[];
6
5
  /**
7
6
  * Maps an interface with method hooks to an object, using the keys of the
@@ -24,7 +23,7 @@ export type MethodHooksObject<HooksType extends Record<string, unknown>> = {
24
23
  * @template HookName - The names of the hooks to select.
25
24
  */
26
25
  export declare function selectHooks<Hooks extends Record<string, unknown>, HookName extends keyof Hooks>(hooks: Hooks, hookNames?: Record<HookName, boolean>): Pick<Hooks, HookName> | undefined;
27
- type DeriveEntropyOptions = {
26
+ type BaseDeriveEntropyOptions = {
28
27
  /**
29
28
  * The input value to derive entropy from.
30
29
  */
@@ -33,10 +32,6 @@ type DeriveEntropyOptions = {
33
32
  * An optional salt to use when deriving entropy.
34
33
  */
35
34
  salt?: string;
36
- /**
37
- * The mnemonic phrase to use for entropy derivation.
38
- */
39
- mnemonicPhrase: Uint8Array;
40
35
  /**
41
36
  * A hardened BIP-32 index, which is used to derive the root key from the
42
37
  * mnemonic phrase.
@@ -47,8 +42,14 @@ type DeriveEntropyOptions = {
47
42
  */
48
43
  cryptographicFunctions: CryptographicFunctions | undefined;
49
44
  };
45
+ type SeedDeriveEntropyOptions = BaseDeriveEntropyOptions & {
46
+ /**
47
+ * The mnemonic seed to use for entropy derivation.
48
+ */
49
+ seed: Uint8Array;
50
+ };
50
51
  /**
51
- * Derive entropy from the given mnemonic phrase and salt.
52
+ * Derive entropy from the given mnemonic seed and salt.
52
53
  *
53
54
  * This is based on the reference implementation of
54
55
  * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).
@@ -56,7 +57,7 @@ type DeriveEntropyOptions = {
56
57
  * @param options - The options for entropy derivation.
57
58
  * @param options.input - The input value to derive entropy from.
58
59
  * @param options.salt - An optional salt to use when deriving entropy.
59
- * @param options.mnemonicPhrase - The mnemonic phrase to use for entropy
60
+ * @param options.seed - The mnemonic seed to use for entropy
60
61
  * derivation.
61
62
  * @param options.magic - A hardened BIP-32 index, which is used to derive the
62
63
  * root key from the mnemonic phrase.
@@ -64,7 +65,7 @@ type DeriveEntropyOptions = {
64
65
  * for the derivation.
65
66
  * @returns The derived entropy.
66
67
  */
67
- export declare function deriveEntropy({ input, salt, mnemonicPhrase, magic, cryptographicFunctions, }: DeriveEntropyOptions): Promise<Hex>;
68
+ export declare function deriveEntropyFromSeed({ input, salt, seed, magic, cryptographicFunctions, }: SeedDeriveEntropyOptions): Promise<`0x${string}`>;
68
69
  /**
69
70
  * Get the path prefix to use for key derivation in `key-tree`. This assumes the
70
71
  * following:
@@ -81,12 +82,17 @@ export declare function deriveEntropy({ input, salt, mnemonicPhrase, magic, cryp
81
82
  * @returns The path prefix, i.e., `bip32` or `slip10`.
82
83
  */
83
84
  export declare function getPathPrefix(curve: SupportedCurve): 'bip32' | 'slip10' | 'cip3';
84
- type GetNodeArgs = {
85
+ type BaseGetNodeArgs = {
85
86
  curve: SupportedCurve;
86
- secretRecoveryPhrase: Uint8Array;
87
87
  path: string[];
88
88
  cryptographicFunctions: CryptographicFunctions | undefined;
89
89
  };
90
+ type GetNodeArgsMnemonic = BaseGetNodeArgs & {
91
+ secretRecoveryPhrase: Uint8Array;
92
+ };
93
+ type GetNodeArgsSeed = BaseGetNodeArgs & {
94
+ seed: Uint8Array;
95
+ };
90
96
  /**
91
97
  * Get a `key-tree`-compatible node.
92
98
  *
@@ -103,7 +109,24 @@ type GetNodeArgs = {
103
109
  * for the node.
104
110
  * @returns The `key-tree` SLIP-10 node.
105
111
  */
106
- export declare function getNode({ curve, secretRecoveryPhrase, path, cryptographicFunctions, }: GetNodeArgs): Promise<SLIP10Node>;
112
+ export declare function getNodeFromMnemonic({ curve, secretRecoveryPhrase, path, cryptographicFunctions, }: GetNodeArgsMnemonic): Promise<SLIP10Node>;
113
+ /**
114
+ * Get a `key-tree`-compatible node.
115
+ *
116
+ * Note: This function assumes that all the parameters have been validated
117
+ * beforehand.
118
+ *
119
+ * @param options - The derivation options.
120
+ * @param options.curve - The curve to use for derivation.
121
+ * @param options.seed - The BIP-39 to use for
122
+ * derivation.
123
+ * @param options.path - The derivation path to use as array, starting with an
124
+ * "m" as the first item.
125
+ * @param options.cryptographicFunctions - The cryptographic functions to use
126
+ * for the node.
127
+ * @returns The `key-tree` SLIP-10 node.
128
+ */
129
+ export declare function getNodeFromSeed({ curve, seed, path, cryptographicFunctions, }: GetNodeArgsSeed): Promise<SLIP10Node>;
107
130
  /**
108
131
  * Validate the key of a state object.
109
132
  *
@@ -113,13 +136,14 @@ export declare function getNode({ curve, secretRecoveryPhrase, path, cryptograph
113
136
  export declare function isValidStateKey(key: string | undefined): boolean;
114
137
  export declare const StateKeyStruct: import("@metamask/superstruct").Struct<string, null>;
115
138
  /**
116
- * Get the secret recovery phrase of the user. This calls the `getMnemonic` hook
117
- * and handles any errors that occur, throwing formatted JSON-RPC errors.
139
+ * Get a value using the entropy source hooks: getMnemonic or getMnemonicSeed.
140
+ * This function calls the passed hook and handles any errors that occur,
141
+ * throwing formatted JSON-RPC errors.
118
142
  *
119
- * @param getMnemonic - The `getMnemonic` hook.
143
+ * @param hook - The hook.
120
144
  * @param source - The entropy source to use.
121
145
  * @returns The secret recovery phrase.
122
146
  */
123
- export declare function getSecretRecoveryPhrase(getMnemonic: (source?: string | undefined) => Promise<Uint8Array>, source?: string | undefined): Promise<Uint8Array>;
147
+ export declare function getValueFromEntropySource(hook: (source?: string | undefined) => Promise<Uint8Array>, source?: string | undefined): Promise<Uint8Array>;
124
148
  export {};
125
149
  //# sourceMappingURL=utils.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,cAAc,EACd,sBAAsB,EACvB,2BAA2B;AAC5B,OAAO,EAAE,UAAU,EAAE,2BAA2B;AAEhD,OAAO,KAAK,EAAE,UAAU,EAAE,8BAA8B;AAExD,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAa3C,eAAO,MAAM,cAAc,UAA4C,CAAC;AAExE;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,CAAC,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI;KACxE,GAAG,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,UAAU,CAAC,CAiBrB"}
package/dist/utils.mjs CHANGED
@@ -49,7 +49,7 @@ function getDerivationPathArray(hash) {
49
49
  return array;
50
50
  }
51
51
  /**
52
- * Derive entropy from the given mnemonic phrase and salt.
52
+ * Get the derivation path to use for entropy derivation.
53
53
  *
54
54
  * This is based on the reference implementation of
55
55
  * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).
@@ -57,27 +57,44 @@ function getDerivationPathArray(hash) {
57
57
  * @param options - The options for entropy derivation.
58
58
  * @param options.input - The input value to derive entropy from.
59
59
  * @param options.salt - An optional salt to use when deriving entropy.
60
- * @param options.mnemonicPhrase - The mnemonic phrase to use for entropy
61
- * derivation.
62
60
  * @param options.magic - A hardened BIP-32 index, which is used to derive the
63
61
  * root key from the mnemonic phrase.
64
- * @param options.cryptographicFunctions - The cryptographic functions to use
65
- * for the derivation.
66
- * @returns The derived entropy.
62
+ * @returns The derivation path to be used for entropy key derivation.
67
63
  */
68
- export async function deriveEntropy({ input, salt = '', mnemonicPhrase, magic, cryptographicFunctions, }) {
64
+ function getEntropyDerivationPath({ input, salt, magic, }) {
69
65
  const inputBytes = stringToBytes(input);
70
66
  const saltBytes = stringToBytes(salt);
71
67
  // Get the derivation path from the snap ID.
72
68
  const hash = keccak256(concatBytes([inputBytes, keccak256(saltBytes)]));
73
69
  const computedDerivationPath = getDerivationPathArray(hash);
70
+ return [`bip32:${magic}`, ...computedDerivationPath];
71
+ }
72
+ /**
73
+ * Derive entropy from the given mnemonic seed and salt.
74
+ *
75
+ * This is based on the reference implementation of
76
+ * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).
77
+ *
78
+ * @param options - The options for entropy derivation.
79
+ * @param options.input - The input value to derive entropy from.
80
+ * @param options.salt - An optional salt to use when deriving entropy.
81
+ * @param options.seed - The mnemonic seed to use for entropy
82
+ * derivation.
83
+ * @param options.magic - A hardened BIP-32 index, which is used to derive the
84
+ * root key from the mnemonic phrase.
85
+ * @param options.cryptographicFunctions - The cryptographic functions to use
86
+ * for the derivation.
87
+ * @returns The derived entropy.
88
+ */
89
+ export async function deriveEntropyFromSeed({ input, salt = '', seed, magic, cryptographicFunctions, }) {
90
+ const computedDerivationPath = getEntropyDerivationPath({
91
+ input,
92
+ salt,
93
+ magic,
94
+ });
74
95
  // Derive the private key using BIP-32.
75
- const { privateKey } = await SLIP10Node.fromDerivationPath({
76
- derivationPath: [
77
- mnemonicPhrase,
78
- `bip32:${magic}`,
79
- ...computedDerivationPath,
80
- ],
96
+ const { privateKey } = await SLIP10Node.fromSeed({
97
+ derivationPath: [seed, ...computedDerivationPath],
81
98
  curve: 'secp256k1',
82
99
  }, cryptographicFunctions);
83
100
  // This should never happen, but this keeps TypeScript happy.
@@ -127,7 +144,7 @@ export function getPathPrefix(curve) {
127
144
  * for the node.
128
145
  * @returns The `key-tree` SLIP-10 node.
129
146
  */
130
- export async function getNode({ curve, secretRecoveryPhrase, path, cryptographicFunctions, }) {
147
+ export async function getNodeFromMnemonic({ curve, secretRecoveryPhrase, path, cryptographicFunctions, }) {
131
148
  const prefix = getPathPrefix(curve);
132
149
  return await SLIP10Node.fromDerivationPath({
133
150
  curve,
@@ -137,6 +154,32 @@ export async function getNode({ curve, secretRecoveryPhrase, path, cryptographic
137
154
  ],
138
155
  }, cryptographicFunctions);
139
156
  }
157
+ /**
158
+ * Get a `key-tree`-compatible node.
159
+ *
160
+ * Note: This function assumes that all the parameters have been validated
161
+ * beforehand.
162
+ *
163
+ * @param options - The derivation options.
164
+ * @param options.curve - The curve to use for derivation.
165
+ * @param options.seed - The BIP-39 to use for
166
+ * derivation.
167
+ * @param options.path - The derivation path to use as array, starting with an
168
+ * "m" as the first item.
169
+ * @param options.cryptographicFunctions - The cryptographic functions to use
170
+ * for the node.
171
+ * @returns The `key-tree` SLIP-10 node.
172
+ */
173
+ export async function getNodeFromSeed({ curve, seed, path, cryptographicFunctions, }) {
174
+ const prefix = getPathPrefix(curve);
175
+ return await SLIP10Node.fromSeed({
176
+ curve,
177
+ derivationPath: [
178
+ seed,
179
+ ...path.slice(1).map((index) => `${prefix}:${index}`),
180
+ ],
181
+ }, cryptographicFunctions);
182
+ }
140
183
  /**
141
184
  * Validate the key of a state object.
142
185
  *
@@ -156,16 +199,17 @@ export const StateKeyStruct = refine(string(), 'state key', (value) => {
156
199
  return true;
157
200
  });
158
201
  /**
159
- * Get the secret recovery phrase of the user. This calls the `getMnemonic` hook
160
- * and handles any errors that occur, throwing formatted JSON-RPC errors.
202
+ * Get a value using the entropy source hooks: getMnemonic or getMnemonicSeed.
203
+ * This function calls the passed hook and handles any errors that occur,
204
+ * throwing formatted JSON-RPC errors.
161
205
  *
162
- * @param getMnemonic - The `getMnemonic` hook.
206
+ * @param hook - The hook.
163
207
  * @param source - The entropy source to use.
164
208
  * @returns The secret recovery phrase.
165
209
  */
166
- export async function getSecretRecoveryPhrase(getMnemonic, source) {
210
+ export async function getValueFromEntropySource(hook, source) {
167
211
  try {
168
- return await getMnemonic(source);
212
+ return await hook(source);
169
213
  }
170
214
  catch (error) {
171
215
  if (error instanceof Error) {
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,UAAU,EAAE,2BAA2B;AAChD,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAEjD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,8BAA8B;AAEvD,OAAO,EACL,gBAAgB,EAChB,KAAK,EACL,MAAM,EACN,WAAW,EACX,cAAc,EACd,aAAa,EACd,wBAAwB;AACzB,OAAO,EAAE,UAAU,IAAI,SAAS,EAAE,2BAA2B;AAE7D,MAAM,cAAc,GAAG,UAAU,CAAC;AAElC,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,aAAa,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AAWxE;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CAIzB,KAAY,EACZ,SAAqC;IAErC,IAAI,SAAS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AAuB;IACrD,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACzD,CAAC;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,EAAE,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE;IACpE,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO,4DAA4D,CAAC;IACtE,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC,CAAC,CAAC;AAEH;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,WAAiE,EACjE,MAA2B;IAE3B,IAAI,CAAC;QACH,OAAO,MAAM,WAAW,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;YAC3B,MAAM,SAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,KAAK,CAAC,OAAO;aACvB,CAAC,CAAC;QACL,CAAC;QAED,MAAM,SAAS,CAAC,QAAQ,CAAC;YACvB,OAAO,EAAE,4BAA4B;YACrC,IAAI,EAAE;gBACJ,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE;aACxB;SACF,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import type {\n HardenedBIP32Node,\n BIP32Node,\n SLIP10PathNode,\n SupportedCurve,\n CryptographicFunctions,\n} from '@metamask/key-tree';\nimport { SLIP10Node } from '@metamask/key-tree';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { MagicValue } from '@metamask/snaps-utils';\nimport { refine, string } from '@metamask/superstruct';\nimport type { Hex } from '@metamask/utils';\nimport {\n assertExhaustive,\n add0x,\n assert,\n concatBytes,\n createDataView,\n stringToBytes,\n} from '@metamask/utils';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\n\nconst HARDENED_VALUE = 0x80000000;\n\nexport const FORBIDDEN_KEYS = ['constructor', '__proto__', 'prototype'];\n\n/**\n * Maps an interface with method hooks to an object, using the keys of the\n * interface, and `true` as value. This ensures that the `methodHooks` object\n * has the same values as the interface.\n */\nexport type MethodHooksObject<HooksType extends Record<string, unknown>> = {\n [Key in keyof HooksType]: true;\n};\n\n/**\n * Returns the subset of the specified `hooks` that are included in the\n * `hookNames` object. This is a Principle of Least Authority (POLA) measure\n * to ensure that each RPC method implementation only has access to the\n * API \"hooks\" it needs to do its job.\n *\n * @param hooks - The hooks to select from.\n * @param hookNames - The names of the hooks to select.\n * @returns The selected hooks.\n * @template Hooks - The hooks to select from.\n * @template HookName - The names of the hooks to select.\n */\nexport function selectHooks<\n Hooks extends Record<string, unknown>,\n HookName extends keyof Hooks,\n>(\n hooks: Hooks,\n hookNames?: Record<HookName, boolean>,\n): Pick<Hooks, HookName> | undefined {\n if (hookNames) {\n return Object.keys(hookNames).reduce<Partial<Pick<Hooks, HookName>>>(\n (hookSubset, _hookName) => {\n const hookName = _hookName as HookName;\n hookSubset[hookName] = hooks[hookName];\n return hookSubset;\n },\n {},\n ) as Pick<Hooks, HookName>;\n }\n return undefined;\n}\n\n/**\n * Get a BIP-32 derivation path array from a hash, which is compatible with\n * `@metamask/key-tree`. The hash is assumed to be 32 bytes long.\n *\n * @param hash - The hash to derive indices from.\n * @returns The derived indices as a {@link HardenedBIP32Node} array.\n */\nfunction getDerivationPathArray(hash: Uint8Array): HardenedBIP32Node[] {\n const array: HardenedBIP32Node[] = [];\n const view = createDataView(hash);\n\n for (let index = 0; index < 8; index++) {\n const uint32 = view.getUint32(index * 4);\n\n // This is essentially `index | 0x80000000`. Because JavaScript numbers are\n // signed, we use the bitwise unsigned right shift operator to ensure that\n // the result is a positive number.\n // eslint-disable-next-line no-bitwise\n const pathIndex = (uint32 | HARDENED_VALUE) >>> 0;\n array.push(`bip32:${pathIndex - HARDENED_VALUE}'` as const);\n }\n\n return array;\n}\n\ntype DeriveEntropyOptions = {\n /**\n * The input value to derive entropy from.\n */\n input: string;\n\n /**\n * An optional salt to use when deriving entropy.\n */\n salt?: string;\n\n /**\n * The mnemonic phrase to use for entropy derivation.\n */\n mnemonicPhrase: Uint8Array;\n\n /**\n * A hardened BIP-32 index, which is used to derive the root key from the\n * mnemonic phrase.\n */\n magic: MagicValue;\n\n /**\n * The cryptographic functions to use for the derivation.\n */\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\n/**\n * Derive entropy from the given mnemonic phrase and salt.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.mnemonicPhrase - The mnemonic phrase to use for entropy\n * derivation.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the derivation.\n * @returns The derived entropy.\n */\nexport async function deriveEntropy({\n input,\n salt = '',\n mnemonicPhrase,\n magic,\n cryptographicFunctions,\n}: DeriveEntropyOptions): Promise<Hex> {\n const inputBytes = stringToBytes(input);\n const saltBytes = stringToBytes(salt);\n\n // Get the derivation path from the snap ID.\n const hash = keccak256(concatBytes([inputBytes, keccak256(saltBytes)]));\n const computedDerivationPath = getDerivationPathArray(hash);\n\n // Derive the private key using BIP-32.\n const { privateKey } = await SLIP10Node.fromDerivationPath(\n {\n derivationPath: [\n mnemonicPhrase,\n `bip32:${magic}`,\n ...computedDerivationPath,\n ],\n curve: 'secp256k1',\n },\n cryptographicFunctions,\n );\n\n // This should never happen, but this keeps TypeScript happy.\n assert(privateKey, 'Failed to derive the entropy.');\n\n return add0x(privateKey);\n}\n\n/**\n * Get the path prefix to use for key derivation in `key-tree`. This assumes the\n * following:\n *\n * - The Secp256k1 curve always uses the BIP-32 specification.\n * - The Ed25519 curve always uses the SLIP-10 specification.\n * - The BIP-32-Ed25519 curve always uses the CIP-3 specification.\n *\n * While this does not matter in most situations (no known case at the time of\n * writing), `key-tree` requires a specific specification to be used.\n *\n * @param curve - The curve to get the path prefix for. The curve is NOT\n * validated by this function.\n * @returns The path prefix, i.e., `bip32` or `slip10`.\n */\nexport function getPathPrefix(\n curve: SupportedCurve,\n): 'bip32' | 'slip10' | 'cip3' {\n switch (curve) {\n case 'secp256k1':\n return 'bip32';\n case 'ed25519':\n return 'slip10';\n case 'ed25519Bip32':\n return 'cip3';\n default:\n return assertExhaustive(curve);\n }\n}\n\ntype GetNodeArgs = {\n curve: SupportedCurve;\n secretRecoveryPhrase: Uint8Array;\n path: string[];\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.secretRecoveryPhrase - The secret recovery phrase to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNode({\n curve,\n secretRecoveryPhrase,\n path,\n cryptographicFunctions,\n}: GetNodeArgs) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromDerivationPath(\n {\n curve,\n derivationPath: [\n secretRecoveryPhrase,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Validate the key of a state object.\n *\n * @param key - The key to validate.\n * @returns `true` if the key is valid, `false` otherwise.\n */\nexport function isValidStateKey(key: string | undefined) {\n if (key === undefined) {\n return true;\n }\n\n return key.split('.').every((part) => part.length > 0);\n}\n\nexport const StateKeyStruct = refine(string(), 'state key', (value) => {\n if (!isValidStateKey(value)) {\n return 'Invalid state key. Each part of the key must be non-empty.';\n }\n\n return true;\n});\n\n/**\n * Get the secret recovery phrase of the user. This calls the `getMnemonic` hook\n * and handles any errors that occur, throwing formatted JSON-RPC errors.\n *\n * @param getMnemonic - The `getMnemonic` hook.\n * @param source - The entropy source to use.\n * @returns The secret recovery phrase.\n */\nexport async function getSecretRecoveryPhrase(\n getMnemonic: (source?: string | undefined) => Promise<Uint8Array>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n try {\n return await getMnemonic(source);\n } catch (error) {\n if (error instanceof Error) {\n throw rpcErrors.invalidParams({\n message: error.message,\n });\n }\n\n throw rpcErrors.internal({\n message: 'An unknown error occurred.',\n data: {\n error: error.toString(),\n },\n });\n }\n}\n"]}
1
+ {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,UAAU,EAAE,2BAA2B;AAChD,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAEjD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,8BAA8B;AACvD,OAAO,EACL,gBAAgB,EAChB,KAAK,EACL,MAAM,EACN,WAAW,EACX,cAAc,EACd,aAAa,EACd,wBAAwB;AACzB,OAAO,EAAE,UAAU,IAAI,SAAS,EAAE,2BAA2B;AAE7D,MAAM,cAAc,GAAG,UAAU,CAAC;AAElC,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,aAAa,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AAWxE;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CAIzB,KAAY,EACZ,SAAqC;IAErC,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,MAAM,CAClC,CAAC,UAAU,EAAE,SAAS,EAAE,EAAE;YACxB,MAAM,QAAQ,GAAG,SAAqB,CAAC;Y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,KAAK,EAAE,CAAC;YAC3B,MAAM,SAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,KAAK,CAAC,OAAO;aACvB,CAAC,CAAC;QACL,CAAC;QAED,MAAM,SAAS,CAAC,QAAQ,CAAC;YACvB,OAAO,EAAE,4BAA4B;YACrC,IAAI,EAAE;gBACJ,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE;aACxB;SACF,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import type {\n HardenedBIP32Node,\n BIP32Node,\n SLIP10PathNode,\n SupportedCurve,\n CryptographicFunctions,\n} from '@metamask/key-tree';\nimport { SLIP10Node } from '@metamask/key-tree';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { MagicValue } from '@metamask/snaps-utils';\nimport { refine, string } from '@metamask/superstruct';\nimport {\n assertExhaustive,\n add0x,\n assert,\n concatBytes,\n createDataView,\n stringToBytes,\n} from '@metamask/utils';\nimport { keccak_256 as keccak256 } from '@noble/hashes/sha3';\n\nconst HARDENED_VALUE = 0x80000000;\n\nexport const FORBIDDEN_KEYS = ['constructor', '__proto__', 'prototype'];\n\n/**\n * Maps an interface with method hooks to an object, using the keys of the\n * interface, and `true` as value. This ensures that the `methodHooks` object\n * has the same values as the interface.\n */\nexport type MethodHooksObject<HooksType extends Record<string, unknown>> = {\n [Key in keyof HooksType]: true;\n};\n\n/**\n * Returns the subset of the specified `hooks` that are included in the\n * `hookNames` object. This is a Principle of Least Authority (POLA) measure\n * to ensure that each RPC method implementation only has access to the\n * API \"hooks\" it needs to do its job.\n *\n * @param hooks - The hooks to select from.\n * @param hookNames - The names of the hooks to select.\n * @returns The selected hooks.\n * @template Hooks - The hooks to select from.\n * @template HookName - The names of the hooks to select.\n */\nexport function selectHooks<\n Hooks extends Record<string, unknown>,\n HookName extends keyof Hooks,\n>(\n hooks: Hooks,\n hookNames?: Record<HookName, boolean>,\n): Pick<Hooks, HookName> | undefined {\n if (hookNames) {\n return Object.keys(hookNames).reduce<Partial<Pick<Hooks, HookName>>>(\n (hookSubset, _hookName) => {\n const hookName = _hookName as HookName;\n hookSubset[hookName] = hooks[hookName];\n return hookSubset;\n },\n {},\n ) as Pick<Hooks, HookName>;\n }\n return undefined;\n}\n\n/**\n * Get a BIP-32 derivation path array from a hash, which is compatible with\n * `@metamask/key-tree`. The hash is assumed to be 32 bytes long.\n *\n * @param hash - The hash to derive indices from.\n * @returns The derived indices as a {@link HardenedBIP32Node} array.\n */\nfunction getDerivationPathArray(hash: Uint8Array): HardenedBIP32Node[] {\n const array: HardenedBIP32Node[] = [];\n const view = createDataView(hash);\n\n for (let index = 0; index < 8; index++) {\n const uint32 = view.getUint32(index * 4);\n\n // This is essentially `index | 0x80000000`. Because JavaScript numbers are\n // signed, we use the bitwise unsigned right shift operator to ensure that\n // the result is a positive number.\n // eslint-disable-next-line no-bitwise\n const pathIndex = (uint32 | HARDENED_VALUE) >>> 0;\n array.push(`bip32:${pathIndex - HARDENED_VALUE}'` as const);\n }\n\n return array;\n}\n\ntype BaseDeriveEntropyOptions = {\n /**\n * The input value to derive entropy from.\n */\n input: string;\n\n /**\n * An optional salt to use when deriving entropy.\n */\n salt?: string;\n\n /**\n * A hardened BIP-32 index, which is used to derive the root key from the\n * mnemonic phrase.\n */\n magic: MagicValue;\n\n /**\n * The cryptographic functions to use for the derivation.\n */\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\ntype SeedDeriveEntropyOptions = BaseDeriveEntropyOptions & {\n /**\n * The mnemonic seed to use for entropy derivation.\n */\n seed: Uint8Array;\n};\n\n/**\n * Get the derivation path to use for entropy derivation.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @returns The derivation path to be used for entropy key derivation.\n */\nfunction getEntropyDerivationPath({\n input,\n salt,\n magic,\n}: Required<Omit<BaseDeriveEntropyOptions, 'cryptographicFunctions'>>):\n | BIP32Node[]\n | SLIP10PathNode[] {\n const inputBytes = stringToBytes(input);\n const saltBytes = stringToBytes(salt);\n\n // Get the derivation path from the snap ID.\n const hash = keccak256(concatBytes([inputBytes, keccak256(saltBytes)]));\n const computedDerivationPath = getDerivationPathArray(hash);\n\n return [`bip32:${magic}`, ...computedDerivationPath];\n}\n\n/**\n * Derive entropy from the given mnemonic seed and salt.\n *\n * This is based on the reference implementation of\n * [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).\n *\n * @param options - The options for entropy derivation.\n * @param options.input - The input value to derive entropy from.\n * @param options.salt - An optional salt to use when deriving entropy.\n * @param options.seed - The mnemonic seed to use for entropy\n * derivation.\n * @param options.magic - A hardened BIP-32 index, which is used to derive the\n * root key from the mnemonic phrase.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the derivation.\n * @returns The derived entropy.\n */\nexport async function deriveEntropyFromSeed({\n input,\n salt = '',\n seed,\n magic,\n cryptographicFunctions,\n}: SeedDeriveEntropyOptions) {\n const computedDerivationPath = getEntropyDerivationPath({\n input,\n salt,\n magic,\n });\n\n // Derive the private key using BIP-32.\n const { privateKey } = await SLIP10Node.fromSeed(\n {\n derivationPath: [seed, ...computedDerivationPath],\n curve: 'secp256k1',\n },\n cryptographicFunctions,\n );\n\n // This should never happen, but this keeps TypeScript happy.\n assert(privateKey, 'Failed to derive the entropy.');\n\n return add0x(privateKey);\n}\n\n/**\n * Get the path prefix to use for key derivation in `key-tree`. This assumes the\n * following:\n *\n * - The Secp256k1 curve always uses the BIP-32 specification.\n * - The Ed25519 curve always uses the SLIP-10 specification.\n * - The BIP-32-Ed25519 curve always uses the CIP-3 specification.\n *\n * While this does not matter in most situations (no known case at the time of\n * writing), `key-tree` requires a specific specification to be used.\n *\n * @param curve - The curve to get the path prefix for. The curve is NOT\n * validated by this function.\n * @returns The path prefix, i.e., `bip32` or `slip10`.\n */\nexport function getPathPrefix(\n curve: SupportedCurve,\n): 'bip32' | 'slip10' | 'cip3' {\n switch (curve) {\n case 'secp256k1':\n return 'bip32';\n case 'ed25519':\n return 'slip10';\n case 'ed25519Bip32':\n return 'cip3';\n default:\n return assertExhaustive(curve);\n }\n}\n\ntype BaseGetNodeArgs = {\n curve: SupportedCurve;\n path: string[];\n cryptographicFunctions: CryptographicFunctions | undefined;\n};\n\ntype GetNodeArgsMnemonic = BaseGetNodeArgs & {\n secretRecoveryPhrase: Uint8Array;\n};\n\ntype GetNodeArgsSeed = BaseGetNodeArgs & {\n seed: Uint8Array;\n};\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.secretRecoveryPhrase - The secret recovery phrase to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNodeFromMnemonic({\n curve,\n secretRecoveryPhrase,\n path,\n cryptographicFunctions,\n}: GetNodeArgsMnemonic) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromDerivationPath(\n {\n curve,\n derivationPath: [\n secretRecoveryPhrase,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Get a `key-tree`-compatible node.\n *\n * Note: This function assumes that all the parameters have been validated\n * beforehand.\n *\n * @param options - The derivation options.\n * @param options.curve - The curve to use for derivation.\n * @param options.seed - The BIP-39 to use for\n * derivation.\n * @param options.path - The derivation path to use as array, starting with an\n * \"m\" as the first item.\n * @param options.cryptographicFunctions - The cryptographic functions to use\n * for the node.\n * @returns The `key-tree` SLIP-10 node.\n */\nexport async function getNodeFromSeed({\n curve,\n seed,\n path,\n cryptographicFunctions,\n}: GetNodeArgsSeed) {\n const prefix = getPathPrefix(curve);\n\n return await SLIP10Node.fromSeed(\n {\n curve,\n derivationPath: [\n seed,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n },\n cryptographicFunctions,\n );\n}\n\n/**\n * Validate the key of a state object.\n *\n * @param key - The key to validate.\n * @returns `true` if the key is valid, `false` otherwise.\n */\nexport function isValidStateKey(key: string | undefined) {\n if (key === undefined) {\n return true;\n }\n\n return key.split('.').every((part) => part.length > 0);\n}\n\nexport const StateKeyStruct = refine(string(), 'state key', (value) => {\n if (!isValidStateKey(value)) {\n return 'Invalid state key. Each part of the key must be non-empty.';\n }\n\n return true;\n});\n\n/**\n * Get a value using the entropy source hooks: getMnemonic or getMnemonicSeed.\n * This function calls the passed hook and handles any errors that occur,\n * throwing formatted JSON-RPC errors.\n *\n * @param hook - The hook.\n * @param source - The entropy source to use.\n * @returns The secret recovery phrase.\n */\nexport async function getValueFromEntropySource(\n hook: (source?: string | undefined) => Promise<Uint8Array>,\n source?: string | undefined,\n): Promise<Uint8Array> {\n try {\n return await hook(source);\n } catch (error) {\n if (error instanceof Error) {\n throw rpcErrors.invalidParams({\n message: error.message,\n });\n }\n\n throw rpcErrors.internal({\n message: 'An unknown error occurred.',\n data: {\n error: error.toString(),\n },\n });\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-rpc-methods",
3
- "version": "11.13.1",
3
+ "version": "12.1.0",
4
4
  "description": "MetaMask Snaps JSON-RPC method implementations",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -55,19 +55,19 @@
55
55
  "test:watch": "jest --watch"
56
56
  },
57
57
  "dependencies": {
58
- "@metamask/key-tree": "^10.0.2",
58
+ "@metamask/key-tree": "^10.1.1",
59
59
  "@metamask/permission-controller": "^11.0.6",
60
60
  "@metamask/rpc-errors": "^7.0.2",
61
- "@metamask/snaps-sdk": "^6.19.0",
62
- "@metamask/snaps-utils": "^9.0.1",
63
- "@metamask/superstruct": "^3.1.0",
61
+ "@metamask/snaps-sdk": "^6.22.0",
62
+ "@metamask/snaps-utils": "^9.2.0",
63
+ "@metamask/superstruct": "^3.2.1",
64
64
  "@metamask/utils": "^11.2.0",
65
- "@noble/hashes": "^1.3.1",
65
+ "@noble/hashes": "^1.7.1",
66
66
  "luxon": "^3.5.0"
67
67
  },
68
68
  "devDependencies": {
69
69
  "@lavamoat/allow-scripts": "^3.0.4",
70
- "@metamask/auto-changelog": "^4.1.0",
70
+ "@metamask/auto-changelog": "^5.0.1",
71
71
  "@metamask/json-rpc-engine": "^10.0.2",
72
72
  "@swc/core": "1.3.78",
73
73
  "@swc/jest": "^0.2.26",