@metamask/snaps-utils 5.0.0 → 5.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 (41) hide show
  1. package/CHANGELOG.md +15 -1
  2. package/dist/cjs/auxiliary-files.js +3 -3
  3. package/dist/cjs/auxiliary-files.js.map +1 -1
  4. package/dist/cjs/base64.js +44 -0
  5. package/dist/cjs/base64.js.map +1 -0
  6. package/dist/cjs/bytes.js +23 -0
  7. package/dist/cjs/bytes.js.map +1 -0
  8. package/dist/cjs/checksum.js +2 -14
  9. package/dist/cjs/checksum.js.map +1 -1
  10. package/dist/cjs/derivation-paths.js +300 -0
  11. package/dist/cjs/derivation-paths.js.map +1 -0
  12. package/dist/cjs/index.browser.js +3 -0
  13. package/dist/cjs/index.browser.js.map +1 -1
  14. package/dist/cjs/index.js +3 -0
  15. package/dist/cjs/index.js.map +1 -1
  16. package/dist/cjs/virtual-file/VirtualFile.js +1 -0
  17. package/dist/cjs/virtual-file/VirtualFile.js.map +1 -1
  18. package/dist/esm/auxiliary-files.js +3 -3
  19. package/dist/esm/auxiliary-files.js.map +1 -1
  20. package/dist/esm/base64.js +36 -0
  21. package/dist/esm/base64.js.map +1 -0
  22. package/dist/esm/bytes.js +18 -0
  23. package/dist/esm/bytes.js.map +1 -0
  24. package/dist/esm/checksum.js +3 -17
  25. package/dist/esm/checksum.js.map +1 -1
  26. package/dist/esm/derivation-paths.js +287 -0
  27. package/dist/esm/derivation-paths.js.map +1 -0
  28. package/dist/esm/index.browser.js +3 -0
  29. package/dist/esm/index.browser.js.map +1 -1
  30. package/dist/esm/index.js +3 -0
  31. package/dist/esm/index.js.map +1 -1
  32. package/dist/esm/virtual-file/VirtualFile.js +1 -0
  33. package/dist/esm/virtual-file/VirtualFile.js.map +1 -1
  34. package/dist/types/auxiliary-files.d.ts +1 -1
  35. package/dist/types/base64.d.ts +15 -0
  36. package/dist/types/bytes.d.ts +8 -0
  37. package/dist/types/checksum.d.ts +1 -8
  38. package/dist/types/derivation-paths.d.ts +24 -0
  39. package/dist/types/index.browser.d.ts +3 -0
  40. package/dist/types/index.d.ts +3 -0
  41. package/package.json +6 -5
@@ -0,0 +1,36 @@
1
+ import { bytesToBase64 } from '@metamask/utils';
2
+ import { getBytes } from './bytes';
3
+ /**
4
+ * Provides fast, asynchronous base64 encoding.
5
+ *
6
+ * @param input - The input value, assumed to be coercable to bytes.
7
+ * @returns A base64 string.
8
+ */ export async function encodeBase64(input) {
9
+ const bytes = getBytes(input);
10
+ // In the browser, FileReader is much faster than bytesToBase64.
11
+ if ('FileReader' in globalThis) {
12
+ return await new Promise((resolve, reject)=>{
13
+ const reader = Object.assign(new FileReader(), {
14
+ onload: ()=>resolve(reader.result.replace('data:application/octet-stream;base64,', '')),
15
+ onerror: ()=>reject(reader.error)
16
+ });
17
+ reader.readAsDataURL(new File([
18
+ bytes
19
+ ], '', {
20
+ type: 'application/octet-stream'
21
+ }));
22
+ });
23
+ }
24
+ return bytesToBase64(bytes);
25
+ }
26
+ /**
27
+ * Provides fast, asynchronous base64 decoding.
28
+ *
29
+ * @param base64 - A base64 string.
30
+ * @returns A Uint8Array of bytes.
31
+ */ export async function decodeBase64(base64) {
32
+ const response = await fetch(`data:application/octet-stream;base64,${base64}`);
33
+ return new Uint8Array(await response.arrayBuffer());
34
+ }
35
+
36
+ //# sourceMappingURL=base64.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/base64.ts"],"sourcesContent":["import { bytesToBase64 } from '@metamask/utils';\n\nimport { getBytes } from './bytes';\nimport type { VirtualFile } from './virtual-file';\n\n/**\n * Provides fast, asynchronous base64 encoding.\n *\n * @param input - The input value, assumed to be coercable to bytes.\n * @returns A base64 string.\n */\nexport async function encodeBase64(input: Uint8Array | VirtualFile | string) {\n const bytes = getBytes(input);\n // In the browser, FileReader is much faster than bytesToBase64.\n if ('FileReader' in globalThis) {\n return await new Promise((resolve, reject) => {\n const reader = Object.assign(new FileReader(), {\n onload: () =>\n resolve(\n (reader.result as string).replace(\n 'data:application/octet-stream;base64,',\n '',\n ),\n ),\n onerror: () => reject(reader.error),\n });\n reader.readAsDataURL(\n new File([bytes], '', { type: 'application/octet-stream' }),\n );\n });\n }\n return bytesToBase64(bytes);\n}\n\n/**\n * Provides fast, asynchronous base64 decoding.\n *\n * @param base64 - A base64 string.\n * @returns A Uint8Array of bytes.\n */\nexport async function decodeBase64(base64: string) {\n const response = await fetch(\n `data:application/octet-stream;base64,${base64}`,\n );\n return new Uint8Array(await response.arrayBuffer());\n}\n"],"names":["bytesToBase64","getBytes","encodeBase64","input","bytes","globalThis","Promise","resolve","reject","reader","Object","assign","FileReader","onload","result","replace","onerror","error","readAsDataURL","File","type","decodeBase64","base64","response","fetch","Uint8Array","arrayBuffer"],"mappings":"AAAA,SAASA,aAAa,QAAQ,kBAAkB;AAEhD,SAASC,QAAQ,QAAQ,UAAU;AAGnC;;;;;CAKC,GACD,OAAO,eAAeC,aAAaC,KAAwC;IACzE,MAAMC,QAAQH,SAASE;IACvB,gEAAgE;IAChE,IAAI,gBAAgBE,YAAY;QAC9B,OAAO,MAAM,IAAIC,QAAQ,CAACC,SAASC;YACjC,MAAMC,SAASC,OAAOC,MAAM,CAAC,IAAIC,cAAc;gBAC7CC,QAAQ,IACNN,QACE,AAACE,OAAOK,MAAM,CAAYC,OAAO,CAC/B,yCACA;gBAGNC,SAAS,IAAMR,OAAOC,OAAOQ,KAAK;YACpC;YACAR,OAAOS,aAAa,CAClB,IAAIC,KAAK;gBAACf;aAAM,EAAE,IAAI;gBAAEgB,MAAM;YAA2B;QAE7D;IACF;IACA,OAAOpB,cAAcI;AACvB;AAEA;;;;;CAKC,GACD,OAAO,eAAeiB,aAAaC,MAAc;IAC/C,MAAMC,WAAW,MAAMC,MACrB,CAAC,qCAAqC,EAAEF,OAAO,CAAC;IAElD,OAAO,IAAIG,WAAW,MAAMF,SAASG,WAAW;AAClD"}
@@ -0,0 +1,18 @@
1
+ import { stringToBytes } from '@metamask/utils';
2
+ import { VirtualFile } from './virtual-file';
3
+ /**
4
+ * Convert a bytes-like input value to a Uint8Array.
5
+ *
6
+ * @param bytes - A bytes-like value.
7
+ * @returns The input value converted to a Uint8Array if necessary.
8
+ */ export function getBytes(bytes) {
9
+ // Unwrap VirtualFiles to extract the content
10
+ // The content is then either a string or Uint8Array
11
+ const unwrapped = bytes instanceof VirtualFile ? bytes.value : bytes;
12
+ if (typeof unwrapped === 'string') {
13
+ return stringToBytes(unwrapped);
14
+ }
15
+ return unwrapped;
16
+ }
17
+
18
+ //# sourceMappingURL=bytes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/bytes.ts"],"sourcesContent":["import { stringToBytes } from '@metamask/utils';\n\nimport { VirtualFile } from './virtual-file';\n\n/**\n * Convert a bytes-like input value to a Uint8Array.\n *\n * @param bytes - A bytes-like value.\n * @returns The input value converted to a Uint8Array if necessary.\n */\nexport function getBytes(bytes: VirtualFile | Uint8Array | string): Uint8Array {\n // Unwrap VirtualFiles to extract the content\n // The content is then either a string or Uint8Array\n const unwrapped = bytes instanceof VirtualFile ? bytes.value : bytes;\n\n if (typeof unwrapped === 'string') {\n return stringToBytes(unwrapped);\n }\n\n return unwrapped;\n}\n"],"names":["stringToBytes","VirtualFile","getBytes","bytes","unwrapped","value"],"mappings":"AAAA,SAASA,aAAa,QAAQ,kBAAkB;AAEhD,SAASC,WAAW,QAAQ,iBAAiB;AAE7C;;;;;CAKC,GACD,OAAO,SAASC,SAASC,KAAwC;IAC/D,6CAA6C;IAC7C,oDAAoD;IACpD,MAAMC,YAAYD,iBAAiBF,cAAcE,MAAME,KAAK,GAAGF;IAE/D,IAAI,OAAOC,cAAc,UAAU;QACjC,OAAOJ,cAAcI;IACvB;IAEA,OAAOA;AACT"}
@@ -1,27 +1,13 @@
1
- import { assert, concatBytes, stringToBytes } from '@metamask/utils';
1
+ import { assert, concatBytes } from '@metamask/utils';
2
2
  import { sha256 } from '@noble/hashes/sha256';
3
- import { VirtualFile } from './virtual-file/VirtualFile';
4
- /**
5
- * Convert an input value to a Uint8Array for use in a checksum.
6
- *
7
- * @param bytes - A value to use for a checksum calculation.
8
- * @returns The input value converted to a Uint8Array if necessary.
9
- */ export function getChecksumBytes(bytes) {
10
- // Unwrap VirtualFiles to extract the content
11
- // The content is then either a string or Uint8Array
12
- const unwrapped = bytes instanceof VirtualFile ? bytes.value : bytes;
13
- if (typeof unwrapped === 'string') {
14
- return stringToBytes(unwrapped);
15
- }
16
- return unwrapped;
17
- }
3
+ import { getBytes } from './bytes';
18
4
  /**
19
5
  * Calculates checksum for a single byte array.
20
6
  *
21
7
  * @param bytes - The byte array to calculate the checksum for.
22
8
  * @returns A single sha-256 checksum.
23
9
  */ export async function checksum(bytes) {
24
- const value = getChecksumBytes(bytes);
10
+ const value = getBytes(bytes);
25
11
  // Use crypto.subtle.digest whenever possible as it is faster.
26
12
  if ('crypto' in globalThis && typeof globalThis.crypto === 'object' && crypto.subtle?.digest) {
27
13
  return new Uint8Array(await crypto.subtle.digest('SHA-256', value));
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/checksum.ts"],"sourcesContent":["import { assert, concatBytes, stringToBytes } from '@metamask/utils';\nimport { sha256 } from '@noble/hashes/sha256';\n\nimport { VirtualFile } from './virtual-file/VirtualFile';\n\n/**\n * Convert an input value to a Uint8Array for use in a checksum.\n *\n * @param bytes - A value to use for a checksum calculation.\n * @returns The input value converted to a Uint8Array if necessary.\n */\nexport function getChecksumBytes(\n bytes: VirtualFile | Uint8Array | string,\n): Uint8Array {\n // Unwrap VirtualFiles to extract the content\n // The content is then either a string or Uint8Array\n const unwrapped = bytes instanceof VirtualFile ? bytes.value : bytes;\n\n if (typeof unwrapped === 'string') {\n return stringToBytes(unwrapped);\n }\n\n return unwrapped;\n}\n\n/**\n * Calculates checksum for a single byte array.\n *\n * @param bytes - The byte array to calculate the checksum for.\n * @returns A single sha-256 checksum.\n */\nexport async function checksum(\n bytes: VirtualFile | Uint8Array | string,\n): Promise<Uint8Array> {\n const value = getChecksumBytes(bytes);\n // Use crypto.subtle.digest whenever possible as it is faster.\n if (\n 'crypto' in globalThis &&\n typeof globalThis.crypto === 'object' &&\n crypto.subtle?.digest\n ) {\n return new Uint8Array(await crypto.subtle.digest('SHA-256', value));\n }\n return sha256(value);\n}\n\n/**\n * Calculates checksum over multiple files in a reproducible way.\n *\n * 1. Sort all the files by their paths.\n * 2. Calculate sha-256 checksum of each file separately.\n * 3. Concatenate all the checksums into one buffer and sha-256 that buffer.\n *\n * The sorting of paths is done using {@link https://tc39.es/ecma262/#sec-islessthan UTF-16 Code Units}.\n *\n * @param files - The files over which to calculate the checksum.\n * @returns A single sha-256 checksum.\n */\nexport async function checksumFiles(files: VirtualFile[]) {\n const checksums = await Promise.all(\n [...files]\n .sort((a, b) => {\n assert(a.path !== b.path, 'Tried to sort files with non-unique paths.');\n if (a.path < b.path) {\n return -1;\n }\n return 1;\n })\n .map(async (file) => checksum(file)),\n );\n\n return checksum(concatBytes(checksums));\n}\n"],"names":["assert","concatBytes","stringToBytes","sha256","VirtualFile","getChecksumBytes","bytes","unwrapped","value","checksum","globalThis","crypto","subtle","digest","Uint8Array","checksumFiles","files","checksums","Promise","all","sort","a","b","path","map","file"],"mappings":"AAAA,SAASA,MAAM,EAAEC,WAAW,EAAEC,aAAa,QAAQ,kBAAkB;AACrE,SAASC,MAAM,QAAQ,uBAAuB;AAE9C,SAASC,WAAW,QAAQ,6BAA6B;AAEzD;;;;;CAKC,GACD,OAAO,SAASC,iBACdC,KAAwC;IAExC,6CAA6C;IAC7C,oDAAoD;IACpD,MAAMC,YAAYD,iBAAiBF,cAAcE,MAAME,KAAK,GAAGF;IAE/D,IAAI,OAAOC,cAAc,UAAU;QACjC,OAAOL,cAAcK;IACvB;IAEA,OAAOA;AACT;AAEA;;;;;CAKC,GACD,OAAO,eAAeE,SACpBH,KAAwC;IAExC,MAAME,QAAQH,iBAAiBC;IAC/B,8DAA8D;IAC9D,IACE,YAAYI,cACZ,OAAOA,WAAWC,MAAM,KAAK,YAC7BA,OAAOC,MAAM,EAAEC,QACf;QACA,OAAO,IAAIC,WAAW,MAAMH,OAAOC,MAAM,CAACC,MAAM,CAAC,WAAWL;IAC9D;IACA,OAAOL,OAAOK;AAChB;AAEA;;;;;;;;;;;CAWC,GACD,OAAO,eAAeO,cAAcC,KAAoB;IACtD,MAAMC,YAAY,MAAMC,QAAQC,GAAG,CACjC;WAAIH;KAAM,CACPI,IAAI,CAAC,CAACC,GAAGC;QACRtB,OAAOqB,EAAEE,IAAI,KAAKD,EAAEC,IAAI,EAAE;QAC1B,IAAIF,EAAEE,IAAI,GAAGD,EAAEC,IAAI,EAAE;YACnB,OAAO,CAAC;QACV;QACA,OAAO;IACT,GACCC,GAAG,CAAC,OAAOC,OAAShB,SAASgB;IAGlC,OAAOhB,SAASR,YAAYgB;AAC9B"}
1
+ {"version":3,"sources":["../../src/checksum.ts"],"sourcesContent":["import { assert, concatBytes } from '@metamask/utils';\nimport { sha256 } from '@noble/hashes/sha256';\n\nimport { getBytes } from './bytes';\nimport type { VirtualFile } from './virtual-file/VirtualFile';\n\n/**\n * Calculates checksum for a single byte array.\n *\n * @param bytes - The byte array to calculate the checksum for.\n * @returns A single sha-256 checksum.\n */\nexport async function checksum(\n bytes: VirtualFile | Uint8Array | string,\n): Promise<Uint8Array> {\n const value = getBytes(bytes);\n // Use crypto.subtle.digest whenever possible as it is faster.\n if (\n 'crypto' in globalThis &&\n typeof globalThis.crypto === 'object' &&\n crypto.subtle?.digest\n ) {\n return new Uint8Array(await crypto.subtle.digest('SHA-256', value));\n }\n return sha256(value);\n}\n\n/**\n * Calculates checksum over multiple files in a reproducible way.\n *\n * 1. Sort all the files by their paths.\n * 2. Calculate sha-256 checksum of each file separately.\n * 3. Concatenate all the checksums into one buffer and sha-256 that buffer.\n *\n * The sorting of paths is done using {@link https://tc39.es/ecma262/#sec-islessthan UTF-16 Code Units}.\n *\n * @param files - The files over which to calculate the checksum.\n * @returns A single sha-256 checksum.\n */\nexport async function checksumFiles(files: VirtualFile[]) {\n const checksums = await Promise.all(\n [...files]\n .sort((a, b) => {\n assert(a.path !== b.path, 'Tried to sort files with non-unique paths.');\n if (a.path < b.path) {\n return -1;\n }\n return 1;\n })\n .map(async (file) => checksum(file)),\n );\n\n return checksum(concatBytes(checksums));\n}\n"],"names":["assert","concatBytes","sha256","getBytes","checksum","bytes","value","globalThis","crypto","subtle","digest","Uint8Array","checksumFiles","files","checksums","Promise","all","sort","a","b","path","map","file"],"mappings":"AAAA,SAASA,MAAM,EAAEC,WAAW,QAAQ,kBAAkB;AACtD,SAASC,MAAM,QAAQ,uBAAuB;AAE9C,SAASC,QAAQ,QAAQ,UAAU;AAGnC;;;;;CAKC,GACD,OAAO,eAAeC,SACpBC,KAAwC;IAExC,MAAMC,QAAQH,SAASE;IACvB,8DAA8D;IAC9D,IACE,YAAYE,cACZ,OAAOA,WAAWC,MAAM,KAAK,YAC7BA,OAAOC,MAAM,EAAEC,QACf;QACA,OAAO,IAAIC,WAAW,MAAMH,OAAOC,MAAM,CAACC,MAAM,CAAC,WAAWJ;IAC9D;IACA,OAAOJ,OAAOI;AAChB;AAEA;;;;;;;;;;;CAWC,GACD,OAAO,eAAeM,cAAcC,KAAoB;IACtD,MAAMC,YAAY,MAAMC,QAAQC,GAAG,CACjC;WAAIH;KAAM,CACPI,IAAI,CAAC,CAACC,GAAGC;QACRnB,OAAOkB,EAAEE,IAAI,KAAKD,EAAEC,IAAI,EAAE;QAC1B,IAAIF,EAAEE,IAAI,GAAGD,EAAEC,IAAI,EAAE;YACnB,OAAO,CAAC;QACV;QACA,OAAO;IACT,GACCC,GAAG,CAAC,OAAOC,OAASlB,SAASkB;IAGlC,OAAOlB,SAASH,YAAYa;AAC9B"}
@@ -0,0 +1,287 @@
1
+ import slip44 from '@metamask/slip44';
2
+ import { isEqual } from './array';
3
+ export const SNAPS_DERIVATION_PATHS = [
4
+ {
5
+ path: [
6
+ 'm',
7
+ `44'`,
8
+ `0'`
9
+ ],
10
+ curve: 'ed25519',
11
+ name: 'Test BIP-32 Path (ed25519)'
12
+ },
13
+ {
14
+ path: [
15
+ 'm',
16
+ `44'`,
17
+ `1'`
18
+ ],
19
+ curve: 'secp256k1',
20
+ name: 'Testnet'
21
+ },
22
+ {
23
+ path: [
24
+ 'm',
25
+ `44'`,
26
+ `0'`
27
+ ],
28
+ curve: 'secp256k1',
29
+ name: 'Bitcoin Legacy'
30
+ },
31
+ {
32
+ path: [
33
+ 'm',
34
+ `49'`,
35
+ `0'`
36
+ ],
37
+ curve: 'secp256k1',
38
+ name: 'Bitcoin Nested SegWit'
39
+ },
40
+ {
41
+ path: [
42
+ 'm',
43
+ `49'`,
44
+ `1'`
45
+ ],
46
+ curve: 'secp256k1',
47
+ name: 'Bitcoin Testnet Nested SegWit'
48
+ },
49
+ {
50
+ path: [
51
+ 'm',
52
+ `84'`,
53
+ `0'`
54
+ ],
55
+ curve: 'secp256k1',
56
+ name: 'Bitcoin Native SegWit'
57
+ },
58
+ {
59
+ path: [
60
+ 'm',
61
+ `84'`,
62
+ `1'`
63
+ ],
64
+ curve: 'secp256k1',
65
+ name: 'Bitcoin Testnet Native SegWit'
66
+ },
67
+ {
68
+ path: [
69
+ 'm',
70
+ `44'`,
71
+ `501'`
72
+ ],
73
+ curve: 'ed25519',
74
+ name: 'Solana'
75
+ },
76
+ {
77
+ path: [
78
+ 'm',
79
+ `44'`,
80
+ `501'`,
81
+ "0'",
82
+ "0'"
83
+ ],
84
+ curve: 'ed25519',
85
+ name: 'Solana'
86
+ },
87
+ {
88
+ path: [
89
+ 'm',
90
+ `44'`,
91
+ `2'`
92
+ ],
93
+ curve: 'secp256k1',
94
+ name: 'Litecoin'
95
+ },
96
+ {
97
+ path: [
98
+ 'm',
99
+ `44'`,
100
+ `3'`
101
+ ],
102
+ curve: 'secp256k1',
103
+ name: 'Dogecoin'
104
+ },
105
+ {
106
+ path: [
107
+ 'm',
108
+ `44'`,
109
+ `60'`
110
+ ],
111
+ curve: 'secp256k1',
112
+ name: 'Ethereum'
113
+ },
114
+ {
115
+ path: [
116
+ 'm',
117
+ `44'`,
118
+ `118'`
119
+ ],
120
+ curve: 'secp256k1',
121
+ name: 'Atom'
122
+ },
123
+ {
124
+ path: [
125
+ 'm',
126
+ `44'`,
127
+ `145'`
128
+ ],
129
+ curve: 'secp256k1',
130
+ name: 'Bitcoin Cash'
131
+ },
132
+ {
133
+ path: [
134
+ 'm',
135
+ `44'`,
136
+ `637'`
137
+ ],
138
+ curve: 'ed25519',
139
+ name: 'Aptos'
140
+ },
141
+ {
142
+ path: [
143
+ 'm',
144
+ `44'`,
145
+ `714'`
146
+ ],
147
+ curve: 'secp256k1',
148
+ name: 'Binance (BNB)'
149
+ },
150
+ {
151
+ path: [
152
+ 'm',
153
+ `44'`,
154
+ `784'`
155
+ ],
156
+ curve: 'ed25519',
157
+ name: 'Sui'
158
+ },
159
+ {
160
+ path: [
161
+ 'm',
162
+ `44'`,
163
+ `931'`
164
+ ],
165
+ curve: 'secp256k1',
166
+ name: 'THORChain (RUNE)'
167
+ },
168
+ {
169
+ path: [
170
+ 'm',
171
+ `44'`,
172
+ `330'`
173
+ ],
174
+ curve: 'secp256k1',
175
+ name: 'Terra (LUNA)'
176
+ },
177
+ {
178
+ path: [
179
+ 'm',
180
+ `44'`,
181
+ `459'`
182
+ ],
183
+ curve: 'secp256k1',
184
+ name: 'Kava'
185
+ },
186
+ {
187
+ path: [
188
+ 'm',
189
+ `44'`,
190
+ `529'`
191
+ ],
192
+ curve: 'secp256k1',
193
+ name: 'Secret Network'
194
+ },
195
+ {
196
+ path: [
197
+ 'm',
198
+ `44'`,
199
+ `397'`,
200
+ `0'`
201
+ ],
202
+ curve: 'ed25519',
203
+ name: 'NEAR Protocol'
204
+ },
205
+ {
206
+ path: [
207
+ 'm',
208
+ `44'`,
209
+ `1'`,
210
+ `0'`
211
+ ],
212
+ curve: 'ed25519',
213
+ name: 'Testnet'
214
+ },
215
+ {
216
+ path: [
217
+ 'm',
218
+ `44'`,
219
+ `472'`
220
+ ],
221
+ curve: 'ed25519',
222
+ name: 'Arweave'
223
+ },
224
+ {
225
+ path: [
226
+ 'm',
227
+ `44'`,
228
+ `12586'`
229
+ ],
230
+ curve: 'secp256k1',
231
+ name: 'Mina'
232
+ },
233
+ {
234
+ path: [
235
+ 'm',
236
+ `44'`,
237
+ `1729'`,
238
+ `0'`,
239
+ `0'`
240
+ ],
241
+ curve: 'ed25519',
242
+ name: 'Tezos'
243
+ },
244
+ {
245
+ path: [
246
+ 'm',
247
+ `1789'`,
248
+ `0'`
249
+ ],
250
+ curve: 'ed25519',
251
+ name: 'Vega'
252
+ }
253
+ ];
254
+ /**
255
+ * Gets the name of a derivation path supported by snaps.
256
+ *
257
+ * @param path - The derivation path.
258
+ * @param curve - The curve used to derive the keys.
259
+ * @returns The name of the derivation path, otherwise null.
260
+ */ export function getSnapDerivationPathName(path, curve) {
261
+ const pathMetadata = SNAPS_DERIVATION_PATHS.find((derivationPath)=>derivationPath.curve === curve && isEqual(derivationPath.path, path));
262
+ if (pathMetadata) {
263
+ return pathMetadata.name;
264
+ }
265
+ // If the curve is secp256k1 and the path is a valid BIP44 path
266
+ // we try looking for the network/protocol name in SLIP44
267
+ if (curve === 'secp256k1' && path[0] === 'm' && path[1] === `44'` && path[2].endsWith(`'`)) {
268
+ const coinType = path[2].slice(0, -1);
269
+ return getSlip44ProtocolName(coinType) ?? null;
270
+ }
271
+ return null;
272
+ }
273
+ /**
274
+ * Gets the name of the SLIP-44 protocol corresponding to the specified
275
+ * `coin_type`.
276
+ *
277
+ * @param coinType - The SLIP-44 `coin_type` value whose name
278
+ * to retrieve.
279
+ * @returns The name of the protocol, otherwise null.
280
+ */ export function getSlip44ProtocolName(coinType) {
281
+ if (String(coinType) === '1') {
282
+ return 'Test Networks';
283
+ }
284
+ return slip44[coinType]?.name ?? null;
285
+ }
286
+
287
+ //# sourceMappingURL=derivation-paths.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/derivation-paths.ts"],"sourcesContent":["import type { SupportedCurve } from '@metamask/key-tree';\nimport slip44 from '@metamask/slip44';\n\nimport { isEqual } from './array';\n\nexport type SnapsDerivationPath = {\n path: ['m', ...string[]];\n curve: SupportedCurve;\n name: string;\n};\n\nexport const SNAPS_DERIVATION_PATHS: SnapsDerivationPath[] = [\n {\n path: ['m', `44'`, `0'`],\n curve: 'ed25519',\n name: 'Test BIP-32 Path (ed25519)',\n },\n {\n path: ['m', `44'`, `1'`],\n curve: 'secp256k1',\n name: 'Testnet',\n },\n {\n path: ['m', `44'`, `0'`],\n curve: 'secp256k1',\n name: 'Bitcoin Legacy',\n },\n {\n path: ['m', `49'`, `0'`],\n curve: 'secp256k1',\n name: 'Bitcoin Nested SegWit',\n },\n {\n path: ['m', `49'`, `1'`],\n curve: 'secp256k1',\n name: 'Bitcoin Testnet Nested SegWit',\n },\n {\n path: ['m', `84'`, `0'`],\n curve: 'secp256k1',\n name: 'Bitcoin Native SegWit',\n },\n {\n path: ['m', `84'`, `1'`],\n curve: 'secp256k1',\n name: 'Bitcoin Testnet Native SegWit',\n },\n {\n path: ['m', `44'`, `501'`],\n curve: 'ed25519',\n name: 'Solana',\n },\n {\n path: ['m', `44'`, `501'`, \"0'\", \"0'\"],\n curve: 'ed25519',\n name: 'Solana',\n },\n {\n path: ['m', `44'`, `2'`],\n curve: 'secp256k1',\n name: 'Litecoin',\n },\n {\n path: ['m', `44'`, `3'`],\n curve: 'secp256k1',\n name: 'Dogecoin',\n },\n {\n path: ['m', `44'`, `60'`],\n curve: 'secp256k1',\n name: 'Ethereum',\n },\n {\n path: ['m', `44'`, `118'`],\n curve: 'secp256k1',\n name: 'Atom',\n },\n {\n path: ['m', `44'`, `145'`],\n curve: 'secp256k1',\n name: 'Bitcoin Cash',\n },\n {\n path: ['m', `44'`, `637'`],\n curve: 'ed25519',\n name: 'Aptos',\n },\n {\n path: ['m', `44'`, `714'`],\n curve: 'secp256k1',\n name: 'Binance (BNB)',\n },\n {\n path: ['m', `44'`, `784'`],\n curve: 'ed25519',\n name: 'Sui',\n },\n {\n path: ['m', `44'`, `931'`],\n curve: 'secp256k1',\n name: 'THORChain (RUNE)',\n },\n {\n path: ['m', `44'`, `330'`],\n curve: 'secp256k1',\n name: 'Terra (LUNA)',\n },\n {\n path: ['m', `44'`, `459'`],\n curve: 'secp256k1',\n name: 'Kava',\n },\n {\n path: ['m', `44'`, `529'`],\n curve: 'secp256k1',\n name: 'Secret Network',\n },\n {\n path: ['m', `44'`, `397'`, `0'`],\n curve: 'ed25519',\n name: 'NEAR Protocol',\n },\n {\n path: ['m', `44'`, `1'`, `0'`],\n curve: 'ed25519',\n name: 'Testnet',\n },\n {\n path: ['m', `44'`, `472'`],\n curve: 'ed25519',\n name: 'Arweave',\n },\n {\n path: ['m', `44'`, `12586'`],\n curve: 'secp256k1',\n name: 'Mina',\n },\n {\n path: ['m', `44'`, `1729'`, `0'`, `0'`],\n curve: 'ed25519',\n name: 'Tezos',\n },\n {\n path: ['m', `1789'`, `0'`],\n curve: 'ed25519',\n name: 'Vega',\n },\n];\n\n/**\n * Gets the name of a derivation path supported by snaps.\n *\n * @param path - The derivation path.\n * @param curve - The curve used to derive the keys.\n * @returns The name of the derivation path, otherwise null.\n */\nexport function getSnapDerivationPathName(\n path: SnapsDerivationPath['path'],\n curve: SupportedCurve,\n): string | null {\n const pathMetadata = SNAPS_DERIVATION_PATHS.find(\n (derivationPath) =>\n derivationPath.curve === curve && isEqual(derivationPath.path, path),\n );\n\n if (pathMetadata) {\n return pathMetadata.name;\n }\n\n // If the curve is secp256k1 and the path is a valid BIP44 path\n // we try looking for the network/protocol name in SLIP44\n if (\n curve === 'secp256k1' &&\n path[0] === 'm' &&\n path[1] === `44'` &&\n path[2].endsWith(`'`)\n ) {\n const coinType = path[2].slice(0, -1);\n return getSlip44ProtocolName(coinType) ?? null;\n }\n\n return null;\n}\n\n/**\n * Gets the name of the SLIP-44 protocol corresponding to the specified\n * `coin_type`.\n *\n * @param coinType - The SLIP-44 `coin_type` value whose name\n * to retrieve.\n * @returns The name of the protocol, otherwise null.\n */\nexport function getSlip44ProtocolName(coinType: number | string) {\n if (String(coinType) === '1') {\n return 'Test Networks';\n }\n\n return slip44[coinType as keyof typeof slip44]?.name ?? null;\n}\n"],"names":["slip44","isEqual","SNAPS_DERIVATION_PATHS","path","curve","name","getSnapDerivationPathName","pathMetadata","find","derivationPath","endsWith","coinType","slice","getSlip44ProtocolName","String"],"mappings":"AACA,OAAOA,YAAY,mBAAmB;AAEtC,SAASC,OAAO,QAAQ,UAAU;AAQlC,OAAO,MAAMC,yBAAgD;IAC3D;QACEC,MAAM;YAAC;YAAK,CAAC,GAAG,CAAC;YAAE,CAAC,EAAE,CAAC;SAAC;QACxBC,OAAO;QACPC,MAAM;IACR;IACA;QACEF,MAAM;YAAC;YAAK,CAAC,GAAG,CAAC;YAAE,CAAC,EAAE,CAAC;SAAC;QACxBC,OAAO;QACPC,MAAM;IACR;IACA;QACEF,MAAM;YAAC;YAAK,CAAC,GAAG,CAAC;YAAE,CAAC,EAAE,CAAC;SAAC;QACxBC,OAAO;QACPC,MAAM;IACR;IACA;QACEF,MAAM;YAAC;YAAK,CAAC,GAAG,CAAC;YAAE,CAAC,EAAE,CAAC;SAAC;QACxBC,OAAO;QACPC,MAAM;IACR;IACA;QACEF,MAAM;YAAC;YAAK,CAAC,GAAG,CAAC;YAAE,CAAC,EAAE,CAAC;SAAC;QACxBC,OAAO;QACPC,MAAM;IACR;IACA;QACEF,MAAM;YAAC;YAAK,CAAC,GAAG,CAAC;YAAE,CAAC,EAAE,CAAC;SAAC;QACxBC,OAAO;QACPC,MAAM;IACR;IACA;QACEF,MAAM;YAAC;YAAK,CAAC,GAAG,CAAC;YAAE,CAAC,EAAE,CAAC;SAAC;QACxBC,OAAO;QACPC,MAAM;IACR;IACA;QACEF,MAAM;YAAC;YAAK,CAAC,GAAG,CAAC;YAAE,CAAC,IAAI,CAAC;SAAC;QA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ACED,UAAU,eACVD,IAAI,CAAC,EAAE,KAAK,OACZA,IAAI,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,IACjBA,IAAI,CAAC,EAAE,CAACO,QAAQ,CAAC,CAAC,CAAC,CAAC,GACpB;QACA,MAAMC,WAAWR,IAAI,CAAC,EAAE,CAACS,KAAK,CAAC,GAAG,CAAC;QACnC,OAAOC,sBAAsBF,aAAa;IAC5C;IAEA,OAAO;AACT;AAEA;;;;;;;CAOC,GACD,OAAO,SAASE,sBAAsBF,QAAyB;IAC7D,IAAIG,OAAOH,cAAc,KAAK;QAC5B,OAAO;IACT;IAEA,OAAOX,MAAM,CAACW,SAAgC,EAAEN,QAAQ;AAC1D"}
@@ -1,10 +1,13 @@
1
1
  export * from './array';
2
2
  export * from './auxiliary-files';
3
+ export * from './base64';
4
+ export * from './bytes';
3
5
  export * from './caveats';
4
6
  export * from './checksum';
5
7
  export * from './cronjob';
6
8
  export * from './deep-clone';
7
9
  export * from './default-endowments';
10
+ export * from './derivation-paths';
8
11
  export * from './entropy';
9
12
  export * from './errors';
10
13
  export * from './handlers';
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.browser.ts"],"sourcesContent":["export * from './array';\nexport * from './auxiliary-files';\nexport * from './caveats';\nexport * from './checksum';\nexport * from './cronjob';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './errors';\nexport * from './handlers';\nexport * from './handler-types';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './localization';\nexport * from './logging';\nexport * from './manifest/index.browser';\nexport * from './namespace';\nexport * from './path';\nexport * from './snaps';\nexport * from './strings';\nexport * from './structs';\nexport * from './types';\nexport * from './ui';\nexport * from './validation';\nexport * from './versions';\nexport * from './virtual-file/index.browser';\n"],"names":[],"mappings":"AAAA,cAAc,UAAU;AACxB,cAAc,oBAAoB;AAClC,cAAc,YAAY;AAC1B,cAAc,aAAa;AAC3B,cAAc,YAAY;AAC1B,cAAc,eAAe;AAC7B,cAAc,uBAAuB;AACrC,cAAc,YAAY;AAC1B,cAAc,WAAW;AACzB,cAAc,aAAa;AAC3B,cAAc,kBAAkB;AAChC,cAAc,WAAW;AACzB,cAAc,SAAS;AACvB,cAAc,aAAa;AAC3B,cAAc,iBAAiB;AAC/B,cAAc,YAAY;AAC1B,cAAc,2BAA2B;AACzC,cAAc,cAAc;AAC5B,cAAc,SAAS;AACvB,cAAc,UAAU;AACxB,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,UAAU;AACxB,cAAc,OAAO;AACrB,cAAc,eAAe;AAC7B,cAAc,aAAa;AAC3B,cAAc,+BAA+B"}
1
+ {"version":3,"sources":["../../src/index.browser.ts"],"sourcesContent":["export * from './array';\nexport * from './auxiliary-files';\nexport * from './base64';\nexport * from './bytes';\nexport * from './caveats';\nexport * from './checksum';\nexport * from './cronjob';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './derivation-paths';\nexport * from './entropy';\nexport * from './errors';\nexport * from './handlers';\nexport * from './handler-types';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './localization';\nexport * from './logging';\nexport * from './manifest/index.browser';\nexport * from './namespace';\nexport * from './path';\nexport * from './snaps';\nexport * from './strings';\nexport * from './structs';\nexport * from './types';\nexport * from './ui';\nexport * from './validation';\nexport * from './versions';\nexport * from './virtual-file/index.browser';\n"],"names":[],"mappings":"AAAA,cAAc,UAAU;AACxB,cAAc,oBAAoB;AAClC,cAAc,WAAW;AACzB,cAAc,UAAU;AACxB,cAAc,YAAY;AAC1B,cAAc,aAAa;AAC3B,cAAc,YAAY;AAC1B,cAAc,eAAe;AAC7B,cAAc,uBAAuB;AACrC,cAAc,qBAAqB;AACnC,cAAc,YAAY;AAC1B,cAAc,WAAW;AACzB,cAAc,aAAa;AAC3B,cAAc,kBAAkB;AAChC,cAAc,WAAW;AACzB,cAAc,SAAS;AACvB,cAAc,aAAa;AAC3B,cAAc,iBAAiB;AAC/B,cAAc,YAAY;AAC1B,cAAc,2BAA2B;AACzC,cAAc,cAAc;AAC5B,cAAc,SAAS;AACvB,cAAc,UAAU;AACxB,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,UAAU;AACxB,cAAc,OAAO;AACrB,cAAc,eAAe;AAC7B,cAAc,aAAa;AAC3B,cAAc,+BAA+B"}
package/dist/esm/index.js CHANGED
@@ -1,10 +1,13 @@
1
1
  export * from './array';
2
2
  export * from './auxiliary-files';
3
+ export * from './base64';
4
+ export * from './bytes';
3
5
  export * from './caveats';
4
6
  export * from './cronjob';
5
7
  export * from './checksum';
6
8
  export * from './deep-clone';
7
9
  export * from './default-endowments';
10
+ export * from './derivation-paths';
8
11
  export * from './entropy';
9
12
  export * from './eval';
10
13
  export * from './errors';
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './array';\nexport * from './auxiliary-files';\nexport * from './caveats';\nexport * from './cronjob';\nexport * from './checksum';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './eval';\nexport * from './errors';\nexport * from './fs';\nexport * from './handlers';\nexport * from './handler-types';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './localization';\nexport * from './logging';\nexport * from './manifest';\nexport * from './mock';\nexport * from './namespace';\nexport * from './npm';\nexport * from './path';\nexport * from './post-process';\nexport * from './snaps';\nexport * from './strings';\nexport * from './structs';\nexport * from './types';\nexport * from './ui';\nexport * from './validation';\nexport * from './versions';\nexport * from './virtual-file';\n"],"names":[],"mappings":"AAAA,cAAc,UAAU;AACxB,cAAc,oBAAoB;AAClC,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,aAAa;AAC3B,cAAc,eAAe;AAC7B,cAAc,uBAAuB;AACrC,cAAc,YAAY;AAC1B,cAAc,SAAS;AACvB,cAAc,WAAW;AACzB,cAAc,OAAO;AACrB,cAAc,aAAa;AAC3B,cAAc,kBAAkB;AAChC,cAAc,WAAW;AACzB,cAAc,SAAS;AACvB,cAAc,aAAa;AAC3B,cAAc,iBAAiB;AAC/B,cAAc,YAAY;AAC1B,cAAc,aAAa;AAC3B,cAAc,SAAS;AACvB,cAAc,cAAc;AAC5B,cAAc,QAAQ;AACtB,cAAc,SAAS;AACvB,cAAc,iBAAiB;AAC/B,cAAc,UAAU;AACxB,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,UAAU;AACxB,cAAc,OAAO;AACrB,cAAc,eAAe;AAC7B,cAAc,aAAa;AAC3B,cAAc,iBAAiB"}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './array';\nexport * from './auxiliary-files';\nexport * from './base64';\nexport * from './bytes';\nexport * from './caveats';\nexport * from './cronjob';\nexport * from './checksum';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './derivation-paths';\nexport * from './entropy';\nexport * from './eval';\nexport * from './errors';\nexport * from './fs';\nexport * from './handlers';\nexport * from './handler-types';\nexport * from './iframe';\nexport * from './json';\nexport * from './json-rpc';\nexport * from './localization';\nexport * from './logging';\nexport * from './manifest';\nexport * from './mock';\nexport * from './namespace';\nexport * from './npm';\nexport * from './path';\nexport * from './post-process';\nexport * from './snaps';\nexport * from './strings';\nexport * from './structs';\nexport * from './types';\nexport * from './ui';\nexport * from './validation';\nexport * from './versions';\nexport * from './virtual-file';\n"],"names":[],"mappings":"AAAA,cAAc,UAAU;AACxB,cAAc,oBAAoB;AAClC,cAAc,WAAW;AACzB,cAAc,UAAU;AACxB,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,aAAa;AAC3B,cAAc,eAAe;AAC7B,cAAc,uBAAuB;AACrC,cAAc,qBAAqB;AACnC,cAAc,YAAY;AAC1B,cAAc,SAAS;AACvB,cAAc,WAAW;AACzB,cAAc,OAAO;AACrB,cAAc,aAAa;AAC3B,cAAc,kBAAkB;AAChC,cAAc,WAAW;AACzB,cAAc,SAAS;AACvB,cAAc,aAAa;AAC3B,cAAc,iBAAiB;AAC/B,cAAc,YAAY;AAC1B,cAAc,aAAa;AAC3B,cAAc,SAAS;AACvB,cAAc,cAAc;AAC5B,cAAc,QAAQ;AACtB,cAAc,SAAS;AACvB,cAAc,iBAAiB;AAC/B,cAAc,UAAU;AACxB,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,UAAU;AACxB,cAAc,OAAO;AACrB,cAAc,eAAe;AAC7B,cAAc,aAAa;AAC3B,cAAc,iBAAiB"}
@@ -30,6 +30,7 @@ export class VirtualFile {
30
30
  } else if (this.value instanceof Uint8Array && encoding === 'hex') {
31
31
  return bytesToHex(this.value);
32
32
  } else if (this.value instanceof Uint8Array && encoding === 'base64') {
33
+ // For large files, this is quite slow, instead use `encodeBase64()`
33
34
  // TODO: Use @metamask/utils for this
34
35
  return base64.encode(this.value);
35
36
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/virtual-file/VirtualFile.ts"],"sourcesContent":["// TODO(ritave): Move into separate package @metamask/vfile / @metamask/utils + @metamask/to-vfile when passes code review\n// TODO(ritave): Streaming vfile contents similar to vinyl maybe?\n// TODO(ritave): Move fixing manifest in cli and bundler plugins to write messages to vfile\n// similar to unified instead of throwing \"ProgrammaticallyFixableErrors\".\n//\n// Using https://github.com/vfile/vfile would be helpful, but they only support ESM and we need to support CommonJS.\n// https://github.com/gulpjs/vinyl is also good, but they normalize paths, which we can't do, because\n// we're calculating checksums based on original path.\nimport { assert, bytesToHex } from '@metamask/utils';\nimport { base64 } from '@scure/base';\n\nimport { deepClone } from '../deep-clone';\n\n/**\n * This map registers the type of the {@link VirtualFile.data} key of a {@link VirtualFile}.\n *\n * This type can be augmented to register custom `data` types.\n *\n * @example\n * declare module '@metamask/snaps-utils' {\n * interface DataMap {\n * // `file.data.name` is typed as `string`\n * name: string\n * }\n * }\n */\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-empty-interface\nexport interface DataMap {}\n\nexport type Value = string | Uint8Array;\nexport type Compatible<Result = unknown> =\n | string\n | Uint8Array\n | Options<Result>;\nexport type Data = Record<string, unknown> & Partial<DataMap>;\nexport type Options<Result = unknown> = {\n value: Value;\n path?: string;\n data?: Data;\n result?: Result;\n};\n\nexport class VirtualFile<Result = unknown> {\n constructor(value?: Compatible<Result>) {\n let options: Options | undefined;\n if (typeof value === 'string' || value instanceof Uint8Array) {\n options = { value };\n } else {\n options = value;\n }\n\n this.value = options?.value ?? '';\n // This situations happens when there's no .result used,\n // we expect the file to have default generic in that situation:\n // VirtualFile<unknown> which will handle undefined properly\n //\n // While not 100% type safe, it'll be way less frustrating to work with.\n // The alternative would be to have VirtualFile.result be Result | undefined\n // and that would result in needing to branch out and check in all situations.\n //\n // In short, optimizing for most common use case.\n this.result = options?.result ?? (undefined as any);\n this.data = options?.data ?? {};\n this.path = options?.path ?? '/';\n }\n\n value: Value;\n\n result: Result;\n\n data: Data;\n\n path: string;\n\n toString(encoding?: string) {\n if (typeof this.value === 'string') {\n assert(encoding === undefined, 'Tried to encode string.');\n return this.value;\n } else if (this.value instanceof Uint8Array && encoding === 'hex') {\n return bytesToHex(this.value);\n } else if (this.value instanceof Uint8Array && encoding === 'base64') {\n // TODO: Use @metamask/utils for this\n return base64.encode(this.value);\n }\n const decoder = new TextDecoder(encoding);\n return decoder.decode(this.value);\n }\n\n clone() {\n const vfile = new VirtualFile<Result>();\n if (typeof this.value === 'string') {\n vfile.value = this.value;\n } else {\n // deep-clone doesn't clone Buffer properly, even if it's a sub-class of Uint8Array\n vfile.value = this.value.slice(0);\n }\n vfile.result = deepClone(this.result);\n vfile.data = deepClone(this.data);\n vfile.path = this.path;\n return vfile;\n }\n}\n"],"names":["assert","bytesToHex","base64","deepClone","VirtualFile","toString","encoding","value","undefined","Uint8Array","encode","decoder","TextDecoder","decode","clone","vfile","slice","result","data","path","constructor","options"],"mappings":"AAAA,0HAA0H;AAC1H,iEAAiE;AACjE,2FAA2F;AAC3F,wFAAwF;AACxF,EAAE;AAC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UAAU,YAAYA,iBAAiBE,YAAY;YAC5DY,UAAU;gBAAEd;YAAM;QACpB,OAAO;YACLc,UAAUd;QACZ;QAEA,IAAI,CAACA,KAAK,GAAGc,SAASd,SAAS;QAC/B,wDAAwD;QACxD,gEAAgE;QAChE,4DAA4D;QAC5D,EAAE;QACF,wEAAwE;QACxE,4EAA4E;QAC5E,8EAA8E;QAC9E,EAAE;QACF,iDAAiD;QACjD,IAAI,CAACU,MAAM,GAAGI,SAASJ,UAAWT;QAClC,IAAI,CAACU,IAAI,GAAGG,SAASH,QAAQ,CAAC;QAC9B,IAAI,CAACC,IAAI,GAAGE,SAASF,QAAQ;IAC/B;AAqCF"}
1
+ {"version":3,"sources":["../../../src/virtual-file/VirtualFile.ts"],"sourcesContent":["// TODO(ritave): Move into separate package @metamask/vfile / @metamask/utils + @metamask/to-vfile when passes code review\n// TODO(ritave): Streaming vfile contents similar to vinyl maybe?\n// TODO(ritave): Move fixing manifest in cli and bundler plugins to write messages to vfile\n// similar to unified instead of throwing \"ProgrammaticallyFixableErrors\".\n//\n// Using https://github.com/vfile/vfile would be helpful, but they only support ESM and we need to support CommonJS.\n// https://github.com/gulpjs/vinyl is also good, but they normalize paths, which we can't do, because\n// we're calculating checksums based on original path.\nimport { assert, bytesToHex } from '@metamask/utils';\nimport { base64 } from '@scure/base';\n\nimport { deepClone } from '../deep-clone';\n\n/**\n * This map registers the type of the {@link VirtualFile.data} key of a {@link VirtualFile}.\n *\n * This type can be augmented to register custom `data` types.\n *\n * @example\n * declare module '@metamask/snaps-utils' {\n * interface DataMap {\n * // `file.data.name` is typed as `string`\n * name: string\n * }\n * }\n */\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-empty-interface\nexport interface DataMap {}\n\nexport type Value = string | Uint8Array;\nexport type Compatible<Result = unknown> =\n | string\n | Uint8Array\n | Options<Result>;\nexport type Data = Record<string, unknown> & Partial<DataMap>;\nexport type Options<Result = unknown> = {\n value: Value;\n path?: string;\n data?: Data;\n result?: Result;\n};\n\nexport class VirtualFile<Result = unknown> {\n constructor(value?: Compatible<Result>) {\n let options: Options | undefined;\n if (typeof value === 'string' || value instanceof Uint8Array) {\n options = { value };\n } else {\n options = value;\n }\n\n this.value = options?.value ?? '';\n // This situations happens when there's no .result used,\n // we expect the file to have default generic in that situation:\n // VirtualFile<unknown> which will handle undefined properly\n //\n // While not 100% type safe, it'll be way less frustrating to work with.\n // The alternative would be to have VirtualFile.result be Result | undefined\n // and that would result in needing to branch out and check in all situations.\n //\n // In short, optimizing for most common use case.\n this.result = options?.result ?? (undefined as any);\n this.data = options?.data ?? {};\n this.path = options?.path ?? '/';\n }\n\n value: Value;\n\n result: Result;\n\n data: Data;\n\n path: string;\n\n toString(encoding?: string) {\n if (typeof this.value === 'string') {\n assert(encoding === undefined, 'Tried to encode string.');\n return this.value;\n } else if (this.value instanceof Uint8Array && encoding === 'hex') {\n return bytesToHex(this.value);\n } else if (this.value instanceof Uint8Array && encoding === 'base64') {\n // For large files, this is quite slow, instead use `encodeBase64()`\n // TODO: Use @metamask/utils for this\n return base64.encode(this.value);\n }\n const decoder = new TextDecoder(encoding);\n return decoder.decode(this.value);\n }\n\n clone() {\n const vfile = new VirtualFile<Result>();\n if (typeof this.value === 'string') {\n vfile.value = this.value;\n } else {\n // deep-clone doesn't clone Buffer properly, even if it's a sub-class of Uint8Array\n vfile.value = this.value.slice(0);\n }\n vfile.result = deepClone(this.result);\n vfile.data = deepClone(this.data);\n vfile.path = this.path;\n return vfile;\n }\n}\n"],"names":["assert","bytesToHex","base64","deepClone","VirtualFile","toString","encoding","value","undefined","Uint8Array","encode","decoder","TextDecoder","decode","clone","vfile","slice","result","data","path","constructor","options"],"mappings":"AAAA,0HAA0H;AAC1H,iEAAiE;AACjE,2FAA2F;AAC3F,wFAAwF;AACxF,EAAE;AACF,oHAAoH;AACpH,qGAAqG;AACrG,sDAAsD;;;;;;;;;;;;;;AACtD,SAASA,MAAM,EAAEC,UAAU,QAAQ,kB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;QACZ;QAEA,IAAI,CAACA,KAAK,GAAGc,SAASd,SAAS;QAC/B,wDAAwD;QACxD,gEAAgE;QAChE,4DAA4D;QAC5D,EAAE;QACF,wEAAwE;QACxE,4EAA4E;QAC5E,8EAA8E;QAC9E,EAAE;QACF,iDAAiD;QACjD,IAAI,CAACU,MAAM,GAAGI,SAASJ,UAAWT;QAClC,IAAI,CAACU,IAAI,GAAGG,SAASH,QAAQ,CAAC;QAC9B,IAAI,CAACC,IAAI,GAAGE,SAASF,QAAQ;IAC/B;AAsCF"}
@@ -6,4 +6,4 @@ import { AuxiliaryFileEncoding } from '@metamask/snaps-sdk';
6
6
  * @param encoding - The chosen encoding.
7
7
  * @returns The file encoded in the requested encoding.
8
8
  */
9
- export declare function encodeAuxiliaryFile(value: string, encoding: AuxiliaryFileEncoding): string;
9
+ export declare function encodeAuxiliaryFile(value: string, encoding: AuxiliaryFileEncoding): Promise<string>;
@@ -0,0 +1,15 @@
1
+ import type { VirtualFile } from './virtual-file';
2
+ /**
3
+ * Provides fast, asynchronous base64 encoding.
4
+ *
5
+ * @param input - The input value, assumed to be coercable to bytes.
6
+ * @returns A base64 string.
7
+ */
8
+ export declare function encodeBase64(input: Uint8Array | VirtualFile | string): Promise<unknown>;
9
+ /**
10
+ * Provides fast, asynchronous base64 decoding.
11
+ *
12
+ * @param base64 - A base64 string.
13
+ * @returns A Uint8Array of bytes.
14
+ */
15
+ export declare function decodeBase64(base64: string): Promise<Uint8Array>;
@@ -0,0 +1,8 @@
1
+ import { VirtualFile } from './virtual-file';
2
+ /**
3
+ * Convert a bytes-like input value to a Uint8Array.
4
+ *
5
+ * @param bytes - A bytes-like value.
6
+ * @returns The input value converted to a Uint8Array if necessary.
7
+ */
8
+ export declare function getBytes(bytes: VirtualFile | Uint8Array | string): Uint8Array;
@@ -1,11 +1,4 @@
1
- import { VirtualFile } from './virtual-file/VirtualFile';
2
- /**
3
- * Convert an input value to a Uint8Array for use in a checksum.
4
- *
5
- * @param bytes - A value to use for a checksum calculation.
6
- * @returns The input value converted to a Uint8Array if necessary.
7
- */
8
- export declare function getChecksumBytes(bytes: VirtualFile | Uint8Array | string): Uint8Array;
1
+ import type { VirtualFile } from './virtual-file/VirtualFile';
9
2
  /**
10
3
  * Calculates checksum for a single byte array.
11
4
  *
@@ -0,0 +1,24 @@
1
+ import type { SupportedCurve } from '@metamask/key-tree';
2
+ export declare type SnapsDerivationPath = {
3
+ path: ['m', ...string[]];
4
+ curve: SupportedCurve;
5
+ name: string;
6
+ };
7
+ export declare const SNAPS_DERIVATION_PATHS: SnapsDerivationPath[];
8
+ /**
9
+ * Gets the name of a derivation path supported by snaps.
10
+ *
11
+ * @param path - The derivation path.
12
+ * @param curve - The curve used to derive the keys.
13
+ * @returns The name of the derivation path, otherwise null.
14
+ */
15
+ export declare function getSnapDerivationPathName(path: SnapsDerivationPath['path'], curve: SupportedCurve): string | null;
16
+ /**
17
+ * Gets the name of the SLIP-44 protocol corresponding to the specified
18
+ * `coin_type`.
19
+ *
20
+ * @param coinType - The SLIP-44 `coin_type` value whose name
21
+ * to retrieve.
22
+ * @returns The name of the protocol, otherwise null.
23
+ */
24
+ export declare function getSlip44ProtocolName(coinType: number | string): string;
@@ -1,10 +1,13 @@
1
1
  export * from './array';
2
2
  export * from './auxiliary-files';
3
+ export * from './base64';
4
+ export * from './bytes';
3
5
  export * from './caveats';
4
6
  export * from './checksum';
5
7
  export * from './cronjob';
6
8
  export * from './deep-clone';
7
9
  export * from './default-endowments';
10
+ export * from './derivation-paths';
8
11
  export * from './entropy';
9
12
  export * from './errors';
10
13
  export * from './handlers';
@@ -1,10 +1,13 @@
1
1
  export * from './array';
2
2
  export * from './auxiliary-files';
3
+ export * from './base64';
4
+ export * from './bytes';
3
5
  export * from './caveats';
4
6
  export * from './cronjob';
5
7
  export * from './checksum';
6
8
  export * from './deep-clone';
7
9
  export * from './default-endowments';
10
+ export * from './derivation-paths';
8
11
  export * from './entropy';
9
12
  export * from './eval';
10
13
  export * from './errors';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-utils",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/MetaMask/snaps.git"
@@ -67,12 +67,13 @@
67
67
  "dependencies": {
68
68
  "@babel/core": "^7.23.2",
69
69
  "@babel/types": "^7.23.0",
70
- "@metamask/base-controller": "^3.2.0",
70
+ "@metamask/base-controller": "^4.0.0",
71
71
  "@metamask/key-tree": "^9.0.0",
72
- "@metamask/permission-controller": "^5.0.1",
72
+ "@metamask/permission-controller": "^6.0.0",
73
73
  "@metamask/rpc-errors": "^6.1.0",
74
- "@metamask/snaps-registry": "^2.1.0",
75
- "@metamask/snaps-sdk": "^1.2.0",
74
+ "@metamask/slip44": "^3.1.0",
75
+ "@metamask/snaps-registry": "^3.0.0",
76
+ "@metamask/snaps-sdk": "^1.3.0",
76
77
  "@metamask/utils": "^8.2.1",
77
78
  "@noble/hashes": "^1.3.1",
78
79
  "@scure/base": "^1.1.1",