Use formatters from primitives-json (#38)
This commit is contained in:
@@ -33,6 +33,7 @@
|
||||
"@polkadot/api-jsonrpc": "^0.6.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/primitives-json": "^0.1.5",
|
||||
"@polkadot/util": "^0.6.7",
|
||||
"babel-runtime": "^6.26.0"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
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 formatNoop = require('./noop');
|
||||
const util = require('./util');
|
||||
|
||||
const formatters: { [FormatInputType]: FormatterFunction } = {
|
||||
'Address': accountIdToJson,
|
||||
'CallData': hashToJson,
|
||||
'H256': h256ToJson,
|
||||
'HeaderHash': h256ToJson,
|
||||
'String': formatNoop
|
||||
};
|
||||
|
||||
module.exports = function formatInputs (inputs: Array<InterfaceInputType>, values: Array<any>): Array<any> {
|
||||
const types = inputs.map(({ type }) => type);
|
||||
|
||||
return util.formatArray(formatters, types, values);
|
||||
};
|
||||
@@ -1,12 +1,12 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const format = require('./index');
|
||||
const { formatInputs } = require('./index');
|
||||
|
||||
describe('input', () => {
|
||||
describe('formatInputs', () => {
|
||||
describe('format', () => {
|
||||
it('formats each value in an array', () => {
|
||||
expect(
|
||||
format(
|
||||
formatInputs(
|
||||
[
|
||||
{ name: 'foo', type: 'Address' },
|
||||
{ name: 'bar', type: 'H256' }
|
||||
@@ -1,10 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const { formatH160 } = require('./hex');
|
||||
|
||||
// TODO: Currently the format assumes 160-bit values (like Ethereum)
|
||||
// this will probably change along the way as things get firmed up
|
||||
module.exports = function format (value: ?string): string {
|
||||
return formatH160(value);
|
||||
};
|
||||
@@ -1,13 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const format = require('./address');
|
||||
|
||||
describe('input/address', () => {
|
||||
describe('format', () => {
|
||||
it('pads to H160 value', () => {
|
||||
expect(
|
||||
format('0x1234567890')
|
||||
).toEqual('0x0000000000000000000000000000001234567890');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,33 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const { hexAddPrefix, hexHasPrefix, hexStripPrefix } = require('@polkadot/util/hex');
|
||||
|
||||
const H64_ZERO: string = '00000000000000000000000000000000';
|
||||
const H128_ZERO: string = `${H64_ZERO}${H64_ZERO}`;
|
||||
const H256_ZERO: string = `${H128_ZERO}${H128_ZERO}`;
|
||||
// const H512_ZERO: string = `${H256_ZERO}${H256_ZERO}`;
|
||||
|
||||
function leftHexPad (value: ?string, bitLength: number): string {
|
||||
const length = 2 * bitLength / 8;
|
||||
|
||||
if (hexHasPrefix(value)) {
|
||||
value = hexStripPrefix(value);
|
||||
}
|
||||
|
||||
return hexAddPrefix(`${H256_ZERO}${value || ''}`.slice(-length));
|
||||
}
|
||||
|
||||
function formatH160 (value: ?string): string {
|
||||
return leftHexPad(value, 160);
|
||||
}
|
||||
|
||||
function formatH256 (value: ?string): string {
|
||||
return leftHexPad(value, 256);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
leftHexPad,
|
||||
formatH160,
|
||||
formatH256
|
||||
};
|
||||
@@ -1,53 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const { leftHexPad, formatH160, formatH256 } = require('./hex');
|
||||
|
||||
describe('input/hex', () => {
|
||||
describe('leftHexPad', () => {
|
||||
it('padds to the required length', () => {
|
||||
expect(
|
||||
leftHexPad('0x123', 16)
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('padds to the required length (no prefix)', () => {
|
||||
expect(
|
||||
leftHexPad('123', 16)
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('pads null values correctly', () => {
|
||||
expect(
|
||||
leftHexPad(null, 16)
|
||||
).toEqual('0x0000');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatH160', () => {
|
||||
it('pads to 40 bytes', () => {
|
||||
expect(
|
||||
formatH160('0x1234567890')
|
||||
).toEqual('0x0000000000000000000000000000001234567890');
|
||||
});
|
||||
|
||||
it('pads null values correctly', () => {
|
||||
expect(
|
||||
formatH160(null)
|
||||
).toMatch(/^0x(00){20,}$/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatH256', () => {
|
||||
it('pads to 64 bytes', () => {
|
||||
expect(
|
||||
formatH256('0x1234567890')
|
||||
).toEqual('0x0000000000000000000000000000000000000000000000000000001234567890');
|
||||
});
|
||||
|
||||
it('pads null values correctly', () => {
|
||||
expect(
|
||||
formatH256(null)
|
||||
).toMatch(/^0x(00){32,}$/);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,24 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
import type { FormatInputType, InterfaceInputType } from '@polkadot/api-jsonrpc/types';
|
||||
import type { FormatterFunction } from '../types';
|
||||
|
||||
const formatAddress = require('./address');
|
||||
const { formatH256 } = require('./hex');
|
||||
const formatNoop = require('../noop');
|
||||
const util = require('../util');
|
||||
|
||||
const formatters: { [FormatInputType]: FormatterFunction } = {
|
||||
'Address': formatAddress,
|
||||
'CallData': formatNoop,
|
||||
'H256': formatH256,
|
||||
'HeaderHash': formatH256,
|
||||
'String': formatNoop
|
||||
};
|
||||
|
||||
module.exports = function format (inputs: Array<InterfaceInputType>, values: Array<any>): Array<any> {
|
||||
const types = inputs.map(({ type }) => type);
|
||||
|
||||
return util.formatArray(formatters, types, values);
|
||||
};
|
||||
@@ -2,17 +2,19 @@
|
||||
// @flow
|
||||
|
||||
import type { FormatOutputType, InterfaceOutputType } from '@polkadot/api-jsonrpc/types';
|
||||
import type { FormatterFunction } from '../types';
|
||||
import type { FormatterFunction } from './types';
|
||||
|
||||
const formatNoop = require('../noop');
|
||||
const util = require('../util');
|
||||
const headerFromJson = require('@polkadot/primitives-json/header/fromJson');
|
||||
|
||||
const formatNoop = require('./noop');
|
||||
const util = require('./util');
|
||||
|
||||
const formatters: { [FormatOutputType]: FormatterFunction } = {
|
||||
'Header': formatNoop,
|
||||
'Header': headerFromJson,
|
||||
'OutData': formatNoop,
|
||||
'StorageData': formatNoop
|
||||
};
|
||||
|
||||
module.exports = function format (output: InterfaceOutputType, value: any): any {
|
||||
module.exports = function formatOutput (output: InterfaceOutputType, value: any): any {
|
||||
return util.format(formatters, output.type, value);
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
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({
|
||||
parentHash: '0x1234',
|
||||
number: new BN(0x1234),
|
||||
stateRoot: '0x5678',
|
||||
transactionRoot: '0xabcd',
|
||||
digest: {
|
||||
parachainActivityBitfield: Buffer.from([0x12, 0x34]),
|
||||
logs: [
|
||||
Buffer.from([0x56, 0x78]),
|
||||
Buffer.from([0x78, 0x9a])
|
||||
]
|
||||
}
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,13 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const format = require('./index');
|
||||
|
||||
describe('output', () => {
|
||||
describe('format', () => {
|
||||
it('formats the value', () => {
|
||||
expect(
|
||||
format('Header', 'test')
|
||||
).toEqual('test');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -45,9 +45,7 @@ function format (formatters: { [any]: FormatterFunction }, type: string, value:
|
||||
|
||||
function formatArray (formatters: { [any]: FormatterFunction }, types: Array<string>, values: Array<any>): Array<any> {
|
||||
return types.map((type, index) => {
|
||||
const value = values[index];
|
||||
|
||||
return format(formatters, type, value);
|
||||
return format(formatters, type, values[index]);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -91,6 +91,17 @@
|
||||
stylelint "^8.2.0"
|
||||
stylelint-config-standard "^18.0.0"
|
||||
|
||||
"@polkadot/primitives-json@^0.1.5":
|
||||
version "0.1.5"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/primitives-json/-/primitives-json-0.1.5.tgz#bd3f1dc2532190ebf78ba9d89f64b518d105eae8"
|
||||
dependencies:
|
||||
"@polkadot/primitives" "^0.1.5"
|
||||
"@polkadot/util" "^0.6.8"
|
||||
|
||||
"@polkadot/primitives@^0.1.5":
|
||||
version "0.1.5"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/primitives/-/primitives-0.1.5.tgz#6eb2fbccb06bda8bd774d205528d9b3a1d3e0b21"
|
||||
|
||||
"@polkadot/util@^0.6.7":
|
||||
version "0.6.7"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.6.7.tgz#eb9a6bd3717b69c962f6fb70830ebc60380c333f"
|
||||
@@ -99,6 +110,14 @@
|
||||
bn.js "^4.11.8"
|
||||
keccak "^1.3.0"
|
||||
|
||||
"@polkadot/util@^0.6.8":
|
||||
version "0.6.10"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.6.10.tgz#09417e607ec64a404ae63fa64f8f008d91c79856"
|
||||
dependencies:
|
||||
babel-runtime "^6.26.0"
|
||||
bn.js "^4.11.8"
|
||||
keccak "^1.3.0"
|
||||
|
||||
JSONStream@^1.0.4:
|
||||
version "1.3.1"
|
||||
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.1.tgz#707f761e01dae9e16f1bcf93703b78c70966579a"
|
||||
|
||||
Reference in New Issue
Block a user