RN-specific getRandomValues (no node crypto) (#747)

* RN-specific getRandomValues (no node crypto)

* Use polyfilled as available

* === object
This commit is contained in:
Jaco Greeff
2020-11-07 15:22:42 +01:00
committed by GitHub
parent 94d6a900f7
commit db0559e9c9
7 changed files with 125 additions and 20 deletions
+1 -1
View File
@@ -28,7 +28,7 @@
"@babel/runtime": "^7.12.1",
"@polkadot/networks": "^4.0.0-6",
"@polkadot/util": "^4.0.0-6",
"@polkadot/wasm-crypto": "^2.0.0-8",
"@polkadot/wasm-crypto": "^2.0.0-9",
"@polkadot/x-randomvalues": "^4.0.0-6",
"base-x": "^3.0.8",
"blakejs": "^1.1.0",
+3
View File
@@ -25,5 +25,8 @@
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/x-randomvalues#readme",
"dependencies": {
"@babel/runtime": "^7.12.1"
},
"devDependencies": {
"@types/react-native": "^0.63.33"
}
}
+27
View File
@@ -0,0 +1,27 @@
// Copyright 2017-2020 @polkadot/x-randomvalues authors & contributors
// 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
let warned = false;
export default function insecureRandomValues <T extends Uint8Array> (arr: T): T {
if (!warned) {
console.warn('Using an insecure random number generator, this should only happen when running in a debugger without support for crypto.getRandomValues');
warned = true;
}
let r = 0;
for (let i = 0; i < arr.length; i++) {
if ((i & 0x03) === 0) {
r = Math.random() * 0x100000000;
}
arr[i] = (r >>> ((i & 0x03) << 3)) & 0xff;
}
return arr;
}
+36 -2
View File
@@ -1,6 +1,40 @@
// Copyright 2017-2020 @polkadot/x-randomvalues authors & contributors
// SPDX-License-Identifier: Apache-2.0
import getRandomValues from './node';
// 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
export default getRandomValues;
import { NativeModules } from 'react-native';
import insecureRandomValues from './fallback';
interface RNExt {
RNGetRandomValues: {
getRandomBase64: (length: number) => string;
}
}
function getRandomValuesNative <T extends Uint8Array> (arr: T): T {
// We may want to use a base64 decoder here, but we certainly have an issue where we
// want/need this part as lean as possible. Since Buffer is already needed elsewhere
// (an generally prevalent in JS), we rather use it as-is.
return Buffer
.from((NativeModules as RNExt).RNGetRandomValues.getRandomBase64(arr.length), 'base64')
.reduce((arr, byte, index): T => {
arr[index] = byte;
return arr;
}, arr);
}
function getRandomValuesGlobal <T extends Uint8Array> (arr: T): T {
return crypto.getRandomValues(arr);
}
export default typeof global.crypto === 'object' && typeof global.crypto.getRandomValues === 'function'
? getRandomValuesGlobal
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-explicit-any
: (typeof (global as any).nativeCallSyncHook === 'undefined' || !NativeModules.ExpoRandom)
? insecureRandomValues
: getRandomValuesNative;
+3 -12
View File
@@ -1,17 +1,8 @@
// Copyright 2017-2020 @polkadot/x-textencoder authors & contributors
// SPDX-License-Identifier: Apache-2.0
export default typeof TextDecoder === 'undefined'
? class {
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-useless-constructor
constructor (_: 'utf-8') {
// nothing
}
import Fallback from './fallback';
decode (value: Uint8Array): string {
return value.reduce((str, code): string => {
return str + String.fromCharCode(code);
}, '');
}
}
export default typeof TextDecoder === 'undefined'
? Fallback
: TextDecoder;
+15
View File
@@ -0,0 +1,15 @@
// Copyright 2017-2020 @polkadot/x-textencoder authors & contributors
// SPDX-License-Identifier: Apache-2.0
export default class TextDecoder {
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-useless-constructor
constructor (_: 'utf-8') {
// nothing
}
decode (value: Uint8Array): string {
return value.reduce((str, code): string => {
return str + String.fromCharCode(code);
}, '');
}
}