Explicit length checks for u8aFixed (#3630)

* Explicit length checks for u8aFixed

* Adjust tests
This commit is contained in:
Jaco
2021-06-09 15:19:48 +02:00
committed by GitHub
parent 09f247432f
commit 400e14d6c9
8 changed files with 21 additions and 18 deletions
+8
View File
@@ -1,5 +1,13 @@
# CHANGELOG
## master
Changes:
- Do strict length checking on `[u8; <length>]` type inputs
- Add upgrade block for Kusama 9040
## 4.13.1 Jun 6, 2021
Upgrade priority: Medium. Required for users connecting to Statemine and future Kusama upgrades.
+1 -1
View File
@@ -57,7 +57,7 @@ export class MockProvider implements ProviderInterface {
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-unsafe-member-access
chain_getBlock: () => this.registry.createType('SignedBlock', rpcSignedBlock.result).toJSON(),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
chain_getBlockHash: () => '0x1234',
chain_getBlockHash: () => '0x1234000000000000000000000000000000000000000000000000000000000000',
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
chain_getFinalizedHead: () => this.registry.createType('Header', rpcHeader.result).hash,
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
+1 -1
View File
@@ -30,7 +30,7 @@ describe('U8aFixed', (): void => {
it('constructs from number[]', (): void => {
expect(
new (U8aFixed.with(32))(registry, [0x02, 0x03]).toU8a()
new (U8aFixed.with(32))(registry, [0x02, 0x03, 0x00, 0x00]).toU8a()
).toEqual(
new Uint8Array([0x02, 0x03, 0x00, 0x00])
);
+7 -11
View File
@@ -4,7 +4,7 @@
import type { AnyU8a, Constructor, Registry } from '../types';
import type { U8aBitLength } from './types';
import { isString, u8aToU8a } from '@polkadot/util';
import { assert, isString, u8aToU8a } from '@polkadot/util';
import { Raw } from './Raw';
@@ -14,20 +14,16 @@ function decodeU8aFixed (value: AnyU8a, bitLength: U8aBitLength): AnyU8a {
return decodeU8aFixed(u8aToU8a(value), bitLength);
}
// ensure that we have an actual u8a with the full length as specified by
// the bitLength input (padded with zeros as required)
const byteLength = bitLength / 8;
const sub = value.subarray(0, byteLength);
if (sub.length === byteLength) {
return sub;
}
const u8a = new Uint8Array(byteLength);
u8a.set(sub, 0);
if (!value || !value.length) {
return u8a;
}
return u8a;
assert(value.length >= byteLength, () => `Expected at least ${byteLength} bytes (${bitLength} bits), found ${value.length} bytes`);
return value.subarray(0, byteLength);
}
/**
@@ -69,7 +69,7 @@ describe('EthereumAccountId', (): void => {
testEncode('toHex', '0x4119b2e6c3cb618f4f0b93ac77f9beec7ff02887');
testEncode('toJSON', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887');
testEncode('toString', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887');
testEncode('toString', '0x0000000000000000000000000000000000000000', '0x00');
testEncode('toString', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000');
testEncode('toU8a', Uint8Array.from([
0x41, 0x19, 0xb2, 0xe6, 0xc3, 0xcb, 0x61, 0x8f, 0x4f, 0x0b,
0x93, 0xac, 0x77, 0xf9, 0xbe, 0xec, 0x7f, 0xf0, 0x28, 0x87
@@ -12,7 +12,7 @@ describe('StorageChangeSet', (): void => {
describe('construction', (): void => {
const set = registry.createType('StorageChangeSet', {
block: '0x1234',
block: '0x1234000000000000000000000000000000000000000000000000000000000000',
changes: [
['0xab', '0xcd']
]
@@ -34,8 +34,7 @@ describe('EventRecord', (): void => {
const er = records[0];
expect(er.phase.type).toEqual('ApplyExtrinsic');
// additional payment info, weight u64 (vs u32)
expect(records.toHex()).toEqual(`${hex as string}00000000000000000000`);
expect(records.toHex()).toEqual(hex);
});
});
});
+1 -1
View File
@@ -7,7 +7,7 @@
"changes": [
[
"0xcc956bdb7605e3547539f321ac2bc95c",
"0x0800000000000000000001000000000000"
"0x08000000000000000000010000000000000000000000000000000000000000000000000000"
]
]
},