Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add EncryptionKey type parameter to ExportableKeyEncryptor #5395

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/keyring-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **BREAKING:** `withKeyring` method now requires a callback argument of type `({ keyring: SelectedKeyring; metadata: KeyringMetadata }) => Promise<CallbackResult>` ([#5372](https://github.com/MetaMask/core/pull/5372))
- Bump `@metamask/keyring-api"` from `^17.0.0` to `^17.2.0` ([#5366](https://github.com/MetaMask/core/pull/5366))
- Bump `@metamask/keyring-internal-api` from `^4.0.1` to `^4.0.3` ([#5356](https://github.com/MetaMask/core/pull/5356)), ([#5366](https://github.com/MetaMask/core/pull/5366))
- `ExportableKeyEncryptor` is now a generic type with a type parameter `EncryptionKey` ([#5395](https://github.com/MetaMask/core/pull/5395))
- The type parameter defaults to `unknown`

### Fixed

Expand Down
116 changes: 60 additions & 56 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,62 +362,66 @@ export type GenericEncryptor = {
* An encryptor interface that supports encrypting and decrypting
* serializable data with a password, and exporting and importing keys.
*/
export type ExportableKeyEncryptor = GenericEncryptor & {
/**
* 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: unknown,
object: Json,
) => Promise<encryptorUtils.EncryptionResult>;
/**
* Encrypts the given object with the given password, and returns the
* encryption result and the exported key string.
*
* @param password - The password to encrypt with.
* @param object - The object to encrypt.
* @param salt - The optional salt to use for encryption.
* @returns The encrypted string and the exported key string.
*/
encryptWithDetail: (
password: string,
object: Json,
salt?: string,
) => Promise<encryptorUtils.DetailedEncryptionResult>;
/**
* 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: unknown, encryptedString: string) => Promise<unknown>;
/**
* Decrypts the given encrypted string with the given password, and returns
* the decrypted object and the salt and exported key string used for
* encryption.
*
* @param password - The password to decrypt with.
* @param encryptedString - The encrypted string to decrypt.
* @returns The decrypted object and the salt and exported key string used for
* encryption.
*/
decryptWithDetail: (
password: string,
encryptedString: string,
) => Promise<encryptorUtils.DetailedDecryptResult>;
/**
* Generates an encryption key from exported key string.
*
* @param key - The exported key string.
* @returns The encryption key.
*/
importKey: (key: string) => Promise<unknown>;
};
export type ExportableKeyEncryptor<EncryptionKey = unknown> =
GenericEncryptor & {
/**
* 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: EncryptionKey,
object: Json,
) => Promise<encryptorUtils.EncryptionResult>;
/**
* Encrypts the given object with the given password, and returns the
* encryption result and the exported key string.
*
* @param password - The password to encrypt with.
* @param object - The object to encrypt.
* @param salt - The optional salt to use for encryption.
* @returns The encrypted string and the exported key string.
*/
encryptWithDetail: (
password: string,
object: Json,
salt?: string,
) => Promise<encryptorUtils.DetailedEncryptionResult>;
/**
* 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: EncryptionKey,
encryptedString: string,
) => Promise<unknown>;
/**
* Decrypts the given encrypted string with the given password, and returns
* the decrypted object and the salt and exported key string used for
* encryption.
*
* @param password - The password to decrypt with.
* @param encryptedString - The encrypted string to decrypt.
* @returns The decrypted object and the salt and exported key string used for
* encryption.
*/
decryptWithDetail: (
password: string,
encryptedString: string,
) => Promise<encryptorUtils.DetailedDecryptResult>;
/**
* Generates an encryption key from exported key string.
*
* @param key - The exported key string.
* @returns The encryption key.
*/
importKey: (key: string) => Promise<EncryptionKey>;
};

export type KeyringSelector =
| {
Expand Down
Loading