Minor readability improvements (#952)
* Minor readability improvements * trigger * Additional * Additional
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
||||
6
|
||||
7
|
||||
|
||||
2.6.2
|
||||
2.12.2
|
||||
|
||||
@@ -11,27 +11,27 @@ import { decodeAddress } from './decode';
|
||||
import { defaults } from './defaults';
|
||||
import { sshash } from './sshash';
|
||||
|
||||
export function encodeAddress (_key: Uint8Array | string, ss58Format: Prefix = defaults.prefix): string {
|
||||
export function encodeAddress (key: Uint8Array | string, ss58Format: Prefix = defaults.prefix): string {
|
||||
// decode it, this means we can re-encode an address
|
||||
const key = decodeAddress(_key);
|
||||
const u8a = decodeAddress(key);
|
||||
|
||||
assert(ss58Format >= 0 && ss58Format <= 16383 && ![46, 47].includes(ss58Format), 'Out of range ss58Format specified');
|
||||
assert(defaults.allowedDecodedLengths.includes(key.length), `Expected a valid key to convert, with length ${defaults.allowedDecodedLengths.join(', ')}`);
|
||||
assert(defaults.allowedDecodedLengths.includes(u8a.length), `Expected a valid key to convert, with length ${defaults.allowedDecodedLengths.join(', ')}`);
|
||||
|
||||
const isPublicKey = [32, 33].includes(key.length);
|
||||
const input = u8aConcat(
|
||||
new Uint8Array(
|
||||
ss58Format < 64
|
||||
? [ss58Format]
|
||||
: [
|
||||
((ss58Format & 0b0000_0000_1111_1100) >> 2) | 0b0100_0000,
|
||||
(ss58Format >> 8) | ((ss58Format & 0b0000_0000_0000_0011) << 6)
|
||||
]
|
||||
),
|
||||
key
|
||||
ss58Format < 64
|
||||
? [ss58Format]
|
||||
: [
|
||||
((ss58Format & 0b0000_0000_1111_1100) >> 2) | 0b0100_0000,
|
||||
(ss58Format >> 8) | ((ss58Format & 0b0000_0000_0000_0011) << 6)
|
||||
],
|
||||
u8a
|
||||
);
|
||||
|
||||
return base58Encode(
|
||||
u8aConcat(input, sshash(input).subarray(0, isPublicKey ? 2 : 1))
|
||||
u8aConcat(
|
||||
input,
|
||||
sshash(input).subarray(0, [32, 33].includes(u8a.length) ? 2 : 1)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import { assert } from '../assert';
|
||||
import { BN_MAX_INTEGER, BN_ONE, BN_ZERO } from '../bn';
|
||||
import { bnToBn } from './toBn';
|
||||
|
||||
const SQRT_MAX_SAFE_INTEGER = new BN(94906265);
|
||||
|
||||
/**
|
||||
* @name bnSqrt
|
||||
* @summary Calculates the integer square root of a BN
|
||||
@@ -35,7 +37,7 @@ export function bnSqrt <ExtToBn extends ToBn> (value: ExtToBn | BN | BigInt | st
|
||||
|
||||
// Use sqrt(MAX_SAFE_INTEGER) as starting point. since we already know the
|
||||
// output will be larger than this, we expect this to be a safe start
|
||||
let x0 = new BN(94906265);
|
||||
let x0 = SQRT_MAX_SAFE_INTEGER.clone();
|
||||
|
||||
while (true) {
|
||||
const x1 = n.div(x0).iadd(x0).ishrn(1);
|
||||
|
||||
@@ -10,14 +10,6 @@ import { isBigInt } from '../is/bigInt';
|
||||
import { isHex } from '../is/hex';
|
||||
import { isToBn } from '../is/toBn';
|
||||
|
||||
function numberToBn <ExtToBn extends ToBn> (value: number | ExtToBn | BN): BN {
|
||||
return BN.isBN(value)
|
||||
? value
|
||||
: isToBn(value)
|
||||
? value.toBn()
|
||||
: new BN(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name bnToBn
|
||||
* @summary Creates a BN value from a BN, BigInt, string (base 10 or hex) or number input.
|
||||
@@ -43,5 +35,9 @@ export function bnToBn <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | s
|
||||
return new BN(value.toString());
|
||||
}
|
||||
|
||||
return numberToBn(value);
|
||||
return BN.isBN(value)
|
||||
? value
|
||||
: isToBn(value)
|
||||
? value.toBn()
|
||||
: new BN(value);
|
||||
}
|
||||
|
||||
@@ -8,5 +8,5 @@ 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]);
|
||||
return items.reduce<BN>((a, v) => BN[type](a, v), items[0]);
|
||||
}
|
||||
|
||||
@@ -45,10 +45,8 @@ export function compactToU8a (_value: BN | BigInt | number): Uint8Array {
|
||||
assert(length >= 4, 'Previous tests match anyting less than 2^30; qed');
|
||||
|
||||
return u8aConcat(
|
||||
new Uint8Array([
|
||||
// substract 4 as minimum (also catered for in decoding)
|
||||
((length - 4) << 2) + 0b11
|
||||
]),
|
||||
// subtract 4 as minimum (also catered for in decoding)
|
||||
[((length - 4) << 2) + 0b11],
|
||||
u8a.subarray(0, length)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -22,9 +22,5 @@ export function hexAddPrefix (value?: string | null): string {
|
||||
return value;
|
||||
}
|
||||
|
||||
const prefix = value && value.length % 2 === 1
|
||||
? '0'
|
||||
: '';
|
||||
|
||||
return `0x${prefix}${value || ''}`;
|
||||
return `0x${value && value.length % 2 === 1 ? '0' : ''}${value || ''}`;
|
||||
}
|
||||
|
||||
@@ -22,13 +22,9 @@ const UNPREFIX_HEX_REGEX = /^[a-fA-F0-9]+$/;
|
||||
export function hexStripPrefix (value?: string | null): string {
|
||||
if (!value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (hexHasPrefix(value)) {
|
||||
} else if (hexHasPrefix(value)) {
|
||||
return value.substr(2);
|
||||
}
|
||||
|
||||
if (UNPREFIX_HEX_REGEX.test(value)) {
|
||||
} else if (UNPREFIX_HEX_REGEX.test(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ function reverse (value: string): string {
|
||||
|
||||
/**
|
||||
* @name hexToBn
|
||||
* @summary Creates a BN.js bignumber object from a hex string.
|
||||
* @summary Creates a BN.js object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
* @param _value The value to convert
|
||||
@@ -43,7 +43,6 @@ export function hexToBn (value?: string | number | null, options: ToBnOptions |
|
||||
// Backwards-compatibility
|
||||
...(isBoolean(options) ? { isLe: options } : options)
|
||||
};
|
||||
|
||||
const _value = hexStripPrefix(value as string);
|
||||
|
||||
// FIXME: Use BN's 3rd argument `isLe` once this issue is fixed
|
||||
|
||||
@@ -23,11 +23,10 @@ import { u8aToU8a } from './toU8a';
|
||||
export function u8aConcat (...list: (Buffer | Uint8Array | number[] | string)[]): Uint8Array {
|
||||
let length = 0;
|
||||
let offset = 0;
|
||||
const u8as = new Array(list.length) as Uint8Array[];
|
||||
const u8as = new Array<Uint8Array>(list.length);
|
||||
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
u8as[i] = u8aToU8a(list[i]);
|
||||
|
||||
length += u8as[i].length;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,5 @@
|
||||
* ```
|
||||
*/
|
||||
export function u8aToBuffer (value?: Uint8Array | null): Buffer {
|
||||
return !value
|
||||
? Buffer.from([])
|
||||
: Buffer.from(value);
|
||||
return Buffer.from(value || []);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ const ALPHABET = new Array(256).fill(0).map((_, n) => n.toString(16).padStart(2,
|
||||
|
||||
/** @internal */
|
||||
function extract (value: Uint8Array): string {
|
||||
const result = new Array(value.length) as string[];
|
||||
const result = new Array<string>(value.length);
|
||||
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
result[i] = ALPHABET[value[i]];
|
||||
@@ -44,8 +44,9 @@ export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed
|
||||
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
|
||||
return prefix + ((byteLength > 0 && value.length > byteLength)
|
||||
? trim(value, Math.ceil(byteLength / 2))
|
||||
: extract(value)
|
||||
return prefix + (
|
||||
(byteLength > 0 && value.length > byteLength)
|
||||
? trim(value, Math.ceil(byteLength / 2))
|
||||
: extract(value)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -40,6 +40,14 @@ describe('u8aToU8a', (): void => {
|
||||
);
|
||||
});
|
||||
|
||||
it('creates from a Buffer', (): void => {
|
||||
expect(
|
||||
u8aToU8a(Buffer.from([1, 2, 3, 128, 0, 10, 11, 12]))
|
||||
).toEqual(
|
||||
new Uint8Array([1, 2, 3, 128, 0, 10, 11, 12])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array as-is (u8a input)', (): void => {
|
||||
expect(
|
||||
u8aToU8a(new Uint8Array([128, 0, 10]))
|
||||
|
||||
@@ -1,25 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { bufferToU8a } from '../buffer/toU8a';
|
||||
import { hexToU8a } from '../hex/toU8a';
|
||||
import { isBuffer } from '../is/buffer';
|
||||
import { isHex } from '../is/hex';
|
||||
import { isString } from '../is/string';
|
||||
import { stringToU8a } from '../string/toU8a';
|
||||
|
||||
function convertArray (value: number[] | Uint8Array): Uint8Array {
|
||||
return Array.isArray(value)
|
||||
? Uint8Array.from(value)
|
||||
: value;
|
||||
}
|
||||
|
||||
function convertString (value: string): Uint8Array {
|
||||
return isHex(value)
|
||||
? hexToU8a(value)
|
||||
: stringToU8a(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aToU8a
|
||||
* @summary Creates a Uint8Array value from a Uint8Array, Buffer, string or hex input.
|
||||
@@ -29,7 +16,7 @@ function convertString (value: string): Uint8Array {
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { { u8aToU8a } from '@polkadot/util';
|
||||
* import { u8aToU8a } from '@polkadot/util';
|
||||
*
|
||||
* u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34])
|
||||
* u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34])
|
||||
@@ -38,11 +25,13 @@ function convertString (value: string): Uint8Array {
|
||||
export function u8aToU8a (value?: number[] | Buffer | Uint8Array | string | null): Uint8Array {
|
||||
if (!value) {
|
||||
return new Uint8Array();
|
||||
} else if (isBuffer(value)) {
|
||||
return bufferToU8a(value);
|
||||
} else if (isString(value)) {
|
||||
return convertString(value);
|
||||
return isHex(value)
|
||||
? hexToU8a(value)
|
||||
: stringToU8a(value);
|
||||
}
|
||||
|
||||
return convertArray(value);
|
||||
return Array.isArray(value) || isBuffer(value)
|
||||
? new Uint8Array(value)
|
||||
: value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user