Remove polyfills (#739)
* Remove polyfills * CHANGELOG * jestConfig * Adjust jest * Revert explicit overrides (from base) * Fix dev * 4.0.0 * Bump wasm-crypto * min/max duplication * Split tests, update dev * startChar * validate params * complexity? * secp256k1Verify options * sign Options * hasher extraction
This commit is contained in:
@@ -1,5 +1,15 @@
|
||||
# CHANGELOG
|
||||
|
||||
## 4.0.0-x
|
||||
|
||||
- **Breaking change** Polyfills are not provided by these libraries anymore to avoid clobbering external globals, the user should provide them if their environment is missing base functionality.
|
||||
|
||||
Changes:
|
||||
|
||||
- Remove built-in utility polyfills for `Array.fill`, `String.padStart`, `String.padEnd` as well as `Object.setPrototypeOf`. These should be provided by the actual user of the libraries if so required.
|
||||
- Remove explicit crypto polyfill injection (aligning with update `@polkadot/wasm`)
|
||||
|
||||
|
||||
## 3.7.1 Nov 1, 2020
|
||||
|
||||
Changes:
|
||||
|
||||
+4
-1
@@ -22,5 +22,8 @@ module.exports = Object.assign({}, config, {
|
||||
'<rootDir>/packages/x-textencoder/build',
|
||||
'<rootDir>/packages/x-ws/build'
|
||||
],
|
||||
resolver: '@polkadot/dev/config/jest-resolver'
|
||||
resolver: '@polkadot/dev/config/jest-resolver',
|
||||
setupFilesAfterEnv: [
|
||||
'@polkadot/dev/config/jest-crypto'
|
||||
]
|
||||
});
|
||||
|
||||
+2
-2
@@ -22,9 +22,9 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.12.3",
|
||||
"@polkadot/dev": "^0.59.12",
|
||||
"@polkadot/dev": "^0.59.18",
|
||||
"@polkadot/ts": "^0.3.53",
|
||||
"@types/jest": "^26.0.15"
|
||||
},
|
||||
"version": "3.7.1"
|
||||
"version": "4.0.0-1"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/keyring",
|
||||
"version": "3.7.1",
|
||||
"version": "4.0.0-1",
|
||||
"description": "Keyring management",
|
||||
"main": "index.js",
|
||||
"publishConfig": {
|
||||
@@ -28,7 +28,7 @@
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/keyring#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.12.1",
|
||||
"@polkadot/util": "^3.7.1",
|
||||
"@polkadot/util-crypto": "^3.7.1"
|
||||
"@polkadot/util": "^4.0.0-1",
|
||||
"@polkadot/util-crypto": "^4.0.0-1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,9 +34,9 @@ const TYPE_PREFIX = {
|
||||
};
|
||||
|
||||
const TYPE_SIGNATURE = {
|
||||
ecdsa: (m: Uint8Array, p: Partial<Keypair>) => secp256k1Sign(m, p, 'blake2'),
|
||||
ecdsa: (m: Uint8Array, p: Partial<Keypair>) => secp256k1Sign(m, p, { hashType: 'blake2' }),
|
||||
ed25519: naclSign,
|
||||
ethereum: (m: Uint8Array, p: Partial<Keypair>) => secp256k1Sign(m, p, 'keccak'),
|
||||
ethereum: (m: Uint8Array, p: Partial<Keypair>) => secp256k1Sign(m, p, { hashType: 'keccak' }),
|
||||
sr25519: schnorrkelSign
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/networks",
|
||||
"version": "3.7.1",
|
||||
"version": "4.0.0-1",
|
||||
"description": "A list of all available Substrate networks and their applicable prefixes",
|
||||
"main": "index.js",
|
||||
"publishConfig": {
|
||||
@@ -29,7 +29,7 @@
|
||||
"@babel/runtime": "^7.12.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/util": "^3.7.1",
|
||||
"@polkadot/x-fetch": "^3.7.1"
|
||||
"@polkadot/util": "^4.0.0-1",
|
||||
"@polkadot/x-fetch": "^4.0.0-1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util-crypto",
|
||||
"version": "3.7.1",
|
||||
"version": "4.0.0-1",
|
||||
"description": "A collection of useful crypto utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -26,9 +26,9 @@
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util-crypto#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.12.1",
|
||||
"@polkadot/networks": "^3.7.1",
|
||||
"@polkadot/util": "^3.7.1",
|
||||
"@polkadot/wasm-crypto": "^1.4.1",
|
||||
"@polkadot/networks": "^4.0.0-1",
|
||||
"@polkadot/util": "^4.0.0-1",
|
||||
"@polkadot/wasm-crypto": "^2.0.0-2",
|
||||
"base-x": "^3.0.8",
|
||||
"blakejs": "^1.1.0",
|
||||
"bn.js": "^5.1.3",
|
||||
|
||||
@@ -4,12 +4,14 @@
|
||||
import { validateChars } from '../base58/validate';
|
||||
import { BASE32_ALPHABET } from './bs32';
|
||||
|
||||
const BASE_CONFIG = { alphabet: BASE32_ALPHABET, ipfsChar: 'b', type: 'base32' };
|
||||
|
||||
/**
|
||||
* @name base32Validate
|
||||
* @summary Validates a base32 value.
|
||||
* @description
|
||||
* Validates the the supplied value is valid base32
|
||||
*/
|
||||
export default function base32Validate (value?: string | null, ipfsCompat = false): true {
|
||||
return validateChars('base32', BASE32_ALPHABET, value, ipfsCompat ? 'b' : undefined);
|
||||
export default function base32Validate (value?: string | null, ipfsCompat?: boolean): true {
|
||||
return validateChars(BASE_CONFIG, value, ipfsCompat);
|
||||
}
|
||||
|
||||
@@ -5,11 +5,19 @@ import { assert } from '@polkadot/util';
|
||||
|
||||
import { BASE58_ALPHABET } from './bs58';
|
||||
|
||||
export function validateChars (type: string, alphabet: string, value?: string | null, startChar?: string): true {
|
||||
assert(value, `Expected non-null, non-empty ${type} input`);
|
||||
assert(!startChar || value[0] === startChar, `Expected ${type} to start with '${startChar || ''}'`);
|
||||
interface CheckConfig {
|
||||
alphabet: string;
|
||||
ipfsChar: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
for (let i = (startChar ? 1 : 0); i < value.length; i++) {
|
||||
const BASE_CONFIG = { alphabet: BASE58_ALPHABET, ipfsChar: 'z', type: 'base58' };
|
||||
|
||||
export function validateChars ({ alphabet, ipfsChar, type }: CheckConfig, value?: string | null, ipfsCompat?: boolean): true {
|
||||
assert(value, `Expected non-null, non-empty ${type} input`);
|
||||
assert(!ipfsCompat || value[0] === ipfsChar, `Expected ${type} to start with '${ipfsChar}'`);
|
||||
|
||||
for (let i = (ipfsCompat ? 1 : 0); i < value.length; i++) {
|
||||
assert(alphabet.includes(value[i]), `Invalid ${type} character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`);
|
||||
}
|
||||
|
||||
@@ -22,6 +30,6 @@ export function validateChars (type: string, alphabet: string, value?: string |
|
||||
* @description
|
||||
* Validates the the supplied value is valid base58
|
||||
*/
|
||||
export default function base58Validate (value?: string | null, ipfsCompat = false): true {
|
||||
return validateChars('base58', BASE58_ALPHABET, value, ipfsCompat ? 'z' : undefined);
|
||||
export default function base58Validate (value?: string | null, ipfsCompat?: boolean): true {
|
||||
return validateChars(BASE_CONFIG, value, ipfsCompat);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// polyfills
|
||||
import './polyfill';
|
||||
|
||||
// out of order and early in the chain, this also kicks off initialization on require
|
||||
export * from './init';
|
||||
|
||||
|
||||
@@ -16,9 +16,9 @@ describe('mnemonicGenerate', (): void => {
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
([12, 15, 18, 21, 24] as WordCount[]).forEach((num: WordCount): void => {
|
||||
it(`generates a valid mnemonic (${num} words)`, (): void => {
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
([12, 15, 18, 21, 24] as WordCount[]).forEach((num: WordCount): void => {
|
||||
it(`generates a valid mnemonic (${num} words, onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
const mnemonic = generate(num, onlyJs);
|
||||
const isValid = validate(mnemonic);
|
||||
|
||||
@@ -27,4 +27,17 @@ describe('mnemonicGenerate', (): void => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
[true, false].forEach((onlyJs): void => {
|
||||
it(`generates not deterministic (onlyJs=${onlyJs.toString()})`, (): void => {
|
||||
[false, true].forEach((onlyJs): void => {
|
||||
const m1 = generate(24, onlyJs);
|
||||
const m2 = generate(24, onlyJs);
|
||||
|
||||
expect(m1 === m2).toEqual(false);
|
||||
expect(validate(m1)).toEqual(true);
|
||||
expect(validate(m2)).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import '../polyfill';
|
||||
|
||||
import { bip39Generate, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { generateMnemonic } from './bip39';
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import '../polyfill';
|
||||
|
||||
/**
|
||||
* @summary Create valid mnemonic strings, validate them using BIP39, and convert them to valid seeds
|
||||
*/
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import '../polyfill';
|
||||
|
||||
import { bip39ToEntropy, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { mnemonicToEntropy } from './bip39';
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import '../polyfill';
|
||||
|
||||
import { pbkdf2Sync } from 'pbkdf2';
|
||||
import { bufferToU8a, stringToU8a, u8aToBuffer } from '@polkadot/util';
|
||||
import { bip39ToMiniSecret, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import '../polyfill';
|
||||
|
||||
import { bip39Validate, isReady } from '@polkadot/wasm-crypto';
|
||||
|
||||
import { validateMnemonic } from './bip39';
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import '@polkadot/wasm-crypto/crypto-polyfill';
|
||||
@@ -3,8 +3,6 @@
|
||||
|
||||
import { Keypair } from '../types';
|
||||
|
||||
import '../polyfill';
|
||||
|
||||
import { sr25519DeriveKeypairHard } from '@polkadot/wasm-crypto';
|
||||
|
||||
import keypairFromU8a from './keypair/fromU8a';
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import '../polyfill';
|
||||
|
||||
import { sr25519DerivePublicSoft } from '@polkadot/wasm-crypto';
|
||||
|
||||
export default function deriveSoft (publicKey: Uint8Array, chainCode: Uint8Array): Uint8Array {
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
|
||||
import { Keypair } from '../types';
|
||||
|
||||
import '../polyfill';
|
||||
|
||||
import { sr25519DeriveKeypairSoft } from '@polkadot/wasm-crypto';
|
||||
|
||||
import keypairFromU8a from './keypair/fromU8a';
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import '../polyfill';
|
||||
|
||||
export { default as schnorrkelDeriveHard } from './deriveHard';
|
||||
export { default as schnorrkelDerivePublic } from './derivePublic';
|
||||
export { default as schnorrkelDeriveSoft } from './deriveSoft';
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
|
||||
import { Keypair } from '../../types';
|
||||
|
||||
import '../../polyfill';
|
||||
|
||||
import { sr25519KeypairFromSeed } from '@polkadot/wasm-crypto';
|
||||
|
||||
import keypairFromU8a from './fromU8a';
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
|
||||
import { Keypair } from '../types';
|
||||
|
||||
import '../polyfill';
|
||||
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
import { sr25519Sign } from '@polkadot/wasm-crypto';
|
||||
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/util-crypto authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import '../polyfill';
|
||||
|
||||
import { assert, u8aToU8a } from '@polkadot/util';
|
||||
import { sr25519Verify } from '@polkadot/wasm-crypto';
|
||||
|
||||
|
||||
@@ -9,13 +9,17 @@ import { assert, bnToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import hasher from './hasher';
|
||||
|
||||
interface Options {
|
||||
hashType: HashType;
|
||||
}
|
||||
|
||||
const ec = new EC('secp256k1');
|
||||
|
||||
/**
|
||||
* @name secp256k1Sign
|
||||
* @description Returns message signature of `message`, using the supplied pair
|
||||
*/
|
||||
export default function secp256k1Sign (message: Uint8Array | string, { secretKey }: Partial<Keypair>, hashType: HashType = 'blake2'): Uint8Array {
|
||||
export default function secp256k1Sign (message: Uint8Array | string, { secretKey }: Partial<Keypair>, { hashType = 'blake2' }: Partial<Options> = {}): Uint8Array {
|
||||
assert(secretKey?.length === 32, 'Expected valid secp256k1 secretKey, 32-bytes');
|
||||
|
||||
const key = ec.keyFromPrivate(secretKey);
|
||||
|
||||
@@ -36,18 +36,18 @@ describe('sign and verify', (): void => {
|
||||
|
||||
it('signs/verifies a message by random key (keccak)', (): void => {
|
||||
const pair = pairFromSeed(randomAsU8a());
|
||||
const signature = sign(MESSAGE, pair, 'keccak');
|
||||
const signature = sign(MESSAGE, pair, { hashType: 'keccak' });
|
||||
const address = hasher('keccak', pair.publicKey);
|
||||
|
||||
expect(verify(MESSAGE, signature, address, 'keccak')).toBe(true);
|
||||
expect(verify(MESSAGE, signature, address, { hashType: 'keccak' })).toBe(true);
|
||||
});
|
||||
|
||||
it('fails verification on hasher mismatches', (): void => {
|
||||
const pair = pairFromSeed(randomAsU8a());
|
||||
const signature = sign(MESSAGE, pair, 'keccak');
|
||||
const signature = sign(MESSAGE, pair, { hashType: 'keccak' });
|
||||
const address = hasher('keccak', pair.publicKey);
|
||||
|
||||
expect(verify(MESSAGE, signature, address, 'blake2')).toBe(false);
|
||||
expect(verify(MESSAGE, signature, address, { hashType: 'blake2' })).toBe(false);
|
||||
});
|
||||
|
||||
it('works over a range of random keys (blake2)', (): void => {
|
||||
@@ -55,7 +55,7 @@ describe('sign and verify', (): void => {
|
||||
const pair = pairFromSeed(randomAsU8a());
|
||||
|
||||
try {
|
||||
expect(verify(MESSAGE, sign(MESSAGE, pair, 'blake2'), hasher('blake2', pair.publicKey), 'blake2')).toBe(true);
|
||||
expect(verify(MESSAGE, sign(MESSAGE, pair, { hashType: 'blake2' }), hasher('blake2', pair.publicKey), { hashType: 'blake2' })).toBe(true);
|
||||
} catch (error) {
|
||||
console.error(`blake2 failed on #${i}`);
|
||||
throw error;
|
||||
@@ -68,7 +68,7 @@ describe('sign and verify', (): void => {
|
||||
const pair = pairFromSeed(randomAsU8a());
|
||||
|
||||
try {
|
||||
expect(verify(MESSAGE, sign(MESSAGE, pair, 'keccak'), hasher('keccak', pair.publicKey), 'keccak')).toBe(true);
|
||||
expect(verify(MESSAGE, sign(MESSAGE, pair, { hashType: 'keccak' }), hasher('keccak', pair.publicKey), { hashType: 'keccak' })).toBe(true);
|
||||
} catch (error) {
|
||||
console.error(`keccak failed on #${i}`);
|
||||
throw error;
|
||||
|
||||
@@ -9,13 +9,18 @@ import { assert, u8aEq, u8aToU8a } from '@polkadot/util';
|
||||
import secp256k1Expand from './expand';
|
||||
import hasher from './hasher';
|
||||
|
||||
interface Options {
|
||||
hashType: HashType;
|
||||
isExpanded: boolean;
|
||||
}
|
||||
|
||||
const ec = new EC('secp256k1');
|
||||
|
||||
/**
|
||||
* @name secp256k1Verify
|
||||
* @description Verifies the signature of `message`, using the supplied pair
|
||||
*/
|
||||
export default function secp256k1Verify (message: Uint8Array | string, signature: Uint8Array | string, address: Uint8Array | string, hashType: HashType = 'blake2', isExpanded = false): boolean {
|
||||
export default function secp256k1Verify (message: Uint8Array | string, signature: Uint8Array | string, address: Uint8Array | string, { hashType = 'blake2', isExpanded = false }: Partial<Options> = {}): boolean {
|
||||
const u8a = u8aToU8a(signature);
|
||||
|
||||
assert(u8a.length === 65, `Expected signature with 65 bytes, ${u8a.length} found instead`);
|
||||
|
||||
@@ -12,17 +12,13 @@ import secp256k1Verify from '../secp256k1/verify';
|
||||
|
||||
type Verifier = [KeypairType, (message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string, isExpanded?: boolean) => boolean];
|
||||
|
||||
const secp256k1VerifyHasher = (hashType: 'blake2' | 'keccak') =>
|
||||
(message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string, isExpanded?: boolean) =>
|
||||
secp256k1Verify(message, signature, publicKey, { hashType, isExpanded });
|
||||
|
||||
const VERIFIERS_ECDSA: Verifier[] = [
|
||||
[
|
||||
'ecdsa',
|
||||
(message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string, isExpanded?: boolean) =>
|
||||
secp256k1Verify(message, signature, publicKey, 'blake2', isExpanded)
|
||||
],
|
||||
[
|
||||
'ethereum',
|
||||
(message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string, isExpanded?: boolean) =>
|
||||
secp256k1Verify(message, signature, publicKey, 'keccak', isExpanded)
|
||||
]
|
||||
['ecdsa', secp256k1VerifyHasher('blake2')],
|
||||
['ethereum', secp256k1VerifyHasher('keccak')]
|
||||
];
|
||||
|
||||
const VERIFIERS: Verifier[] = [
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util",
|
||||
"version": "3.7.1",
|
||||
"version": "4.0.0-1",
|
||||
"description": "A collection of useful utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
@@ -26,8 +26,8 @@
|
||||
"homepage": "https://github.com/polkadot-js/common/tree/master/packages/util#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.12.1",
|
||||
"@polkadot/x-textdecoder": "^3.7.1",
|
||||
"@polkadot/x-textencoder": "^3.7.1",
|
||||
"@polkadot/x-textdecoder": "^4.0.0-1",
|
||||
"@polkadot/x-textencoder": "^4.0.0-1",
|
||||
"@types/bn.js": "^4.11.6",
|
||||
"bn.js": "^5.1.3",
|
||||
"camelcase": "^5.3.1",
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import assert from '../assert';
|
||||
import { checkMaxMin } from './util';
|
||||
|
||||
/**
|
||||
* @name bnMax
|
||||
@@ -19,7 +19,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.max(acc, val), items[0]);
|
||||
return checkMaxMin('max', items);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import assert from '../assert';
|
||||
import { checkMaxMin } from './util';
|
||||
|
||||
/**
|
||||
* @name bnMin
|
||||
@@ -19,7 +19,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.min(acc, val), items[0]);
|
||||
return checkMaxMin('min', items);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import assert from '../assert';
|
||||
|
||||
export function checkMaxMin (type: 'max' | 'min', items: BN[]): BN {
|
||||
assert(items.length >= 1, 'Must provide one or more BN arguments');
|
||||
|
||||
return items.reduce((acc: BN, val: BN) => BN[type](acc, val), items[0]);
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import BN from 'bn.js';
|
||||
|
||||
import isToBn from '../is/toBn';
|
||||
|
||||
function getValue <ExtToBn extends ToBn> (value?: BN | ExtToBn | Date | number | null): number {
|
||||
function getValue <ExtToBn extends ToBn> (value: BN | ExtToBn | Date | number): number {
|
||||
if (isToBn(value)) {
|
||||
return getValue(value.toBn());
|
||||
} else if (value instanceof Date) {
|
||||
@@ -16,7 +16,7 @@ function getValue <ExtToBn extends ToBn> (value?: BN | ExtToBn | Date | number |
|
||||
return getValue(value.toNumber());
|
||||
}
|
||||
|
||||
return value || 0;
|
||||
return value;
|
||||
}
|
||||
|
||||
function formatValue (elapsed: number): string {
|
||||
@@ -33,7 +33,7 @@ function formatValue (elapsed: number): string {
|
||||
|
||||
export default function formatElapsed <ExtToBn extends ToBn> (now?: Date | null, value?: BN | ExtToBn | Date | number | null): string {
|
||||
const tsNow = (now && now.getTime()) || 0;
|
||||
const tsValue = getValue(value);
|
||||
const tsValue = getValue(value || 0);
|
||||
|
||||
if (tsNow && tsValue) {
|
||||
const elapsed = Math.max(Math.abs(tsNow - tsValue), 0) / 1000;
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import './polyfill';
|
||||
|
||||
/**
|
||||
* @summary Utility methods for this package are split into groups
|
||||
*/
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable no-extend-native */
|
||||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
|
||||
describe('Array.fill', (): void => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let arrayFill: (value: any, start?: number | undefined, end?: number | undefined) => any[];
|
||||
let u8aFill: (value: number, start?: number | undefined, end?: number | undefined) => Uint8Array;
|
||||
|
||||
beforeEach((): void => {
|
||||
arrayFill = Array.prototype.fill;
|
||||
u8aFill = Uint8Array.prototype.fill;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
Array.prototype.fill = null;
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
Uint8Array.prototype.fill = null;
|
||||
|
||||
require('./fill');
|
||||
});
|
||||
|
||||
afterEach((): void => {
|
||||
Array.prototype.fill = arrayFill;
|
||||
Uint8Array.prototype.fill = u8aFill;
|
||||
});
|
||||
|
||||
it('uses the polyfills', (): void => {
|
||||
expect(
|
||||
[1, 2, 3, 4, 5, 6, 7, 8].fill(5, 3)
|
||||
).toEqual([1, 2, 3, 5, 5, 5, 5, 5]);
|
||||
expect(
|
||||
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]).fill(5, 3)
|
||||
).toEqual(new Uint8Array([1, 2, 3, 5, 5, 5, 5, 5]));
|
||||
});
|
||||
});
|
||||
@@ -1,37 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable no-extend-native */
|
||||
|
||||
import assert from '../assert';
|
||||
|
||||
function getOffset (value: number, length: number): number {
|
||||
return value < 0
|
||||
? Math.max(length + value, 0)
|
||||
: Math.min(value, length);
|
||||
}
|
||||
|
||||
if (!Array.prototype.fill) {
|
||||
// eslint-disable-next-line no-extend-native
|
||||
Array.prototype.fill = function fill <T> (value: T, start = 0, end?: number): T[] {
|
||||
assert(this, 'this is null or not defined');
|
||||
|
||||
const A = Object(this) as T[];
|
||||
const relativeEnd = end ?? A.length;
|
||||
const final = getOffset(relativeEnd, A.length);
|
||||
let k = getOffset(start, A.length);
|
||||
|
||||
while (k < final) {
|
||||
A[k] = value;
|
||||
k++;
|
||||
}
|
||||
|
||||
return A;
|
||||
};
|
||||
}
|
||||
|
||||
if (!Uint8Array.prototype.fill) {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
Uint8Array.prototype.fill = Array.prototype.fill;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import './fill';
|
||||
import './padEnd';
|
||||
import './padStart';
|
||||
import './setPrototypeOf';
|
||||
@@ -1,27 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
|
||||
describe('String padEnd', (): void => {
|
||||
let stringEnd: (length: number, fill?: string) => string;
|
||||
|
||||
beforeEach((): void => {
|
||||
stringEnd = String.prototype.padEnd;
|
||||
|
||||
// eslint-disable-next-line no-extend-native,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment
|
||||
String.prototype.padEnd = null as any;
|
||||
|
||||
require('./padEnd');
|
||||
});
|
||||
|
||||
afterEach((): void => {
|
||||
// eslint-disable-next-line no-extend-native
|
||||
String.prototype.padEnd = stringEnd;
|
||||
});
|
||||
|
||||
it('does padding', (): void => {
|
||||
expect('test'.padEnd(8, 'A')).toEqual('testAAAA');
|
||||
expect('test'.padEnd(8)).toEqual('test ');
|
||||
});
|
||||
});
|
||||
@@ -1,17 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable no-extend-native */
|
||||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
|
||||
if (!String.prototype.padEnd) {
|
||||
String.prototype.padEnd = function padEnd (length: number, char = ' '): string {
|
||||
let result = String(this);
|
||||
|
||||
while (result.length < length) {
|
||||
result = result + char;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
|
||||
describe('String padStart', (): void => {
|
||||
let stringStart: (length: number, fill?: string) => string;
|
||||
|
||||
beforeEach((): void => {
|
||||
stringStart = String.prototype.padStart;
|
||||
|
||||
// eslint-disable-next-line no-extend-native,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment
|
||||
String.prototype.padStart = null as any;
|
||||
|
||||
require('./padStart');
|
||||
});
|
||||
|
||||
afterEach((): void => {
|
||||
// eslint-disable-next-line no-extend-native
|
||||
String.prototype.padStart = stringStart;
|
||||
});
|
||||
|
||||
it('does padding', (): void => {
|
||||
expect('test'.padStart(8, 'A')).toEqual('AAAAtest');
|
||||
expect('test'.padStart(8)).toEqual(' test');
|
||||
});
|
||||
});
|
||||
@@ -1,17 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable no-extend-native */
|
||||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
|
||||
if (!String.prototype.padStart) {
|
||||
String.prototype.padStart = function padStart (length: number, char = ' '): string {
|
||||
let result = String(this);
|
||||
|
||||
while (result.length < length) {
|
||||
result = char + result;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable no-proto */
|
||||
|
||||
describe('setPrototypeOf', (): void => {
|
||||
let orig: (o: Record<string, unknown>, proto: Record<string, unknown> | null) => Record<string, unknown>;
|
||||
|
||||
beforeEach((): void => {
|
||||
orig = Object.setPrototypeOf;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
Object.setPrototypeOf = null;
|
||||
|
||||
require('./setPrototypeOf');
|
||||
});
|
||||
|
||||
afterEach((): void => {
|
||||
Object.setPrototypeOf = orig;
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions', (): void => {
|
||||
const obj = {};
|
||||
const proto = { foo: 'bar' };
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
expect(Object.setPrototypeOf(obj, proto).__proto__).toBe(proto);
|
||||
});
|
||||
});
|
||||
@@ -1,12 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// React Native does not have Object.setPrototypeOf
|
||||
if (!Object.setPrototypeOf) {
|
||||
Object.setPrototypeOf = function (obj: Record<string, unknown>, proto: Record<string, unknown> | null): Record<string, unknown> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,no-proto
|
||||
obj.__proto__ = proto;
|
||||
|
||||
return obj;
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/x-fetch",
|
||||
"version": "3.7.1",
|
||||
"version": "4.0.0-1",
|
||||
"description": "A cross-environment fetch replacement",
|
||||
"browser": "browser.js",
|
||||
"main": "node.js",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/x-textdecoder",
|
||||
"version": "3.7.1",
|
||||
"version": "4.0.0-1",
|
||||
"description": "A TextDecoder replacement",
|
||||
"browser": "browser.js",
|
||||
"main": "node.js",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/x-textencoder",
|
||||
"version": "3.7.1",
|
||||
"version": "4.0.0-1",
|
||||
"description": "A TextDecoder replacement",
|
||||
"browser": "browser.js",
|
||||
"main": "node.js",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/x-ws",
|
||||
"version": "3.7.1",
|
||||
"version": "4.0.0-1",
|
||||
"description": "A cross-environment WebSocket replacement",
|
||||
"browser": "browser.js",
|
||||
"main": "node.js",
|
||||
|
||||
Reference in New Issue
Block a user