@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
163 lines • 6.74 kB
text/typescript
import type { SupportedCurve, CryptographicFunctions } from "@metamask/key-tree";
import { SLIP10Node } from "@metamask/key-tree";
import type { Messenger } from "@metamask/messenger";
import type { MagicValue } from "@metamask/snaps-utils";
import { SnapEndowments } from "./endowments/index.mjs";
import type { KeyringControllerWithKeyringV2UnsafeAction } from "./types.mjs";
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;
};
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>;
/**
* The permissions that allow a Snap to show UI. Snaps must have at least one
* of these permissions to use the interface management RPC methods.
*/
export declare const UI_PERMISSIONS: readonly ["snap_dialog", "snap_notify", SnapEndowments.HomePage, SnapEndowments.SettingsPage, SnapEndowments.TransactionInsight, SnapEndowments.SignatureInsight];
export declare const HD_KEYRING = "hd";
/**
* Get the mnemonic for a given entropy source. If no source is
* provided, the primary HD keyring's mnemonic will be returned.
*
* @param messenger - The messenger.
* @param source - The ID of the entropy source keyring.
* @returns The mnemonic.
*/
export declare function getMnemonic(messenger: Messenger<string, KeyringControllerWithKeyringV2UnsafeAction>, source?: string | undefined): Promise<Uint8Array>;
/**
* Get the mnemonic seed for a given entropy source. If no source is
* provided, the primary HD keyring's mnemonic seed will be returned.
*
* @param messenger - The messenger.
* @param source - The ID of the entropy source keyring.
* @returns The mnemonic seed.
*/
export declare function getMnemonicSeed(messenger: Messenger<string, KeyringControllerWithKeyringV2UnsafeAction>, source?: string | undefined): Promise<Uint8Array>;
export {};
//# sourceMappingURL=utils.d.mts.map