Add u8a-only strict variants (#1503)

This commit is contained in:
Jaco
2022-05-25 10:02:01 +03:00
committed by GitHub
parent 2698f6779f
commit fbf973dc2a
20 changed files with 105 additions and 37 deletions
+4 -4
View File
@@ -3,7 +3,7 @@
import { assert } from '../assert';
import { BN, BN_ONE, BN_TWO, bnToBn, bnToU8a } from '../bn';
import { u8aConcat } from '../u8a';
import { u8aConcatStrict } from '../u8a';
const MAX_U8 = BN_TWO.pow(new BN(8 - 2)).isub(BN_ONE);
const MAX_U16 = BN_TWO.pow(new BN(16 - 2)).isub(BN_ONE);
@@ -44,9 +44,9 @@ export function compactToU8a (value: BN | bigint | number): Uint8Array {
assert(length >= 4, 'Invalid length, previous checks match anything less than 2^30');
return u8aConcat(
return u8aConcatStrict([
// subtract 4 as minimum (also catered for in decoding)
[((length - 4) << 2) + 0b11],
new Uint8Array([((length - 4) << 2) + 0b11]),
u8a.subarray(0, length)
);
]);
}