Skip to content

Commit d29660d

Browse files
authored
Adjust size-assert for U8Fixed (#4787)
1 parent 1870ad2 commit d29660d

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Contributed:
99
Changes:
1010

1111
- Extract metadata v14 `BTreeSet` types into correct type
12+
- Assert to ensure that `[u8; <size>]` has exact sizes (non-u8a inputs)
1213
- Adjust `tx.paymentInfo` signing process
1314
- Add Kusama 9190 upgrade block
1415
- Add Westend 9200 upgrade block

packages/types-codec/src/extended/U8aFixed.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,43 @@ describe('U8aFixed', (): void => {
3535
new Uint8Array([0x02, 0x03, 0x00, 0x00])
3636
);
3737
});
38+
39+
it('constructs when passed Uint8Array is >= length', (): void => {
40+
expect(
41+
new (U8aFixed.with(32))(registry, new Uint8Array([0x00, 0x01, 0x02, 0x03])).toU8a()
42+
).toEqual(
43+
new Uint8Array([0x00, 0x01, 0x02, 0x03])
44+
);
45+
expect(
46+
new (U8aFixed.with(32))(registry, new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05])).toU8a()
47+
).toEqual(
48+
new Uint8Array([0x00, 0x01, 0x02, 0x03])
49+
);
50+
});
51+
52+
it('constructs when passed string is === length', (): void => {
53+
expect(
54+
new (U8aFixed.with(32))(registry, '1234').toU8a()
55+
).toEqual(
56+
new Uint8Array([49, 50, 51, 52])
57+
);
58+
});
59+
60+
it('fails construction when passed string is > length', (): void => {
61+
expect(
62+
() => new (U8aFixed.with(32))(registry, '0x000102030405').toU8a()
63+
).toThrow(/Expected input with 4 bytes/);
64+
expect(
65+
() => new (U8aFixed.with(256))(registry, '1363HWTPzDrzAQ6ChFiMU6mP4b6jmQid2ae55JQcKtZnpLGv')
66+
).toThrow(/Expected input with 32 bytes/);
67+
});
3868
});
3969

4070
describe('utils', (): void => {
4171
let u8a: U8aFixed;
4272

4373
beforeEach((): void => {
44-
u8a = new U8aFixed(registry, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 32);
74+
u8a = new U8aFixed(registry, [1, 2, 3, 4], 32);
4575
});
4676

4777
it('limits the length', (): void => {

packages/types-codec/src/extended/U8aFixed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import type { AnyU8a, CodecClass, Registry, U8aBitLength } from '../types';
55

6-
import { assert, u8aToU8a } from '@polkadot/util';
6+
import { assert, isU8a, u8aToU8a } from '@polkadot/util';
77

88
import { Raw } from '../native/Raw';
99

@@ -16,7 +16,7 @@ function decodeU8aFixed (value: AnyU8a, bitLength: U8aBitLength): [AnyU8a, numbe
1616
return [new Uint8Array(byteLength), 0];
1717
}
1818

19-
assert(u8a.length >= byteLength, () => `Expected at least ${byteLength} bytes (${bitLength} bits), found ${u8a.length} bytes`);
19+
assert(isU8a(value) ? u8a.length >= byteLength : u8a.length === byteLength, () => `Expected input with ${byteLength} bytes (${bitLength} bits), found ${u8a.length} bytes`);
2020

2121
return [u8a.subarray(0, byteLength), byteLength];
2222
}

0 commit comments

Comments
 (0)