From 335f2f72527269318eab3d03e526136711f6388a Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Thu, 6 Nov 2025 12:14:18 +0100 Subject: [PATCH 1/3] chore: bump `@metamask/keyring-controller` --- app/core/Encryptor/Encryptor.ts | 4 +-- app/core/Encryptor/types.ts | 25 +++++-------------- .../controllers/keyring-controller-init.ts | 22 +++++++++------- package.json | 3 ++- yarn.lock | 18 +++---------- 5 files changed, 26 insertions(+), 46 deletions(-) diff --git a/app/core/Encryptor/Encryptor.ts b/app/core/Encryptor/Encryptor.ts index 4e43841b5176..801f06b47801 100644 --- a/app/core/Encryptor/Encryptor.ts +++ b/app/core/Encryptor/Encryptor.ts @@ -57,7 +57,7 @@ const isEncryptionKey = (key: unknown): key is EncryptionKey => * It supports generating a salt, deriving an encryption key from a * password and salt, and performing the encryption and decryption processes. */ -class Encryptor implements WithKeyEncryptor { +class Encryptor implements WithKeyEncryptor { /** * The key derivation parameters used for encryption and decryption operations. * These parameters include the algorithm and its specific parameters, for example, number of iterations for key derivation. @@ -134,7 +134,6 @@ class Encryptor implements WithKeyEncryptor { * @param data - The data to encrypt. * @returns A promise that resolves to an object containing the cipher text and initialization vector (IV). */ - //@ts-expect-error - TODO: will be implemented at the keyring controller the support for this key type encryptWithKey = async ( key: EncryptionKey, data: Json, @@ -159,7 +158,6 @@ class Encryptor implements WithKeyEncryptor { * @param payload - The encrypted payload to decrypt. * @returns The decrypted object. */ - //@ts-expect-error - TODO: will be implemented at the keyring controller the support for this key type decryptWithKey = async ( key: EncryptionKey, payload: EncryptionResult, diff --git a/app/core/Encryptor/types.ts b/app/core/Encryptor/types.ts index ee42af8adcff..49c35c51f36b 100644 --- a/app/core/Encryptor/types.ts +++ b/app/core/Encryptor/types.ts @@ -1,6 +1,6 @@ import { Json } from '@metamask/utils'; import type { KeyDerivationOptions } from '@metamask/browser-passworder'; -import { ExportableKeyEncryptor } from '@metamask/keyring-controller'; +import { Encryptor } from '@metamask/keyring-controller'; /** Key derivation function options. */ export type { KeyDerivationOptions }; @@ -124,21 +124,8 @@ export interface GenericEncryptor { * An encryptor interface that supports encrypting and decrypting * serializable data with a password, and exporting and importing keys. */ -export type WithKeyEncryptor = ExportableKeyEncryptor & { - /** - * Encrypts the given object with the given encryption key. - * - * @param key - The encryption key to encrypt with. - * @param object - The object to encrypt. - * @returns The encryption result. - */ - encryptWithKey: (key: Key, data: Data) => Promise; - /** - * Decrypts the given encrypted string with the given encryption key. - * - * @param key - The encryption key to decrypt with. - * @param encryptedString - The encrypted string to decrypt. - * @returns The decrypted object. - */ - decryptWithKey: (key: Key, payload: EncryptionResult) => Promise; -}; +export type WithKeyEncryptor = Encryptor< + Key, + KeyDerivationOptions, + EncryptionResult +>; diff --git a/app/core/Engine/controllers/keyring-controller-init.ts b/app/core/Engine/controllers/keyring-controller-init.ts index 1c77799fc92a..338700bf6693 100644 --- a/app/core/Engine/controllers/keyring-controller-init.ts +++ b/app/core/Engine/controllers/keyring-controller-init.ts @@ -2,6 +2,7 @@ import { ControllerInitFunction } from '../types'; import { KeyringController, KeyringControllerMessenger, + KeyringControllerOptions, } from '@metamask/keyring-controller'; import { QrKeyring } from '@metamask/eth-qr-keyring'; import { @@ -11,6 +12,11 @@ import { } from '@metamask/eth-ledger-bridge-keyring'; import { HdKeyring } from '@metamask/eth-hd-keyring'; import { Encryptor, LEGACY_DERIVATION_OPTIONS, pbkdf2 } from '../../Encryptor'; +import { + EncryptionKey, + EncryptionResult, + KeyDerivationOptions, +} from 'app/core/Encryptor/types'; const encryptor = new Encryptor({ keyDerivationOptions: LEGACY_DERIVATION_OPTIONS, @@ -34,7 +40,7 @@ export const keyringControllerInit: ControllerInitFunction< qrKeyringScanner, getController, }) => { - const additionalKeyrings = []; + const additionalKeyrings: KeyringControllerOptions['keyringBuilders'] = []; const qrKeyringBuilder = () => { const keyring = new QrKeyring({ bridge: qrKeyringScanner }); @@ -64,21 +70,19 @@ export const keyringControllerInit: ControllerInitFunction< ///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps) const snapKeyringBuilder = getController('SnapKeyringBuilder'); + // @ts-expect-error SnapKeyring is missing the `addAccounts` method additionalKeyrings.push(snapKeyringBuilder); ///: END:ONLY_INCLUDE_IF - const preferencesController = getController('PreferencesController'); - - const controller = new KeyringController({ - removeIdentity: (address: string) => - preferencesController.removeIdentity(address), + const controller = new KeyringController< + EncryptionKey, + KeyDerivationOptions, + EncryptionResult + >({ encryptor, messenger: controllerMessenger, state: initialKeyringState || persistedState.KeyringController, - // @ts-expect-error: TODO: Update the type of QRHardwareKeyring to - // `Keyring`. keyringBuilders: additionalKeyrings, - cacheEncryptionKey: true, }); return { diff --git a/package.json b/package.json index 1ce8cf93ede0..110d4a8f1f95 100644 --- a/package.json +++ b/package.json @@ -176,7 +176,8 @@ "@scure/bip32": "1.7.0", "@metamask/snaps-sdk": "^10.0.0", "react-native@0.76.9": "patch:react-native@npm%3A0.76.9#./.yarn/patches/react-native-npm-0.76.9-1c25352097.patch", - "@metamask/transaction-controller@npm:^61.0.0": "patch:@metamask/transaction-controller@npm%3A61.0.0#~/.yarn/patches/@metamask-transaction-controller-npm-61.0.0-cccac388c7.patch" + "@metamask/transaction-controller@npm:^61.0.0": "patch:@metamask/transaction-controller@npm%3A61.0.0#~/.yarn/patches/@metamask-transaction-controller-npm-61.0.0-cccac388c7.patch", + "@metamask/keyring-controller": "npm:@metamask-previews/keyring-controller@24.0.0-preview-70abd50a" }, "dependencies": { "@config-plugins/detox": "^9.0.0", diff --git a/yarn.lock b/yarn.lock index 7ca213d0d9a2..2af0aa061eba 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7112,15 +7112,6 @@ __metadata: languageName: node linkType: hard -"@metamask/browser-passworder@npm:^4.3.0": - version: 4.3.0 - resolution: "@metamask/browser-passworder@npm:4.3.0" - dependencies: - "@metamask/utils": "npm:^8.2.0" - checksum: 10/8ba5c50cd6274b0cc0f90a1ee16b960ee150f14c29083f3515f4abe018a28ead32c21f5f4a62a6e27a946b1228adc2ff1f195e71e38782fa39fa8fff116173e6 - languageName: node - linkType: hard - "@metamask/browser-passworder@npm:^5.0.0": version: 5.0.0 resolution: "@metamask/browser-passworder@npm:5.0.0" @@ -7759,13 +7750,12 @@ __metadata: languageName: node linkType: hard -"@metamask/keyring-controller@npm:^24.0.0": - version: 24.0.0 - resolution: "@metamask/keyring-controller@npm:24.0.0" +"@metamask/keyring-controller@npm:@metamask-previews/keyring-controller@24.0.0-preview-70abd50a": + version: 24.0.0-preview-70abd50a + resolution: "@metamask-previews/keyring-controller@npm:24.0.0-preview-70abd50a" dependencies: "@ethereumjs/util": "npm:^9.1.0" "@metamask/base-controller": "npm:^9.0.0" - "@metamask/browser-passworder": "npm:^4.3.0" "@metamask/eth-hd-keyring": "npm:^13.0.0" "@metamask/eth-sig-util": "npm:^8.2.0" "@metamask/eth-simple-keyring": "npm:^11.0.0" @@ -7778,7 +7768,7 @@ __metadata: immer: "npm:^9.0.6" lodash: "npm:^4.17.21" ulid: "npm:^2.3.0" - checksum: 10/35050ec779f364dab6dfa477cc3764b923277074769e2e7a5496ca82000493cf9292a56a048eaa1b361b2ca15369725ae8e2bb6a7f6d91b37ad1d86fab582c23 + checksum: 10/3941c3158ea5bbb91b375b44f6efebe55feaf60ee16864d1bd976120e3fde5a102c60bddda5896962e1d4ce9cb5412ea2d07989f12b73de2917b5844033bc4bc languageName: node linkType: hard From f6071d64e3e566708470afa60e4bd7065021f3a3 Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Thu, 6 Nov 2025 16:13:22 +0100 Subject: [PATCH 2/3] fix `keyring-controller-init.test.ts` --- app/core/Engine/controllers/keyring-controller-init.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/core/Engine/controllers/keyring-controller-init.test.ts b/app/core/Engine/controllers/keyring-controller-init.test.ts index b661dbfc3a3a..d079adec7f49 100644 --- a/app/core/Engine/controllers/keyring-controller-init.test.ts +++ b/app/core/Engine/controllers/keyring-controller-init.test.ts @@ -54,10 +54,8 @@ describe('keyringControllerInit', () => { expect(controllerMock).toHaveBeenCalledWith({ messenger: expect.any(Object), state: undefined, - cacheEncryptionKey: true, encryptor: expect.any(Encryptor), keyringBuilders: expect.any(Array), - removeIdentity: expect.any(Function), }); }); }); From bf1889a1143264cde75d6daed0ffe6b0a40f3c8e Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Thu, 13 Nov 2025 11:09:49 +0100 Subject: [PATCH 3/3] update preview build --- package.json | 2 +- yarn.lock | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index f5d9525dea69..afbbfb6254eb 100644 --- a/package.json +++ b/package.json @@ -177,7 +177,7 @@ "@metamask/snaps-sdk": "^10.0.0", "react-native@0.76.9": "patch:react-native@npm%3A0.76.9#./.yarn/patches/react-native-npm-0.76.9-1c25352097.patch", "@metamask/transaction-controller@npm:^61.1.0": "patch:@metamask/transaction-controller@npm%3A61.0.0#~/.yarn/patches/@metamask-transaction-controller-npm-61.0.0-cccac388c7.patch", - "@metamask/keyring-controller": "npm:@metamask-previews/keyring-controller@24.0.0-preview-70abd50a" + "@metamask/keyring-controller": "npm:@metamask-previews/keyring-controller@24.0.0-preview-20df3e99" }, "dependencies": { "@config-plugins/detox": "^9.0.0", diff --git a/yarn.lock b/yarn.lock index b525544656fd..b33dea26fc7e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7117,6 +7117,15 @@ __metadata: languageName: node linkType: hard +"@metamask/browser-passworder@npm:^6.0.0": + version: 6.0.0 + resolution: "@metamask/browser-passworder@npm:6.0.0" + dependencies: + "@metamask/utils": "npm:^11.0.1" + checksum: 10/44b31729ccd52e62df27a948fbea2320a11861d6465128b1626a723fc47e25f9e3a8c3bdc09329f3f270b5d9e023107cb439cfb6961a3614c6daa58ea4524f00 + languageName: node + linkType: hard + "@metamask/build-utils@npm:^3.0.0": version: 3.0.4 resolution: "@metamask/build-utils@npm:3.0.4" @@ -7746,12 +7755,13 @@ __metadata: languageName: node linkType: hard -"@metamask/keyring-controller@npm:@metamask-previews/keyring-controller@24.0.0-preview-70abd50a": - version: 24.0.0-preview-70abd50a - resolution: "@metamask-previews/keyring-controller@npm:24.0.0-preview-70abd50a" +"@metamask/keyring-controller@npm:@metamask-previews/keyring-controller@24.0.0-preview-20df3e99": + version: 24.0.0-preview-20df3e99 + resolution: "@metamask-previews/keyring-controller@npm:24.0.0-preview-20df3e99" dependencies: "@ethereumjs/util": "npm:^9.1.0" "@metamask/base-controller": "npm:^9.0.0" + "@metamask/browser-passworder": "npm:^6.0.0" "@metamask/eth-hd-keyring": "npm:^13.0.0" "@metamask/eth-sig-util": "npm:^8.2.0" "@metamask/eth-simple-keyring": "npm:^11.0.0" @@ -7764,7 +7774,7 @@ __metadata: immer: "npm:^9.0.6" lodash: "npm:^4.17.21" ulid: "npm:^2.3.0" - checksum: 10/3941c3158ea5bbb91b375b44f6efebe55feaf60ee16864d1bd976120e3fde5a102c60bddda5896962e1d4ce9cb5412ea2d07989f12b73de2917b5844033bc4bc + checksum: 10/f5332805b9ff74cefeae49b5b3b4cf9b6b1617535e78f30e052ccf79a8eaff9cef49527873b28f0249cd6528f3a823c4b9e1e4b3b1124854886eb8ace1540372 languageName: node linkType: hard