Skip to content

Commit 3bdbfbf

Browse files
chore: refactoring & tests for ensureDelegatedIdentityKey with Secure Store
1 parent 90458ae commit 3bdbfbf

File tree

15 files changed

+121
-54
lines changed

15 files changed

+121
-54
lines changed

suite-common/secure-storage/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export type {
66
EncryptionError,
77
} from './secureStorage';
88
export { asEncryptedHex, EncryptionUnavailable, DecryptionFailed } from './secureStorage';
9+
export type { SecureStorageDep } from './secureStorage';

suite-common/secure-storage/src/secureStorage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export const DecryptionFailed = (): DecryptionFailed => ({
2727
export type EncryptionError = EncryptionUnavailable;
2828
export type DecryptionError = EncryptionUnavailable | DecryptionFailed;
2929

30+
export type SecureStorageDep = { secureStorage: SecureStorage };
31+
3032
export interface SecureStorage {
3133
encrypt: <T extends EncryptableBranded>(params: {
3234
value: T;

suite-common/suite-sync-storage/src/Owner.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ export const CreateSuiteSyncOwnerError = (message: string): CreateSuiteSyncOwner
1111
export type CreateSuiteSyncOwner = (params: {
1212
data: string;
1313
}) => Result<SuiteSyncOwner, CreateSuiteSyncOwnerError>;
14+
15+
export type CreateSuiteSyncOwnerDep = { createSuiteSyncOwner: CreateSuiteSyncOwner };

suite-common/suite-sync-storage/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ export type {
22
SuiteSyncStorageRepository,
33
CreateSuiteSyncStorageRepository,
44
CreateSuiteStorage,
5+
CreateSuiteStorageDep,
56
} from './SuiteSyncStorageRepository';
67
export { createSuiteSyncStorageRepositoryFactory } from './SuiteSyncStorageRepository';
78
export type { SuiteSyncStorage } from './SuiteSyncStorage';
8-
export type { CreateSuiteSyncOwner } from './Owner';
9+
export type { CreateSuiteSyncOwner, CreateSuiteSyncOwnerDep } from './Owner';
910
export { CreateSuiteSyncOwnerError } from './Owner';
1011
export type {
1112
SuiteSync,

suite-common/suite-sync/src/createSuiteSyncCompositionRoot.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { Dispatch } from '@reduxjs/toolkit';
22

3-
import { SecureStorage } from '@suite-common/secure-storage';
3+
import { SecureStorageDep } from '@suite-common/secure-storage';
44
import {
5-
CreateSuiteStorage,
6-
CreateSuiteSyncOwner,
5+
CreateSuiteStorageDep,
6+
CreateSuiteSyncOwnerDep,
77
SuiteSync,
88
createSuiteSyncStorageRepositoryFactory,
99
} from '@suite-common/suite-sync-storage';
1010
import {
1111
createEnsureDelegatedIdentityKey,
12-
createGetDelegatedIdentityKey,
12+
createLoadDelegatedIdentityKeyFromState,
1313
createSaveDelegatedIdentityKey,
1414
selectAllDeviceOwners,
15+
selectDeviceDelegatedIdentityKey,
1516
} from '@suite-common/wallet-core';
1617

1718
import {
@@ -29,13 +30,12 @@ import { selectSuiteSyncRelayUrl } from './suiteSyncSelectors';
2930
import { createTurnOffSuiteSync } from './turnOffSuiteSync';
3031

3132
type CreateSuiteSyncCompositionRootDeps = {
32-
createSuiteStorage: CreateSuiteStorage;
33-
createSuiteSyncOwner: CreateSuiteSyncOwner;
3433
getState: () => any;
3534
dispatch: Dispatch;
3635
trezorConnect: EnsureSuiteSyncOwnerDeps['trezorConnect'];
37-
secureStorage: SecureStorage;
38-
};
36+
} & CreateSuiteStorageDep &
37+
CreateSuiteSyncOwnerDep &
38+
SecureStorageDep;
3939

4040
export const createSuiteSyncCompositionRoot = (
4141
deps: CreateSuiteSyncCompositionRootDeps,
@@ -61,10 +61,11 @@ export const createSuiteSyncCompositionRoot = (
6161

6262
// Todo: this shall be extracted upstream in the composition root
6363
const ensureDelegatedIdentityKey = createEnsureDelegatedIdentityKey({
64-
getDelegatedIdentityKey: createGetDelegatedIdentityKey({
64+
loadDelegatedIdentityKeyFromState: createLoadDelegatedIdentityKeyFromState({
6565
dispatch: deps.dispatch,
66-
getState: deps.getState,
6766
secureStorage: deps.secureStorage,
67+
getDeviceDelegatedIdentityKey: deviceId =>
68+
selectDeviceDelegatedIdentityKey(deps.getState(), deviceId),
6869
}),
6970
saveDelegatedIdentityKey: createSaveDelegatedIdentityKey({
7071
dispatch: deps.dispatch,

suite-common/suite-sync/src/device/ensureSuiteSyncOwnerKeys.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export type EnsureSuiteSyncOwnerKeys = (
3535
>
3636
>;
3737

38+
export type EnsureSuiteSyncOwnerDep = {
39+
ensureSuiteSyncOwnerKeys: EnsureSuiteSyncOwnerKeys;
40+
};
41+
3842
export const createEnsureSuiteSyncOwnerKeys =
3943
(deps: EnsureSuiteSyncOwnerDeps): EnsureSuiteSyncOwnerKeys =>
4044
async ({ device, delegatedKey }) => {

suite-common/suite-sync/src/refreshSuiteSyncKeys.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
import { Dispatch } from '@reduxjs/toolkit';
22

3-
import { EncryptionUnavailable } from '@suite-common/secure-storage';
43
import { CreateSuiteSyncOwnerError } from '@suite-common/suite-sync-storage';
54
import { TrezorDevice } from '@suite-common/suite-types';
65
import {
76
DeviceCancelledErr,
87
DeviceError,
9-
EnsureDelegatedIdentityKey,
8+
EnsureDelegatedIdentityKeyDep,
109
ProofOfDelegatedSignFailed,
1110
deviceActions,
1211
selectDevices,
1312
} from '@suite-common/wallet-core';
1413
import { isTrezorDeviceWithState } from '@suite-common/wallet-utils';
1514
import { Result, err, ok } from '@trezor/type-utils';
1615

17-
import { EnsureSuiteSyncOwnerKeys } from './device/ensureSuiteSyncOwnerKeys';
16+
import { EnsureSuiteSyncOwnerDep } from './device/ensureSuiteSyncOwnerKeys';
1817

1918
export type RefreshSuiteSyncKeysDeps = {
2019
getState: () => any;
2120
dispatch: Dispatch;
22-
ensureDelegatedIdentityKey: EnsureDelegatedIdentityKey;
23-
ensureSuiteSyncOwnerKeys: EnsureSuiteSyncOwnerKeys;
24-
};
21+
} & EnsureSuiteSyncOwnerDep &
22+
EnsureDelegatedIdentityKeyDep;
2523

2624
type RefreshSuiteSyncKeysParams = {
2725
device: TrezorDevice;
@@ -42,7 +40,6 @@ export type RefreshSuiteSyncKeys = (
4240
void,
4341
| DeviceError
4442
| DeviceCancelledErr
45-
| EncryptionUnavailable
4643
| DeviceDoesNotSupportSuiteSyncErr
4744
| ProofOfDelegatedSignFailed
4845
| CreateSuiteSyncOwnerError
@@ -69,9 +66,7 @@ export const createRefreshSuiteSyncKeys =
6966
return err(DeviceDoesNotSupportSuiteSyncErr());
7067
}
7168

72-
const delegatedKeyResult = await deps.ensureDelegatedIdentityKey({
73-
device,
74-
});
69+
const delegatedKeyResult = await deps.ensureDelegatedIdentityKey({ device });
7570

7671
if (!delegatedKeyResult.ok) {
7772
return delegatedKeyResult;

suite-common/suite-sync/src/storage/subscribeSuiteSyncStorage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ export const createSubscribeSuiteSyncStorage =
5151
);
5252

5353
return;
54+
55+
// Those errors are most likely due to Bug in the code or data corruption
5456
case 'CreateSuiteSyncOwnerError':
55-
case 'EncryptionUnavailable':
5657
case 'ProofOfDelegatedSignFailed':
5758
console.error(result.error);
5859
// Todo: dispatch better notification

suite-common/wallet-core/src/device/delegatedIdentityKey/ensureDelegatedIdentityKey.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1-
import { EncryptionUnavailable } from '@suite-common/secure-storage';
21
import { DelegatedIdentityKey, TrezorDeviceWithState } from '@suite-common/suite-types';
32
// Circular issue, see: https://github.com/trezor/trezor-suite/issues/21553
43
import { selectThp } from '@suite-common/thp/src/thpSelectors';
54
import { Result, ok } from '@trezor/type-utils';
65

76
import { DeviceCancelledErr, DeviceError } from '../deviceUtils';
8-
import { GetDelegatedIdentityKey } from './getDelegatedIdentityKey';
9-
import { retrieveDelegatedIdentityKey } from './retrieveDelegatedIdentityKey';
10-
import { SaveDelegatedIdentityKey } from './saveDelegatedIdentityKey';
7+
import { LoadDelegatedIdentityKeyFromStateDep } from './loadDelegatedIdentityKeyFromState';
8+
import { retrieveDelegatedIdentityKeyFromDevice } from './retrieveDelegatedIdentityKeyFromDevice';
9+
import { SaveDelegatedIdentityKeyDep } from './saveDelegatedIdentityKey';
1110

1211
type EnsureDelegatedIdentityKeyParams = {
1312
device: TrezorDeviceWithState;
1413
};
1514

1615
export type EnsureDelegatedIdentityKey = (
1716
params: EnsureDelegatedIdentityKeyParams,
18-
) => Promise<
19-
Result<DelegatedIdentityKey, DeviceError | DeviceCancelledErr | EncryptionUnavailable>
20-
>;
21-
22-
type EnsureDelegatedIdentityKeyDeps = {
23-
getState: () => any;
24-
getDelegatedIdentityKey: GetDelegatedIdentityKey;
25-
saveDelegatedIdentityKey: SaveDelegatedIdentityKey;
17+
) => Promise<Result<DelegatedIdentityKey, DeviceError | DeviceCancelledErr>>;
18+
19+
type EnsureDelegatedIdentityKeyDeps = { getState: () => any } & SaveDelegatedIdentityKeyDep &
20+
LoadDelegatedIdentityKeyFromStateDep;
21+
22+
export type EnsureDelegatedIdentityKeyDep = {
23+
ensureDelegatedIdentityKey: EnsureDelegatedIdentityKey;
2624
};
2725

2826
export const createEnsureDelegatedIdentityKey =
2927
(deps: EnsureDelegatedIdentityKeyDeps): EnsureDelegatedIdentityKey =>
3028
async ({ device }) => {
31-
const currentDelegatedKey = await deps.getDelegatedIdentityKey({
29+
const currentDelegatedKey = await deps.loadDelegatedIdentityKeyFromState({
3230
deviceId: device.id,
3331
});
3432

@@ -37,7 +35,7 @@ export const createEnsureDelegatedIdentityKey =
3735
}
3836

3937
const thpStaticHostKey = selectThp(deps.getState()).staticKey;
40-
const result = await retrieveDelegatedIdentityKey({ device, thpStaticHostKey });
38+
const result = await retrieveDelegatedIdentityKeyFromDevice({ device, thpStaticHostKey });
4139

4240
if (!result.ok) {
4341
return result;

suite-common/wallet-core/src/device/delegatedIdentityKey/getDelegatedIdentityKey.ts renamed to suite-common/wallet-core/src/device/delegatedIdentityKey/loadDelegatedIdentityKeyFromState.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
import { Dispatch } from '@reduxjs/toolkit';
22

3-
import { SecureStorage } from '@suite-common/secure-storage';
3+
import { EncryptedHex, SecureStorageDep } from '@suite-common/secure-storage';
44
import { DelegatedIdentityKey } from '@suite-common/suite-types';
55
import { exhaustive } from '@trezor/type-utils';
66

77
import { deviceActions } from '../deviceActions';
8-
import { selectPersistentDeviceData } from '../deviceSelectors';
98

10-
export type GetCurrentDelegatedIdentityKeyDeps = {
11-
getState: () => any;
12-
secureStorage: SecureStorage;
9+
export type LoadDelegatedIdentityKeyFromStateDeps = {
10+
getDeviceDelegatedIdentityKey: (deviceId: string) => EncryptedHex<DelegatedIdentityKey> | null;
1311
dispatch: Dispatch;
14-
};
12+
} & SecureStorageDep;
1513

16-
export type GetDelegatedIdentityKeyParams = {
14+
export type LoadDelegatedIdentityKeyFromStateParams = {
1715
deviceId: string;
1816
};
1917

20-
export type GetDelegatedIdentityKey = (
21-
params: GetDelegatedIdentityKeyParams,
18+
export type LoadDelegatedIdentityKeyFromStateDep = {
19+
loadDelegatedIdentityKeyFromState: LoadDelegatedIdentityKeyFromState;
20+
};
21+
22+
export type LoadDelegatedIdentityKeyFromState = (
23+
params: LoadDelegatedIdentityKeyFromStateParams,
2224
) => Promise<DelegatedIdentityKey | null>;
2325

24-
export const createGetDelegatedIdentityKey =
25-
(deps: GetCurrentDelegatedIdentityKeyDeps): GetDelegatedIdentityKey =>
26+
export const createLoadDelegatedIdentityKeyFromState =
27+
(deps: LoadDelegatedIdentityKeyFromStateDeps): LoadDelegatedIdentityKeyFromState =>
2628
async ({ deviceId }) => {
27-
const persistedData = selectPersistentDeviceData(deps.getState());
28-
const devicePersistedData = persistedData.find(it => it.device_id === deviceId);
29-
30-
const encryptedCurrentDelegatedKey = devicePersistedData?.delegatedIdentityKey ?? null;
29+
const encryptedCurrentDelegatedKey = deps.getDeviceDelegatedIdentityKey(deviceId);
3130

3231
if (encryptedCurrentDelegatedKey === null) {
3332
return null;

0 commit comments

Comments
 (0)