diff --git a/CHANGELOG.md b/CHANGELOG.md index a64cbb7fd2..0bb88f3ce7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 0.91.0-beta.x - The `getRuntimeVersion` and `subscribeRuntimeVersion` RPCs are now only available on the `rpc.state.*` endpoints. This aligns with the Substrate implementation. +- Support for extrinsics with versions that is not in the base Substrate implementation (V1-V3) can now be done by providing an implementation for `ExtrinsicUnknown` # 0.90.1 diff --git a/packages/types/src/primitive/Bytes.ts b/packages/types/src/primitive/Bytes.ts index 139526a1c1..7bb4d24afc 100644 --- a/packages/types/src/primitive/Bytes.ts +++ b/packages/types/src/primitive/Bytes.ts @@ -24,41 +24,45 @@ export default class Bytes extends U8a { private static decodeBytes (value?: AnyU8a): Uint8Array | undefined { if (Array.isArray(value) || isString(value)) { - const u8a = u8aToU8a(value); - - return Bytes.decodeBytes( - Compact.addLengthPrefix(u8a) + return Bytes.decodeBytesU8a( + Compact.addLengthPrefix(u8aToU8a(value)) ); } else if (value instanceof ClassOf('StorageData')) { - // Here we cater for the actual StorageData that _could_ have a length prefix. In the - // case of `:code` it is not added, for others it is - const u8a = value as Uint8Array; - const [offset, length] = Compact.decodeU8a(u8a); - - return u8a.length === length.addn(offset).toNumber() - ? u8a.subarray(offset) - : u8a; - } else if (value instanceof U8a) { - // This is required. In the case of a U8a we already have gotten rid of the length, - // i.e. new Bytes(new Bytes(...)) will work as expected - return value; - } else if (isU8a(value)) { - if (!value.length) { - return new Uint8Array(); - } - - // handle all other Uint8Array inputs, these do have a length prefix - const [offset, length] = Compact.decodeU8a(value); - const total = offset + length.toNumber(); - - assert(total <= value.length, `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`); - - return value.subarray(offset, total); + return Bytes.decodeBytesStorage(value); + } else if (!(value instanceof U8a) && isU8a(value)) { + // We are ensuring we are not a U8a instance. In the case of a U8a we already have gotten + // rid of the length, i.e. new Bytes(new Bytes(...)) will work as expected + return Bytes.decodeBytesU8a(value); } return value; } + private static decodeBytesStorage (value: Uint8Array): Uint8Array { + // Here we cater for the actual StorageData that _could_ have a length prefix. In the + // case of `:code` it is not added, for others it is + const u8a = value as Uint8Array; + const [offset, length] = Compact.decodeU8a(u8a); + + return u8a.length === length.addn(offset).toNumber() + ? u8a.subarray(offset) + : u8a; + } + + private static decodeBytesU8a (value: Uint8Array): Uint8Array { + if (!value.length) { + return new Uint8Array(); + } + + // handle all other Uint8Array inputs, these do have a length prefix + const [offset, length] = Compact.decodeU8a(value); + const total = offset + length.toNumber(); + + assert(total <= value.length, `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`); + + return value.subarray(offset, total); + } + /** * @description The length of the value when encoded as a Uint8Array */ diff --git a/packages/types/src/primitive/Extrinsic/Extrinsic.ts b/packages/types/src/primitive/Extrinsic/Extrinsic.ts index cc9ae72820..d6ec13e466 100644 --- a/packages/types/src/primitive/Extrinsic/Extrinsic.ts +++ b/packages/types/src/primitive/Extrinsic/Extrinsic.ts @@ -64,32 +64,9 @@ export default class Extrinsic extends Base impl public static decodeExtrinsic (value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, version: number = DEFAULT_VERSION): ExtrinsicVx | ExtrinsicUnknown { if (Array.isArray(value) || isHex(value)) { - // Instead of the block below, it should simply be: - // return Extrinsic.decodeExtrinsic(hexToU8a(value as string)); - const u8a = u8aToU8a(value); - - // HACK 11 Jan 2019 - before https://github.com/paritytech/substrate/pull/1388 - // extrinsics didn't have the length, cater for both approaches - const [offset, length] = Compact.decodeU8a(u8a); - const withPrefix = u8a.length === (offset + length.toNumber()); - - return Extrinsic.decodeExtrinsic( - withPrefix - ? u8a - : Compact.addLengthPrefix(u8a), - version - ); + return Extrinsic.decodeU8aLike(value, version); } else if (isU8a(value)) { - if (!value.length) { - return Extrinsic.newFromValue(new Uint8Array(), version); - } - - const [offset, length] = Compact.decodeU8a(value); - const total = offset + length.toNumber(); - - assert(total <= value.length, `Extrinsic: required length less than remainder, expected at least ${total}, found ${value.length}`); - - return Extrinsic.decodeU8a(value.subarray(offset, total)); + return Extrinsic.decodeU8a(value, version); } else if (value instanceof ClassOf('Call')) { return Extrinsic.newFromValue({ method: value }, version); } @@ -97,8 +74,38 @@ export default class Extrinsic extends Base impl return Extrinsic.newFromValue(value, version); } - private static decodeU8a (value: Uint8Array): ExtrinsicVx | ExtrinsicUnknown { - return Extrinsic.newFromValue(value.subarray(1), value[0]); + private static decodeU8aLike (value: string | number[], version: number): ExtrinsicVx | ExtrinsicUnknown { + // Instead of the block below, it should simply be: + // return Extrinsic.decodeExtrinsic(hexToU8a(value as string)); + const u8a = u8aToU8a(value); + + // HACK 11 Jan 2019 - before https://github.com/paritytech/substrate/pull/1388 + // extrinsics didn't have the length, cater for both approaches. This is very + // inconsistent with any other `Vec` implementation + const [offset, length] = Compact.decodeU8a(u8a); + const withPrefix = u8a.length === (offset + length.toNumber()); + + return Extrinsic.decodeU8a( + withPrefix + ? u8a + : Compact.addLengthPrefix(u8a), + version + ); + } + + private static decodeU8a (value: Uint8Array, version: number): ExtrinsicVx | ExtrinsicUnknown { + if (!value.length) { + return Extrinsic.newFromValue(new Uint8Array(), version); + } + + const [offset, length] = Compact.decodeU8a(value); + const total = offset + length.toNumber(); + + assert(total <= value.length, `Extrinsic: required length less than remainder, expected at least ${total}, found ${value.length}`); + + const data = value.subarray(offset, total); + + return Extrinsic.newFromValue(data.subarray(1), data[0]); } /** diff --git a/packages/types/src/primitive/Extrinsic/ExtrinsicEra.ts b/packages/types/src/primitive/Extrinsic/ExtrinsicEra.ts index 0e6ca7ef3c..ce274b8c0e 100644 --- a/packages/types/src/primitive/Extrinsic/ExtrinsicEra.ts +++ b/packages/types/src/primitive/Extrinsic/ExtrinsicEra.ts @@ -63,14 +63,7 @@ export class MortalEra extends Tuple { } else if (isU8a(value)) { return MortalEra.decodeMortalU8a(value); } else if (isObject(value)) { - const { current, period } = value; - let calPeriod = Math.pow(2, Math.ceil(Math.log2(period))); - calPeriod = Math.min(Math.max(calPeriod, 4), 1 << 16); - const phase = current % calPeriod; - const quantizeFactor = Math.max(calPeriod >> 12, 1); - const quantizedPhase = phase / quantizeFactor * quantizeFactor; - - return [new U64(calPeriod), new U64(quantizedPhase)]; + return MortalEra.decodeMortalObject(value); } else if (!value) { return [new U64(), new U64()]; } @@ -78,6 +71,17 @@ export class MortalEra extends Tuple { throw new Error('Invalid data passed to Mortal era'); } + private static decodeMortalObject (value: MortalMethod): MortalEraValue { + const { current, period } = value; + let calPeriod = Math.pow(2, Math.ceil(Math.log2(period))); + calPeriod = Math.min(Math.max(calPeriod, 4), 1 << 16); + const phase = current % calPeriod; + const quantizeFactor = Math.max(calPeriod >> 12, 1); + const quantizedPhase = phase / quantizeFactor * quantizeFactor; + + return [new U64(calPeriod), new U64(quantizedPhase)]; + } + private static decodeMortalU8a (value: Uint8Array): MortalEraValue { if (value.length === 0) { return [new U64(), new U64()];