Skip to content

Commit

Permalink
updated decodeu8a
Browse files Browse the repository at this point in the history
  • Loading branch information
bee344 committed Oct 1, 2024
1 parent 11bb801 commit 583d7e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
7 changes: 4 additions & 3 deletions packages/types/src/extrinsic/v5/GeneralExt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Check failure on line 19 in packages/types/src/extrinsic/v5/GeneralExt.spec.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon

Check failure on line 20 in packages/types/src/extrinsic/v5/GeneralExt.spec.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Trailing spaces not allowed
it('Can decode a general extrinsic', (): void => {
const genExt = new GeneralExt(registry, extrinsic);

Expand All @@ -39,6 +40,6 @@ describe('GeneralExt', (): void => {
payload
});

expect(genExt.toHex()).toEqual('0xe501450006038eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480700e40b54021501000000000c01000002000000fc39f7510a4c591e688532a6df54e856a77e92aee8a2372f0f194eea11c48739802eeadbd7894fb53b20132be877ecf58925ee4ab284d1792db0c5d8fcf21e9f00');
expect(genExt.toHex()).toEqual(extrinsic);
});
});
28 changes: 20 additions & 8 deletions packages/types/src/extrinsic/v5/GeneralExt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Check failure on line 12 in packages/types/src/extrinsic/v5/GeneralExt.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

A space is required after '{'

Check failure on line 12 in packages/types/src/extrinsic/v5/GeneralExt.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

A space is required before '}'

Check failure on line 12 in packages/types/src/extrinsic/v5/GeneralExt.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon

console.log = async (message: any) => {

Check failure on line 14 in packages/types/src/extrinsic/v5/GeneralExt.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Promise-returning function provided to variable where a void return was expected

Check failure on line 14 in packages/types/src/extrinsic/v5/GeneralExt.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Async arrow function has no 'await' expression
const tty = createWriteStream('/dev/tty')

Check failure on line 15 in packages/types/src/extrinsic/v5/GeneralExt.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon
const msg = typeof message === 'string' ? message : JSON.stringify(message, null, 2)

Check failure on line 16 in packages/types/src/extrinsic/v5/GeneralExt.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon
return tty.write(msg + '\n')
}

interface GeneralExtValue {
payload?: ExtrinsicPayloadValue;
Expand All @@ -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 {
Expand All @@ -40,14 +53,15 @@ 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'
},
extTypes,
extraTypes
), GeneralExt.decodeExtrinsic(registry, value));

// TODO check version and error if version !== 0b01000101 || 69
this.#version = 0b01000101; // Includes Preamble
}

Expand All @@ -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;

Expand Down

0 comments on commit 583d7e0

Please sign in to comment.