Skip to content

Commit

Permalink
Fix types in getSessionBackupPrivateKey (#3595)
Browse files Browse the repository at this point in the history
* Fix type issue around `getSessionBackupPrivateKey`

* Fix sending auth: null due to broken types around UIA

* Discard changes to src/crypto/index.ts

* Add comment

* Fix types

* Fix types for MatrixClient::addThreePid

* Iterate
  • Loading branch information
t3chguy authored Jul 12, 2023
1 parent 9602aa8 commit e82b5fe
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/@types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ export interface IBindThreePidBody {
sid: string;
}

export interface IAddThreePidBody {
client_secret: string;
id_server: string;
sid: string;
}

export interface IRelationsRequestOpts {
from?: string;
to?: string;
Expand Down
3 changes: 2 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ import {
IFilterResponse,
ITagsResponse,
IStatusResponse,
IAddThreePidBody,
} from "./@types/requests";
import {
EventType,
Expand Down Expand Up @@ -8519,7 +8520,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @returns Promise which resolves: on success
* @returns Rejects: with an error response.
*/
public addThreePid(creds: IBindThreePidBody, bind: boolean): Promise<{ submit_url?: string }> {
public addThreePid(creds: IAddThreePidBody, bind: boolean): Promise<{ submit_url?: string }> {
const path = "/account/3pid";
const data = {
threePidCreds: creds,
Expand Down
12 changes: 7 additions & 5 deletions src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1242,20 +1242,22 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
* @returns the key, if any, or null
*/
public async getSessionBackupPrivateKey(): Promise<Uint8Array | null> {
let key = await new Promise<Uint8Array | IEncryptedPayload | null>((resolve) => {
const encodedKey = await new Promise<Uint8Array | IEncryptedPayload | string | null>((resolve) => {
this.cryptoStore.doTxn("readonly", [IndexedDBCryptoStore.STORE_ACCOUNT], (txn) => {
this.cryptoStore.getSecretStorePrivateKey(txn, resolve, "m.megolm_backup.v1");
});
});

let key: Uint8Array | null = null;

// make sure we have a Uint8Array, rather than a string
if (key && typeof key === "string") {
key = new Uint8Array(olmlib.decodeBase64(fixBackupKey(key) || key));
if (typeof encodedKey === "string") {
key = new Uint8Array(olmlib.decodeBase64(fixBackupKey(encodedKey) || encodedKey));
await this.storeSessionBackupPrivateKey(key);
}
if (key && typeof key === "object" && "ciphertext" in key) {
if (encodedKey && typeof encodedKey === "object" && "ciphertext" in encodedKey) {
const pickleKey = Buffer.from(this.olmDevice.pickleKey);
const decrypted = await decryptAES(key, pickleKey, "m.megolm_backup.v1");
const decrypted = await decryptAES(encodedKey, pickleKey, "m.megolm_backup.v1");
key = olmlib.decodeBase64(decrypted);
}
return key;
Expand Down

0 comments on commit e82b5fe

Please sign in to comment.