Small cleanups, u8aConcat optimization (#607)

* Small cleanups, u8aConcat oprimizations

* Cleanup

* Pre-allocate arrays

* Don't recreate PREFIX
This commit is contained in:
Jaco Greeff
2020-06-01 09:29:44 +02:00
committed by GitHub
parent 0f6e996799
commit d4491aeb7f
12 changed files with 39 additions and 31 deletions
+2 -2
View File
@@ -2,10 +2,10 @@
## 2.12.0-beta.x
- **Important** Removed `mnemonicToSeed`, it has not been in-active use since POC-2, use `mnemonicToMiniSecret` for the expected outcome
- **Important** Removed `mnemonicToSeed`, it is only for bip39, use `mnemonicToMiniSecret` for the Substrate variant
- Added `u8aEq` to compare two Uint8Array or hex values for an exact match
- Added `addressEq` to compare two addresses (ss58, Uint8array or hex) for an exact match
- Performance optimizations in `u8aToHex` conversions
- Performance optimizations in `u8aToHex` & `u8aConcat`
## 2.11.1 May 26, 2020
+4 -2
View File
@@ -3,15 +3,17 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
import { bnToU8a, compactToU8a, u8aConcat, u8aSorted } from '@polkadot/util';
import { bnToU8a, compactToU8a, stringToU8a, u8aConcat, u8aSorted } from '@polkadot/util';
import blake2AsU8a from '../blake2/asU8a';
import decodeAddress from './decode';
const PREFIX = stringToU8a('modlpy/utilisuba');
export default function createKeyMulti (who: (Uint8Array | string)[], threshold: BigInt | BN | number): Uint8Array {
return blake2AsU8a(
u8aConcat(
'modlpy/utilisuba',
PREFIX,
compactToU8a(who.length),
...u8aSorted(who.map((who) => decodeAddress(who))),
bnToU8a(threshold, { bitLength: 16, isLe: true })
+4 -2
View File
@@ -3,15 +3,17 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
import { bnToU8a, u8aConcat } from '@polkadot/util';
import { bnToU8a, stringToU8a, u8aConcat } from '@polkadot/util';
import blake2AsU8a from '../blake2/asU8a';
import decodeAddress from './decode';
const PREFIX = stringToU8a('modlpy/utilisuba');
export default function createKeySub (who: Uint8Array | string, index: BigInt | BN | number): Uint8Array {
return blake2AsU8a(
u8aConcat(
'modlpy/utilisuba',
PREFIX,
decodeAddress(who),
bnToU8a(index, { bitLength: 16, isLe: true })
)
+1 -1
View File
@@ -22,5 +22,5 @@ import assert from '../assert';
export default function max (...items: BN[]): BN {
assert(items && items.length >= 1, 'Must provide one or more BN arguments');
return items.reduce((acc: BN, val: BN): BN => BN.max(acc, val), items[0]);
return items.reduce((acc: BN, val: BN) => BN.max(acc, val), items[0]);
}
+1 -1
View File
@@ -22,5 +22,5 @@ import assert from '../assert';
export default function min (...items: BN[]): BN {
assert(items && items.length >= 1, 'Must provide one or more BN arguments');
return items.reduce((acc: BN, val: BN): BN => BN.min(acc, val), items[0]);
return items.reduce((acc: BN, val: BN) => BN.min(acc, val), items[0]);
}
+1 -1
View File
@@ -39,7 +39,7 @@ function flattenVersions (_all: (VersionPath | string)[]): string {
const verLength = all.reduce((max, { version }): number => Math.max(max, version.length), 0);
return all
.map(({ path, version }): string => `\t${version.padEnd(verLength)}\t${expandPath(path)}`)
.map(({ path, version }) => `\t${version.padEnd(verLength)}\t${expandPath(path)}`)
.join('\n');
}
+3 -2
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
type FnType = (...params: unknown[]) => unknown;
/**
* @name isFunction
* @summary Tests for a `function`.
@@ -16,7 +18,6 @@
* isFunction(() => false); // => true
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export default function isFunction (value: unknown): value is Function {
export default function isFunction (value: unknown): value is FnType {
return typeof value === 'function';
}
+2 -4
View File
@@ -67,8 +67,7 @@ export function format (value: unknown): unknown {
function apply (log: LogType, type: string, values: Logger$Data): void {
if (values.length === 1 && isFunction(values[0])) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/ban-types
const fnResult = (values[0] as Function)();
const fnResult = values[0]();
return apply(log, type, Array.isArray(fnResult) ? fnResult : [fnResult]);
}
@@ -83,8 +82,7 @@ function apply (log: LogType, type: string, values: Logger$Data): void {
);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function noop (...values: Logger$Data): void {
function noop (): void {
// noop
}
+1 -2
View File
@@ -7,8 +7,7 @@ if (typeof TextDecoder === 'undefined') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-var-requires
const { TextDecoder: UTD } = require('util') as { TextDecoder: { new(): TextDecoder } };
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-member-access
(global as any).TextDecoder = UTD;
global.TextDecoder = UTD;
} catch (error) {
// noop
}
+2 -2
View File
@@ -17,8 +17,8 @@
* await promisify(null, (cb) => cb(new Error('error!'))); // rejects with `error!`
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types,@typescript-eslint/explicit-module-boundary-types,@typescript-eslint/no-explicit-any
export default function promisify <R = any> (self: unknown, fn: Function, ...params: any[]): Promise<R> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function promisify <R = any> (self: unknown, fn: (...params: any) => any, ...params: any[]): Promise<R> {
return new Promise((resolve, reject): void => {
const handler = (error: Error | null, result?: R): void => {
if (error) {
+2 -4
View File
@@ -20,10 +20,8 @@ export interface SiDef {
value: string;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Logger$Data$Fn = () => any[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Logger$Data = (any | Logger$Data$Fn)[];
type Logger$Data$Fn = () => unknown[];
export type Logger$Data = (unknown | Logger$Data$Fn)[];
export interface Logger {
debug: (...values: Logger$Data) => void;
+16 -8
View File
@@ -21,15 +21,23 @@ import u8aToU8a from './toU8a';
* ); // [1, 2, 3, 4, 5, 6]
* ```
*/
export default function u8aConcat (..._list: (Uint8Array | string)[]): Uint8Array {
const list = _list.map(u8aToU8a);
const result = new Uint8Array(list.reduce((total, item): number => total + item.length, 0));
export default function u8aConcat (...list: (Uint8Array | string)[]): Uint8Array {
let length = 0;
let offset = 0;
const u8as = new Array(list.length) as Uint8Array[];
return list.reduce((result, item): Uint8Array => {
result.set(item, offset);
offset += item.length;
for (let i = 0; i < list.length; i++) {
u8as[i] = u8aToU8a(list[i]);
return result;
}, result);
length += u8as[i].length;
}
const result = new Uint8Array(length);
for (let i = 0; i < u8as.length; i++) {
result.set(u8as[i], offset);
offset += u8as[i].length;
}
return result;
}