Type alias Keyring<State>

Keyring<State>: {
    type: string;
    addAccounts(number: number): Promise<`0x${string}`[]>;
    decryptMessage?(account: `0x${string}`, encryptedData: Eip1024EncryptedData): Promise<string>;
    deserialize(state: State): Promise<void>;
    destroy?(): Promise<void>;
    exportAccount?(address: `0x${string}`, options?: Record<string, unknown>): Promise<string>;
    generateRandomMnemonic?(): Promise<void>;
    getAccounts(): Promise<`0x${string}`[]>;
    getAppKeyAddress?(address: `0x${string}`, origin: string): Promise<`0x${string}`>;
    getEncryptionPublicKey?(account: `0x${string}`, options?: Record<string, unknown>): Promise<string>;
    init?(): Promise<void>;
    removeAccount?(address: `0x${string}`): void;
    serialize(): Promise<State>;
    signEip7702Authorization?(address: `0x${string}`, authorization: [chainId: number, contractAddress: `0x${string}`, nonce: number], options?: Record<string, unknown>): Promise<string>;
    signMessage?(address: `0x${string}`, message: string, options?: Record<string, unknown>): Promise<string>;
    signPersonalMessage?(address: `0x${string}`, message: `0x${string}`, options?: {
        version?: string;
    } & Record<string, unknown>): Promise<string>;
    signTransaction?(address: `0x${string}`, transaction: TypedTransaction, options?: Record<string, unknown>): Promise<TxData>;
    signTypedData?(address: `0x${string}`, typedData: Record<string, unknown>, options?: Record<string, unknown>): Promise<string>;
}

A keyring is something that can sign messages. Keyrings are used to add new signing strategies; each strategy is a new keyring.

Each keyring manages a collection of key pairs, which we call "accounts". Each account is referred to by its "address", which is a unique identifier derived from the public key. The address is always a "0x"-prefixed hexidecimal string.

The keyring might store the private key for each account as well, but it's not guaranteed. Some keyrings delegate signing, so they don't need the private key directly. The keyring (and in particular the keyring state) should be treated with care though, just in case it does contain sensitive material such as a private key.

Deprecated

This type has been moved to the @metamask/keyring-utils package. See Keyring Utils.

Type Parameters

Type declaration

  • type: string

    The name of this type of keyring. This must match the type property of the keyring class.

  • addAccounts:function
    • Add an account to the keyring.

      Returns

      A list of the newly added account addresses.

      Parameters

      • number: number

        The number of accounts to add. Usually defaults to 1.

      Returns Promise<`0x${string}`[]>

  • decryptMessage?:function
    • Decrypt an encrypted message. This is equivalent to the eth_decrypt JSON-RPC method. See the Docs for more information.

      Returns

      The decrypted data.

      Parameters

      • account: `0x${string}`

        The address of the account you want to use to decrypt the message.

      • encryptedData: Eip1024EncryptedData

        The encrypted data that you want to decrypt.

      Returns Promise<string>

  • deserialize:function
    • Deserialize the given keyring state, overwriting any existing state with the serialized state provided.

      Parameters

      • state: State

        A JSON-serializable representation of the keyring state.

      Returns Promise<void>

  • destroy?:function
    • Destroy the keyring.

      Returns Promise<void>

  • exportAccount?:function
    • Export the private key for one of the keyring accounts.

      Some keyrings accept an "options" parameter as well. See the documentation for the specific keyring for more information about what these options are. For some keyrings, the options parameter is used to allow exporting a private key that is derived from the given account, rather than exporting that account's private key directly.

      Returns

      The non-prefixed, hex-encoded private key that was requested.

      Parameters

      • address: `0x${string}`

        The address of the account to export.

      • Optional options: Record<string, unknown>

        Export options; differs between keyrings.

      Returns Promise<string>

  • generateRandomMnemonic?:function
    • Generates the properties for the keyring based on the given BIP39-compliant mnemonic.

      Returns

      A promise resolving when the keyring has generated the properties.

      Returns Promise<void>

  • getAccounts:function
    • Get the addresses for all accounts in this keyring.

      Returns

      A list of the account addresses for this keyring

      Returns Promise<`0x${string}`[]>

  • getAppKeyAddress?:function
    • Get the "app key" address for the given account and origin. An app key is an application-specific key pair. See https://eips.ethereum.org/EIPS/eip-1775|EIP-1775 for more information. The https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin|origin is used as the unique identifier for the application, and it's used as part of the key derivation process.

      Returns

      The address of the app key for the given account and origin.

      Parameters

      • address: `0x${string}`

        The address of the account the app key is derived from.

      • origin: string

        The origin of the application.

      Returns Promise<`0x${string}`>

  • getEncryptionPublicKey?:function
    • Get a public key to use for encryption. This is equivalent to the eth_getEncryptionPublicKey JSON-RPC method. See the Docs for more information.

      Some keyrings accept an "options" parameter as well. See the documentation for the specific keyring for more information about what these options are. For some keyrings, the options parameter can even change which key is used (e.g. encrypting with app keys).

      Parameters

      • account: `0x${string}`

        The address of the account you want the encryption key for.

      • Optional options: Record<string, unknown>

        Options; differs between keyrings.

      Returns Promise<string>

  • init?:function
    • Method to include asynchronous configuration.

      Returns Promise<void>

  • removeAccount?:function
    • Remove an account from the keyring.

      Parameters

      • address: `0x${string}`

        The address of the account to remove.

      Returns void

  • serialize:function
    • Serialize the keyring state as a JSON-serializable object.

      Returns

      A JSON-serializable representation of the keyring state.

      Returns Promise<State>

  • signEip7702Authorization?:function
    • Sign an EIP-7702 authorization. This is a signing method for authorizing a specific contract on a specific chain.

      Returns

      The signed authorization as a hex string.

      Parameters

      • address: `0x${string}`

        The address of the account to use for signing.

      • authorization: [chainId: number, contractAddress: `0x${string}`, nonce: number]

        An array containing the chain ID, contract address, and nonce.

      • Optional options: Record<string, unknown>

        Signing options; differs between keyrings.

      Returns Promise<string>

  • signMessage?:function
    • Sign a message. This is equivalent to an older version of the the eth_sign Ethereum JSON-RPC method. The message is signed using ECDSA, using the curve secp256k1 the Keccak-256 hash function.

      For more information about this method and why we still support it, see the Docs.

      Some keyrings accept an "options" parameter as well. See the documentation for the specific keyring for more information about what these options are. For some keyrings, the options parameter can even change which key is used for signing (e.g. signing with app keys).

      Returns

      The signed message.

      Parameters

      • address: `0x${string}`

        The address of the account to use for signing.

      • message: string

        The message to sign.

      • Optional options: Record<string, unknown>

        Signing options; differs between keyrings.

      Returns Promise<string>

  • signPersonalMessage?:function
    • Sign a message. This is equivalent to the eth_sign Ethereum JSON-RPC method, which is exposed by MetaMask as the method personal_sign. See the Ethereum JSON-RPC API documentation for more details.

      For more information about this method and why we call it personal_sign, see the Docs.

      Some keyrings accept an "options" parameter as well. See the documentation for the specific keyring for more information about what these options are. For some keyrings, the options parameter can even change which key is used for signing (e.g. signing with app keys).

      Returns

      The signed message.

      Parameters

      • address: `0x${string}`

        The address of the account to use for signing.

      • message: `0x${string}`

        The message to sign.

      • Optional options: {
            version?: string;
        } & Record<string, unknown>

        Signing options; differs between keyrings.

      Returns Promise<string>

  • signTransaction?:function
    • Sign a transaction. This is equivalent to the eth_signTransaction Ethereum JSON-RPC method. See the Ethereum JSON-RPC API documentation for more details.

      Some keyrings accept an "options" parameter as well. See the documentation for the specific keyring for more information about what these options are. For some keyrings, the options parameter can even change which key is used for signing (e.g. signing with app keys).

      Returns

      The signed transaction.

      Parameters

      • address: `0x${string}`

        The address of the account to use for signing.

      • transaction: TypedTransaction

        The transaction to sign.

      • Optional options: Record<string, unknown>

        Signing options; differs between keyrings.

      Returns Promise<TxData>

  • signTypedData?:function
    • Sign a message. This is equivalent to the eth_signTypedData Ethereum JSON-RPC method. See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md|EIP-712 for more details.

      The "version" option dictates which version of eth_signTypedData is used. The latest version reflects the specification most closely, whereas earlier versions reflect earlier drafts of the specification that are still supported for backwards-compatibility reasons. For more information about why we support multiple versions, see the Docs.

      Some keyrings accept additional options as well. See the documentation for the specific keyring for more information about what these options are. For some keyrings, the options parameter can even change which key is used for signing (e.g. signing with app keys).

      Returns

      The signed message.

      Parameters

      • address: `0x${string}`

        The address of the account to use for signing.

      • typedData: Record<string, unknown>

        The data to sign.

      • Optional options: Record<string, unknown>

        Signing options; differs between keyrings.

      Returns Promise<string>