Align x-randomvalues with webCrypto (#1782)

* Align x-randomvalues with webCrypto

* Deno export from browser

* export * for x-ws

* export * from RN

* CHANGELOG
This commit is contained in:
Jaco
2023-04-21 10:27:30 +03:00
committed by GitHub
parent f5a3a6fd73
commit dfd07e04d4
14 changed files with 39 additions and 42 deletions
+5 -4
View File
@@ -5,9 +5,10 @@ import { xglobal } from '@polkadot/x-global';
export { packageInfo } from './packageInfo.js';
export const crypto = xglobal.crypto;
// getRandomValues needs to be called on the crypto object,
// hence the need for the wrapper function
export function getRandomValues <T extends Uint8Array> (arr: T): T {
// We use x-global here - this prevents packagers such as rollup
// confusing this with the "normal" Node.js import and stubbing it
// (and also aligns with eg. x-fetch, where x-global is used)
return xglobal.crypto.getRandomValues(arr);
return crypto.getRandomValues(arr);
}
+1 -5
View File
@@ -1,8 +1,4 @@
// Copyright 2017-2023 @polkadot/x-randomvalues authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { packageInfo } from './packageInfo.js';
export function getRandomValues <T extends Uint8Array> (arr: T): T {
return crypto.getRandomValues(arr);
}
export * from './browser.js';
+9 -8
View File
@@ -1,16 +1,17 @@
// Copyright 2017-2023 @polkadot/x-randomvalues authors & contributors
// SPDX-License-Identifier: Apache-2.0
import crypto from 'node:crypto';
// Needs Node 15+ for webcrypto
import nodeCrypto from 'node:crypto';
import { extractGlobal } from '@polkadot/x-global';
export { packageInfo } from './packageInfo.js';
export const crypto = /*#__PURE__*/ extractGlobal('crypto', nodeCrypto.webcrypto);
// getRandomValues needs to be called on the crypto object,
// hence the need for the wrapper function
export function getRandomValues <T extends Uint8Array> (output: T): T {
const bytes = crypto.randomBytes(output.length);
for (let i = 0; i < bytes.length; i++) {
output[i] = bytes[i];
}
return output;
return crypto.getRandomValues(output);
}
+8 -1
View File
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// Adapted from https://github.com/LinusU/react-native-get-random-values/blob/85f48393821c23b83b89a8177f56d3a81dc8b733/index.js
//
// Copyright (c) 2018, 2020 Linus Unnebäck
// SPDX-License-Identifier: MIT
@@ -10,7 +11,7 @@ import { NativeModules } from 'react-native';
import { base64Decode } from '@polkadot/wasm-util/base64';
import { xglobal } from '@polkadot/x-global';
import { getRandomValues as getRandomValuesBrowser } from './browser.js';
import { crypto as cryptoBrowser, getRandomValues as getRandomValuesBrowser } from './browser.js';
import { insecureRandomValues } from './fallback.js';
export { packageInfo } from './packageInfo.js';
@@ -46,3 +47,9 @@ export const getRandomValues = (
? insecureRandomValues
: getRandomValuesRn
);
export const crypto = (
getRandomValues === getRandomValuesBrowser
? cryptoBrowser
: { getRandomValues }
);
+7
View File
@@ -0,0 +1,7 @@
// Copyright 2017-2023 @polkadot/x-randomvalues authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { exposeGlobal } from '@polkadot/x-global';
import { crypto } from '@polkadot/x-randomvalues';
exposeGlobal('crypto', crypto);