Skip to content

Commit 554804c

Browse files
authored
Clean up typescript types related to rust crypto (#4706)
* Simplify bootstrapSecretStorage logic might as well just export the keys immediately, rather than having multiple tests. * Clean up typescript types related to rust crypto A forthcoming release of matrix-rust-sdk-crypto-wasm tightens up a number of typescript types. In preparation, we need to get our house in order too.
1 parent 33648a7 commit 554804c

10 files changed

+38
-43
lines changed

spec/unit/rust-crypto/CrossSigningIdentity.spec.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe("CrossSigningIdentity", () => {
6363
hasMaster: true,
6464
hasSelfSigning: true,
6565
hasUserSigning: true,
66-
});
66+
} as unknown as RustSdkCryptoJs.CrossSigningStatus);
6767
// in secret storage
6868
secretStorage.get.mockResolvedValue("base64-saved-in-storage");
6969
await crossSigning.bootstrapCrossSigning({});
@@ -72,19 +72,23 @@ describe("CrossSigningIdentity", () => {
7272
});
7373

7474
it("should call bootstrapCrossSigning if a reset is forced", async () => {
75-
olmMachine.bootstrapCrossSigning.mockResolvedValue([]);
75+
olmMachine.bootstrapCrossSigning.mockResolvedValue(
76+
[] as unknown as RustSdkCryptoJs.CrossSigningBootstrapRequests,
77+
);
7678
await crossSigning.bootstrapCrossSigning({ setupNewCrossSigning: true });
7779
expect(olmMachine.bootstrapCrossSigning).toHaveBeenCalledWith(true);
7880
});
7981

8082
it("Shoud update 4S on reset if 4S is set up", async () => {
81-
olmMachine.bootstrapCrossSigning.mockResolvedValue([]);
83+
olmMachine.bootstrapCrossSigning.mockResolvedValue(
84+
[] as unknown as RustSdkCryptoJs.CrossSigningBootstrapRequests,
85+
);
8286
secretStorage.hasKey.mockResolvedValue(true);
8387
olmMachine.exportCrossSigningKeys.mockResolvedValue({
8488
masterKey: "base64_aaaaaaaaaa",
8589
self_signing_key: "base64_bbbbbbbbbbb",
8690
userSigningKey: "base64_cccccccc",
87-
});
91+
} as unknown as RustSdkCryptoJs.CrossSigningKeyExport);
8892
await crossSigning.bootstrapCrossSigning({ setupNewCrossSigning: true });
8993
expect(olmMachine.bootstrapCrossSigning).toHaveBeenCalledWith(true);
9094
expect(secretStorage.store).toHaveBeenCalledTimes(3);
@@ -95,8 +99,10 @@ describe("CrossSigningIdentity", () => {
9599
hasMaster: false,
96100
hasSelfSigning: false,
97101
hasUserSigning: false,
98-
});
99-
olmMachine.bootstrapCrossSigning.mockResolvedValue([]);
102+
} as RustSdkCryptoJs.CrossSigningStatus);
103+
olmMachine.bootstrapCrossSigning.mockResolvedValue(
104+
[] as unknown as RustSdkCryptoJs.CrossSigningBootstrapRequests,
105+
);
100106
await crossSigning.bootstrapCrossSigning({});
101107
expect(olmMachine.bootstrapCrossSigning).toHaveBeenCalledWith(true);
102108
});

spec/unit/rust-crypto/KeyClaimManager.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ describe("KeyClaimManager", () => {
7474
// ... and we now resolve the original promise with the resolver for that second promise.
7575
resolveCalledPromise(resolveCompletePromise);
7676
});
77-
return completePromise;
77+
await completePromise;
78+
return true;
7879
});
7980
});
8081
}
@@ -91,7 +92,7 @@ describe("KeyClaimManager", () => {
9192
fetchMock.postOnce("https://example.com/_matrix/client/v3/keys/claim", '{ "k": "v" }');
9293

9394
// also stub out olmMachine.markRequestAsSent
94-
olmMachine.markRequestAsSent.mockResolvedValueOnce(undefined);
95+
olmMachine.markRequestAsSent.mockResolvedValueOnce(true);
9596

9697
// fire off the request
9798
await keyClaimManager.ensureSessionsForUsers(new LogSpan(logger, "test"), [u1, u2]);

spec/unit/rust-crypto/OutgoingRequestProcessor.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ describe("OutgoingRequestProcessor", () => {
5656
return new Promise((resolve, _reject) => {
5757
olmMachine.markRequestAsSent.mockImplementationOnce(async () => {
5858
resolve(undefined);
59+
return true;
5960
});
6061
});
6162
}

spec/unit/rust-crypto/PerSessionKeyBackupDownloader.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ describe("PerSessionKeyBackupDownloader", () => {
302302

303303
beforeEach(async () => {
304304
mockRustBackupManager.getActiveBackupVersion.mockResolvedValue(null);
305-
mockOlmMachine.getBackupKeys.mockResolvedValue(null);
305+
mockOlmMachine.getBackupKeys.mockResolvedValue({} as RustSdkCryptoJs.BackupKeys);
306306

307307
// @ts-ignore access to private function
308308
getConfigSpy = jest.spyOn(downloader, "getOrCreateBackupConfiguration");
@@ -349,7 +349,7 @@ describe("PerSessionKeyBackupDownloader", () => {
349349
// it is trusted
350350
mockRustBackupManager.getActiveBackupVersion.mockResolvedValue(TestData.SIGNED_BACKUP_DATA.version!);
351351
// but the key is not cached
352-
mockOlmMachine.getBackupKeys.mockResolvedValue(null);
352+
mockOlmMachine.getBackupKeys.mockResolvedValue({} as RustSdkCryptoJs.BackupKeys);
353353

354354
downloader.onDecryptionKeyMissingError("!roomId", "sessionId");
355355

@@ -414,7 +414,7 @@ describe("PerSessionKeyBackupDownloader", () => {
414414

415415
// but at this point it's not trusted and we don't have the key
416416
mockRustBackupManager.getActiveBackupVersion.mockResolvedValue(null);
417-
mockOlmMachine.getBackupKeys.mockResolvedValue(null);
417+
mockOlmMachine.getBackupKeys.mockResolvedValue({} as RustSdkCryptoJs.BackupKeys);
418418

419419
fetchMock.get(`express:/_matrix/client/v3/room_keys/keys/:roomId/:sessionId`, mockCipherKey);
420420

spec/unit/rust-crypto/RoomEncryptor.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ describe("RoomEncryptor", () => {
126126
mockOlmMachine.shareRoomKey.mockImplementationOnce(async () => {
127127
insideOlmShareRoom.resolve();
128128
await deferredShare.promise;
129+
return [];
129130
});
130131

131132
roomEncryptor.prepareForEncryption(false, defaultDevicesIsolationMode);
@@ -151,7 +152,7 @@ describe("RoomEncryptor", () => {
151152
const firstTargetMembers = defer<void>();
152153
const secondTargetMembers = defer<void>();
153154

154-
mockOlmMachine.shareRoomKey.mockResolvedValue(undefined);
155+
mockOlmMachine.shareRoomKey.mockResolvedValue([]);
155156

156157
// Hook into this method to demonstrate the race condition
157158
mockRoom.getEncryptionTargetMembers
@@ -265,6 +266,7 @@ describe("RoomEncryptor", () => {
265266
capturedSettings = undefined;
266267
mockOlmMachine.shareRoomKey.mockImplementationOnce(async (roomId, users, encryptionSettings) => {
267268
capturedSettings = encryptionSettings.sharingStrategy;
269+
return [];
268270
});
269271
});
270272

spec/unit/rust-crypto/backup.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ describe("Upload keys to backup", () => {
9595
.mockResolvedValueOnce(mockBackupRequest(100))
9696
.mockResolvedValueOnce(mockBackupRequest(100))
9797
.mockResolvedValueOnce(mockBackupRequest(2))
98-
.mockResolvedValue(null);
98+
.mockResolvedValue(undefined);
9999

100100
mockOlmMachine.roomKeyCounts.mockResolvedValue({
101101
total: 602,
102102
// First iteration won't call roomKeyCounts(); it will be called on the second iteration after 200 keys have been saved.
103103
backedUp: 200,
104-
});
104+
} as unknown as RustSdkCryptoJs.RoomKeyCounts);
105105

106106
await rustBackupManager.checkKeyBackupAndEnable(false);
107107
await jest.runAllTimersAsync();
@@ -130,7 +130,7 @@ describe("Upload keys to backup", () => {
130130
});
131131

132132
// Only returns 2 keys on the first call, then none.
133-
mockOlmMachine.backupRoomKeys.mockResolvedValueOnce(mockBackupRequest(2)).mockResolvedValue(null);
133+
mockOlmMachine.backupRoomKeys.mockResolvedValueOnce(mockBackupRequest(2)).mockResolvedValue(undefined);
134134

135135
await rustBackupManager.checkKeyBackupAndEnable(false);
136136
await jest.runAllTimersAsync();

spec/unit/rust-crypto/rust-crypto.spec.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ describe("initRustCrypto", () => {
213213
jest.spyOn(Migration, "migrateMegolmSessions").mockResolvedValue(undefined);
214214

215215
const testOlmMachine = makeTestOlmMachine();
216-
testOlmMachine.trackedUsers.mockResolvedValue([]);
216+
testOlmMachine.trackedUsers.mockResolvedValue(new Set([]));
217217
jest.spyOn(OlmMachine, "initFromStore").mockResolvedValue(testOlmMachine);
218218
});
219219

@@ -806,11 +806,6 @@ describe("RustCrypto", () => {
806806
asJSON: jest.fn().mockReturnValue("{}"),
807807
}),
808808
saveBackupDecryptionKey: jest.fn(),
809-
crossSigningStatus: jest.fn().mockResolvedValue({
810-
hasMaster: true,
811-
hasSelfSigning: true,
812-
hasUserSigning: true,
813-
}),
814809
exportCrossSigningKeys: jest.fn().mockResolvedValue({
815810
masterKey: "sosecret",
816811
userSigningKey: "secrets",

src/rust-crypto/CrossSigningIdentity.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ export class CrossSigningIdentity {
104104
}
105105

106106
// Get the current device
107-
const device: RustSdkCryptoJs.Device = await this.olmMachine.getDevice(
107+
const device: RustSdkCryptoJs.Device = (await this.olmMachine.getDevice(
108108
this.olmMachine.userId,
109109
this.olmMachine.deviceId,
110-
);
110+
))!;
111111
try {
112112
// Sign the device with our cross-signing key and upload the signature
113113
const request: RustSdkCryptoJs.SignatureUploadRequest = await device.verify();
@@ -172,7 +172,8 @@ export class CrossSigningIdentity {
172172
* (If secret storage is *not* configured, we assume that the export will happen when it is set up)
173173
*/
174174
private async exportCrossSigningKeysToStorage(): Promise<void> {
175-
const exported: RustSdkCryptoJs.CrossSigningKeyExport | null = await this.olmMachine.exportCrossSigningKeys();
175+
const exported: RustSdkCryptoJs.CrossSigningKeyExport | undefined =
176+
await this.olmMachine.exportCrossSigningKeys();
176177
/* istanbul ignore else (this function is only called when we know the olm machine has keys) */
177178
if (exported?.masterKey) {
178179
await this.secretStorage.store("m.cross_signing.master", exported.masterKey);

src/rust-crypto/backup.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
386386

387387
while (!this.stopped) {
388388
// Get a batch of room keys to upload
389-
let request: RustSdkCryptoJs.KeysBackupRequest | null = null;
389+
let request: RustSdkCryptoJs.KeysBackupRequest | undefined = undefined;
390390
try {
391391
request = await logDuration(
392392
logger,

src/rust-crypto/rust-crypto.ts

+7-18
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,13 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventH
783783
await this.addSecretStorageKeyToSecretStorage(recoveryKey);
784784
}
785785

786-
const crossSigningStatus: RustSdkCryptoJs.CrossSigningStatus = await this.olmMachine.crossSigningStatus();
786+
const crossSigningPrivateKeys: RustSdkCryptoJs.CrossSigningKeyExport | undefined =
787+
await this.olmMachine.exportCrossSigningKeys();
787788
const hasPrivateKeys =
788-
crossSigningStatus.hasMaster && crossSigningStatus.hasSelfSigning && crossSigningStatus.hasUserSigning;
789+
crossSigningPrivateKeys &&
790+
crossSigningPrivateKeys.masterKey !== undefined &&
791+
crossSigningPrivateKeys.self_signing_key !== undefined &&
792+
crossSigningPrivateKeys.userSigningKey !== undefined;
789793

790794
// If we have cross-signing private keys cached, store them in secret
791795
// storage if they are not there already.
@@ -795,21 +799,6 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventH
795799
) {
796800
this.logger.info("bootstrapSecretStorage: cross-signing keys not yet exported; doing so now.");
797801

798-
const crossSigningPrivateKeys: RustSdkCryptoJs.CrossSigningKeyExport =
799-
await this.olmMachine.exportCrossSigningKeys();
800-
801-
if (!crossSigningPrivateKeys.masterKey) {
802-
throw new Error("missing master key in cross signing private keys");
803-
}
804-
805-
if (!crossSigningPrivateKeys.userSigningKey) {
806-
throw new Error("missing user signing key in cross signing private keys");
807-
}
808-
809-
if (!crossSigningPrivateKeys.self_signing_key) {
810-
throw new Error("missing self signing key in cross signing private keys");
811-
}
812-
813802
await this.secretStorage.store("m.cross_signing.master", crossSigningPrivateKeys.masterKey);
814803
await this.secretStorage.store("m.cross_signing.user_signing", crossSigningPrivateKeys.userSigningKey);
815804
await this.secretStorage.store("m.cross_signing.self_signing", crossSigningPrivateKeys.self_signing_key);
@@ -1819,7 +1808,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventH
18191808
* @param name - The name of the secret received.
18201809
*/
18211810
public async checkSecrets(name: string): Promise<void> {
1822-
const pendingValues: string[] = await this.olmMachine.getSecretsFromInbox(name);
1811+
const pendingValues: Set<string> = await this.olmMachine.getSecretsFromInbox(name);
18231812
for (const value of pendingValues) {
18241813
if (await this.handleSecretReceived(name, value)) {
18251814
// If we have a valid secret for that name there is no point of processing the other secrets values.

0 commit comments

Comments
 (0)