assert does asserts conditions (TS 3.7) (#489)
* assert does asserts conditions (TS 3.7) * Throw in condition * Fix cleanup
This commit is contained in:
@@ -1,3 +1,9 @@
|
||||
# 1.7.0-beta.x
|
||||
|
||||
- `formatBalance` signature now allows to force unit displays
|
||||
- Keypair now allows for derive on a pair, with `pair.derive(...)`
|
||||
- `assert` now acts like a real asserts, with no return - signature adapted for new TS 3.7
|
||||
|
||||
# 1.6.1
|
||||
|
||||
- `pair.sign(message)` now takes an optional second `options?: SignOptions` parameter, where `SignOptions` is currently defined as `{ withType: boolean }`. Here `withType` instructs the signing to prepend the type of signature (ed2551, sr25519 or ecdsa). This is used for the new Polkadot/Substrate MultiSignature format.
|
||||
|
||||
@@ -14,10 +14,9 @@ type DecodeResult = PairInfo & {
|
||||
secretKey: Uint8Array;
|
||||
};
|
||||
|
||||
export default function decode (passphrase?: string, _encrypted?: Uint8Array | null): DecodeResult {
|
||||
assert(_encrypted, 'No encrypted data available to decode');
|
||||
export default function decode (passphrase?: string, encrypted?: Uint8Array | null): DecodeResult {
|
||||
assert(encrypted, 'No encrypted data available to decode');
|
||||
|
||||
const encrypted = (_encrypted as Uint8Array);
|
||||
const encoded = passphrase
|
||||
? (naclDecrypt(
|
||||
encrypted.subarray(NONCE_LENGTH),
|
||||
|
||||
@@ -14,7 +14,7 @@ export default function encode ({ publicKey, secretKey }: PairInfo, passphrase?:
|
||||
|
||||
const encoded = u8aConcat(
|
||||
PKCS8_HEADER,
|
||||
secretKey as Uint8Array,
|
||||
secretKey,
|
||||
PKCS8_DIVIDER,
|
||||
publicKey
|
||||
);
|
||||
|
||||
@@ -119,7 +119,7 @@ export default function createPair (type: KeypairType, { publicKey, secretKey }:
|
||||
assert(secretKey, 'Cannot derive on a locked keypair');
|
||||
|
||||
const { path } = keyExtractPath(suri);
|
||||
const derived = keyFromPath({ publicKey, secretKey: secretKey as Uint8Array }, path, type);
|
||||
const derived = keyFromPath({ publicKey, secretKey: secretKey }, path, type);
|
||||
|
||||
return createPair(type, derived, meta, null);
|
||||
},
|
||||
|
||||
@@ -28,6 +28,6 @@ export default function naclSign (message: Uint8Array | string, { publicKey, sec
|
||||
const messageU8a = u8aToU8a(message);
|
||||
|
||||
return isReady()
|
||||
? ed25519Sign(publicKey as Uint8Array, (secretKey as Uint8Array).subarray(0, 32), messageU8a)
|
||||
: nacl.sign.detached(messageU8a, secretKey as Uint8Array);
|
||||
? ed25519Sign(publicKey as Uint8Array, secretKey.subarray(0, 32), messageU8a)
|
||||
: nacl.sign.detached(messageU8a, secretKey);
|
||||
}
|
||||
|
||||
@@ -14,10 +14,10 @@ import { sr25519Sign } from '@polkadot/wasm-crypto';
|
||||
* @description Returns message signature of `message`, using the supplied pair
|
||||
*/
|
||||
export default function schnorrkelSign (message: Uint8Array | string, { publicKey, secretKey }: Partial<Keypair>): Uint8Array {
|
||||
assert(publicKey && publicKey.length === 32, 'Expected valid publicKey, 32-bytes');
|
||||
assert(secretKey && secretKey.length === 64, 'Expected valid secretKey, 64-bytes');
|
||||
assert(publicKey?.length === 32, 'Expected valid publicKey, 32-bytes');
|
||||
assert(secretKey?.length === 64, 'Expected valid secretKey, 64-bytes');
|
||||
|
||||
const messageU8a = u8aToU8a(message);
|
||||
|
||||
return sr25519Sign(publicKey as Uint8Array, secretKey as Uint8Array, messageU8a);
|
||||
return sr25519Sign(publicKey, secretKey, messageU8a);
|
||||
}
|
||||
|
||||
@@ -30,5 +30,5 @@ export default function decode (input: Uint8Array): DecodeOutput {
|
||||
|
||||
assert(decoder, 'Unable to find decoder for input type');
|
||||
|
||||
return (decoder as Decoder).fn(decode, input);
|
||||
return decoder.fn(decode, input);
|
||||
}
|
||||
|
||||
@@ -45,5 +45,5 @@ export default function toU8a (value?: any): Uint8Array {
|
||||
|
||||
assert(encoder, 'invalid type');
|
||||
|
||||
return (encoder as Encoder).fn(value);
|
||||
return encoder.fn(value);
|
||||
}
|
||||
|
||||
@@ -6,20 +6,18 @@ import { assert } from '.';
|
||||
|
||||
describe('assert', (): void => {
|
||||
it('should not throw an error when test is true', (): void => {
|
||||
expect(
|
||||
assert(true, 'nothing should be thrown')
|
||||
).toEqual(true);
|
||||
assert(true, 'nothing should be thrown');
|
||||
});
|
||||
|
||||
it('should throw an error when test is not true', (): void => {
|
||||
expect(
|
||||
(): boolean => assert(false, 'error thrown')
|
||||
(): void => assert(false, 'error thrown')
|
||||
).toThrow(/error thrown/);
|
||||
});
|
||||
|
||||
it('should throw an error when message: () => string', (): void => {
|
||||
expect(
|
||||
(): boolean => assert(false, (): string => 'message from function')
|
||||
(): void => assert(false, (): string => 'message from function')
|
||||
).toThrow(/message from function/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,8 +6,6 @@ import isFunction from './is/function';
|
||||
|
||||
type MessageFn = () => string;
|
||||
|
||||
type Falsy = null | undefined | false | 0 | ''; // No NaN type
|
||||
|
||||
/**
|
||||
* @name assert
|
||||
* @summary Checks for a valid test, if not Error is thrown.
|
||||
@@ -19,19 +17,18 @@ type Falsy = null | undefined | false | 0 | ''; // No NaN type
|
||||
* ```javascript
|
||||
* const { assert } from '@polkadot/util';
|
||||
*
|
||||
* assert(true, 'True should be true'); // true returned
|
||||
* assert(true, 'True should be true'); // passes
|
||||
* assert(false, 'False should not be true'); // Error thrown
|
||||
* assert(false, () => 'message'); // Error with 'message'
|
||||
* ```
|
||||
*/
|
||||
export default function assert<T> (test: Falsy | T, message: string | MessageFn): test is T {
|
||||
if (test) {
|
||||
return true;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export default function assert (condition: any, message: string | MessageFn): asserts condition {
|
||||
if (!condition) {
|
||||
throw new Error(
|
||||
isFunction(message)
|
||||
? message()
|
||||
: message
|
||||
);
|
||||
}
|
||||
|
||||
if (isFunction(message)) {
|
||||
message = message();
|
||||
}
|
||||
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user