Skip to content

Commit

Permalink
fix(bug): toTxMethod sanitizing (#183)
Browse files Browse the repository at this point in the history
* fix number check for toTxMethod

* toTxMethod test

* add kusama registry for kusama v9160

* fix naming

* fix inline comment grumbles

* fix expected response in tests

* fix and refactor santization

* add inline comments

* fix imports, and small grumble

* Update packages/txwrapper-core/src/core/method/toTxMethod.ts

Co-authored-by: David <dvdplm@gmail.com>

Co-authored-by: David <dvdplm@gmail.com>
  • Loading branch information
TarikGul and dvdplm authored Feb 22, 2022
1 parent 1a42a58 commit c7f8e12
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 12 deletions.
36 changes: 36 additions & 0 deletions packages/txwrapper-core/src/core/method/toTxMethod.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { hexToU8a } from '@polkadot/util';

import { KUSAMA_9160_TEST_OPTIONS } from '../../test-helpers';
import { toTxMethod } from './toTxMethod';

const { registry } = KUSAMA_9160_TEST_OPTIONS;

describe('toTxMethod', () => {
// transferKeepAlive signedTx constructed using kusama v9160 metadata
const signedTx =
'0x4d028400d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d014c39c61d2526afa7adf0a5683a97cefe6cd1cbdc4dd220eaa34ee20c88051057ad726f28371bbe9dcede5213ed29ef5817da15090db7d1886698836847098887550100000403008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a4813f6ffffffffff3f01';
const extrinsic = registry.createType('Extrinsic', hexToU8a(signedTx), {
isSigned: true,
});
const txMethodCall = registry.createType('Call', extrinsic.method);

it('decode an extrinsics method', () => {
const expectedResponse = {
args: {
dest: {
id: 'FoQJpPyadYccjavVdTWxpxU7rUEaYhfLCPwXgkfD6Zat9QP',
},
value: '90071992547409910',
},
name: 'transferKeepAlive',
pallet: 'balances',
};

const txMethod = toTxMethod(
KUSAMA_9160_TEST_OPTIONS.registry,
txMethodCall
);

expect(txMethod).toStrictEqual(expectedResponse);
});
});
23 changes: 11 additions & 12 deletions packages/txwrapper-core/src/core/method/toTxMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { createTypeUnsafe, TypeRegistry } from '@polkadot/types';
import { Compact } from '@polkadot/types';
import { Call } from '@polkadot/types/interfaces';
import { Codec } from '@polkadot/types/types';
import { AbstractInt } from '@polkadot/types-codec/abstract/AbstractInt';
import { BN, stringCamelCase } from '@polkadot/util';
import { AbstractInt, UInt } from '@polkadot/types-codec';
import { stringCamelCase } from '@polkadot/util';

import { Args, TxMethod } from '../../types/method';

Expand Down Expand Up @@ -35,16 +35,15 @@ export function toTxMethod(registry: TypeRegistry, method: Call): TxMethod {
codec = codec.unwrap() as Codec;
}

// Forcibly serialize all integers to strings
let jsonArg =
codec instanceof AbstractInt
? codec.toString(RADIX_PARAM)
: (codec as BN).toJSON();

// Sanity check to check that `jsonArg` is a number, and if it is
// to change it to a string
if (!Number.isNaN(jsonArg)) {
jsonArg = (jsonArg as unknown as number).toString(RADIX_PARAM);
/**
* Forcibly serialize the codec value to an integer. If it is not a number
* then it will return the codec type as its `toJSON` value.
*/
let jsonArg;
if (codec instanceof UInt || codec instanceof AbstractInt) {
jsonArg = codec.toString(RADIX_PARAM);
} else {
jsonArg = (codec as Codec).toJSON();
}

accumulator[stringCamelCase(key)] = jsonArg;
Expand Down
10 changes: 10 additions & 0 deletions packages/txwrapper-core/src/test-helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { getRegistryKusama } from './getRegistryKusama';
import { getRegistryPolkadot } from './getRegistryPolkadot';
import { kusamaV9160MetadataHex } from './metadata/kusamaV9160MetadataHex';
import { polkadotV9122MetadataHex } from './metadata/polkadotV9122MetadataHex';
import { metadataRpc } from './staticV3-1-1';
import { metadataRpcV29 } from './staticV4-3-1';
Expand Down Expand Up @@ -73,6 +75,14 @@ export const POLKADOT_9122_TEST_OPTIONS_CALLS_ONLY = {
asCallsOnlyArg: true,
};

/**
* Test options for runtime v9160 Kusama
*/
export const KUSAMA_9160_TEST_OPTIONS = {
metadataRpc: kusamaV9160MetadataHex,
registry: getRegistryKusama(9160, kusamaV9160MetadataHex),
};

/**
* Dummy arguments for all methods we're testing.
*/
Expand Down
30 changes: 30 additions & 0 deletions packages/txwrapper-core/src/test-helpers/getRegistryKusama.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { TypeRegistry } from '@polkadot/types';
import { getSpecTypes } from '@polkadot/types-known';

import { getRegistryBase } from '../core';

/**
* Get the Kusama type registry for a given spec version for testing purposes
*
* @param specVersion
* @param metadataRpc
*/
export function getRegistryKusama(
specVersion: number,
metadataRpc: `0x${string}`
): TypeRegistry {
return getRegistryBase({
chainProperties: {
ss58Format: 2,
tokenDecimals: 12,
tokenSymbol: 'KSM',
},
specTypes: getSpecTypes(
new TypeRegistry(),
'Kusama',
'kusama',
specVersion
),
metadataRpc,
});
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getRegistryKusama } from '../getRegistryKusama';
import { kusamaV9160MetadataHex } from '../metadata/kusamaV9160MetadataHex';

/**
* Polkadot v9122 TypeRegistry
*/
export const kusamaRegistryV9160 = getRegistryKusama(
9160,
kusamaV9160MetadataHex
);

0 comments on commit c7f8e12

Please sign in to comment.