x-randomvalues w/ shared base64 decoding (#1807)
* x-randomvalues w/ shared base64 decoding * Adjust * npm publish
This commit is contained in:
@@ -25,18 +25,22 @@ const INVALID_MNEMONIC = 'Invalid mnemonic';
|
||||
const INVALID_ENTROPY = 'Invalid entropy';
|
||||
const INVALID_CHECKSUM = 'Invalid mnemonic checksum';
|
||||
|
||||
/** @internal */
|
||||
function normalize (str?: string): string {
|
||||
return (str || '').normalize('NFKD');
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function binaryToByte (bin: string): number {
|
||||
return parseInt(bin, 2);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function bytesToBinary (bytes: number[]): string {
|
||||
return bytes.map((x) => x.toString(2).padStart(8, '0')).join('');
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function deriveChecksumBits (entropyBuffer: Uint8Array): string {
|
||||
return bytesToBinary(
|
||||
Array.from(sha256AsU8a(entropyBuffer))
|
||||
|
||||
@@ -20,11 +20,11 @@
|
||||
"main": "node.js",
|
||||
"react-native": "react-native.js",
|
||||
"dependencies": {
|
||||
"@polkadot/wasm-util": "^7.0.3",
|
||||
"@polkadot/x-global": "11.1.4-10-x",
|
||||
"tslib": "^2.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/util": "11.1.4-10-x",
|
||||
"@types/react-native": "^0.71.5"
|
||||
},
|
||||
"denoDependencies": {
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/// <reference types="@polkadot/dev-test/globals.d.ts" />
|
||||
|
||||
import { perf } from '@polkadot/util/test';
|
||||
|
||||
import { base64Decode } from './base64.js';
|
||||
|
||||
const HELLO_DUAL = 'aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==';
|
||||
|
||||
const TESTS = [
|
||||
{
|
||||
// hello world Приветствую ми 你好
|
||||
base64: HELLO_DUAL,
|
||||
expect: new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 32, 208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 209, 129, 209, 130, 208, 178, 209, 131, 209, 142, 32, 208, 188, 208, 184, 32, 228, 189, 160, 229, 165, 189])
|
||||
},
|
||||
{
|
||||
// ✓ à la mode
|
||||
base64: '4pyTIMOgIGxhIG1vZGU=',
|
||||
expect: new Uint8Array([226, 156, 147, 32, 195, 160, 32, 108, 97, 32, 109, 111, 100, 101])
|
||||
},
|
||||
{
|
||||
// Hello World!
|
||||
base64: 'SGVsbG8gV29ybGQh',
|
||||
expect: new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
|
||||
},
|
||||
{
|
||||
base64: 'AA==',
|
||||
expect: new Uint8Array([0])
|
||||
},
|
||||
{
|
||||
base64: 'AAA=',
|
||||
expect: new Uint8Array([0, 0])
|
||||
},
|
||||
{
|
||||
base64: 'AAAA',
|
||||
expect: new Uint8Array([0, 0, 0])
|
||||
}
|
||||
];
|
||||
|
||||
describe('base64Decode', (): void => {
|
||||
for (const test of TESTS) {
|
||||
it(test.base64, (): void => {
|
||||
expect(
|
||||
base64Decode(test.base64)
|
||||
).toEqual(test.expect);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
perf('base64Decode (hello, dual)', 100_000, [[HELLO_DUAL]], base64Decode);
|
||||
@@ -1,37 +0,0 @@
|
||||
// Copyright 2017-2023 @polkadot/x-randomvalues authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// A tiny base64 decoder for RN usage when atob is not available.
|
||||
// The alternative would be to rely on Buffer with 'base64'
|
||||
//
|
||||
// Uint8Array.from(Buffer.from(data, 'base64'))
|
||||
//
|
||||
// We provide an own (tiny) decoder to not have Buffer deps
|
||||
|
||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
||||
const map = new Uint8Array(256);
|
||||
|
||||
// We use charCodeAt for access here and in the decoder loop - this is faster
|
||||
// on lookups (array + numbers) and also faster than accessing the specific
|
||||
// character via data[i] (around a 50% improvement on perf tests)
|
||||
for (let i = 0; i < chars.length; i++) {
|
||||
map[chars.charCodeAt(i)] = i;
|
||||
}
|
||||
|
||||
export function base64Decode (data: string): Uint8Array {
|
||||
const bytes = [];
|
||||
let byte = 0;
|
||||
let bits = 0;
|
||||
|
||||
for (let i = 0; i < data.length && data[i] !== '='; i++) {
|
||||
// each character represents 6 bits
|
||||
byte = (byte << 6) | map[data.charCodeAt(i)];
|
||||
|
||||
// each byte needs to contain 8 bits
|
||||
if ((bits += 6) >= 8) {
|
||||
bytes.push((byte >>> (bits -= 8)) & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
return Uint8Array.from(bytes);
|
||||
}
|
||||
@@ -7,9 +7,9 @@
|
||||
|
||||
import { NativeModules } from 'react-native';
|
||||
|
||||
import { base64Decode } from '@polkadot/wasm-util/base64';
|
||||
import { xglobal } from '@polkadot/x-global';
|
||||
|
||||
import { base64Decode } from './base64.js';
|
||||
import { getRandomValues as getRandomValuesGlobal } from './browser.js';
|
||||
import { insecureRandomValues } from './fallback.js';
|
||||
|
||||
@@ -28,18 +28,19 @@ interface GlobalExt {
|
||||
nativeCallSyncHook: unknown;
|
||||
}
|
||||
|
||||
function getRandomValuesNative <T extends Uint8Array> (output: T): T {
|
||||
const bytes = base64Decode(
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* A getRandomValues util that detects and uses the available RN
|
||||
* random utiliy generation functions.
|
||||
**/
|
||||
function getRandomValuesNative (output: Uint8Array): Uint8Array {
|
||||
return base64Decode(
|
||||
(NativeModules as RNExt).RNGetRandomValues
|
||||
? (NativeModules as RNExt).RNGetRandomValues.getRandomBase64(output.length)
|
||||
: (NativeModules as RNExt).ExpoRandom.getRandomBase64String(output.length)
|
||||
: (NativeModules as RNExt).ExpoRandom.getRandomBase64String(output.length),
|
||||
output
|
||||
);
|
||||
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
output[i] = bytes[i];
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
export const getRandomValues = (
|
||||
|
||||
@@ -732,7 +732,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@polkadot/wasm-util@npm:7.0.3":
|
||||
"@polkadot/wasm-util@npm:7.0.3, @polkadot/wasm-util@npm:^7.0.3":
|
||||
version: 7.0.3
|
||||
resolution: "@polkadot/wasm-util@npm:7.0.3"
|
||||
dependencies:
|
||||
@@ -785,7 +785,7 @@ __metadata:
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@polkadot/x-randomvalues@workspace:packages/x-randomvalues"
|
||||
dependencies:
|
||||
"@polkadot/util": 11.1.4-10-x
|
||||
"@polkadot/wasm-util": ^7.0.3
|
||||
"@polkadot/x-global": 11.1.4-10-x
|
||||
"@types/react-native": ^0.71.5
|
||||
tslib: ^2.5.0
|
||||
|
||||
Reference in New Issue
Block a user