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:
@@ -1,8 +1,4 @@
|
||||
// Copyright 2017-2023 @polkadot/x-fetch authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { packageInfo } from './packageInfo.js';
|
||||
|
||||
const _fetch = fetch;
|
||||
|
||||
export { _fetch as fetch };
|
||||
export * from './browser.js';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2017-2023 @polkadot/x-fetch authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { fetch, packageInfo } from './browser.js';
|
||||
export * from './browser.js';
|
||||
|
||||
@@ -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,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';
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 }
|
||||
);
|
||||
|
||||
@@ -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);
|
||||
@@ -1,8 +1,4 @@
|
||||
// Copyright 2017-2023 @polkadot/x-textencoder authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { packageInfo } from './packageInfo.js';
|
||||
|
||||
const _TextDecoder = TextDecoder;
|
||||
|
||||
export { _TextDecoder as TextDecoder };
|
||||
export * from './browser.js';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2017-2023 @polkadot/x-textencoder authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { TextDecoder, packageInfo } from './browser.js';
|
||||
export * from './browser.js';
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
// Copyright 2017-2023 @polkadot/x-textencoder authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { packageInfo } from './packageInfo.js';
|
||||
|
||||
const _TextEncoder = TextEncoder;
|
||||
|
||||
export { _TextEncoder as TextEncoder };
|
||||
export * from './browser.js';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2017-2023 @polkadot/x-textencoder authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { TextEncoder, packageInfo } from './browser.js';
|
||||
export * from './browser.js';
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
// Copyright 2017-2023 @polkadot/x-ws authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { packageInfo } from './packageInfo.js';
|
||||
|
||||
const _WebSocket = WebSocket;
|
||||
|
||||
export { _WebSocket as WebSocket };
|
||||
export * from './browser.js';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2017-2023 @polkadot/x-ws authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { WebSocket, packageInfo } from './browser.js';
|
||||
export * from './browser.js';
|
||||
|
||||
Reference in New Issue
Block a user