Skip to content

Commit

Permalink
feat: store unencryptedKeyringData
Browse files Browse the repository at this point in the history
  • Loading branch information
heisenberg-2077 committed Dec 20, 2024
1 parent 0e18a2d commit 60a7619
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions src/background/service/keyring/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ import {
} from 'background/utils/password';
import uninstalledMetricService from '../uninstalled';

const UNENCRYPTED_IGNORE_KEYRING = [
KEYRING_TYPE.SimpleKeyring,
KEYRING_TYPE.HdKeyring,
];

export const KEYRING_SDK_TYPES = {
SimpleKeyring,
HdKeyring,
Expand All @@ -64,6 +69,11 @@ export const KEYRING_SDK_TYPES = {
EthImKeyKeyring,
};

export type KeyringSerializedData<T = any> = {
type: string;
data: T;
};

interface MemStoreState {
isUnlocked: boolean;
keyringTypes: any[];
Expand Down Expand Up @@ -346,6 +356,11 @@ export class KeyringService extends EventEmitter {
this.setUnlocked();
}

// force store unencrypted keyring data if not exist
if (!this.store.getState().unencryptedKeyringData) {
await this.persistAllKeyrings();
}

return this.fullUpdate();
}

Expand Down Expand Up @@ -786,36 +801,43 @@ export class KeyringService extends EventEmitter {
* @param {string} password - The keyring controller password.
* @returns {Promise<boolean>} Resolves to true once keyrings are persisted.
*/
persistAllKeyrings(): Promise<boolean> {
async persistAllKeyrings(): Promise<boolean> {
if (!this.isUnlocked()) {
return Promise.reject(
new Error('KeyringController - password is not a string')
);
}

return Promise.all(
const serializedKeyrings = await Promise.all(
this.keyrings.map((keyring) => {
return Promise.all([keyring.type, keyring.serialize()]).then(
(serializedKeyringArray) => {
// Label the output values on each serialized Keyring:
return {
type: serializedKeyringArray[0],
data: serializedKeyringArray[1],
};
} as KeyringSerializedData;
}
);
})
)
.then((serializedKeyrings) => {
return passwordEncrypt({
data: serializedKeyrings,
password: this.password,
});
);

const unencryptedKeyringData = serializedKeyrings
.map(({ type, data }) => {
if (!UNENCRYPTED_IGNORE_KEYRING.includes(type as any)) {
return { type, data };
}
})
.then((encryptedString) => {
this.store.updateState({ vault: encryptedString });
return true;
});
.filter(Boolean) as KeyringSerializedData[];

const encryptedString = await passwordEncrypt({
data: serializedKeyrings,
password: this.password,
});

this.store.updateState({ vault: encryptedString, unencryptedKeyringData });

return true;
}

/**
Expand Down

0 comments on commit 60a7619

Please sign in to comment.