Type checking on u8aToU8a before invalid return (#954)

* Type checking on u8aToU8a before invalid return

* Remove extra assignment

* CHANGELOG

* Correct expectation
This commit is contained in:
Jaco
2021-04-22 10:25:22 +02:00
committed by GitHub
parent 6b705a9838
commit 132ba0128d
6 changed files with 37 additions and 23 deletions
+4 -4
View File
@@ -15,25 +15,25 @@ describe('compactFromU8a', (): void => {
it('decodes from same u16 encoded value', (): void => {
expect(
compactFromU8a(new Uint8Array([0b11111101, 0b00000111]), 32)
compactFromU8a(new Uint8Array([0b11111101, 0b00000111]))
).toEqual([2, new BN(511)]);
});
it('decodes from same u32 encoded value (short)', (): void => {
expect(
compactFromU8a(new Uint8Array([254, 255, 3, 0]), 32)
compactFromU8a(new Uint8Array([254, 255, 3, 0]))
).toEqual([4, new BN(0xffff)]);
});
it('decodes from same u32 encoded value (full)', (): void => {
expect(
compactFromU8a(new Uint8Array([3, 249, 255, 255, 255]), 32)
compactFromU8a(new Uint8Array([3, 249, 255, 255, 255]))
).toEqual([5, new BN(0xfffffff9)]);
});
it('decodes from same u32 as u64 encoded value (full, default)', (): void => {
expect(
compactFromU8a(new Uint8Array([3 + ((4 - 4) << 2), 249, 255, 255, 255]), 64)
compactFromU8a(new Uint8Array([3 + ((4 - 4) << 2), 249, 255, 255, 255]))
).toEqual([5, new BN(0xfffffff9)]);
});
+8 -11
View File
@@ -1,12 +1,9 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BitLength } from './types';
import BN from 'bn.js';
import { u8aToBn, u8aToU8a } from '../u8a';
import { DEFAULT_BITLENGTH } from './defaults';
/**
* @name compactFromU8a
@@ -17,13 +14,12 @@ import { DEFAULT_BITLENGTH } from './defaults';
* ```javascript
* import { compactFromU8a } from '@polkadot/util';
*
* const [offset, length] = compactFromU8a(new Uint8Array([254, 255, 3, 0]), 32));
* const [offset, length] = compactFromU8a(new Uint8Array([254, 255, 3, 0]));
*
* console.log('value offset=', offset, 'length=', length); // 4, 0xffff
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function compactFromU8a (_input: Uint8Array | string, bitLength: BitLength = DEFAULT_BITLENGTH): [number, BN] {
export function compactFromU8a (_input: Buffer | Uint8Array | string | number[]): [number, BN] {
const input = u8aToU8a(_input);
const flag = input[0] & 0b11;
@@ -35,11 +31,12 @@ export function compactFromU8a (_input: Uint8Array | string, bitLength: BitLengt
return [4, u8aToBn(input.slice(0, 4), true).shrn(2)];
}
const length = new BN(input[0])
.shrn(2) // clear flag
.addn(4) // add 4 for base length
.toNumber();
const offset = 1 + length;
const offset = 1 + (
new BN(input[0])
.shrn(2) // clear flag
.addn(4) // add 4 for base length
.toNumber()
);
return [offset, u8aToBn(input.subarray(1, offset), true)];
}
+2 -5
View File
@@ -1,9 +1,6 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BitLength } from './types';
import { DEFAULT_BITLENGTH } from './defaults';
import { compactFromU8a } from './fromU8a';
/**
@@ -18,8 +15,8 @@ import { compactFromU8a } from './fromU8a';
* console.log(compactStripLength(new Uint8Array([2 << 2, 0xde, 0xad]))); // [2, Uint8Array[0xde, 0xad]]
* ```
*/
export function compactStripLength (input: Uint8Array, bitLength: BitLength = DEFAULT_BITLENGTH): [number, Uint8Array] {
const [offset, length] = compactFromU8a(input, bitLength);
export function compactStripLength (input: Uint8Array): [number, Uint8Array] {
const [offset, length] = compactFromU8a(input);
const total = offset + length.toNumber();
return [
+7
View File
@@ -55,4 +55,11 @@ describe('u8aToU8a', (): void => {
new Uint8Array([128, 0, 10])
);
});
it('fails to create from unknown typed values', (): void => {
expect(
// this is where completely invalid data is being passed
() => u8aToU8a(123 as unknown as Uint8Array)
).toThrow('Unable to convert 123 (typeof number) to a Uint8Array');
});
});
+7 -3
View File
@@ -1,10 +1,12 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { assert } from '../assert';
import { hexToU8a } from '../hex/toU8a';
import { isBuffer } from '../is/buffer';
import { isHex } from '../is/hex';
import { isString } from '../is/string';
import { isU8a } from '../is/u8a';
import { stringToU8a } from '../string/toU8a';
/**
@@ -29,9 +31,11 @@ export function u8aToU8a (value?: number[] | Buffer | Uint8Array | string | null
return isHex(value)
? hexToU8a(value)
: stringToU8a(value);
} else if (Array.isArray(value) || isBuffer(value)) {
return new Uint8Array(value);
}
return Array.isArray(value) || isBuffer(value)
? new Uint8Array(value)
: value;
assert(isU8a(value), `Unable to convert ${value.toString()} (typeof ${typeof value}) to a Uint8Array`);
return value;
}