Skip to content

Commit 0dc400f

Browse files
chore: tests for all getDelegatedKeys methods
1 parent 99ba633 commit 0dc400f

File tree

6 files changed

+166
-43
lines changed

6 files changed

+166
-43
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import {
88
createSuiteSyncStorageRepositoryFactory,
99
} from '@suite-common/suite-sync-storage';
1010
import {
11+
RetrieveDelegatedIdentityKeyFromDeviceDeps,
1112
createEnsureDelegatedIdentityKey,
1213
createLoadDelegatedIdentityKeyFromState,
14+
createRetrieveDelegatedIdentityKeyFromDevice,
1315
createSaveDelegatedIdentityKey,
1416
selectAllDeviceOwners,
1517
selectDeviceDelegatedIdentityKey,
@@ -28,11 +30,11 @@ import { createSubscriptionStorage } from './storage/subscriptionStorage';
2830
import { createUnsubscribeSuiteSyncStorage } from './storage/unsubscribeSuiteSync';
2931
import { selectSuiteSyncRelayUrl } from './suiteSyncSelectors';
3032
import { createTurnOffSuiteSync } from './turnOffSuiteSync';
31-
3233
type CreateSuiteSyncCompositionRootDeps = {
3334
getState: () => any;
3435
dispatch: Dispatch;
35-
trezorConnect: EnsureSuiteSyncOwnerDeps['trezorConnect'];
36+
trezorConnect: EnsureSuiteSyncOwnerDeps['trezorConnect'] &
37+
RetrieveDelegatedIdentityKeyFromDeviceDeps['trezorConnect'];
3638
} & CreateSuiteStorageDep &
3739
CreateSuiteSyncOwnerDep &
3840
SecureStorageDep;
@@ -67,6 +69,9 @@ export const createSuiteSyncCompositionRoot = (
6769
getDeviceDelegatedIdentityKey: deviceId =>
6870
selectDeviceDelegatedIdentityKey(deps.getState(), deviceId),
6971
}),
72+
retrieveDelegatedIdentityKeyFromDevice: createRetrieveDelegatedIdentityKeyFromDevice({
73+
trezorConnect: deps.trezorConnect,
74+
}),
7075
saveDelegatedIdentityKey: createSaveDelegatedIdentityKey({
7176
dispatch: deps.dispatch,
7277
secureStorage: deps.secureStorage,

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@ import { Result, ok } from '@trezor/type-utils';
55

66
import { DeviceCancelledErr, DeviceError } from '../deviceUtils';
77
import { LoadDelegatedIdentityKeyFromStateDep } from './loadDelegatedIdentityKeyFromState';
8-
import { retrieveDelegatedIdentityKeyFromDevice } from './retrieveDelegatedIdentityKeyFromDevice';
8+
import {
9+
RetrieveDelegatedIdentityKeyFromDeviceDep,
10+
RetrieveDelegatedIdentityKeyParams,
11+
} from './retrieveDelegatedIdentityKeyFromDevice';
912
import { SaveDelegatedIdentityKeyDep } from './saveDelegatedIdentityKey';
1013

11-
type EnsureDelegatedIdentityKeyParams = {
12-
device: TrezorDeviceWithState;
14+
export type EnsureDelegatedIdentityKeyParams = {
15+
device: Pick<TrezorDeviceWithState, 'id'> & RetrieveDelegatedIdentityKeyParams['device'];
1316
};
1417

1518
export type EnsureDelegatedIdentityKey = (
1619
params: EnsureDelegatedIdentityKeyParams,
1720
) => Promise<Result<DelegatedIdentityKey, DeviceError | DeviceCancelledErr>>;
1821

19-
type EnsureDelegatedIdentityKeyDeps = { getState: () => any } & SaveDelegatedIdentityKeyDep &
20-
LoadDelegatedIdentityKeyFromStateDep;
22+
export type EnsureDelegatedIdentityKeyDeps = { getState: () => any } & SaveDelegatedIdentityKeyDep &
23+
LoadDelegatedIdentityKeyFromStateDep &
24+
RetrieveDelegatedIdentityKeyFromDeviceDep;
2125

2226
export type EnsureDelegatedIdentityKeyDep = {
2327
ensureDelegatedIdentityKey: EnsureDelegatedIdentityKey;
@@ -35,7 +39,10 @@ export const createEnsureDelegatedIdentityKey =
3539
}
3640

3741
const thpStaticHostKey = selectThp(deps.getState()).staticKey;
38-
const result = await retrieveDelegatedIdentityKeyFromDevice({ device, thpStaticHostKey });
42+
const result = await deps.retrieveDelegatedIdentityKeyFromDevice({
43+
device,
44+
thpStaticHostKey,
45+
});
3946

4047
if (!result.ok) {
4148
return result;
Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,61 @@
1-
import { TrezorDeviceWithState, asDelegatedIdentityKey } from '@suite-common/suite-types';
1+
import {
2+
DelegatedIdentityKey,
3+
TrezorDeviceWithState,
4+
asDelegatedIdentityKey,
5+
} from '@suite-common/suite-types';
26
import TrezorConnect from '@trezor/connect';
3-
import { err, ok } from '@trezor/type-utils';
7+
import { Result, err, ok } from '@trezor/type-utils';
48

59
import { DeviceCancelledErr, DeviceError, isCanceledErrorMessage } from '../deviceUtils';
610

7-
type RetrieveDelegatedIdentityKeyParams = {
8-
device: TrezorDeviceWithState;
11+
export type RetrieveDelegatedIdentityKeyParams = {
12+
device: Pick<
13+
TrezorDeviceWithState,
14+
'path' | 'state' | 'instance' | 'useEmptyPassphrase' | 'thp'
15+
>;
916
thpStaticHostKey: string | undefined;
1017
};
1118

12-
export const retrieveDelegatedIdentityKeyFromDevice = async ({
13-
device,
14-
thpStaticHostKey,
15-
}: RetrieveDelegatedIdentityKeyParams) => {
16-
const thpCredential = device.thp?.credentials?.[0].credential;
17-
18-
const result = await TrezorConnect.evoluGetDelegatedIdentityKey({
19-
device: {
20-
path: device.path,
21-
state: device.state,
22-
instance: device.instance ?? 0,
23-
},
24-
useEmptyPassphrase: device.useEmptyPassphrase ?? false,
25-
thp:
26-
thpStaticHostKey !== undefined && thpCredential !== undefined
27-
? {
28-
credential: thpCredential,
29-
staticHostKey: thpStaticHostKey,
30-
}
31-
: undefined,
32-
});
33-
34-
if (result.success) {
35-
return ok(asDelegatedIdentityKey(result.payload.private_key));
36-
}
37-
38-
if (isCanceledErrorMessage(result.payload.error)) {
39-
return err(DeviceCancelledErr());
40-
}
41-
42-
return err(DeviceError(result.payload.error));
19+
export type RetrieveDelegatedIdentityKeyFromDeviceDeps = {
20+
trezorConnect: Pick<typeof TrezorConnect, 'evoluGetDelegatedIdentityKey'>;
4321
};
22+
23+
type RetrieveDelegatedIdentityKeyFromDevice = (
24+
params: RetrieveDelegatedIdentityKeyParams,
25+
) => Promise<Result<DelegatedIdentityKey, DeviceCancelledErr | DeviceError>>;
26+
27+
export type RetrieveDelegatedIdentityKeyFromDeviceDep = {
28+
retrieveDelegatedIdentityKeyFromDevice: RetrieveDelegatedIdentityKeyFromDevice;
29+
};
30+
31+
export const createRetrieveDelegatedIdentityKeyFromDevice =
32+
(deps: RetrieveDelegatedIdentityKeyFromDeviceDeps): RetrieveDelegatedIdentityKeyFromDevice =>
33+
async ({ device, thpStaticHostKey }) => {
34+
const thpCredential = device.thp?.credentials?.[0].credential;
35+
36+
const result = await deps.trezorConnect.evoluGetDelegatedIdentityKey({
37+
device: {
38+
path: device.path,
39+
state: device.state,
40+
instance: device.instance ?? 0,
41+
},
42+
useEmptyPassphrase: device.useEmptyPassphrase ?? false,
43+
thp:
44+
thpStaticHostKey !== undefined && thpCredential !== undefined
45+
? {
46+
credential: thpCredential,
47+
staticHostKey: thpStaticHostKey,
48+
}
49+
: undefined,
50+
});
51+
52+
if (result.success) {
53+
return ok(asDelegatedIdentityKey(result.payload.private_key));
54+
}
55+
56+
if (isCanceledErrorMessage(result.payload.error)) {
57+
return err(DeviceCancelledErr());
58+
}
59+
60+
return err(DeviceError(result.payload.error));
61+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { asDelegatedIdentityKey } from '@suite-common/suite-types';
2+
import { asDeviceUniquePath } from '@trezor/connect';
3+
import { ok } from '@trezor/type-utils';
4+
5+
import {
6+
EnsureDelegatedIdentityKeyDeps,
7+
EnsureDelegatedIdentityKeyParams,
8+
createEnsureDelegatedIdentityKey,
9+
} from '../ensureDelegatedIdentityKey';
10+
11+
const deps: EnsureDelegatedIdentityKeyDeps = {
12+
loadDelegatedIdentityKeyFromState: () =>
13+
Promise.resolve(asDelegatedIdentityKey('redux-delegated-identity-key')),
14+
saveDelegatedIdentityKey: () => Promise.resolve(),
15+
retrieveDelegatedIdentityKeyFromDevice: () =>
16+
Promise.resolve(ok(asDelegatedIdentityKey('trezor-delegated-key-123'))),
17+
getState: () => {},
18+
};
19+
20+
const device: EnsureDelegatedIdentityKeyParams['device'] = {
21+
id: 'device-123-id',
22+
path: asDeviceUniquePath('1/2/3'),
23+
state: {
24+
staticSessionId: '1@2:3',
25+
},
26+
thp: {
27+
credentials: [
28+
{ credential: 'thp-credential', trezor_static_public_key: 'trezor_static_public_key' },
29+
],
30+
channel: 'thp-channel',
31+
},
32+
};
33+
34+
describe(createEnsureDelegatedIdentityKey.name, () => {
35+
it('returns saved DelegatedIdentityKey when successfully loaded (from Redux)', async () => {
36+
const ensureDelegatedIdentityKey = createEnsureDelegatedIdentityKey(deps);
37+
38+
const result = await ensureDelegatedIdentityKey({ device });
39+
40+
expect(result.ok).toBe(true);
41+
expect(result.ok && result.value).toBe('redux-delegated-identity-key');
42+
});
43+
44+
it('retrieves DelegatedIdentityKey from Device whe not loaded (from Redux)', async () => {
45+
const ensureDelegatedIdentityKey = createEnsureDelegatedIdentityKey({
46+
...deps,
47+
loadDelegatedIdentityKeyFromState: () => Promise.resolve(null),
48+
});
49+
50+
const result = await ensureDelegatedIdentityKey({ device });
51+
52+
expect(result.ok).toBe(true);
53+
expect(result.ok && result.value).toBe('trezor-delegated-key-123');
54+
});
55+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { DeviceIdentity, asDeviceUniquePath } from '@trezor/connect';
2+
3+
import {
4+
RetrieveDelegatedIdentityKeyFromDeviceDeps,
5+
RetrieveDelegatedIdentityKeyParams,
6+
createRetrieveDelegatedIdentityKeyFromDevice,
7+
} from '../retrieveDelegatedIdentityKeyFromDevice';
8+
9+
const device123: RetrieveDelegatedIdentityKeyParams['device'] = {
10+
path: asDeviceUniquePath('1/2/3'),
11+
state: { staticSessionId: '1@2:3' },
12+
};
13+
14+
const connectSimple: RetrieveDelegatedIdentityKeyFromDeviceDeps['trezorConnect'] = {
15+
evoluGetDelegatedIdentityKey: device =>
16+
Promise.resolve({
17+
device: device as DeviceIdentity,
18+
success: true,
19+
payload: { private_key: 'delegated-key-123' },
20+
}),
21+
};
22+
23+
describe(createRetrieveDelegatedIdentityKeyFromDevice.name, () => {
24+
it('calls TrezorConnect to get the delegated key', async () => {
25+
const retrieveDelegatedIdentityKeyFromDevice = createRetrieveDelegatedIdentityKeyFromDevice(
26+
{ trezorConnect: connectSimple },
27+
);
28+
29+
const result = await retrieveDelegatedIdentityKeyFromDevice({
30+
device: device123,
31+
thpStaticHostKey: 'thp-static-key',
32+
});
33+
34+
expect(result.ok).toBe(true);
35+
expect(result.ok && result.value).toBe('delegated-key-123');
36+
});
37+
});

suite-common/wallet-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export * from './device/delegatedIdentityKey/getProofOfDelegatedIdentity';
1313
export * from './device/delegatedIdentityKey/ensureDelegatedIdentityKey';
1414
export * from './device/delegatedIdentityKey/saveDelegatedIdentityKey';
1515
export * from './device/delegatedIdentityKey/loadDelegatedIdentityKeyFromState';
16+
export * from './device/delegatedIdentityKey/retrieveDelegatedIdentityKeyFromDevice';
1617
export * from './device/deviceActions';
1718
export * from './device/deviceConstants';
1819
export * from './device/deviceReducer';

0 commit comments

Comments
 (0)