Use compactFromU8aLim & u8aConcatStrict (#4859)
* Use compactFromU8aLim & u8aConcatStrict * Bump common
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, Codec, Inspect, IU8a, IVec, Registry } from '../types';
|
||||
|
||||
import { compactToU8a, u8aConcat, u8aToHex } from '@polkadot/util';
|
||||
import { compactToU8a, u8aConcatStrict, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { compareArray } from '../utils/compareArray';
|
||||
|
||||
@@ -156,8 +156,8 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
const encoded = this.toU8aInner();
|
||||
|
||||
return isBare
|
||||
? u8aConcat(...encoded)
|
||||
: u8aConcat(compactToU8a(this.length), ...encoded);
|
||||
? u8aConcatStrict(encoded)
|
||||
: u8aConcatStrict([compactToU8a(this.length), ...encoded]);
|
||||
}
|
||||
|
||||
public toU8aInner (isBare?: boolean): Uint8Array[] {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, Codec, CodecClass, IEnum, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { assert, isCodec, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { assert, isCodec, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { mapToTypeMap, typesToMap } from '../utils';
|
||||
import { Null } from './Null';
|
||||
@@ -432,9 +432,11 @@ export class Enum implements IEnum {
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public toU8a (isBare?: boolean): Uint8Array {
|
||||
return u8aConcat(
|
||||
new Uint8Array(isBare ? [] : [this.index]),
|
||||
this.#raw.toU8a(isBare)
|
||||
);
|
||||
return isBare
|
||||
? this.#raw.toU8a(isBare)
|
||||
: u8aConcatStrict([
|
||||
new Uint8Array([this.index]),
|
||||
this.#raw.toU8a(isBare)
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import type { AnyTupleValue, Codec, CodecClass, Inspect, ITuple, Registry } from '../types';
|
||||
|
||||
import { isFunction, isHex, isString, isU8a, stringify, u8aConcat, u8aToU8a } from '@polkadot/util';
|
||||
import { isFunction, isHex, isString, isU8a, stringify, u8aConcatStrict, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { AbstractArray } from '../abstract/AbstractArray';
|
||||
import { decodeU8a, mapToTypeMap, typeToConstructor } from '../utils';
|
||||
@@ -147,6 +147,6 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public override toU8a (isBare?: boolean): Uint8Array {
|
||||
return u8aConcat(...this.toU8aInner(isBare));
|
||||
return u8aConcatStrict(this.toU8aInner(isBare));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Codec, CodecClass, Registry } from '../types';
|
||||
|
||||
import { assert, compactFromU8a, isU8a, logger, u8aToU8a } from '@polkadot/util';
|
||||
import { assert, compactFromU8aLim, isU8a, logger, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { AbstractArray } from '../abstract/AbstractArray';
|
||||
import { decodeU8aVec, typeToConstructor } from '../utils';
|
||||
@@ -28,11 +28,11 @@ function decodeVecLength (value: Uint8Array | HexString | unknown[]): [Uint8Arra
|
||||
}
|
||||
|
||||
const u8a = u8aToU8a(value);
|
||||
const [startAt, length] = compactFromU8a(u8a);
|
||||
const [startAt, length] = compactFromU8aLim(u8a);
|
||||
|
||||
assert(length.lten(MAX_LENGTH), () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
assert(length <= MAX_LENGTH, () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
|
||||
return [u8a, length.toNumber(), startAt];
|
||||
return [u8a, length, startAt];
|
||||
}
|
||||
|
||||
export function decodeVec<T extends Codec> (registry: Registry, result: T[], value: Uint8Array | HexString | unknown[], startAt: number, Type: CodecClass<T>): [number, number] {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Codec, CodecClass, Inspect, Registry } from '../types';
|
||||
|
||||
import { isU8a, u8aConcat } from '@polkadot/util';
|
||||
import { isU8a, u8aConcatStrict } from '@polkadot/util';
|
||||
|
||||
import { AbstractArray } from '../abstract/AbstractArray';
|
||||
import { decodeU8aVec, typeToConstructor } from '../utils';
|
||||
@@ -90,7 +90,7 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
|
||||
const encoded = this.toU8aInner();
|
||||
|
||||
return encoded.length
|
||||
? u8aConcat(...encoded)
|
||||
? u8aConcatStrict(encoded)
|
||||
: new Uint8Array([]);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, Codec, CodecClass, Inspect, ISet, IU8a, Registry } from '../types';
|
||||
|
||||
import { compactFromU8a, compactToU8a, isHex, isU8a, logger, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { compactFromU8aLim, compactToU8a, isHex, isU8a, logger, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { compareSet, decodeU8aVec, sortSet, typeToConstructor } from '../utils';
|
||||
|
||||
@@ -13,8 +13,7 @@ const l = logger('BTreeSet');
|
||||
/** @internal */
|
||||
function decodeSetFromU8a<V extends Codec> (registry: Registry, ValClass: CodecClass<V>, u8a: Uint8Array): [CodecClass<V>, Set<V>, number] {
|
||||
const output = new Set<V>();
|
||||
const [offset, length] = compactFromU8a(u8a);
|
||||
const count = length.toNumber();
|
||||
const [offset, count] = compactFromU8aLim(u8a);
|
||||
const result = new Array<V>(count);
|
||||
const [decodedLength] = decodeU8aVec(registry, result, u8a, offset, ValClass);
|
||||
|
||||
@@ -216,6 +215,6 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
encoded.push(v.toU8a(isBare));
|
||||
}
|
||||
|
||||
return u8aConcat(...encoded);
|
||||
return u8aConcatStrict(encoded);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import type { AnyU8a, Inspect, Registry } from '../types';
|
||||
|
||||
import { assert, compactFromU8a, compactToU8a, isString, u8aConcat, u8aToU8a } from '@polkadot/util';
|
||||
import { assert, compactFromU8aLim, compactToU8a, isString, u8aConcatStrict, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { Raw } from '../native/Raw';
|
||||
|
||||
@@ -14,12 +14,12 @@ function decodeBitVecU8a (value?: Uint8Array): [number, Uint8Array] {
|
||||
}
|
||||
|
||||
// handle all other Uint8Array inputs, these do have a length prefix which is the number of bits encoded
|
||||
const [offset, length] = compactFromU8a(value);
|
||||
const total = offset + Math.ceil(length.toNumber() / 8);
|
||||
const [offset, length] = compactFromU8aLim(value);
|
||||
const total = offset + Math.ceil(length / 8);
|
||||
|
||||
assert(total <= value.length, () => `BitVec: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
||||
|
||||
return [length.toNumber(), value.subarray(offset, total)];
|
||||
return [length, value.subarray(offset, total)];
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
@@ -96,6 +96,6 @@ export class BitVec extends Raw {
|
||||
|
||||
return isBare
|
||||
? bitVec
|
||||
: u8aConcat(compactToU8a(this.#decodedLength), bitVec);
|
||||
: u8aConcatStrict([compactToU8a(this.#decodedLength), bitVec]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import type { AnyU8a, Inspect, Registry } from '../types';
|
||||
|
||||
import { assert, compactAddLength, compactFromU8a, compactToU8a, isCodec, isString, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
import { assert, compactAddLength, compactFromU8aLim, compactToU8a, isCodec, isString, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { Raw } from '../native/Raw';
|
||||
|
||||
@@ -17,10 +17,10 @@ function decodeBytesU8a (value: Uint8Array): [Uint8Array, number] {
|
||||
}
|
||||
|
||||
// handle all other Uint8Array inputs, these do have a length prefix
|
||||
const [offset, length] = compactFromU8a(value);
|
||||
const total = offset + length.toNumber();
|
||||
const [offset, length] = compactFromU8aLim(value);
|
||||
const total = offset + length;
|
||||
|
||||
assert(length.lten(MAX_LENGTH), () => `Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
assert(length <= MAX_LENGTH, () => `Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
assert(total <= value.length, () => `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
||||
|
||||
return [value.subarray(offset, total), total];
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, Codec, CodecClass, IMap, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { compactFromU8a, compactToU8a, isHex, isObject, isU8a, logger, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { compactFromU8aLim, compactToU8a, isHex, isObject, isU8a, logger, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { AbstractArray } from '../abstract/AbstractArray';
|
||||
import { Enum } from '../base/Enum';
|
||||
@@ -16,8 +16,7 @@ const l = logger('Map');
|
||||
/** @internal */
|
||||
function decodeMapFromU8a<K extends Codec, V extends Codec> (registry: Registry, KeyClass: CodecClass<K>, ValClass: CodecClass<V>, u8a: Uint8Array): [CodecClass<K>, CodecClass<V>, Map<K, V>, number] {
|
||||
const output = new Map<K, V>();
|
||||
const [offset, length] = compactFromU8a(u8a);
|
||||
const count = length.toNumber();
|
||||
const [offset, count] = compactFromU8aLim(u8a);
|
||||
const types = [];
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
@@ -231,6 +230,6 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
encoded.push(k.toU8a(isBare), v.toU8a(isBare));
|
||||
}
|
||||
|
||||
return u8aConcat(...encoded);
|
||||
return u8aConcatStrict(encoded);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, BareOpts, Codec, CodecClass, Inspect, IStruct, IU8a, Registry } from '../types';
|
||||
|
||||
import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { compareMap, decodeU8a, mapToTypeMap, typesToMap } from '../utils';
|
||||
|
||||
@@ -321,6 +321,6 @@ export class Struct<
|
||||
}
|
||||
}
|
||||
|
||||
return u8aConcat(...encoded);
|
||||
return u8aConcatStrict(encoded);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyString, AnyU8a, Inspect, IText, IU8a, Registry } from '../types';
|
||||
|
||||
import { assert, compactAddLength, compactFromU8a, compactToU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
|
||||
import { assert, compactAddLength, compactFromU8aLim, compactToU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
|
||||
|
||||
import { Raw } from './Raw';
|
||||
|
||||
@@ -23,10 +23,10 @@ function decodeText (value?: null | AnyString | AnyU8a | { toString: () => strin
|
||||
return [u8aToString(value), 0];
|
||||
}
|
||||
|
||||
const [offset, length] = compactFromU8a(value);
|
||||
const total = offset + length.toNumber();
|
||||
const [offset, length] = compactFromU8aLim(value);
|
||||
const total = offset + length;
|
||||
|
||||
assert(length.lten(MAX_LENGTH), () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
assert(length <= MAX_LENGTH, () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
assert(total <= value.length, () => `Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
||||
|
||||
return [u8aToString(value.subarray(offset, total)), total];
|
||||
|
||||
Reference in New Issue
Block a user