Small formatting cleanups (#554)
* Small formatting cleanups * Cleanup typos
This commit is contained in:
@@ -14,19 +14,7 @@ type DecodeResult = PairInfo & {
|
||||
secretKey: Uint8Array;
|
||||
};
|
||||
|
||||
export default function decode (passphrase?: string, encrypted?: Uint8Array | null): DecodeResult {
|
||||
assert(encrypted, 'No encrypted data available to decode');
|
||||
|
||||
const encoded = passphrase
|
||||
? (naclDecrypt(
|
||||
encrypted.subarray(NONCE_LENGTH),
|
||||
encrypted.subarray(0, NONCE_LENGTH),
|
||||
u8aFixLength(stringToU8a(passphrase), 256, true)
|
||||
) as Uint8Array)
|
||||
: encrypted;
|
||||
|
||||
assert(encoded, 'Unable to unencrypt using the supplied passphrase');
|
||||
|
||||
function decodePkcs8 (encoded: Uint8Array): DecodeResult {
|
||||
const header = encoded.subarray(0, PKCS8_HEADER.length);
|
||||
|
||||
assert(header.toString() === PKCS8_HEADER.toString(), 'Invalid Pkcs8 header found in body');
|
||||
@@ -52,3 +40,19 @@ export default function decode (passphrase?: string, encrypted?: Uint8Array | nu
|
||||
secretKey
|
||||
};
|
||||
}
|
||||
|
||||
export default function decode (passphrase?: string, encrypted?: Uint8Array | null): DecodeResult {
|
||||
assert(encrypted, 'No encrypted data available to decode');
|
||||
|
||||
const encoded = passphrase
|
||||
? naclDecrypt(
|
||||
encrypted.subarray(NONCE_LENGTH),
|
||||
encrypted.subarray(0, NONCE_LENGTH),
|
||||
u8aFixLength(stringToU8a(passphrase), 256, true)
|
||||
)
|
||||
: encrypted;
|
||||
|
||||
assert(encoded, 'Unable to unencrypt using the supplied passphrase');
|
||||
|
||||
return decodePkcs8(encoded);
|
||||
}
|
||||
|
||||
@@ -13,9 +13,9 @@ import hexToBn from '../hex/toBn';
|
||||
|
||||
/**
|
||||
* @name bnToBn
|
||||
* @summary Creates a BN value from a BN.js bignumber or number input.
|
||||
* @summary Creates a BN value from a BN, BigInt, string (base 10 or hex) or number input.
|
||||
* @description
|
||||
* `null` inputs returns a `0x0` result, BN values returns the value, numnbers returns a BN representation.
|
||||
* `null` inputs returns a `0x0` result, BN values returns the value, numbers returns a BN representation.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
|
||||
@@ -25,19 +25,11 @@ export default function hexFixLength (value: string, bitLength = -1, withPadding
|
||||
const strLength = Math.ceil(bitLength / 4);
|
||||
const hexLength = strLength + 2;
|
||||
|
||||
if (bitLength === -1 || value.length === hexLength || (!withPadding && value.length < hexLength)) {
|
||||
return hexAddPrefix(
|
||||
hexStripPrefix(value)
|
||||
);
|
||||
}
|
||||
|
||||
if (value.length > hexLength) {
|
||||
return hexAddPrefix(
|
||||
hexStripPrefix(value).slice(-1 * strLength)
|
||||
);
|
||||
}
|
||||
|
||||
return hexAddPrefix(
|
||||
`${'0'.repeat(strLength)}${hexStripPrefix(value)}`.slice(-1 * strLength)
|
||||
(bitLength === -1 || value.length === hexLength || (!withPadding && value.length < hexLength))
|
||||
? hexStripPrefix(value)
|
||||
: (value.length > hexLength)
|
||||
? hexStripPrefix(value).slice(-1 * strLength)
|
||||
: `${'0'.repeat(strLength)}${hexStripPrefix(value)}`.slice(-1 * strLength)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -19,9 +19,7 @@ import hexToBn from './toBn';
|
||||
* ```
|
||||
*/
|
||||
export default function hexToNumber (value?: string | null): number {
|
||||
if (!value) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
return hexToBn(value).toNumber();
|
||||
return value
|
||||
? hexToBn(value).toNumber()
|
||||
: NaN;
|
||||
}
|
||||
|
||||
@@ -26,9 +26,5 @@ export default function numberToHex (value?: number | null, bitLength = -1): str
|
||||
return '0x';
|
||||
}
|
||||
|
||||
return hexFixLength(
|
||||
(value || 0).toString(16),
|
||||
bitLength,
|
||||
true
|
||||
);
|
||||
return hexFixLength(value.toString(16), bitLength, true);
|
||||
}
|
||||
|
||||
@@ -17,9 +17,7 @@
|
||||
* ```
|
||||
*/
|
||||
export default function stringLowerFirst (value?: string | null): string {
|
||||
if (!value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return value.charAt(0).toLowerCase() + value.slice(1);
|
||||
return value
|
||||
? value.charAt(0).toLowerCase() + value.slice(1)
|
||||
: '';
|
||||
}
|
||||
|
||||
@@ -37,9 +37,7 @@ try {
|
||||
* ```
|
||||
*/
|
||||
export default function stringToU8a (value?: string): Uint8Array {
|
||||
if (!value) {
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
|
||||
return encoder.encode(value);
|
||||
return value
|
||||
? encoder.encode(value)
|
||||
: new Uint8Array([]);
|
||||
}
|
||||
|
||||
@@ -17,9 +17,7 @@
|
||||
* ```
|
||||
*/
|
||||
export default function stringUpperFirst (value?: string | null): string {
|
||||
if (!value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return value.charAt(0).toUpperCase() + value.slice(1);
|
||||
return value
|
||||
? value.charAt(0).toUpperCase() + value.slice(1)
|
||||
: '';
|
||||
}
|
||||
|
||||
@@ -26,10 +26,7 @@ import { ToBnOptions } from '../types';
|
||||
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
|
||||
* ```
|
||||
*/
|
||||
export default function u8aToBn (
|
||||
value: Uint8Array,
|
||||
options: ToBnOptions | boolean = { isLe: true, isNegative: false }
|
||||
): BN {
|
||||
export default function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = { isLe: true, isNegative: false }): BN {
|
||||
return hexToBn(
|
||||
u8aToHex(value),
|
||||
options
|
||||
|
||||
@@ -23,7 +23,7 @@ export default function u8aToHex (value?: Uint8Array | null, bitLength = -1, isP
|
||||
? '0x'
|
||||
: '';
|
||||
|
||||
if (!value || !value.length) {
|
||||
if (!value?.length) {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user