Skip to content

Commit

Permalink
Fix decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed Oct 1, 2024
1 parent 583d7e0 commit 24fa6b0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
19 changes: 12 additions & 7 deletions packages/types/src/extrinsic/v5/GeneralExt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@ 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 = '0xe501450006038eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480700e40b54021501000000000c01000002000000fc39f7510a4c591e688532a6df54e856a77e92aee8a2372f0f194eea11c48739802eeadbd7894fb53b20132be877ecf58925ee4ab284d1792db0c5d8fcf21e9f00'

const extrinsic = '0xe90145006500000000000c01000002000000fc39f7510a4c591e688532a6df54e856a77e92aee8a2372f0f194eea11c487393d5c33fd9c2370dfcb4941c7fe85fc19c31e5c9b0bd5ecb74d3923947c9c5ccf00060000d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d0700e40b5402';

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

expect(genExt.version).toEqual(69);
expect(genExt.transactionExtensionVersion).toEqual(0);
expect(genExt.transactionExtensionVersion.toNumber()).toEqual(0);
expect(genExt.method.toHuman()).toEqual({ args: { dest: { Id: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY' }, value: '10,000,000,000' }, method: 'transferAllowDeath', section: 'balances' });
expect(genExt.era.toHuman()).toEqual({ MortalEra: { period: '64', phase: '6' } });
expect(genExt.tip.toNumber()).toEqual(0);
expect(genExt.mode.toNumber()).toEqual(0);
expect(genExt.assetId.toHuman()).toEqual(null);
expect(genExt.nonce.toNumber()).toEqual(0);
});

it('Can encode a general extrinsic', (): void => {
const payload = {
blockHash: '0x802eeadbd7894fb53b20132be877ecf58925ee4ab284d1792db0c5d8fcf21e9f',
era: '0x1501',
blockHash: '0x3d5c33fd9c2370dfcb4941c7fe85fc19c31e5c9b0bd5ecb74d3923947c9c5ccf',
era: '0x6500',
genesisHash: '0xfc39f7510a4c591e688532a6df54e856a77e92aee8a2372f0f194eea11c48739',
method: '0x0603008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480700e40b5402',
method: '0x060000d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d0700e40b5402',
nonce: '0x00000000',
specVersion: '0x0000010c',
tip: '0x00000000000000000000000000000000',
Expand Down
24 changes: 12 additions & 12 deletions packages/types/src/extrinsic/v5/GeneralExt.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
// Copyright 2017-2024 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { ExtrinsicEra, Hash, MultiLocation } from '@polkadot/types/interfaces';
import type { Call, ExtrinsicEra, Hash, MultiLocation } from '@polkadot/types/interfaces';
import type { ExtrinsicPayloadValue, ICompact, INumber, IOption, Registry } from '@polkadot/types/types';
import type { HexString } from '@polkadot/util/types';

import { Struct } from '@polkadot/types-codec';
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;
Expand Down Expand Up @@ -53,12 +46,15 @@ export class GeneralExt extends Struct {
super(registry, objectSpread(
{
// eslint-disable-next-line sort-keys
transactionExtensionVersion: 'u8',
transactionExtensionVersion: 'u8'
// eslint-disable-next-line sort-keys
method: 'Call'

},
extTypes,
extraTypes
extraTypes,
{
method: 'Call'
}
), GeneralExt.decodeExtrinsic(registry, value));

// TODO check version and error if version !== 0b01000101 || 69
Expand Down Expand Up @@ -117,6 +113,10 @@ export class GeneralExt extends Struct {
return this.getT('transactionExtensionVersion');
}

public get method (): Call {
return this.getT('method');
}

public get version () {
return this.#version;
}
Expand All @@ -136,6 +136,6 @@ export class GeneralExt extends Struct {
}

public encode () {
return u8aConcat(new Uint8Array([this.version]), super.toU8a({ method: true }));
return u8aConcat(new Uint8Array([this.version]), super.toU8a());
}
}

0 comments on commit 24fa6b0

Please sign in to comment.