@pezkuwi/x-randomvalues 14.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/package.json +35 -0
- package/src/browser.ts +14 -0
- package/src/fallback.spec.ts +22 -0
- package/src/fallback.ts +28 -0
- package/src/mod.ts +4 -0
- package/src/node.ts +17 -0
- package/src/packageInfo.ts +6 -0
- package/src/react-native.ts +64 -0
- package/src/shim.ts +7 -0
- package/tsconfig.build.json +15 -0
- package/tsconfig.spec.json +17 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "Jaco Greeff <jacogr@gmail.com>",
|
|
3
|
+
"bugs": "https://github.com/pezkuwichain/pezkuwi-common/issues",
|
|
4
|
+
"description": "A cross-environment window.crypto.getRandomValues replacement",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": ">=18"
|
|
7
|
+
},
|
|
8
|
+
"homepage": "https://github.com/pezkuwichain/pezkuwi-common/tree/master/packages/x-randomvalues#readme",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"name": "@pezkuwi/x-randomvalues",
|
|
11
|
+
"repository": {
|
|
12
|
+
"directory": "packages/x-randomvalues",
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/pezkuwichain/pezkuwi-common.git"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"type": "module",
|
|
18
|
+
"version": "14.0.1",
|
|
19
|
+
"browser": "browser.js",
|
|
20
|
+
"main": "node.js",
|
|
21
|
+
"react-native": "react-native.js",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@pezkuwi/x-global": "14.0.1",
|
|
24
|
+
"tslib": "^2.8.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@pezkuwi/util": "14.0.1",
|
|
28
|
+
"@pezkuwi/wasm-util": "^7.5.3",
|
|
29
|
+
"@types/react-native": "^0.73.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@pezkuwi/util": "14.0.1",
|
|
33
|
+
"@pezkuwi/wasm-util": "*"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/browser.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/x-randomvalues authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { xglobal } from '@pezkuwi/x-global';
|
|
5
|
+
|
|
6
|
+
export { packageInfo } from './packageInfo.js';
|
|
7
|
+
|
|
8
|
+
export const crypto = xglobal.crypto;
|
|
9
|
+
|
|
10
|
+
// getRandomValues needs to be called on the crypto object,
|
|
11
|
+
// hence the need for the wrapper function
|
|
12
|
+
export function getRandomValues <T extends Uint8Array> (arr: T): T {
|
|
13
|
+
return crypto.getRandomValues(arr);
|
|
14
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/x-randomvalues authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { insecureRandomValues } from './fallback.js';
|
|
7
|
+
|
|
8
|
+
describe('fallback (insecure)', (): void => {
|
|
9
|
+
it('subsequent results does not match', (): void => {
|
|
10
|
+
expect(
|
|
11
|
+
insecureRandomValues(new Uint8Array(32)).toString()
|
|
12
|
+
).not.toEqual(
|
|
13
|
+
insecureRandomValues(new Uint8Array(32)).toString()
|
|
14
|
+
);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('generates with the supplied length', (): void => {
|
|
18
|
+
expect(
|
|
19
|
+
insecureRandomValues(new Uint8Array(128))
|
|
20
|
+
).toHaveLength(128);
|
|
21
|
+
});
|
|
22
|
+
});
|
package/src/fallback.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/x-randomvalues authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Adapted from https://github.com/LinusU/react-native-get-random-values/blob/85f48393821c23b83b89a8177f56d3a81dc8b733/index.js
|
|
5
|
+
//
|
|
6
|
+
// Copyright (c) 2018, 2020 Linus Unnebäck
|
|
7
|
+
// SPDX-License-Identifier: MIT
|
|
8
|
+
|
|
9
|
+
let warned = false;
|
|
10
|
+
|
|
11
|
+
export function insecureRandomValues <T extends Uint8Array> (arr: T): T {
|
|
12
|
+
if (!warned) {
|
|
13
|
+
console.warn('Using an insecure random number generator, this should only happen when running in a debugger without support for crypto');
|
|
14
|
+
warned = true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let r = 0;
|
|
18
|
+
|
|
19
|
+
for (let i = 0, count = arr.length; i < count; i++) {
|
|
20
|
+
if ((i & 0b11) === 0) {
|
|
21
|
+
r = Math.random() * 0x100000000;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
arr[i] = (r >>> ((i & 0b11) << 3)) & 0xff;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return arr;
|
|
28
|
+
}
|
package/src/mod.ts
ADDED
package/src/node.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/x-randomvalues authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Needs Node 15+ for webcrypto
|
|
5
|
+
import nodeCrypto from 'node:crypto';
|
|
6
|
+
|
|
7
|
+
import { extractGlobal } from '@pezkuwi/x-global';
|
|
8
|
+
|
|
9
|
+
export { packageInfo } from './packageInfo.js';
|
|
10
|
+
|
|
11
|
+
export const crypto = /*#__PURE__*/ extractGlobal('crypto', nodeCrypto.webcrypto);
|
|
12
|
+
|
|
13
|
+
// getRandomValues needs to be called on the crypto object,
|
|
14
|
+
// hence the need for the wrapper function
|
|
15
|
+
export function getRandomValues <T extends Uint8Array> (output: T): T {
|
|
16
|
+
return crypto.getRandomValues(output);
|
|
17
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/x-randomvalues authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Do not edit, auto-generated by @polkadot/dev
|
|
5
|
+
|
|
6
|
+
export const packageInfo = { name: '@polkadot/x-randomvalues', path: 'auto', type: 'auto', version: '14.0.1' };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/x-randomvalues authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Adapted from https://github.com/LinusU/react-native-get-random-values/blob/85f48393821c23b83b89a8177f56d3a81dc8b733/index.js
|
|
5
|
+
//
|
|
6
|
+
// Copyright (c) 2018, 2020 Linus Unnebäck
|
|
7
|
+
// SPDX-License-Identifier: MIT
|
|
8
|
+
|
|
9
|
+
import { NativeModules } from 'react-native';
|
|
10
|
+
|
|
11
|
+
import { base64Decode } from '@pezkuwi/wasm-util/base64';
|
|
12
|
+
import { xglobal } from '@pezkuwi/x-global';
|
|
13
|
+
|
|
14
|
+
import { crypto as cryptoBrowser, getRandomValues as getRandomValuesBrowser } from './browser.js';
|
|
15
|
+
|
|
16
|
+
export { packageInfo } from './packageInfo.js';
|
|
17
|
+
|
|
18
|
+
interface RNExt {
|
|
19
|
+
ExpoRandom: {
|
|
20
|
+
getRandomBase64String: (length: number) => string;
|
|
21
|
+
};
|
|
22
|
+
RNGetRandomValues: {
|
|
23
|
+
getRandomBase64: (length: number) => string;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @internal
|
|
29
|
+
*
|
|
30
|
+
* A getRandomValues util that detects and uses the available RN
|
|
31
|
+
* random utiliy generation functions.
|
|
32
|
+
**/
|
|
33
|
+
function getRandomValuesRn (output: Uint8Array): Uint8Array {
|
|
34
|
+
if (!NativeModules['ExpoRandom'] && !(NativeModules as RNExt).RNGetRandomValues) {
|
|
35
|
+
throw new Error('No secure random number generator available. This environment does not support crypto.getRandomValues and no React Native secure RNG module is available.');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return base64Decode(
|
|
39
|
+
(NativeModules as RNExt).RNGetRandomValues
|
|
40
|
+
? (NativeModules as RNExt).RNGetRandomValues.getRandomBase64(output.length)
|
|
41
|
+
: (NativeModules as RNExt).ExpoRandom.getRandomBase64String(output.length),
|
|
42
|
+
output
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Check for native RN modules first (highest priority)
|
|
47
|
+
const hasNativeRNModules = !!NativeModules['ExpoRandom'] || !!(NativeModules as RNExt).RNGetRandomValues;
|
|
48
|
+
const hasNativeCrypto = typeof xglobal.crypto === 'object' && typeof xglobal.crypto.getRandomValues === 'function';
|
|
49
|
+
|
|
50
|
+
export const getRandomValues = (
|
|
51
|
+
hasNativeRNModules
|
|
52
|
+
? getRandomValuesRn
|
|
53
|
+
: hasNativeCrypto
|
|
54
|
+
? getRandomValuesBrowser
|
|
55
|
+
: () => {
|
|
56
|
+
throw new Error('No secure random number generator available. This environment does not support crypto.getRandomValues.');
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
export const crypto = (
|
|
61
|
+
getRandomValues === getRandomValuesBrowser
|
|
62
|
+
? cryptoBrowser
|
|
63
|
+
: { getRandomValues }
|
|
64
|
+
);
|
package/src/shim.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": "..",
|
|
5
|
+
"outDir": "./build",
|
|
6
|
+
"rootDir": "./src"
|
|
7
|
+
},
|
|
8
|
+
"exclude": [
|
|
9
|
+
"**/*.spec.ts",
|
|
10
|
+
"**/mod.ts"
|
|
11
|
+
],
|
|
12
|
+
"references": [
|
|
13
|
+
{ "path": "../x-global/tsconfig.build.json" }
|
|
14
|
+
]
|
|
15
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": "..",
|
|
5
|
+
"outDir": "./build",
|
|
6
|
+
"rootDir": "./src",
|
|
7
|
+
"emitDeclarationOnly": false,
|
|
8
|
+
"noEmit": true
|
|
9
|
+
},
|
|
10
|
+
"include": [
|
|
11
|
+
"**/*.spec.ts"
|
|
12
|
+
],
|
|
13
|
+
"references": [
|
|
14
|
+
{ "path": "../util/tsconfig.build.json" },
|
|
15
|
+
{ "path": "../x-global/tsconfig.build.json" }
|
|
16
|
+
]
|
|
17
|
+
}
|