@metamask/snaps-rpc-methods 3.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.
- package/CHANGELOG.md +60 -0
- package/LICENSE +18 -0
- package/README.md +3 -0
- package/dist/cjs/index.js +43 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/permitted/common/snapInstallation.js +33 -0
- package/dist/cjs/permitted/common/snapInstallation.js.map +1 -0
- package/dist/cjs/permitted/getFile.js +63 -0
- package/dist/cjs/permitted/getFile.js.map +1 -0
- package/dist/cjs/permitted/getSnaps.js +39 -0
- package/dist/cjs/permitted/getSnaps.js.map +1 -0
- package/dist/cjs/permitted/handlers.js +33 -0
- package/dist/cjs/permitted/handlers.js.map +1 -0
- package/dist/cjs/permitted/index.js +21 -0
- package/dist/cjs/permitted/index.js.map +1 -0
- package/dist/cjs/permitted/invokeKeyring.js +88 -0
- package/dist/cjs/permitted/invokeKeyring.js.map +1 -0
- package/dist/cjs/permitted/invokeSnapSugar.js +62 -0
- package/dist/cjs/permitted/invokeSnapSugar.js.map +1 -0
- package/dist/cjs/permitted/middleware.js +38 -0
- package/dist/cjs/permitted/middleware.js.map +1 -0
- package/dist/cjs/permitted/requestSnaps.js +122 -0
- package/dist/cjs/permitted/requestSnaps.js.map +1 -0
- package/dist/cjs/request.js +6 -0
- package/dist/cjs/request.js.map +1 -0
- package/dist/cjs/restricted/caveats/index.js +38 -0
- package/dist/cjs/restricted/caveats/index.js.map +1 -0
- package/dist/cjs/restricted/caveats/permittedCoinTypes.js +83 -0
- package/dist/cjs/restricted/caveats/permittedCoinTypes.js.map +1 -0
- package/dist/cjs/restricted/caveats/permittedDerivationPaths.js +67 -0
- package/dist/cjs/restricted/caveats/permittedDerivationPaths.js.map +1 -0
- package/dist/cjs/restricted/caveats/snapIds.js +59 -0
- package/dist/cjs/restricted/caveats/snapIds.js.map +1 -0
- package/dist/cjs/restricted/dialog.js +150 -0
- package/dist/cjs/restricted/dialog.js.map +1 -0
- package/dist/cjs/restricted/getBip32Entropy.js +76 -0
- package/dist/cjs/restricted/getBip32Entropy.js.map +1 -0
- package/dist/cjs/restricted/getBip32PublicKey.js +91 -0
- package/dist/cjs/restricted/getBip32PublicKey.js.map +1 -0
- package/dist/cjs/restricted/getBip44Entropy.js +77 -0
- package/dist/cjs/restricted/getBip44Entropy.js.map +1 -0
- package/dist/cjs/restricted/getEntropy.js +76 -0
- package/dist/cjs/restricted/getEntropy.js.map +1 -0
- package/dist/cjs/restricted/getLocale.js +49 -0
- package/dist/cjs/restricted/getLocale.js.map +1 -0
- package/dist/cjs/restricted/index.js +65 -0
- package/dist/cjs/restricted/index.js.map +1 -0
- package/dist/cjs/restricted/invokeSnap.js +99 -0
- package/dist/cjs/restricted/invokeSnap.js.map +1 -0
- package/dist/cjs/restricted/manageAccounts.js +68 -0
- package/dist/cjs/restricted/manageAccounts.js.map +1 -0
- package/dist/cjs/restricted/manageState.js +216 -0
- package/dist/cjs/restricted/manageState.js.map +1 -0
- package/dist/cjs/restricted/notify.js +94 -0
- package/dist/cjs/restricted/notify.js.map +1 -0
- package/dist/cjs/utils.js +98 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/permitted/common/snapInstallation.js +33 -0
- package/dist/esm/permitted/common/snapInstallation.js.map +1 -0
- package/dist/esm/permitted/getFile.js +45 -0
- package/dist/esm/permitted/getFile.js.map +1 -0
- package/dist/esm/permitted/getSnaps.js +31 -0
- package/dist/esm/permitted/getSnaps.js.map +1 -0
- package/dist/esm/permitted/handlers.js +15 -0
- package/dist/esm/permitted/handlers.js.map +1 -0
- package/dist/esm/permitted/index.js +4 -0
- package/dist/esm/permitted/index.js.map +1 -0
- package/dist/esm/permitted/invokeKeyring.js +80 -0
- package/dist/esm/permitted/invokeKeyring.js.map +1 -0
- package/dist/esm/permitted/invokeSnapSugar.js +60 -0
- package/dist/esm/permitted/invokeSnapSugar.js.map +1 -0
- package/dist/esm/permitted/middleware.js +34 -0
- package/dist/esm/permitted/middleware.js.map +1 -0
- package/dist/esm/permitted/requestSnaps.js +117 -0
- package/dist/esm/permitted/requestSnaps.js.map +1 -0
- package/dist/esm/request.js +3 -0
- package/dist/esm/request.js.map +1 -0
- package/dist/esm/restricted/caveats/index.js +20 -0
- package/dist/esm/restricted/caveats/index.js.map +1 -0
- package/dist/esm/restricted/caveats/permittedCoinTypes.js +77 -0
- package/dist/esm/restricted/caveats/permittedCoinTypes.js.map +1 -0
- package/dist/esm/restricted/caveats/permittedDerivationPaths.js +64 -0
- package/dist/esm/restricted/caveats/permittedDerivationPaths.js.map +1 -0
- package/dist/esm/restricted/caveats/snapIds.js +50 -0
- package/dist/esm/restricted/caveats/snapIds.js.map +1 -0
- package/dist/esm/restricted/dialog.js +137 -0
- package/dist/esm/restricted/dialog.js.map +1 -0
- package/dist/esm/restricted/getBip32Entropy.js +67 -0
- package/dist/esm/restricted/getBip32Entropy.js.map +1 -0
- package/dist/esm/restricted/getBip32PublicKey.js +79 -0
- package/dist/esm/restricted/getBip32PublicKey.js.map +1 -0
- package/dist/esm/restricted/getBip44Entropy.js +70 -0
- package/dist/esm/restricted/getBip44Entropy.js.map +1 -0
- package/dist/esm/restricted/getEntropy.js +58 -0
- package/dist/esm/restricted/getEntropy.js.map +1 -0
- package/dist/esm/restricted/getLocale.js +42 -0
- package/dist/esm/restricted/getLocale.js.map +1 -0
- package/dist/esm/restricted/index.js +29 -0
- package/dist/esm/restricted/index.js.map +1 -0
- package/dist/esm/restricted/invokeSnap.js +89 -0
- package/dist/esm/restricted/invokeSnap.js.map +1 -0
- package/dist/esm/restricted/manageAccounts.js +60 -0
- package/dist/esm/restricted/manageAccounts.js.map +1 -0
- package/dist/esm/restricted/manageState.js +221 -0
- package/dist/esm/restricted/manageState.js.map +1 -0
- package/dist/esm/restricted/notify.js +89 -0
- package/dist/esm/restricted/notify.js.map +1 -0
- package/dist/esm/utils.js +124 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/types/__fixtures__/entropy.d.ts +14 -0
- package/dist/types/__fixtures__/index.d.ts +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/permitted/common/snapInstallation.d.ts +16 -0
- package/dist/types/permitted/getFile.d.ts +14 -0
- package/dist/types/permitted/getSnaps.d.ts +13 -0
- package/dist/types/permitted/handlers.d.ts +14 -0
- package/dist/types/permitted/index.d.ts +5 -0
- package/dist/types/permitted/invokeKeyring.d.ts +17 -0
- package/dist/types/permitted/invokeSnapSugar.d.ts +32 -0
- package/dist/types/permitted/middleware.d.ts +10 -0
- package/dist/types/permitted/requestSnaps.d.ts +50 -0
- package/dist/types/request.d.ts +99 -0
- package/dist/types/restricted/caveats/index.d.ts +8 -0
- package/dist/types/restricted/caveats/permittedCoinTypes.d.ts +29 -0
- package/dist/types/restricted/caveats/permittedDerivationPaths.d.ts +32 -0
- package/dist/types/restricted/caveats/snapIds.d.ts +21 -0
- package/dist/types/restricted/dialog.d.ts +113 -0
- package/dist/types/restricted/getBip32Entropy.d.ts +45 -0
- package/dist/types/restricted/getBip32PublicKey.d.ts +60 -0
- package/dist/types/restricted/getBip44Entropy.d.ts +49 -0
- package/dist/types/restricted/getEntropy.d.ts +60 -0
- package/dist/types/restricted/getLocale.d.ts +47 -0
- package/dist/types/restricted/index.d.ts +232 -0
- package/dist/types/restricted/invokeSnap.d.ts +62 -0
- package/dist/types/restricted/manageAccounts.d.ts +63 -0
- package/dist/types/restricted/manageState.d.ts +127 -0
- package/dist/types/restricted/notify.d.ts +81 -0
- package/dist/types/utils.d.ts +94 -0
- package/package.json +88 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { SLIP10Node } from '@metamask/key-tree';
|
|
2
|
+
import { add0x, assert, concatBytes, createDataView, stringToBytes } from '@metamask/utils';
|
|
3
|
+
import { keccak_256 as keccak256 } from '@noble/hashes/sha3';
|
|
4
|
+
const HARDENED_VALUE = 0x80000000;
|
|
5
|
+
/**
|
|
6
|
+
* Returns the subset of the specified `hooks` that are included in the
|
|
7
|
+
* `hookNames` object. This is a Principle of Least Authority (POLA) measure
|
|
8
|
+
* to ensure that each RPC method implementation only has access to the
|
|
9
|
+
* API "hooks" it needs to do its job.
|
|
10
|
+
*
|
|
11
|
+
* @param hooks - The hooks to select from.
|
|
12
|
+
* @param hookNames - The names of the hooks to select.
|
|
13
|
+
* @returns The selected hooks.
|
|
14
|
+
* @template Hooks - The hooks to select from.
|
|
15
|
+
* @template HookName - The names of the hooks to select.
|
|
16
|
+
*/ export function selectHooks(hooks, hookNames) {
|
|
17
|
+
if (hookNames) {
|
|
18
|
+
return Object.keys(hookNames).reduce((hookSubset, _hookName)=>{
|
|
19
|
+
const hookName = _hookName;
|
|
20
|
+
hookSubset[hookName] = hooks[hookName];
|
|
21
|
+
return hookSubset;
|
|
22
|
+
}, {});
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Get a BIP-32 derivation path array from a hash, which is compatible with
|
|
28
|
+
* `@metamask/key-tree`. The hash is assumed to be 32 bytes long.
|
|
29
|
+
*
|
|
30
|
+
* @param hash - The hash to derive indices from.
|
|
31
|
+
* @returns The derived indices as a {@link HardenedBIP32Node} array.
|
|
32
|
+
*/ function getDerivationPathArray(hash) {
|
|
33
|
+
const array = [];
|
|
34
|
+
const view = createDataView(hash);
|
|
35
|
+
for(let index = 0; index < 8; index++){
|
|
36
|
+
const uint32 = view.getUint32(index * 4);
|
|
37
|
+
// This is essentially `index | 0x80000000`. Because JavaScript numbers are
|
|
38
|
+
// signed, we use the bitwise unsigned right shift operator to ensure that
|
|
39
|
+
// the result is a positive number.
|
|
40
|
+
// eslint-disable-next-line no-bitwise
|
|
41
|
+
const pathIndex = (uint32 | HARDENED_VALUE) >>> 0;
|
|
42
|
+
array.push(`bip32:${pathIndex - HARDENED_VALUE}'`);
|
|
43
|
+
}
|
|
44
|
+
return array;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Derive entropy from the given mnemonic phrase and salt.
|
|
48
|
+
*
|
|
49
|
+
* This is based on the reference implementation of
|
|
50
|
+
* [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).
|
|
51
|
+
*
|
|
52
|
+
* @param options - The options for entropy derivation.
|
|
53
|
+
* @param options.input - The input value to derive entropy from.
|
|
54
|
+
* @param options.salt - An optional salt to use when deriving entropy.
|
|
55
|
+
* @param options.mnemonicPhrase - The mnemonic phrase to use for entropy
|
|
56
|
+
* derivation.
|
|
57
|
+
* @param options.magic - A hardened BIP-32 index, which is used to derive the
|
|
58
|
+
* root key from the mnemonic phrase.
|
|
59
|
+
* @returns The derived entropy.
|
|
60
|
+
*/ export async function deriveEntropy({ input, salt = '', mnemonicPhrase, magic }) {
|
|
61
|
+
const inputBytes = stringToBytes(input);
|
|
62
|
+
const saltBytes = stringToBytes(salt);
|
|
63
|
+
// Get the derivation path from the snap ID.
|
|
64
|
+
const hash = keccak256(concatBytes([
|
|
65
|
+
inputBytes,
|
|
66
|
+
keccak256(saltBytes)
|
|
67
|
+
]));
|
|
68
|
+
const computedDerivationPath = getDerivationPathArray(hash);
|
|
69
|
+
// Derive the private key using BIP-32.
|
|
70
|
+
const { privateKey } = await SLIP10Node.fromDerivationPath({
|
|
71
|
+
derivationPath: [
|
|
72
|
+
mnemonicPhrase,
|
|
73
|
+
`bip32:${magic}`,
|
|
74
|
+
...computedDerivationPath
|
|
75
|
+
],
|
|
76
|
+
curve: 'secp256k1'
|
|
77
|
+
});
|
|
78
|
+
// This should never happen, but this keeps TypeScript happy.
|
|
79
|
+
assert(privateKey, 'Failed to derive the entropy.');
|
|
80
|
+
return add0x(privateKey);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Get the path prefix to use for key derivation in `key-tree`. This assumes the
|
|
84
|
+
* following:
|
|
85
|
+
*
|
|
86
|
+
* - The Secp256k1 curve always use the BIP-32 specification.
|
|
87
|
+
* - The Ed25519 curve always use the SLIP-10 specification.
|
|
88
|
+
*
|
|
89
|
+
* While this does not matter in most situations (no known case at the time of
|
|
90
|
+
* writing), `key-tree` requires a specific specification to be used.
|
|
91
|
+
*
|
|
92
|
+
* @param curve - The curve to get the path prefix for. The curve is NOT
|
|
93
|
+
* validated by this function.
|
|
94
|
+
* @returns The path prefix, i.e., `secp256k1` or `ed25519`.
|
|
95
|
+
*/ export function getPathPrefix(curve) {
|
|
96
|
+
if (curve === 'secp256k1') {
|
|
97
|
+
return 'bip32';
|
|
98
|
+
}
|
|
99
|
+
return 'slip10';
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Get a `key-tree`-compatible node.
|
|
103
|
+
*
|
|
104
|
+
* Note: This function assumes that all the parameters have been validated
|
|
105
|
+
* beforehand.
|
|
106
|
+
*
|
|
107
|
+
* @param options - The derivation options.
|
|
108
|
+
* @param options.curve - The curve to use for derivation.
|
|
109
|
+
* @param options.secretRecoveryPhrase - The secret recovery phrase to use for
|
|
110
|
+
* derivation.
|
|
111
|
+
* @param options.path - The derivation path to use as array, starting with an
|
|
112
|
+
* "m" as the first item.
|
|
113
|
+
*/ export async function getNode({ curve, secretRecoveryPhrase, path }) {
|
|
114
|
+
const prefix = getPathPrefix(curve);
|
|
115
|
+
return await SLIP10Node.fromDerivationPath({
|
|
116
|
+
curve,
|
|
117
|
+
derivationPath: [
|
|
118
|
+
secretRecoveryPhrase,
|
|
119
|
+
...path.slice(1).map((index)=>`${prefix}:${index}`)
|
|
120
|
+
]
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils.ts"],"sourcesContent":["import type {\n HardenedBIP32Node,\n BIP32Node,\n SLIP10PathNode,\n} from '@metamask/key-tree';\nimport { SLIP10Node } from '@metamask/key-tree';\nimport type { MagicValue } from '@metamask/snaps-utils';\nimport type { Hex } from '@metamask/utils';\nimport {\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\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/**\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 * @returns The derived entropy.\n */\nexport async function deriveEntropy({\n input,\n salt = '',\n mnemonicPhrase,\n magic,\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 derivationPath: [\n mnemonicPhrase,\n `bip32:${magic}`,\n ...computedDerivationPath,\n ],\n curve: 'secp256k1',\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 use the BIP-32 specification.\n * - The Ed25519 curve always use the SLIP-10 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., `secp256k1` or `ed25519`.\n */\nexport function getPathPrefix(\n curve: 'secp256k1' | 'ed25519',\n): 'bip32' | 'slip10' {\n if (curve === 'secp256k1') {\n return 'bip32';\n }\n\n return 'slip10';\n}\n\ntype GetNodeArgs = {\n curve: 'secp256k1' | 'ed25519';\n secretRecoveryPhrase: Uint8Array;\n path: string[];\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 */\nexport async function getNode({\n curve,\n secretRecoveryPhrase,\n path,\n}: GetNodeArgs) {\n const prefix = getPathPrefix(curve);\n return await SLIP10Node.fromDerivationPath({\n curve,\n derivationPath: [\n secretRecoveryPhrase,\n ...(path.slice(1).map((index) => `${prefix}:${index}`) as\n | BIP32Node[]\n | SLIP10PathNode[]),\n ],\n });\n}\n"],"names":["SLIP10Node","add0x","assert","concatBytes","createDataView","stringToBytes","keccak_256","keccak256","HARDENED_VALUE","selectHooks","hooks","hookNames","Object","keys","reduce","hookSubset","_hookName","hookName","undefined","getDerivationPathArray","hash","array","view","index","uint32","getUint32","pathIndex","push","deriveEntropy","input","salt","mnemonicPhrase","magic","inputBytes","saltBytes","computedDerivationPath","privateKey","fromDerivationPath","derivationPath","curve","getPathPrefix","getNode","secretRecoveryPhrase","path","prefix","slice","map"],"mappings":"AAKA,SAASA,UAAU,QAAQ,qBAAqB;AAGhD,SACEC,KAAK,EACLC,MAAM,EACNC,WAAW,EACXC,cAAc,EACdC,aAAa,QACR,kBAAkB;AACzB,SAASC,cAAcC,SAAS,QAAQ,qBAAqB;AAE7D,MAAMC,iBAAiB;AAWvB;;;;;;;;;;;CAWC,GACD,OAAO,SAASC,YAIdC,KAAY,EACZC,SAAqC;IAErC,IAAIA,WAAW;QACb,OAAOC,OAAOC,IAAI,CAACF,WAAWG,MAAM,CAClC,CAACC,YAAYC;YACX,MAAMC,WAAWD;YACjBD,UAAU,CAACE,SAAS,GAAGP,KAAK,CAACO,SAAS;YACtC,OAAOF;QACT,GACA,CAAC;IAEL;IACA,OAAOG;AACT;AAEA;;;;;;CAMC,GACD,SAASC,uBAAuBC,IAAgB;IAC9C,MAAMC,QAA6B,EAAE;IACrC,MAAMC,OAAOlB,eAAegB;IAE5B,IAAK,IAAIG,QAAQ,GAAGA,QAAQ,GAAGA,QAAS;QACtC,MAAMC,SAASF,KAAKG,SAAS,CAACF,QAAQ;QAEtC,2EAA2E;QAC3E,0EAA0E;QAC1E,mCAAmC;QACnC,sCAAsC;QACtC,MAAMG,YAAY,AAACF,CAAAA,SAAShB,cAAa,MAAO;QAChDa,MAAMM,IAAI,CAAC,CAAC,MAAM,EAAED,YAAYlB,eAAe,CAAC,CAAC;IACnD;IAEA,OAAOa;AACT;AAyBA;;;;;;;;;;;;;;CAcC,GACD,OAAO,eAAeO,cAAc,EAClCC,KAAK,EACLC,OAAO,EAAE,EACTC,cAAc,EACdC,KAAK,EACgB;IACrB,MAAMC,aAAa5B,cAAcwB;IACjC,MAAMK,YAAY7B,cAAcyB;IAEhC,4CAA4C;IAC5C,MAAMV,OAAOb,UAAUJ,YAAY;QAAC8B;QAAY1B,UAAU2B;KAAW;IACrE,MAAMC,yBAAyBhB,uBAAuBC;IAEtD,uCAAuC;IACvC,MAAM,EAAEgB,UAAU,EAAE,GAAG,MAAMpC,WAAWqC,kBAAkB,CAAC;QACzDC,gBAAgB;YACdP;YACA,CAAC,MAAM,EAAEC,MAAM,CAAC;eACbG;SACJ;QACDI,OAAO;IACT;IAEA,6DAA6D;IAC7DrC,OAAOkC,YAAY;IAEnB,OAAOnC,MAAMmC;AACf;AAEA;;;;;;;;;;;;;CAaC,GACD,OAAO,SAASI,cACdD,KAA8B;IAE9B,IAAIA,UAAU,aAAa;QACzB,OAAO;IACT;IAEA,OAAO;AACT;AAQA;;;;;;;;;;;;CAYC,GACD,OAAO,eAAeE,QAAQ,EAC5BF,KAAK,EACLG,oBAAoB,EACpBC,IAAI,EACQ;IACZ,MAAMC,SAASJ,cAAcD;IAC7B,OAAO,MAAMvC,WAAWqC,kBAAkB,CAAC;QACzCE;QACAD,gBAAgB;YACdI;eACIC,KAAKE,KAAK,CAAC,GAAGC,GAAG,CAAC,CAACvB,QAAU,CAAC,EAAEqB,OAAO,CAAC,EAAErB,MAAM,CAAC;SAGtD;IACH;AACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SIP-6 test vectors.
|
|
3
|
+
*/
|
|
4
|
+
export declare const ENTROPY_VECTORS: ({
|
|
5
|
+
snapId: string;
|
|
6
|
+
derivationPath: string;
|
|
7
|
+
entropy: string;
|
|
8
|
+
salt?: undefined;
|
|
9
|
+
} | {
|
|
10
|
+
snapId: string;
|
|
11
|
+
salt: string;
|
|
12
|
+
derivationPath: string;
|
|
13
|
+
entropy: string;
|
|
14
|
+
})[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './entropy';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type { PermittedRpcMethodHooks } from './permitted';
|
|
2
|
+
export { handlers as permittedMethods, createSnapsMethodMiddleware, } from './permitted';
|
|
3
|
+
export * from './restricted';
|
|
4
|
+
export { SnapCaveatType } from '@metamask/snaps-utils';
|
|
5
|
+
export { selectHooks } from './utils';
|
|
6
|
+
export type { RequestFunction, SnapsGlobalObject } from './request';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { RequestedPermissions } from '@metamask/permission-controller';
|
|
2
|
+
import type { InstallSnapsResult } from '@metamask/snaps-utils';
|
|
3
|
+
export type { InstallSnapsResult } from '@metamask/snaps-utils';
|
|
4
|
+
export declare type InstallSnapsHook = (requestedSnaps: RequestedPermissions) => Promise<InstallSnapsResult>;
|
|
5
|
+
/**
|
|
6
|
+
* Typechecks the requested snaps and passes them to the permissions
|
|
7
|
+
* controller for installation.
|
|
8
|
+
*
|
|
9
|
+
* @param requestedSnaps - An object containing the requested snaps to be installed. The key of the
|
|
10
|
+
* object is the snap id and the value is potential extra data, i.e. version.
|
|
11
|
+
* @param installSnaps - A function that tries to install a given snap, prompting the user if
|
|
12
|
+
* necessary.
|
|
13
|
+
* @returns An object containing the installed snaps.
|
|
14
|
+
* @throws If the params are invalid or the snap installation fails.
|
|
15
|
+
*/
|
|
16
|
+
export declare function handleInstallSnaps(requestedSnaps: RequestedPermissions, installSnaps: InstallSnapsHook): Promise<InstallSnapsResult>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from '@metamask/permission-controller';
|
|
2
|
+
import type { Infer } from 'superstruct';
|
|
3
|
+
export declare const GetFileArgsStruct: import("superstruct").Struct<{
|
|
4
|
+
path: string;
|
|
5
|
+
encoding?: "base64" | "utf8" | "hex" | undefined;
|
|
6
|
+
}, {
|
|
7
|
+
path: import("superstruct").Struct<string, null>;
|
|
8
|
+
encoding: import("superstruct").Struct<"base64" | "utf8" | "hex" | undefined, null>;
|
|
9
|
+
}>;
|
|
10
|
+
export declare type GetFileArgs = Infer<typeof GetFileArgsStruct>;
|
|
11
|
+
export declare const getFileHandler: PermittedHandlerExport<GetFileHooks, GetFileArgs, string>;
|
|
12
|
+
export declare type GetFileHooks = {
|
|
13
|
+
getSnapFile: (path: GetFileArgs['path'], encoding: GetFileArgs['encoding']) => Promise<string>;
|
|
14
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from '@metamask/permission-controller';
|
|
2
|
+
import type { InstallSnapsResult } from '@metamask/snaps-utils';
|
|
3
|
+
import type { JsonRpcParams } from '@metamask/utils';
|
|
4
|
+
/**
|
|
5
|
+
* `wallet_getSnaps` gets the requester's permitted and installed Snaps.
|
|
6
|
+
*/
|
|
7
|
+
export declare const getSnapsHandler: PermittedHandlerExport<GetSnapsHooks, JsonRpcParams, InstallSnapsResult>;
|
|
8
|
+
export declare type GetSnapsHooks = {
|
|
9
|
+
/**
|
|
10
|
+
* @returns The permitted and installed snaps for the requesting origin.
|
|
11
|
+
*/
|
|
12
|
+
getSnaps: () => Promise<InstallSnapsResult>;
|
|
13
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const methodHandlers: {
|
|
2
|
+
wallet_getSnaps: import("@metamask/permission-controller").PermittedHandlerExport<import("./getSnaps").GetSnapsHooks, import("@metamask/utils").JsonRpcParams, import("@metamask/snaps-utils").InstallSnapsResult>;
|
|
3
|
+
wallet_requestSnaps: import("@metamask/permission-controller").PermittedHandlerExport<import("./requestSnaps").RequestSnapsHooks, import("@metamask/permission-controller").RequestedPermissions, import("@metamask/snaps-utils").InstallSnapsResult>;
|
|
4
|
+
wallet_invokeSnap: import("@metamask/permission-controller").PermittedHandlerExport<void, import("./invokeSnapSugar").InvokeSnapSugarArgs, import("@metamask/utils").Json>;
|
|
5
|
+
wallet_invokeKeyring: import("@metamask/permission-controller").PermittedHandlerExport<import("./invokeKeyring").InvokeKeyringHooks, import("@metamask/utils").JsonRpcRequest<import("@metamask/utils").JsonRpcParams>, import("@metamask/utils").Json>;
|
|
6
|
+
snap_getFile: import("@metamask/permission-controller").PermittedHandlerExport<import("./getFile").GetFileHooks, {
|
|
7
|
+
path: string;
|
|
8
|
+
encoding?: "base64" | "utf8" | "hex" | undefined;
|
|
9
|
+
}, string>;
|
|
10
|
+
};
|
|
11
|
+
export declare const handlers: (import("@metamask/permission-controller").PermittedHandlerExport<import("./getSnaps").GetSnapsHooks, import("@metamask/utils").JsonRpcParams, import("@metamask/snaps-utils").InstallSnapsResult> | import("@metamask/permission-controller").PermittedHandlerExport<import("./requestSnaps").RequestSnapsHooks, import("@metamask/permission-controller").RequestedPermissions, import("@metamask/snaps-utils").InstallSnapsResult> | import("@metamask/permission-controller").PermittedHandlerExport<import("./getFile").GetFileHooks, {
|
|
12
|
+
path: string;
|
|
13
|
+
encoding?: "base64" | "utf8" | "hex" | undefined;
|
|
14
|
+
}, string> | import("@metamask/permission-controller").PermittedHandlerExport<void, import("./invokeSnapSugar").InvokeSnapSugarArgs, import("@metamask/utils").Json> | import("@metamask/permission-controller").PermittedHandlerExport<import("./invokeKeyring").InvokeKeyringHooks, import("@metamask/utils").JsonRpcRequest<import("@metamask/utils").JsonRpcParams>, import("@metamask/utils").Json>)[];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { PermittedHandlerExport } from '@metamask/permission-controller';
|
|
2
|
+
import type { Snap } from '@metamask/snaps-utils';
|
|
3
|
+
import { type SnapId, type SnapRpcHookArgs } from '@metamask/snaps-utils';
|
|
4
|
+
import type { JsonRpcRequest } from '@metamask/utils';
|
|
5
|
+
import { type Json } from '@metamask/utils';
|
|
6
|
+
/**
|
|
7
|
+
* `wallet_invokeKeyring` gets the requester's permitted and installed Snaps.
|
|
8
|
+
*/
|
|
9
|
+
export declare const invokeKeyringHandler: PermittedHandlerExport<InvokeKeyringHooks, JsonRpcRequest, Json>;
|
|
10
|
+
export declare type InvokeKeyringHooks = {
|
|
11
|
+
hasPermission: (origin: string, permissionName: string) => boolean;
|
|
12
|
+
handleSnapRpcRequest: ({ snapId, origin, handler, request, }: SnapRpcHookArgs & {
|
|
13
|
+
snapId: SnapId;
|
|
14
|
+
}) => Promise<unknown>;
|
|
15
|
+
getSnap: (snapId: SnapId) => Snap | undefined;
|
|
16
|
+
getAllowedKeyringMethods: (origin: string) => string[];
|
|
17
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from '@metamask/json-rpc-engine';
|
|
2
|
+
import type { PermittedHandlerExport } from '@metamask/permission-controller';
|
|
3
|
+
import type { Json, JsonRpcRequest } from '@metamask/utils';
|
|
4
|
+
export declare type InvokeSnapSugarArgs = {
|
|
5
|
+
snapId: string;
|
|
6
|
+
request: Record<string, Json>;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* `wallet_invokeSnap` attempts to invoke an RPC method of the specified Snap.
|
|
10
|
+
*/
|
|
11
|
+
export declare const invokeSnapSugarHandler: PermittedHandlerExport<void, InvokeSnapSugarArgs, Json>;
|
|
12
|
+
/**
|
|
13
|
+
* The `wallet_invokeSnap` method implementation.
|
|
14
|
+
* Reroutes incoming JSON-RPC requests that are targeting snaps, by modifying the method and params.
|
|
15
|
+
*
|
|
16
|
+
* @param req - The JSON-RPC request object.
|
|
17
|
+
* @param _res - The JSON-RPC response object. Not used by this
|
|
18
|
+
* function.
|
|
19
|
+
* @param next - The `json-rpc-engine` "next" callback.
|
|
20
|
+
* @param end - The `json-rpc-engine` "end" callback.
|
|
21
|
+
* @returns Nothing.
|
|
22
|
+
* @throws If the params are invalid.
|
|
23
|
+
*/
|
|
24
|
+
export declare function invokeSnapSugar(req: JsonRpcRequest<InvokeSnapSugarArgs>, _res: unknown, next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback): void;
|
|
25
|
+
/**
|
|
26
|
+
* Validates the wallet_invokeSnap method `params` and returns them cast to the correct
|
|
27
|
+
* type. Throws if validation fails.
|
|
28
|
+
*
|
|
29
|
+
* @param params - The unvalidated params object from the method request.
|
|
30
|
+
* @returns The validated method parameter object.
|
|
31
|
+
*/
|
|
32
|
+
export declare function getValidatedParams(params: unknown): InvokeSnapSugarArgs;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
|
|
2
|
+
import type { Json, JsonRpcParams } from '@metamask/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a middleware that handles permitted snap RPC methods.
|
|
5
|
+
*
|
|
6
|
+
* @param isSnap - A flag that should indicate whether the requesting origin is a snap or not.
|
|
7
|
+
* @param hooks - An object containing the hooks made available to the permitted RPC methods.
|
|
8
|
+
* @returns The middleware.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createSnapsMethodMiddleware(isSnap: boolean, hooks: Record<string, unknown>): JsonRpcMiddleware<JsonRpcParams, Json>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { PermissionConstraint, RequestedPermissions, PermittedHandlerExport } from '@metamask/permission-controller';
|
|
2
|
+
import type { SnapsPermissionRequest } from '@metamask/snaps-utils';
|
|
3
|
+
import type { InstallSnapsHook, InstallSnapsResult } from './common/snapInstallation';
|
|
4
|
+
/**
|
|
5
|
+
* `wallet_requestSnaps` installs the requested Snaps and requests permission to use them if necessary.
|
|
6
|
+
*/
|
|
7
|
+
export declare const requestSnapsHandler: PermittedHandlerExport<RequestSnapsHooks, RequestedPermissions, InstallSnapsResult>;
|
|
8
|
+
export declare type RequestSnapsHooks = {
|
|
9
|
+
/**
|
|
10
|
+
* Installs the requested snaps if they are permitted.
|
|
11
|
+
*/
|
|
12
|
+
installSnaps: InstallSnapsHook;
|
|
13
|
+
/**
|
|
14
|
+
* Initiates a permission request for the requesting origin.
|
|
15
|
+
*
|
|
16
|
+
* @returns The result of the permissions request.
|
|
17
|
+
*/
|
|
18
|
+
requestPermissions: (permissions: RequestedPermissions) => Promise<[
|
|
19
|
+
Record<string, PermissionConstraint>,
|
|
20
|
+
{
|
|
21
|
+
data: Record<string, unknown>;
|
|
22
|
+
id: string;
|
|
23
|
+
origin: string;
|
|
24
|
+
}
|
|
25
|
+
]>;
|
|
26
|
+
/**
|
|
27
|
+
* Gets the current permissions for the requesting origin.
|
|
28
|
+
*
|
|
29
|
+
* @returns The current permissions of the requesting origin.
|
|
30
|
+
*/
|
|
31
|
+
getPermissions: () => Promise<Record<string, PermissionConstraint> | undefined>;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Checks whether an origin has existing `wallet_snap` permission and
|
|
35
|
+
* whether or not it has the requested snapIds caveat.
|
|
36
|
+
*
|
|
37
|
+
* @param existingPermissions - The existing permissions for the origin.
|
|
38
|
+
* @param requestedSnaps - The requested snaps.
|
|
39
|
+
* @returns True if the existing permissions satisfy the requested snaps, otherwise false.
|
|
40
|
+
*/
|
|
41
|
+
export declare function hasRequestedSnaps(existingPermissions: Record<string, PermissionConstraint>, requestedSnaps: Record<string, unknown>): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Constructs a valid permission request with merged caveats based on existing permissions
|
|
44
|
+
* and the requested snaps.
|
|
45
|
+
*
|
|
46
|
+
* @param existingPermissions - The existing permissions for the origin.
|
|
47
|
+
* @param requestedPermissions - The permission request passed into `requestPermissions`.
|
|
48
|
+
* @returns `requestedPermissions`.
|
|
49
|
+
*/
|
|
50
|
+
export declare function getSnapPermissionsRequest(existingPermissions: Record<string, PermissionConstraint>, requestedPermissions: unknown): SnapsPermissionRequest;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { PermissionSpecificationBuilder, PermissionType, RestrictedMethodOptions, PermittedHandlerExport } from '@metamask/permission-controller';
|
|
2
|
+
import type { JsonRpcParams } from '@metamask/utils';
|
|
3
|
+
import type { methodHandlers } from './permitted';
|
|
4
|
+
import type { restrictedMethodPermissionBuilders } from './restricted';
|
|
5
|
+
/**
|
|
6
|
+
* Get the method implementation from a {@link PermittedHandlerExport}.
|
|
7
|
+
*
|
|
8
|
+
* @template Handler - A permitted handler export.
|
|
9
|
+
*/
|
|
10
|
+
declare type PermittedMethodImplementation<Handler> = Handler extends PermittedHandlerExport<any, infer Args, infer Result> ? (args: Args) => Promise<Result> : never;
|
|
11
|
+
/**
|
|
12
|
+
* Get a JSON-RPC method type from a {@link PermittedHandlerExport} and a method
|
|
13
|
+
* name.
|
|
14
|
+
*
|
|
15
|
+
* @template MethodName - The name of the method.
|
|
16
|
+
* @template Handler - A permitted handler export.
|
|
17
|
+
*/
|
|
18
|
+
declare type PermittedMethod<MethodName extends string, Handler> = PermittedMethodImplementation<Handler> extends (args: infer Args) => infer Return ? (args: {
|
|
19
|
+
method: MethodName;
|
|
20
|
+
params?: Args;
|
|
21
|
+
}) => Return : never;
|
|
22
|
+
/**
|
|
23
|
+
* Get a restricted method implementation from a
|
|
24
|
+
* {@link PermissionSpecificationBuilder}.
|
|
25
|
+
*
|
|
26
|
+
* @template Builder - A permission specification builder.
|
|
27
|
+
*/
|
|
28
|
+
declare type RestrictedMethodImplementation<Builder> = Builder extends {
|
|
29
|
+
specificationBuilder: PermissionSpecificationBuilder<PermissionType.RestrictedMethod, any, infer Specification>;
|
|
30
|
+
} ? Specification['methodImplementation'] : never;
|
|
31
|
+
/**
|
|
32
|
+
* Get a JSON-RPC method type from a {@link PermissionSpecificationBuilder}.
|
|
33
|
+
*
|
|
34
|
+
* @template Builder - A permission specification builder.
|
|
35
|
+
*/
|
|
36
|
+
declare type RestrictedMethod<Builder extends {
|
|
37
|
+
targetName: string;
|
|
38
|
+
}> = RestrictedMethodImplementation<Builder> extends (args: infer Args) => infer Return ? Args extends RestrictedMethodOptions<infer Params> ? (args: {
|
|
39
|
+
method: Builder['targetName'];
|
|
40
|
+
params?: Params;
|
|
41
|
+
}) => Return : never : never;
|
|
42
|
+
/**
|
|
43
|
+
* A type containing all permitted JSON-RPC methods as functions.
|
|
44
|
+
*/
|
|
45
|
+
declare type PermittedMethodFunction = {
|
|
46
|
+
[MethodName in keyof typeof methodHandlers]: PermittedMethod<MethodName, (typeof methodHandlers)[MethodName]>;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* A type containing all restricted JSON-RPC methods as functions.
|
|
50
|
+
*/
|
|
51
|
+
declare type RestrictedMethodFunction = {
|
|
52
|
+
[Builder in keyof typeof restrictedMethodPermissionBuilders]: RestrictedMethod<(typeof restrictedMethodPermissionBuilders)[Builder]>;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* A type containing all supported JSON-RPC methods as functions.
|
|
56
|
+
*/
|
|
57
|
+
declare type MethodFunction = RestrictedMethodFunction & PermittedMethodFunction;
|
|
58
|
+
/**
|
|
59
|
+
* Fallback method name. `wallet_*` is supported by Snaps, but these functions
|
|
60
|
+
* are not implemented in `@metamask/snaps-rpc-methods`, so we don't have a type for
|
|
61
|
+
* them.
|
|
62
|
+
*/
|
|
63
|
+
declare type WalletMethodName = `wallet_${string}`;
|
|
64
|
+
/**
|
|
65
|
+
* Get a typed function if the method is defined in {@link MethodFunction}, or
|
|
66
|
+
* a generic function if the method name extends {@link WalletMethodName}.
|
|
67
|
+
* Otherwise, this returns `never`.
|
|
68
|
+
*/
|
|
69
|
+
declare type MethodFunctionFallback<MethodName> = MethodName extends keyof MethodFunction ? MethodFunction[MethodName] : MethodName extends WalletMethodName ? (args: {
|
|
70
|
+
method: MethodName;
|
|
71
|
+
params?: JsonRpcParams;
|
|
72
|
+
}) => Promise<unknown> : never;
|
|
73
|
+
/**
|
|
74
|
+
* Get the JSON-RPC object from a method name.
|
|
75
|
+
*
|
|
76
|
+
* @template MethodName - The name of the method. In most cases this is inferred
|
|
77
|
+
* from the args.
|
|
78
|
+
*/
|
|
79
|
+
export declare type ObjectFromMethodName<MethodName extends keyof MethodFunction | WalletMethodName> = {
|
|
80
|
+
method: MethodName;
|
|
81
|
+
params?: Parameters<MethodFunctionFallback<MethodName>>[0] extends {
|
|
82
|
+
params?: infer Params;
|
|
83
|
+
} ? Params : never;
|
|
84
|
+
};
|
|
85
|
+
export declare type MethodReturnType<MethodName extends keyof MethodFunction | WalletMethodName> = ReturnType<MethodFunctionFallback<MethodName>>;
|
|
86
|
+
/**
|
|
87
|
+
* A function that takes a JSON-RPC request and returns a JSON-RPC response.
|
|
88
|
+
*
|
|
89
|
+
* @template MethodName - The name of the method. In most cases this is inferred
|
|
90
|
+
* from the args.
|
|
91
|
+
*/
|
|
92
|
+
export declare type RequestFunction = <MethodName extends keyof MethodFunction | WalletMethodName>(args: ObjectFromMethodName<MethodName>) => MethodReturnType<MethodName>;
|
|
93
|
+
/**
|
|
94
|
+
* The global `snap` object. This is injected into the global scope of a snap.
|
|
95
|
+
*/
|
|
96
|
+
export declare type SnapsGlobalObject = {
|
|
97
|
+
request: RequestFunction;
|
|
98
|
+
};
|
|
99
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { PermissionConstraint } from '@metamask/permission-controller';
|
|
2
|
+
import type { Json } from '@metamask/utils';
|
|
3
|
+
export declare const caveatSpecifications: {
|
|
4
|
+
readonly snapIds: import("@metamask/permission-controller").RestrictedMethodCaveatSpecificationConstraint;
|
|
5
|
+
readonly permittedCoinTypes: import("@metamask/permission-controller").RestrictedMethodCaveatSpecificationConstraint;
|
|
6
|
+
readonly permittedDerivationPaths: import("@metamask/permission-controller").RestrictedMethodCaveatSpecificationConstraint;
|
|
7
|
+
};
|
|
8
|
+
export declare const caveatMappers: Record<string, (value: Json) => Pick<PermissionConstraint, 'caveats'>>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { PermissionConstraint, RestrictedMethodCaveatSpecificationConstraint, Caveat } from '@metamask/permission-controller';
|
|
2
|
+
import { SnapCaveatType } from '@metamask/snaps-utils';
|
|
3
|
+
import type { Json } from '@metamask/utils';
|
|
4
|
+
import type { GetBip44EntropyParams } from '../getBip44Entropy';
|
|
5
|
+
/**
|
|
6
|
+
* Map a raw value from the `initialPermissions` to a caveat specification.
|
|
7
|
+
* Note that this function does not do any validation, that's handled by the
|
|
8
|
+
* PermissionsController when the permission is requested.
|
|
9
|
+
*
|
|
10
|
+
* @param value - The raw value from the `initialPermissions`.
|
|
11
|
+
* @returns The caveat specification.
|
|
12
|
+
*/
|
|
13
|
+
export declare function permittedCoinTypesCaveatMapper(value: Json): Pick<PermissionConstraint, 'caveats'>;
|
|
14
|
+
/**
|
|
15
|
+
* Validate the params for `snap_getBip44Entropy`.
|
|
16
|
+
*
|
|
17
|
+
* @param value - The params to validate.
|
|
18
|
+
* @throws If the params are invalid.
|
|
19
|
+
*/
|
|
20
|
+
export declare function validateBIP44Params(value: unknown): asserts value is GetBip44EntropyParams;
|
|
21
|
+
/**
|
|
22
|
+
* Validate the coin types values associated with a caveat. This checks if the
|
|
23
|
+
* values are non-negative integers (>= 0).
|
|
24
|
+
*
|
|
25
|
+
* @param caveat - The caveat to validate.
|
|
26
|
+
* @throws If the caveat is invalid.
|
|
27
|
+
*/
|
|
28
|
+
export declare function validateBIP44Caveat(caveat: Caveat<string, any>): void;
|
|
29
|
+
export declare const PermittedCoinTypesCaveatSpecification: Record<SnapCaveatType.PermittedCoinTypes, RestrictedMethodCaveatSpecificationConstraint>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Caveat, PermissionConstraint, RestrictedMethodCaveatSpecificationConstraint } from '@metamask/permission-controller';
|
|
2
|
+
import type { Bip32Entropy } from '@metamask/snaps-utils';
|
|
3
|
+
import { SnapCaveatType } from '@metamask/snaps-utils';
|
|
4
|
+
import type { Json } from '@metamask/utils';
|
|
5
|
+
/**
|
|
6
|
+
* Map a raw value from the `initialPermissions` to a caveat specification.
|
|
7
|
+
* Note that this function does not do any validation, that's handled by the
|
|
8
|
+
* PermissionsController when the permission is requested.
|
|
9
|
+
*
|
|
10
|
+
* @param value - The raw value from the `initialPermissions`.
|
|
11
|
+
* @returns The caveat specification.
|
|
12
|
+
*/
|
|
13
|
+
export declare function permittedDerivationPathsCaveatMapper(value: Json): Pick<PermissionConstraint, 'caveats'>;
|
|
14
|
+
/**
|
|
15
|
+
* Validate a caveat path object. The object must consist of a `path` array and
|
|
16
|
+
* a `curve` string. Paths must start with `m`, and must contain at
|
|
17
|
+
* least two indices. If `ed25519` is used, this checks if all the path indices
|
|
18
|
+
* are hardened.
|
|
19
|
+
*
|
|
20
|
+
* @param value - The value to validate.
|
|
21
|
+
* @throws If the value is invalid.
|
|
22
|
+
*/
|
|
23
|
+
export declare function validateBIP32Path(value: unknown): asserts value is Bip32Entropy;
|
|
24
|
+
/**
|
|
25
|
+
* Validate the path values associated with a caveat. This validates that the
|
|
26
|
+
* value is a non-empty array with valid derivation paths and curves.
|
|
27
|
+
*
|
|
28
|
+
* @param caveat - The caveat to validate.
|
|
29
|
+
* @throws If the value is invalid.
|
|
30
|
+
*/
|
|
31
|
+
export declare function validateBIP32CaveatPaths(caveat: Caveat<string, any>): asserts caveat is Caveat<string, Bip32Entropy[]>;
|
|
32
|
+
export declare const PermittedDerivationPathsCaveatSpecification: Record<SnapCaveatType.PermittedDerivationPaths, RestrictedMethodCaveatSpecificationConstraint>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Caveat, RestrictedMethodCaveatSpecificationConstraint, PermissionConstraint } from '@metamask/permission-controller';
|
|
2
|
+
import type { SnapIds } from '@metamask/snaps-utils';
|
|
3
|
+
import { SnapCaveatType } from '@metamask/snaps-utils';
|
|
4
|
+
import type { Json } from '@metamask/utils';
|
|
5
|
+
/**
|
|
6
|
+
* Map a raw value from the `initialPermissions` to a caveat specification.
|
|
7
|
+
* Note that this function does not do any validation, that's handled by the
|
|
8
|
+
* PermissionsController when the permission is requested.
|
|
9
|
+
*
|
|
10
|
+
* @param value - The raw value from the `initialPermissions`.
|
|
11
|
+
* @returns The caveat specification.
|
|
12
|
+
*/
|
|
13
|
+
export declare function snapIdsCaveatMapper(value: Json): Pick<PermissionConstraint, 'caveats'>;
|
|
14
|
+
/**
|
|
15
|
+
* Validates that the caveat value exists and is a non-empty object.
|
|
16
|
+
*
|
|
17
|
+
* @param caveat - The caveat to validate.
|
|
18
|
+
* @throws If the caveat is invalid.
|
|
19
|
+
*/
|
|
20
|
+
export declare function validateSnapIdsCaveat(caveat: Caveat<string, any>): asserts caveat is Caveat<string, SnapIds>;
|
|
21
|
+
export declare const SnapIdsCaveatSpecification: Record<SnapCaveatType.SnapIds, RestrictedMethodCaveatSpecificationConstraint>;
|