assert cleanups (#957)
This commit is contained in:
@@ -28,8 +28,8 @@ export class Ledger {
|
||||
|
||||
constructor (transport: LedgerTypes, chain: Chain) {
|
||||
// u2f is deprecated
|
||||
assert(['hid', 'webusb'].includes(transport), `Unsupported transport ${transport}`);
|
||||
assert(Object.keys(ledgerApps).includes(chain), `Unsupported chain ${chain}`);
|
||||
assert(['hid', 'webusb'].includes(transport), () => `Unsupported transport ${transport}`);
|
||||
assert(Object.keys(ledgerApps).includes(chain), () => `Unsupported chain ${chain}`);
|
||||
|
||||
this.#chain = chain;
|
||||
this.#transport = transport;
|
||||
@@ -73,7 +73,7 @@ export class Ledger {
|
||||
if (!this.#app) {
|
||||
const def = transports.find(({ type }) => type === this.#transport);
|
||||
|
||||
assert(def, `Unable to find a transport for ${this.#transport}`);
|
||||
assert(def, () => `Unable to find a transport for ${this.#transport}`);
|
||||
|
||||
const transport = await def.create();
|
||||
|
||||
@@ -98,7 +98,7 @@ export class Ledger {
|
||||
#wrapError = async <T extends ResponseBase> (promise: Promise<T>): Promise<T> => {
|
||||
const result = await promise;
|
||||
|
||||
assert(result.return_code === LEDGER_SUCCESS_CODE, result.error_message);
|
||||
assert(result.return_code === LEDGER_SUCCESS_CODE, () => result.error_message);
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
@@ -47,7 +47,7 @@ export class Keyring implements KeyringInstance {
|
||||
constructor (options: KeyringOptions = {}) {
|
||||
options.type = options.type || 'ed25519';
|
||||
|
||||
assert(options && ['ecdsa', 'ethereum', 'ed25519', 'sr25519'].includes(options.type || 'undefined'), `Expected a keyring type of either 'ed25519', 'sr25519', 'ethereum' or 'ecdsa', found '${options.type}`);
|
||||
assert(['ecdsa', 'ethereum', 'ed25519', 'sr25519'].includes(options.type || 'undefined'), () => `Expected a keyring type of either 'ed25519', 'sr25519', 'ethereum' or 'ecdsa', found '${options.type || 'unknown'}`);
|
||||
|
||||
this.#pairs = new Pairs();
|
||||
this.#ss58 = options.ss58Format;
|
||||
@@ -168,7 +168,7 @@ export class Keyring implements KeyringInstance {
|
||||
? [type]
|
||||
: type;
|
||||
|
||||
assert(['ed25519', 'sr25519', 'ecdsa', 'ethereum'].includes(cryptoType), `Unknown crypto type ${cryptoType}`);
|
||||
assert(['ed25519', 'sr25519', 'ecdsa', 'ethereum'].includes(cryptoType), () => `Unknown crypto type ${cryptoType}`);
|
||||
|
||||
// Here the address and publicKey are 32 bytes and isomorphic. This is why the address field needs to be the public key for ethereum type pairs
|
||||
const publicKey = isHex(address)
|
||||
|
||||
@@ -24,13 +24,11 @@ export class Pairs implements KeyringPairs {
|
||||
public get (address: string | Uint8Array): KeyringPair {
|
||||
const pair = this.#map[decodeAddress(address).toString()];
|
||||
|
||||
assert(pair, (): string => {
|
||||
const formatted: string = isU8a(address) || isHex(address)
|
||||
assert(pair, () => `Unable to retrieve keypair '${
|
||||
isU8a(address) || isHex(address)
|
||||
? u8aToHex(u8aToU8a(address))
|
||||
: address;
|
||||
|
||||
return `Unable to retrieve keypair '${formatted}'`;
|
||||
});
|
||||
: address
|
||||
}'`);
|
||||
|
||||
return pair;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ export function decodeAddress (encoded?: string | Uint8Array | null, ignoreCheck
|
||||
const [isValid, endPos, ss58Length, ss58Decoded] = checkAddressChecksum(decoded);
|
||||
|
||||
assert(ignoreChecksum || isValid, 'Invalid decoded address checksum');
|
||||
assert([-1, ss58Decoded].includes(ss58Format), `Expected ss58Format ${ss58Format}, received ${ss58Decoded}`);
|
||||
assert([-1, ss58Decoded].includes(ss58Format), () => `Expected ss58Format ${ss58Format}, received ${ss58Decoded}`);
|
||||
|
||||
return decoded.slice(ss58Length, endPos);
|
||||
} catch (error) {
|
||||
|
||||
@@ -16,7 +16,7 @@ export function encodeAddress (key: Uint8Array | string, ss58Format: Prefix = de
|
||||
const u8a = decodeAddress(key);
|
||||
|
||||
assert(ss58Format >= 0 && ss58Format <= 16383 && ![46, 47].includes(ss58Format), 'Out of range ss58Format specified');
|
||||
assert(defaults.allowedDecodedLengths.includes(u8a.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 input = u8aConcat(
|
||||
ss58Format < 64
|
||||
|
||||
@@ -14,11 +14,11 @@ interface CheckConfig {
|
||||
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}'`);
|
||||
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}`);
|
||||
assert(alphabet.includes(value[i]), () => `Invalid ${type} character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -28,7 +28,7 @@ export function keyExtractPath (derivePath: string): ExtractResult {
|
||||
});
|
||||
}
|
||||
|
||||
assert(constructed === derivePath, `Re-constructed path "${constructed}" does not match input`);
|
||||
assert(constructed === derivePath, () => `Re-constructed path "${constructed}" does not match input`);
|
||||
|
||||
return {
|
||||
parts,
|
||||
|
||||
@@ -26,7 +26,7 @@ import { mnemonicValidate } from './validate';
|
||||
*/
|
||||
export function mnemonicToLegacySeed (mnemonic: string, password = '', onlyJs = false, byteLength: 32 | 64 = 32): Uint8Array {
|
||||
assert(mnemonicValidate(mnemonic), 'Invalid bip39 mnemonic specified');
|
||||
assert([32, 64].includes(byteLength), `Invalid seed length ${byteLength}, expected 32 or 64`);
|
||||
assert([32, 64].includes(byteLength), () => `Invalid seed length ${byteLength}, expected 32 or 64`);
|
||||
|
||||
if (byteLength && byteLength === 32) {
|
||||
return isReady() && !onlyJs
|
||||
|
||||
@@ -25,8 +25,8 @@ export function naclVerify (message: Uint8Array | string, signature: Uint8Array
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
|
||||
assert(publicKeyU8a.length === 32, `Invalid publicKey, received ${publicKeyU8a.length}, expected 32`);
|
||||
assert(signatureU8a.length === 64, `Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
|
||||
assert(publicKeyU8a.length === 32, () => `Invalid publicKey, received ${publicKeyU8a.length}, expected 32`);
|
||||
assert(signatureU8a.length === 64, () => `Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
|
||||
|
||||
return isReady() && !onlyJs
|
||||
? ed25519Verify(signatureU8a, messageU8a, publicKeyU8a)
|
||||
|
||||
@@ -12,8 +12,8 @@ export function schnorrkelVerify (message: Uint8Array | string, signature: Uint8
|
||||
const publicKeyU8a = u8aToU8a(publicKey);
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
|
||||
assert(publicKeyU8a.length === 32, `Invalid publicKey, received ${publicKeyU8a.length} bytes, expected 32`);
|
||||
assert(signatureU8a.length === 64, `Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
|
||||
assert(publicKeyU8a.length === 32, () => `Invalid publicKey, received ${publicKeyU8a.length} bytes, expected 32`);
|
||||
assert(signatureU8a.length === 64, () => `Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
|
||||
|
||||
return sr25519Verify(signatureU8a, u8aToU8a(message), publicKeyU8a);
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ function verifyDetect (result: VerifyResult, { message, publicKey, signature }:
|
||||
}
|
||||
|
||||
function verifyMultisig (result: VerifyResult, { message, publicKey, signature }: VerifyInput): VerifyResult {
|
||||
assert([0, 1, 2].includes(signature[0]), `Unknown crypto type, expected signature prefix [0..2], found ${signature[0]}`);
|
||||
assert([0, 1, 2].includes(signature[0]), () => `Unknown crypto type, expected signature prefix [0..2], found ${signature[0]}`);
|
||||
|
||||
const type = CRYPTO_TYPES[signature[0]] || 'none';
|
||||
|
||||
@@ -77,7 +77,7 @@ function verifyMultisig (result: VerifyResult, { message, publicKey, signature }
|
||||
export function signatureVerify (message: Uint8Array | string, signature: Uint8Array | string, addressOrPublicKey: Uint8Array | string): VerifyResult {
|
||||
const signatureU8a = u8aToU8a(signature);
|
||||
|
||||
assert([64, 65, 66].includes(signatureU8a.length), `Invalid signature length, expected [64..66] bytes, found ${signatureU8a.length}`);
|
||||
assert([64, 65, 66].includes(signatureU8a.length), () => `Invalid signature length, expected [64..66] bytes, found ${signatureU8a.length}`);
|
||||
|
||||
const publicKey = decodeAddress(addressOrPublicKey);
|
||||
const input = { message, publicKey, signature: signatureU8a };
|
||||
|
||||
@@ -42,7 +42,7 @@ export function compactToU8a (_value: BN | BigInt | number): Uint8Array {
|
||||
length--;
|
||||
}
|
||||
|
||||
assert(length >= 4, 'Previous tests match anyting less than 2^30; qed');
|
||||
assert(length >= 4, 'Invalid length, previous checks match anything less than 2^30');
|
||||
|
||||
return u8aConcat(
|
||||
// subtract 4 as minimum (also catered for in decoding)
|
||||
|
||||
@@ -25,7 +25,7 @@ export function hexToU8a (_value?: string | null, bitLength = -1): Uint8Array {
|
||||
return new Uint8Array();
|
||||
}
|
||||
|
||||
assert(isHex(_value), `Expected hex value to convert, found '${_value}'`);
|
||||
assert(isHex(_value), () => `Expected hex value to convert, found '${_value}'`);
|
||||
|
||||
const value = hexStripPrefix(_value);
|
||||
const valLength = value.length / 2;
|
||||
|
||||
@@ -35,7 +35,7 @@ export function u8aToU8a (value?: number[] | Buffer | Uint8Array | string | null
|
||||
return new Uint8Array(value);
|
||||
}
|
||||
|
||||
assert(isU8a(value), `Unable to convert ${value.toString()} (typeof ${typeof value}) to a Uint8Array`);
|
||||
assert(isU8a(value), () => `Unable to convert ${value.toString()} (typeof ${typeof value}) to a Uint8Array`);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ function getPath (pathOrFn?: FnString | string | false): string {
|
||||
* @summary Checks that a specific package is only imported once
|
||||
*/
|
||||
export function detectPackage ({ name, version }: PackageJson, pathOrFn?: FnString | string | false, deps: PackageJson[] = []): void {
|
||||
assert(name.startsWith('@polkadot'), `Invalid package descriptor ${name}`);
|
||||
assert(name.startsWith('@polkadot'), () => `Invalid package descriptor ${name}`);
|
||||
|
||||
const entry = getEntry(name);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user