Skip to content

Commit

Permalink
Merge branch 'main' into feat/etherscan-api-key
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwalsh0 authored Oct 1, 2024
2 parents 6374a95 + 991cace commit 2917ae4
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import {
getUserStorageAllFeatureEntries,
upsertUserStorage,
} from './services';
import { waitForExpectedValue } from './utils';

// TODO: add external NetworkController event
// Need to listen for when a network gets added
Expand Down Expand Up @@ -320,6 +319,7 @@ export default class UserStorageController extends BaseController<
);
},
getInternalAccountsList: async (): Promise<InternalAccount[]> => {
// eslint-disable-next-line @typescript-eslint/await-thenable
const internalAccountsList = await this.messagingSystem.call(
'AccountsController:listAccounts',
);
Expand Down Expand Up @@ -811,18 +811,9 @@ export default class UserStorageController extends BaseController<

// Create new accounts to match the user storage accounts list

// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const _ of Array.from({
length: numberOfAccountsToAdd,
})) {
const expectedAccountsCountAfterAddition =
this.#accounts.addedAccountsCount + 1;
for (let i = 0; i < numberOfAccountsToAdd; i++) {
await this.messagingSystem.call('KeyringController:addNewAccount');
await waitForExpectedValue(
() => this.#accounts.addedAccountsCount,
expectedAccountsCountAfterAddition,
5000,
);

this.#config?.accountSyncing?.onAccountAdded?.(profileId);
}
}
Expand All @@ -831,7 +822,7 @@ export default class UserStorageController extends BaseController<
// Get the internal accounts list again since new accounts might have been added in the previous step
internalAccountsList = await this.#accounts.getInternalAccountsList();

for await (const internalAccount of internalAccountsList) {
for (const internalAccount of internalAccountsList) {
const userStorageAccount = userStorageAccountsList.find(
(account) => account.a === internalAccount.address,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ export const mapInternalAccountToUserStorageAccount = (
internalAccount: InternalAccount,
): UserStorageAccount => {
const { address, id, metadata } = internalAccount;
const { name, nameLastUpdatedAt } = metadata;

return {
[USER_STORAGE_VERSION_KEY]: USER_STORAGE_VERSION,
a: address,
i: id,
n: metadata.name,
nlu: metadata.nameLastUpdatedAt,
n: name,
...(isNameDefaultAccountName(name) ? {} : { nlu: nameLastUpdatedAt }),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,26 @@ export async function getUserStorageAllFeatureEntries(
return null;
}

const decryptedData = userStorage?.flatMap((entry) => {
const decryptedData: string[] = [];

for (const entry of userStorage) {
if (!entry.Data) {
return [];
continue;
}

return encryption.decryptString(
entry.Data,
opts.storageKey,
nativeScryptCrypto,
);
});
try {
const data = await encryption.decryptString(
entry.Data,
opts.storageKey,
nativeScryptCrypto,
);
decryptedData.push(data);
} catch {
// do nothing
}
}

return (await Promise.allSettled(decryptedData))
.map((d) => (d.status === 'fulfilled' ? d.value : undefined))
.filter((d): d is string => d !== undefined);
return decryptedData;
} catch (e) {
log.error('Failed to get user storage', e);
return null;
Expand Down Expand Up @@ -208,18 +213,14 @@ export async function batchUpsertUserStorage(

const { bearerToken, path, storageKey, nativeScryptCrypto } = opts;

const encryptedData = await Promise.all(
data.map(async (d) => {
return [
createSHA256Hash(d[0] + storageKey),
await encryption.encryptString(
d[1],
opts.storageKey,
nativeScryptCrypto,
),
];
}),
);
const encryptedData: string[][] = [];

for (const d of data) {
encryptedData.push([
createSHA256Hash(d[0] + storageKey),
await encryption.encryptString(d[1], opts.storageKey, nativeScryptCrypto),
]);
}

const url = new URL(`${USER_STORAGE_ENDPOINT}/${path}`);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setDifference, setIntersection, waitForExpectedValue } from './utils';
import { setDifference, setIntersection } from './utils';

describe('utils - setDifference()', () => {
it('should return the difference between 2 sets', () => {
Expand Down Expand Up @@ -27,22 +27,3 @@ describe('utils - setIntersection()', () => {
expect(inBothSetsWithParamsReversed).toStrictEqual([3]);
});
});

describe('utils - waitForExpectedValue()', () => {
it('should resolve when the expected value is returned', async () => {
const expectedValue = 'expected value';
const getter = jest.fn().mockReturnValue(expectedValue);

const value = await waitForExpectedValue(getter, expectedValue);
expect(value).toBe(expectedValue);
});

it('should reject when the timeout is reached', async () => {
const expectedValue = 'expected value';
const getter = jest.fn().mockReturnValue('wrong value');

await expect(
waitForExpectedValue(getter, expectedValue, 100),
).rejects.toThrow('Timed out waiting for expected value');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,3 @@ export function setIntersection<TItem>(
a.forEach((e) => b.has(e) && intersection.add(e));
return intersection;
}

/**
*
* Waits for a value to be returned from a getter function.
*
* @param getter - Function that returns the value to check
* @param expectedValue - The value to wait for
* @param timeout - The time to wait before timing out
* @returns A promise that resolves when the expected value is returned
* or rejects if the timeout is reached.
*/
export function waitForExpectedValue<TVariable>(
getter: () => TVariable,
expectedValue: TVariable,
timeout = 1000,
): Promise<TVariable> {
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
const value = getter();
if (value === expectedValue) {
clearInterval(interval);
resolve(value);
}
}, 100);

setTimeout(() => {
clearInterval(interval);
reject(new Error('Timed out waiting for expected value'));
}, timeout);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type CachedEntry = {
key: Uint8Array;
};

const MAX_PASSWORD_CACHES = 3;
const MAX_SALT_CACHES = 10;
const MAX_PASSWORD_CACHES = 100;
const MAX_SALT_CACHES = 100;

/**
* In-Memory Caching derived keys based from a given salt and password.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class EncryptorDecryptor {
p: SCRYPT_p,
dkLen: ALGORITHM_KEY_SIZE,
},
randomBytes(SCRYPT_SALT_SIZE),
undefined,
nativeScryptCrypto,
);

Expand Down

0 comments on commit 2917ae4

Please sign in to comment.