@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
149 lines • 6 kB
text/typescript
import type { SupportedCurve, CryptographicFunctions } from "@metamask/key-tree";
import { SLIP10Node } from "@metamask/key-tree";
import type { MagicValue } from "@metamask/snaps-utils";
export declare const FORBIDDEN_KEYS: string[];
/**
* Maps an interface with method hooks to an object, using the keys of the
* interface, and `true` as value. This ensures that the `methodHooks` object
* has the same values as the interface.
*/
export type MethodHooksObject<HooksType extends Record<string, unknown>> = {
[Key in keyof HooksType]: true;
};
/**
* Returns the subset of the specified `hooks` that are included in the
* `hookNames` object. This is a Principle of Least Authority (POLA) measure
* to ensure that each RPC method implementation only has access to the
* API "hooks" it needs to do its job.
*
* @param hooks - The hooks to select from.
* @param hookNames - The names of the hooks to select.
* @returns The selected hooks.
* @template Hooks - The hooks to select from.
* @template HookName - The names of the hooks to select.
*/
export declare function selectHooks<Hooks extends Record<string, unknown>, HookName extends keyof Hooks>(hooks: Hooks, hookNames?: Record<HookName, boolean>): Pick<Hooks, HookName> | undefined;
type BaseDeriveEntropyOptions = {
/**
* The input value to derive entropy from.
*/
input: string;
/**
* An optional salt to use when deriving entropy.
*/
salt?: string;
/**
* A hardened BIP-32 index, which is used to derive the root key from the
* mnemonic phrase.
*/
magic: MagicValue;
/**
* The cryptographic functions to use for the derivation.
*/
cryptographicFunctions: CryptographicFunctions | undefined;
};
type SeedDeriveEntropyOptions = BaseDeriveEntropyOptions & {
/**
* The mnemonic seed to use for entropy derivation.
*/
seed: Uint8Array;
};
/**
* Derive entropy from the given mnemonic seed and salt.
*
* This is based on the reference implementation of
* [SIP-6](https://metamask.github.io/SIPs/SIPS/sip-6).
*
* @param options - The options for entropy derivation.
* @param options.input - The input value to derive entropy from.
* @param options.salt - An optional salt to use when deriving entropy.
* @param options.seed - The mnemonic seed to use for entropy
* derivation.
* @param options.magic - A hardened BIP-32 index, which is used to derive the
* root key from the mnemonic phrase.
* @param options.cryptographicFunctions - The cryptographic functions to use
* for the derivation.
* @returns The derived entropy.
*/
export declare function deriveEntropyFromSeed({ input, salt, seed, magic, cryptographicFunctions, }: SeedDeriveEntropyOptions): Promise<`0x${string}`>;
/**
* Get the path prefix to use for key derivation in `key-tree`. This assumes the
* following:
*
* - The Secp256k1 curve always uses the BIP-32 specification.
* - The Ed25519 curve always uses the SLIP-10 specification.
* - The BIP-32-Ed25519 curve always uses the CIP-3 specification.
*
* While this does not matter in most situations (no known case at the time of
* writing), `key-tree` requires a specific specification to be used.
*
* @param curve - The curve to get the path prefix for. The curve is NOT
* validated by this function.
* @returns The path prefix, i.e., `bip32` or `slip10`.
*/
export declare function getPathPrefix(curve: SupportedCurve): 'bip32' | 'slip10' | 'cip3';
type BaseGetNodeArgs = {
curve: SupportedCurve;
path: string[];
cryptographicFunctions: CryptographicFunctions | undefined;
};
type GetNodeArgsMnemonic = BaseGetNodeArgs & {
secretRecoveryPhrase: Uint8Array;
};
type GetNodeArgsSeed = BaseGetNodeArgs & {
seed: Uint8Array;
};
/**
* Get a `key-tree`-compatible node.
*
* Note: This function assumes that all the parameters have been validated
* beforehand.
*
* @param options - The derivation options.
* @param options.curve - The curve to use for derivation.
* @param options.secretRecoveryPhrase - The secret recovery phrase to use for
* derivation.
* @param options.path - The derivation path to use as array, starting with an
* "m" as the first item.
* @param options.cryptographicFunctions - The cryptographic functions to use
* for the node.
* @returns The `key-tree` SLIP-10 node.
*/
export declare function getNodeFromMnemonic({ curve, secretRecoveryPhrase, path, cryptographicFunctions, }: GetNodeArgsMnemonic): Promise<SLIP10Node>;
/**
* Get a `key-tree`-compatible node.
*
* Note: This function assumes that all the parameters have been validated
* beforehand.
*
* @param options - The derivation options.
* @param options.curve - The curve to use for derivation.
* @param options.seed - The BIP-39 to use for
* derivation.
* @param options.path - The derivation path to use as array, starting with an
* "m" as the first item.
* @param options.cryptographicFunctions - The cryptographic functions to use
* for the node.
* @returns The `key-tree` SLIP-10 node.
*/
export declare function getNodeFromSeed({ curve, seed, path, cryptographicFunctions, }: GetNodeArgsSeed): Promise<SLIP10Node>;
/**
* Validate the key of a state object.
*
* @param key - The key to validate.
* @returns `true` if the key is valid, `false` otherwise.
*/
export declare function isValidStateKey(key: string | undefined): boolean;
export declare const StateKeyStruct: import("@metamask/superstruct").Struct<string, null>;
/**
* Get a value using the entropy source hooks: getMnemonic or getMnemonicSeed.
* This function calls the passed hook and handles any errors that occur,
* throwing formatted JSON-RPC errors.
*
* @param hook - The hook.
* @param source - The entropy source to use.
* @returns The secret recovery phrase.
*/
export declare function getValueFromEntropySource(hook: (source?: string | undefined) => Promise<Uint8Array>, source?: string | undefined): Promise<Uint8Array>;
export {};
//# sourceMappingURL=utils.d.cts.map