Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Contributed:
Changes:

- Extract metadata v14 `BTreeSet` types into correct type
- Assert to ensure that `[u8; <size>]` has exact sizes (non-u8a inputs)
- Adjust `tx.paymentInfo` signing process
- Add Kusama 9190 upgrade block
- Add Westend 9200 upgrade block
Expand Down
32 changes: 31 additions & 1 deletion packages/types-codec/src/extended/U8aFixed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,43 @@ describe('U8aFixed', (): void => {
new Uint8Array([0x02, 0x03, 0x00, 0x00])
);
});

it('constructs when passed Uint8Array is >= length', (): void => {
expect(
new (U8aFixed.with(32))(registry, new Uint8Array([0x00, 0x01, 0x02, 0x03])).toU8a()
).toEqual(
new Uint8Array([0x00, 0x01, 0x02, 0x03])
);
expect(
new (U8aFixed.with(32))(registry, new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05])).toU8a()
).toEqual(
new Uint8Array([0x00, 0x01, 0x02, 0x03])
);
});

it('constructs when passed string is === length', (): void => {
expect(
new (U8aFixed.with(32))(registry, '1234').toU8a()
).toEqual(
new Uint8Array([49, 50, 51, 52])
);
});

it('fails construction when passed string is > length', (): void => {
expect(
() => new (U8aFixed.with(32))(registry, '0x000102030405').toU8a()
).toThrow(/Expected input with 4 bytes/);
expect(
() => new (U8aFixed.with(256))(registry, '1363HWTPzDrzAQ6ChFiMU6mP4b6jmQid2ae55JQcKtZnpLGv')
).toThrow(/Expected input with 32 bytes/);
});
});

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

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

it('limits the length', (): void => {
Expand Down
4 changes: 2 additions & 2 deletions packages/types-codec/src/extended/U8aFixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

import { assert, u8aToU8a } from '@polkadot/util';
import { assert, isU8a, u8aToU8a } from '@polkadot/util';

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

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

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

return [u8a.subarray(0, byteLength), byteLength];
}
Expand Down