From 583d7e020543185c35aaf1f3e407000256f6b0bf Mon Sep 17 00:00:00 2001 From: bee344 Date: Tue, 1 Oct 2024 19:40:30 -0300 Subject: [PATCH] updated decodeu8a --- .../types/src/extrinsic/v5/GeneralExt.spec.ts | 7 +++-- packages/types/src/extrinsic/v5/GeneralExt.ts | 28 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/types/src/extrinsic/v5/GeneralExt.spec.ts b/packages/types/src/extrinsic/v5/GeneralExt.spec.ts index 1123b515740..94f4def12e7 100644 --- a/packages/types/src/extrinsic/v5/GeneralExt.spec.ts +++ b/packages/types/src/extrinsic/v5/GeneralExt.spec.ts @@ -15,8 +15,9 @@ const metadata = new Metadata(registry, rpcMetadata); registry.setMetadata(metadata); describe('GeneralExt', (): void => { - const extrinsic = new Uint8Array([181, 1, 69, 0, 0, 1, 168, 233, 106, 49, 105, 38, 163, 218, 171, 202, 93, 136, 17, 15, 0, 153, 39, 227, 172, 193, 76, 18, 216, 240, 169, 102, 211, 43, 191, 124, 81, 18, 51, 177, 255, 243, 61, 86, 88, 188, 237, 234, 116, 68, 15, 154, 78, 127, 45, 238, 86, 104, 223, 203, 13, 26, 41, 115, 42, 107, 130, 67, 198, 131, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 92, 0, 0, 2, 0, 42, 0, 0, 0]); - + // const extrinsic = new Uint8Array([181, 1, 69, 0, 0, 1, 168, 233, 106, 49, 105, 38, 163, 218, 171, 202, 93, 136, 17, 15, 0, 153, 39, 227, 172, 193, 76, 18, 216, 240, 169, 102, 211, 43, 191, 124, 81, 18, 51, 177, 255, 243, 61, 86, 88, 188, 237, 234, 116, 68, 15, 154, 78, 127, 45, 238, 86, 104, 223, 203, 13, 26, 41, 115, 42, 107, 130, 67, 198, 131, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 92, 0, 0, 2, 0, 42, 0, 0, 0]); + const extrinsic = '0xe501450006038eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480700e40b54021501000000000c01000002000000fc39f7510a4c591e688532a6df54e856a77e92aee8a2372f0f194eea11c48739802eeadbd7894fb53b20132be877ecf58925ee4ab284d1792db0c5d8fcf21e9f00' + it('Can decode a general extrinsic', (): void => { const genExt = new GeneralExt(registry, extrinsic); @@ -39,6 +40,6 @@ describe('GeneralExt', (): void => { payload }); - expect(genExt.toHex()).toEqual('0xe501450006038eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480700e40b54021501000000000c01000002000000fc39f7510a4c591e688532a6df54e856a77e92aee8a2372f0f194eea11c48739802eeadbd7894fb53b20132be877ecf58925ee4ab284d1792db0c5d8fcf21e9f00'); + expect(genExt.toHex()).toEqual(extrinsic); }); }); diff --git a/packages/types/src/extrinsic/v5/GeneralExt.ts b/packages/types/src/extrinsic/v5/GeneralExt.ts index 7dccd6dd086..d6d39a05730 100644 --- a/packages/types/src/extrinsic/v5/GeneralExt.ts +++ b/packages/types/src/extrinsic/v5/GeneralExt.ts @@ -6,9 +6,16 @@ import type { ExtrinsicPayloadValue, ICompact, INumber, IOption, Registry } from import type { HexString } from '@polkadot/util/types'; import { Struct } from '@polkadot/types-codec'; -import { compactAddLength, compactFromU8a, hexToU8a, isHex, isObject, isU8a, objectSpread, u8aConcat, u8aToHex } from '@polkadot/util'; +import { compactAddLength, compactFromU8a, isHex, isObject, isU8a, objectSpread, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util'; import { EMPTY_U8A } from '../constants.js'; +import {createWriteStream} from 'fs' + +console.log = async (message: any) => { + const tty = createWriteStream('/dev/tty') + const msg = typeof message === 'string' ? message : JSON.stringify(message, null, 2) + return tty.write(msg + '\n') +} interface GeneralExtValue { payload?: ExtrinsicPayloadValue; @@ -17,13 +24,19 @@ interface GeneralExtValue { function decodeU8a (u8a: Uint8Array) { if (!u8a.length) { - return {}; + return new Uint8Array(); } const [offset, length] = compactFromU8a(u8a); const total = offset + length.toNumber(); - return {}; + if (total > u8a.length) { + throw new Error(`Extrinsic: length less than remainder, expected at least ${total}, found ${u8a.length}`); + } + + const data = u8a.subarray(offset, total); + + return data.subarray(1); } export class GeneralExt extends Struct { @@ -40,7 +53,7 @@ export class GeneralExt extends Struct { super(registry, objectSpread( { // eslint-disable-next-line sort-keys - transactoinExtensionVersion: 'u8', + transactionExtensionVersion: 'u8', // eslint-disable-next-line sort-keys method: 'Call' }, @@ -48,6 +61,7 @@ export class GeneralExt extends Struct { extraTypes ), GeneralExt.decodeExtrinsic(registry, value)); + // TODO check version and error if version !== 0b01000101 || 69 this.#version = 0b01000101; // Includes Preamble } @@ -57,10 +71,8 @@ export class GeneralExt extends Struct { return EMPTY_U8A; } else if (value instanceof GeneralExt) { return value; - } else if (isU8a(value)) { - return decodeU8a(value); - } else if (isHex(value)) { - return decodeU8a(hexToU8a(value)); + } else if (isU8a(value) || Array.isArray(value) || isHex(value)) { + return decodeU8a(u8aToU8a(value)); } else if (isObject(value)) { const { payload, transactionExtensionVersion } = value;