Skip to content

Commit 26f7732

Browse files
authored
chore: replace window with globalThis in TypeScript utils for better node compatibility (#132)
1 parent ad55c0c commit 26f7732

5 files changed

Lines changed: 12 additions & 16 deletions

File tree

examples/password_manager/frontend/src/lib/enums.ts

Whitespace-only changes.

frontend/ic_vetkeys/src/key_manager/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class KeyManager {
123123
vetkeyName: Uint8Array,
124124
): Promise<Uint8Array> {
125125
// create a random transport key
126-
const seed = window.crypto.getRandomValues(new Uint8Array(32));
126+
const seed = globalThis.crypto.getRandomValues(new Uint8Array(32));
127127
const tsk = new TransportSecretKey(seed);
128128
const encryptedVetkey = await this.canisterClient.get_encrypted_vetkey(
129129
keyOwner,

frontend/ic_vetkeys/src/utils/utils.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ test("hkdf using webcrypto", async () => {
233233
hash: "SHA-256",
234234
length: 32 * 8,
235235
};
236-
const derived = await window.crypto.subtle.deriveKey(
236+
const derived = await globalThis.crypto.subtle.deriveKey(
237237
algorithm,
238238
wckey,
239239
derivedAlgo,
@@ -242,7 +242,7 @@ test("hkdf using webcrypto", async () => {
242242
);
243243

244244
const derivedBytes = new Uint8Array(
245-
await window.crypto.subtle.exportKey("raw", derived),
245+
await globalThis.crypto.subtle.exportKey("raw", derived),
246246
);
247247
assertEqual(
248248
bytesToHex(derivedBytes),
@@ -318,7 +318,9 @@ test("AES-GCM encryption", async () => {
318318

319319
// Test appending random bytes
320320
for (let trial = 1; trial < 32; trial++) {
321-
const extraBytes = window.crypto.getRandomValues(new Uint8Array(trial));
321+
const extraBytes = globalThis.crypto.getRandomValues(
322+
new Uint8Array(trial),
323+
);
322324
const modMsg = new Uint8Array([...msg3, ...extraBytes]);
323325

324326
await expect(async () => {

frontend/ic_vetkeys/src/utils/utils.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ export class DerivedKeyMaterial {
440440
*/
441441
static async setup(bytes: Uint8Array) {
442442
const exportable = false;
443-
const hkdf = await window.crypto.subtle.importKey(
443+
const hkdf = await globalThis.crypto.subtle.importKey(
444444
"raw",
445445
bytes,
446446
"HKDF",
@@ -482,7 +482,7 @@ export class DerivedKeyMaterial {
482482
length: 32 * 8,
483483
};
484484

485-
return window.crypto.subtle.deriveKey(
485+
return globalThis.crypto.subtle.deriveKey(
486486
algorithm,
487487
this.#hkdf,
488488
gcmParams,
@@ -503,10 +503,10 @@ export class DerivedKeyMaterial {
503503
const gcmKey = await this.deriveAesGcmCryptoKey(domainSep);
504504

505505
// The nonce must never be reused with a given key
506-
const nonce = window.crypto.getRandomValues(new Uint8Array(12));
506+
const nonce = globalThis.crypto.getRandomValues(new Uint8Array(12));
507507

508508
const ciphertext = new Uint8Array(
509-
await window.crypto.subtle.encrypt(
509+
await globalThis.crypto.subtle.encrypt(
510510
{ name: "AES-GCM", iv: nonce },
511511
gcmKey,
512512
asBytes(message),
@@ -541,7 +541,7 @@ export class DerivedKeyMaterial {
541541
const gcmKey = await this.deriveAesGcmCryptoKey(domainSep);
542542

543543
try {
544-
const ptext = await window.crypto.subtle.decrypt(
544+
const ptext = await globalThis.crypto.subtle.decrypt(
545545
{ name: "AES-GCM", iv: nonce },
546546
gcmKey,
547547
ciphertext,
@@ -805,7 +805,7 @@ export class IbeSeed {
805805
*/
806806
static random() {
807807
return new IbeSeed(
808-
window.crypto.getRandomValues(new Uint8Array(SEED_BYTES)),
808+
globalThis.crypto.getRandomValues(new Uint8Array(SEED_BYTES)),
809809
);
810810
}
811811

frontend/ic_vetkeys/test/setup.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { beforeAll } from "vitest";
22
import indexeddb from "fake-indexeddb";
3-
import crypto from "node:crypto";
43

54
beforeAll(() => {
6-
Object.defineProperty(window, "crypto", {
7-
value: crypto.webcrypto,
8-
writable: true,
9-
});
10-
115
globalThis.indexedDB = indexeddb;
126
});

0 commit comments

Comments
 (0)