Skip to content

Commit 029fb28

Browse files
committed
Fix up types
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
1 parent c59a6ab commit 029fb28

File tree

16 files changed

+71
-54
lines changed

16 files changed

+71
-54
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"@types/content-type": "^1.1.5",
8686
"@types/debug": "^4.1.7",
8787
"@types/jest": "^29.0.0",
88-
"@types/node": "18",
88+
"@types/node": "^22",
8989
"@types/sdp-transform": "^2.4.5",
9090
"@typescript-eslint/eslint-plugin": "^8.0.0",
9191
"@typescript-eslint/parser": "^8.0.0",

spec/integ/crypto/cross-signing.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe("cross-signing", () => {
7474
function createCryptoCallbacks(): CryptoCallbacks {
7575
return {
7676
getSecretStorageKey: (keys, name) => {
77-
return Promise.resolve<[string, Uint8Array]>(["key_id", encryptionKey]);
77+
return Promise.resolve<[string, Uint8Array<ArrayBuffer>]>(["key_id", encryptionKey]);
7878
},
7979
};
8080
}

spec/integ/crypto/crypto.spec.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,20 @@ describe("crypto", () => {
321321
*/
322322
function createCryptoCallbacks(): CryptoCallbacks {
323323
// Store the cached secret storage key and return it when `getSecretStorageKey` is called
324-
let cachedKey: { keyId: string; key: Uint8Array };
325-
const cacheSecretStorageKey = (keyId: string, keyInfo: SecretStorageKeyDescription, key: Uint8Array) => {
324+
let cachedKey: { keyId: string; key: Uint8Array<ArrayBuffer> };
325+
const cacheSecretStorageKey = (
326+
keyId: string,
327+
keyInfo: SecretStorageKeyDescription,
328+
key: Uint8Array<ArrayBuffer>,
329+
) => {
326330
cachedKey = {
327331
keyId,
328332
key,
329333
};
330334
};
331335

332-
const getSecretStorageKey = () => Promise.resolve<[string, Uint8Array]>([cachedKey.keyId, cachedKey.key]);
336+
const getSecretStorageKey = () =>
337+
Promise.resolve<[string, Uint8Array<ArrayBuffer>]>([cachedKey.keyId, cachedKey.key]);
333338

334339
return {
335340
cacheSecretStorageKey,

spec/unit/oidc/authorize.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ describe("oidc authorization", () => {
5353
jest.setSystemTime(now);
5454

5555
fetchMock.get(delegatedAuthConfig.issuer + ".well-known/openid-configuration", mockOpenIdConfiguration());
56-
globalThis.TextEncoder = TextEncoder;
56+
// XXX: jsdom lacks a TextEncoder and Node's implementation has marginally different types, so we fudge it
57+
(globalThis as any).TextEncoder = TextEncoder;
5758
});
5859

5960
beforeEach(() => {

src/@types/global.d.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,4 @@ declare global {
4444
// on webkit: we should check if we still need to do this
4545
webkitGetUserMedia?: unknown;
4646
}
47-
48-
export interface Uint8ArrayToBase64Options {
49-
alphabet?: "base64" | "base64url";
50-
omitPadding?: boolean;
51-
}
52-
53-
interface Uint8Array {
54-
// https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tobase64
55-
toBase64?(options?: Uint8ArrayToBase64Options): string;
56-
}
57-
58-
export interface Uint8ArrayFromBase64Options {
59-
alphabet?: "base64"; // Our fallback code only handles base64.
60-
lastChunkHandling?: "loose"; // Our fallback code doesn't support other handling at this time.
61-
}
62-
63-
interface Uint8ArrayConstructor {
64-
// https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.frombase64
65-
fromBase64?(base64: string, options?: Uint8ArrayFromBase64Options): Uint8Array;
66-
}
6747
}

src/base64.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ limitations under the License.
1818
* Base64 encoding and decoding utilities
1919
*/
2020

21+
declare global {
22+
export interface Uint8ArrayToBase64Options {
23+
alphabet?: "base64" | "base64url";
24+
omitPadding?: boolean;
25+
}
26+
27+
interface Uint8Array {
28+
// https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tobase64
29+
toBase64?(options?: Uint8ArrayToBase64Options): string;
30+
}
31+
32+
export interface Uint8ArrayFromBase64Options {
33+
alphabet?: "base64"; // Our fallback code only handles base64.
34+
lastChunkHandling?: "loose"; // Our fallback code doesn't support other handling at this time.
35+
}
36+
37+
interface Uint8ArrayConstructor {
38+
// https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.frombase64
39+
fromBase64?(base64: string, options?: Uint8ArrayFromBase64Options): Uint8Array<ArrayBuffer>;
40+
}
41+
}
42+
2143
function toBase64(uint8Array: Uint8Array, options: Uint8ArrayToBase64Options): string {
2244
if (typeof uint8Array.toBase64 === "function") {
2345
// Currently this is only supported in Firefox,
@@ -64,7 +86,7 @@ export function encodeUnpaddedBase64Url(uint8Array: Uint8Array): string {
6486
return toBase64(uint8Array, { alphabet: "base64url", omitPadding: true });
6587
}
6688

67-
function fromBase64(base64: string, options: Uint8ArrayFromBase64Options): Uint8Array {
89+
function fromBase64(base64: string, options: Uint8ArrayFromBase64Options): Uint8Array<ArrayBuffer> {
6890
if (typeof Uint8Array.fromBase64 === "function") {
6991
// Currently this is only supported in Firefox,
7092
// but we match the options in the hope in the future we can rely on it for all environments.
@@ -80,7 +102,7 @@ function fromBase64(base64: string, options: Uint8ArrayFromBase64Options): Uint8
80102
* @param base64 - The base64 to decode.
81103
* @returns The decoded data.
82104
*/
83-
export function decodeBase64(base64: string): Uint8Array {
105+
export function decodeBase64(base64: string): Uint8Array<ArrayBuffer> {
84106
// The function requires us to select an alphabet, but we don't know if base64url was used so we convert.
85107
return fromBase64(base64.replace(/-/g, "+").replace(/_/g, "/"), { alphabet: "base64", lastChunkHandling: "loose" });
86108
}

src/crypto-api/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,12 +1191,12 @@ export interface CryptoCallbacks {
11911191
keys: Record<string, SecretStorageKeyDescription>;
11921192
},
11931193
name: string,
1194-
) => Promise<[string, Uint8Array] | null>;
1194+
) => Promise<[string, Uint8Array<ArrayBuffer>] | null>;
11951195

11961196
/** @deprecated: unused with the Rust crypto stack. */
1197-
getCrossSigningKey?: (keyType: string, pubKey: string) => Promise<Uint8Array | null>;
1197+
getCrossSigningKey?: (keyType: string, pubKey: string) => Promise<Uint8Array<ArrayBuffer> | null>;
11981198
/** @deprecated: unused with the Rust crypto stack. */
1199-
saveCrossSigningKeys?: (keys: Record<string, Uint8Array>) => void;
1199+
saveCrossSigningKeys?: (keys: Record<string, Uint8Array<ArrayBuffer>>) => void;
12001200
/** @deprecated: unused with the Rust crypto stack. */
12011201
shouldUpgradeDeviceVerifications?: (users: Record<string, any>) => Promise<string[]>;
12021202

@@ -1210,7 +1210,7 @@ export interface CryptoCallbacks {
12101210
* @param keyInfo - secret storage key info
12111211
* @param key - private key to store
12121212
*/
1213-
cacheSecretStorageKey?: (keyId: string, keyInfo: SecretStorageKeyDescription, key: Uint8Array) => void;
1213+
cacheSecretStorageKey?: (keyId: string, keyInfo: SecretStorageKeyDescription, key: Uint8Array<ArrayBuffer>) => void;
12141214

12151215
/** @deprecated: unused with the Rust crypto stack. */
12161216
onSecretRequested?: (
@@ -1224,11 +1224,11 @@ export interface CryptoCallbacks {
12241224
/** @deprecated: unused with the Rust crypto stack. */
12251225
getDehydrationKey?: (
12261226
keyInfo: SecretStorageKeyDescription,
1227-
checkFunc: (key: Uint8Array) => void,
1228-
) => Promise<Uint8Array>;
1227+
checkFunc: (key: Uint8Array<ArrayBuffer>) => void,
1228+
) => Promise<Uint8Array<ArrayBuffer>>;
12291229

12301230
/** @deprecated: unused with the Rust crypto stack. */
1231-
getBackupKey?: () => Promise<Uint8Array>;
1231+
getBackupKey?: () => Promise<Uint8Array<ArrayBuffer>>;
12321232
}
12331233

12341234
/**
@@ -1304,7 +1304,7 @@ export interface GeneratedSecretStorageKey {
13041304
name?: string;
13051305
};
13061306
/** The raw generated private key. */
1307-
privateKey: Uint8Array;
1307+
privateKey: Uint8Array<ArrayBuffer>;
13081308
/** The generated key, encoded for display to the user per https://spec.matrix.org/v1.7/client-server-api/#key-representation. */
13091309
encodedPrivateKey?: string;
13101310
}

src/crypto-api/key-passphrase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function deriveRecoveryKeyFromPassphrase(
3030
salt: string,
3131
iterations: number,
3232
numBits = DEFAULT_BIT_SIZE,
33-
): Promise<Uint8Array> {
33+
): Promise<Uint8Array<ArrayBuffer>> {
3434
if (!globalThis.crypto.subtle || !TextEncoder) {
3535
throw new Error("Password-based backup is not available on this platform");
3636
}

src/rust-crypto/libolm_migration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ async function migrateBaseData(
161161
userId: string,
162162
deviceId: string,
163163
legacyStore: CryptoStore,
164-
pickleKey: Uint8Array,
164+
pickleKey: Uint8Array<ArrayBuffer>,
165165
storeHandle: RustSdkCryptoJs.StoreHandle,
166166
logger: Logger,
167167
): Promise<void> {
@@ -414,7 +414,7 @@ export async function migrateRoomSettingsFromLegacyCrypto({
414414

415415
async function getAndDecryptCachedSecretKey(
416416
legacyStore: CryptoStore,
417-
legacyPickleKey: Uint8Array,
417+
legacyPickleKey: Uint8Array<ArrayBuffer>,
418418
name: string,
419419
): Promise<string | undefined> {
420420
const key = await new Promise<any>((resolve) => {

src/rust-crypto/rust-crypto.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,8 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventH
15561556
}
15571557

15581558
// 2. Upload the encrypted bundle to the server
1559-
const uploadResponse = await this.http.uploadContent(bundle.encryptedData);
1559+
// XXX: `slice` workaround for TS5.9 lib DOM type changes which are incompatible with latest rust crypto release
1560+
const uploadResponse = await this.http.uploadContent(bundle.encryptedData.slice());
15601561
logger.info(`Uploaded encrypted key blob: ${JSON.stringify(uploadResponse)}`);
15611562

15621563
// 3. We may not share a room with the user, so get a fresh list of devices for the invited user.

0 commit comments

Comments
 (0)