Use primitives-json encode & decode (#41)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
module.exports = function format (value: any): any {
|
||||
module.exports = function echo (value: any): any {
|
||||
return value;
|
||||
};
|
||||
@@ -1,13 +1,13 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const format = require('./noop');
|
||||
const echo = require('./echo');
|
||||
|
||||
describe('noop', () => {
|
||||
describe('echo', () => {
|
||||
it('returns input value as output value', () => {
|
||||
const input = { 'some': 'object' };
|
||||
|
||||
expect(
|
||||
format(input)
|
||||
echo(input)
|
||||
).toEqual(input);
|
||||
});
|
||||
});
|
||||
@@ -4,19 +4,20 @@
|
||||
import type { FormatInputType, InterfaceInputType } from '@polkadot/api-jsonrpc/types';
|
||||
import type { FormatterFunction } from './types';
|
||||
|
||||
const accountIdToJson = require('@polkadot/primitives-json/accountId/toJson');
|
||||
const h256ToJson = require('@polkadot/primitives-json/h256/toJson');
|
||||
const hashToJson = require('@polkadot/primitives-json/hash/toJson');
|
||||
const accountIdEncode = require('@polkadot/primitives-json/accountId/encode');
|
||||
const h256Encode = require('@polkadot/primitives-json/h256/encode');
|
||||
const hashEncode = require('@polkadot/primitives-json/hash/encode');
|
||||
const headerHashEncode = require('@polkadot/primitives-json/headerHash/encode');
|
||||
|
||||
const formatNoop = require('./noop');
|
||||
const echo = require('./echo');
|
||||
const util = require('./util');
|
||||
|
||||
const formatters: { [FormatInputType]: FormatterFunction } = {
|
||||
'Address': accountIdToJson,
|
||||
'CallData': hashToJson,
|
||||
'H256': h256ToJson,
|
||||
'HeaderHash': h256ToJson,
|
||||
'String': formatNoop
|
||||
'Address': accountIdEncode,
|
||||
'CallData': hashEncode,
|
||||
'H256': h256Encode,
|
||||
'HeaderHash': headerHashEncode,
|
||||
'String': echo
|
||||
};
|
||||
|
||||
module.exports = function formatInputs (inputs: Array<InterfaceInputType>, values: Array<any>): Array<any> {
|
||||
|
||||
@@ -3,20 +3,18 @@
|
||||
const { formatInputs } = require('./index');
|
||||
|
||||
describe('formatInputs', () => {
|
||||
describe('format', () => {
|
||||
it('formats each value in an array', () => {
|
||||
expect(
|
||||
formatInputs(
|
||||
[
|
||||
{ name: 'foo', type: 'Address' },
|
||||
{ name: 'bar', type: 'H256' }
|
||||
],
|
||||
['0x1234', '0xabcd']
|
||||
)
|
||||
).toEqual([
|
||||
'0x0000000000000000000000000000000000001234',
|
||||
'0x000000000000000000000000000000000000000000000000000000000000abcd'
|
||||
]);
|
||||
});
|
||||
it('formats each value in an array', () => {
|
||||
expect(
|
||||
formatInputs(
|
||||
[
|
||||
{ name: 'foo', type: 'Address' },
|
||||
{ name: 'bar', type: 'H256' }
|
||||
],
|
||||
['0x1234', '0xabcd']
|
||||
)
|
||||
).toEqual([
|
||||
'0x0000000000000000000000000000000000001234',
|
||||
'0x000000000000000000000000000000000000000000000000000000000000abcd'
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,15 +4,15 @@
|
||||
import type { FormatOutputType, InterfaceOutputType } from '@polkadot/api-jsonrpc/types';
|
||||
import type { FormatterFunction } from './types';
|
||||
|
||||
const headerFromJson = require('@polkadot/primitives-json/header/fromJson');
|
||||
const headerDecode = require('@polkadot/primitives-json/header/decode');
|
||||
|
||||
const formatNoop = require('./noop');
|
||||
const echo = require('./echo');
|
||||
const util = require('./util');
|
||||
|
||||
const formatters: { [FormatOutputType]: FormatterFunction } = {
|
||||
'Header': headerFromJson,
|
||||
'OutData': formatNoop,
|
||||
'StorageData': formatNoop
|
||||
'Header': headerDecode,
|
||||
'OutData': echo,
|
||||
'StorageData': echo
|
||||
};
|
||||
|
||||
module.exports = function formatOutput (output: InterfaceOutputType, value: any): any {
|
||||
|
||||
@@ -5,36 +5,34 @@ const BN = require('bn.js');
|
||||
const { formatOutput } = require('./index');
|
||||
|
||||
describe('formatOutput', () => {
|
||||
describe('format', () => {
|
||||
it('formats the value', () => {
|
||||
expect(
|
||||
JSON.stringify(
|
||||
formatOutput({ type: 'Header' }, {
|
||||
parentHash: '0x1234',
|
||||
number: '0x1234',
|
||||
stateRoot: '0x5678',
|
||||
transactionRoot: '0xabcd',
|
||||
digest: {
|
||||
parachainActivityBitfield: '0x1234',
|
||||
logs: ['0x5678', '0x789a']
|
||||
}
|
||||
})
|
||||
)
|
||||
).toEqual(
|
||||
JSON.stringify({
|
||||
it('formats the value', () => {
|
||||
expect(
|
||||
JSON.stringify(
|
||||
formatOutput({ type: 'Header' }, {
|
||||
parentHash: '0x1234',
|
||||
number: new BN(0x1234),
|
||||
number: '0x1234',
|
||||
stateRoot: '0x5678',
|
||||
transactionRoot: '0xabcd',
|
||||
digest: {
|
||||
parachainActivityBitfield: Buffer.from([0x12, 0x34]),
|
||||
logs: [
|
||||
Buffer.from([0x56, 0x78]),
|
||||
Buffer.from([0x78, 0x9a])
|
||||
]
|
||||
parachainActivityBitfield: '0x1234',
|
||||
logs: ['0x5678', '0x789a']
|
||||
}
|
||||
})
|
||||
);
|
||||
});
|
||||
)
|
||||
).toEqual(
|
||||
JSON.stringify({
|
||||
parentHash: '0x0000000000000000000000000000000000000000000000000000000000001234',
|
||||
number: new BN(0x1234),
|
||||
stateRoot: '0x0000000000000000000000000000000000000000000000000000000000005678',
|
||||
transactionRoot: '0x000000000000000000000000000000000000000000000000000000000000abcd',
|
||||
digest: {
|
||||
parachainActivityBitfield: Buffer.from([0x12, 0x34]),
|
||||
logs: [
|
||||
Buffer.from([0x56, 0x78]),
|
||||
Buffer.from([0x78, 0x9a])
|
||||
]
|
||||
}
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { FormatterFunction } from './types';
|
||||
|
||||
const assert = require('@polkadot/util/assert');
|
||||
const isUndefined = require('@polkadot/util/is/undefined');
|
||||
const formatNoop = require('./noop');
|
||||
const echo = require('./echo');
|
||||
|
||||
const arrayTypeRegex = /\[\]$/;
|
||||
|
||||
@@ -13,9 +13,9 @@ function formatSingleType (formatters: { [any]: FormatterFunction }, type: strin
|
||||
const formatter: FormatterFunction = formatters[type];
|
||||
|
||||
if (isUndefined(formatter)) {
|
||||
console.warn(`Unable to find default formatter for '${type}', falling back to noop`);
|
||||
console.warn(`Unable to find default formatter for '${type}', falling back to echo`);
|
||||
|
||||
return formatNoop(value);
|
||||
return echo(value);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { format, formatArray } = require('./util');
|
||||
const formatNoop = require('./noop');
|
||||
const echo = require('./echo');
|
||||
|
||||
describe('util', () => {
|
||||
let formatters;
|
||||
let noopSpy;
|
||||
let echoSpy;
|
||||
let warnSpy;
|
||||
|
||||
beforeEach(() => {
|
||||
noopSpy = jest.fn(formatNoop);
|
||||
echoSpy = jest.fn(echo);
|
||||
warnSpy = jest.spyOn(console, 'warn');
|
||||
|
||||
formatters = {
|
||||
'Address': noopSpy,
|
||||
'Address[]': noopSpy,
|
||||
'Address': echoSpy,
|
||||
'Address[]': echoSpy,
|
||||
'Exception': () => {
|
||||
throw new Error('something went wrong');
|
||||
}
|
||||
@@ -22,7 +22,7 @@ describe('util', () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
noopSpy.mockRestore();
|
||||
echoSpy.mockRestore();
|
||||
warnSpy.mockRestore();
|
||||
});
|
||||
|
||||
@@ -35,7 +35,7 @@ describe('util', () => {
|
||||
|
||||
it('logs a warning with unknown types', () => {
|
||||
format(formatters, 'Unknown', 'test');
|
||||
expect(warnSpy).toHaveBeenCalledWith("Unable to find default formatter for 'Unknown', falling back to noop");
|
||||
expect(warnSpy).toHaveBeenCalledWith("Unable to find default formatter for 'Unknown', falling back to echo");
|
||||
});
|
||||
|
||||
it('wraps exceptions with the type', () => {
|
||||
@@ -48,7 +48,7 @@ describe('util', () => {
|
||||
it('formats known types using the supplied formatter', () => {
|
||||
format(formatters, 'Address', '0xaddress');
|
||||
|
||||
expect(noopSpy).toHaveBeenCalledWith('0xaddress');
|
||||
expect(echoSpy).toHaveBeenCalledWith('0xaddress');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -62,8 +62,8 @@ describe('util', () => {
|
||||
it('formats using the primitive type', () => {
|
||||
format(formatters, 'Address[]', ['0x123', '0x234']);
|
||||
|
||||
expect(noopSpy).toHaveBeenCalledWith('0x123');
|
||||
expect(noopSpy).toHaveBeenCalledWith('0x234');
|
||||
expect(echoSpy).toHaveBeenCalledWith('0x123');
|
||||
expect(echoSpy).toHaveBeenCalledWith('0x234');
|
||||
});
|
||||
|
||||
it('returns formatted values as arrays', () => {
|
||||
|
||||
Reference in New Issue
Block a user