Skip to content

Commit

Permalink
QRCode: fix breakage on node 16 (#3527)
Browse files Browse the repository at this point in the history
[`crypto.getRandomValues`](https://nodejs.org/docs/latest-v18.x/api/crypto.html#cryptogetrandomvaluestypedarray)
was added to the nodejs library in node 17. However, it was actually available
in node 16, hiding under
[`crypto.webcrypto`](https://nodejs.org/docs/latest-v16.x/api/webcrypto.html#cryptogetrandomvaluestypedarray). We
have some shims in `src/crypto/crypto.ts`, so let's use them.

All of this means that we don't need to monkey-patch `crypto` to run the tests
on node 16.
  • Loading branch information
richvdh committed Jun 29, 2023
1 parent 24cee68 commit 1828826
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 27 deletions.
26 changes: 0 additions & 26 deletions spec/integ/crypto/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,7 @@ import { E2EKeyReceiver } from "../../test-utils/E2EKeyReceiver";
// to ensure that we don't end up with dangling timeouts.
jest.useFakeTimers();

let previousCrypto: Crypto | undefined;

beforeAll(async () => {
// Stub out global.crypto
previousCrypto = global["crypto"];

Object.defineProperty(global, "crypto", {
value: {
getRandomValues: function <T extends Uint8Array>(array: T): T {
array.fill(0x12);
return array;
},
},
});

// we use the libolm primitives in the test, so init the Olm library
await global.Olm.init();
});
Expand All @@ -82,18 +68,6 @@ afterEach(() => {
indexedDB = new IDBFactory();
});

// restore the original global.crypto
afterAll(() => {
if (previousCrypto === undefined) {
// @ts-ignore deleting a non-optional property. It *is* optional really.
delete global.crypto;
} else {
Object.defineProperty(global, "crypto", {
value: previousCrypto,
});
}
});

/** The homeserver url that we give to the test client, and where we intercept /sync, /keys, etc requests. */
const TEST_HOMESERVER_URL = "https://alice-server.com";

Expand Down
3 changes: 2 additions & 1 deletion src/crypto/verification/QRCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
* QR code key verification.
*/

import { crypto } from "../crypto";
import { VerificationBase as Base } from "./Base";
import { newKeyMismatchError, newUserCancelledError } from "./Error";
import { decodeBase64, encodeUnpaddedBase64 } from "../olmlib";
Expand Down Expand Up @@ -200,7 +201,7 @@ export class QRCodeData {

private static generateSharedSecret(): string {
const secretBytes = new Uint8Array(11);
global.crypto.getRandomValues(secretBytes);
crypto.getRandomValues(secretBytes);
return encodeUnpaddedBase64(secretBytes);
}

Expand Down

0 comments on commit 1828826

Please sign in to comment.