diff --git a/packages/api-format/package.json b/packages/api-format/package.json index d268506956c4..c066bbce882e 100644 --- a/packages/api-format/package.json +++ b/packages/api-format/package.json @@ -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" } diff --git a/packages/api-format/src/input.js b/packages/api-format/src/input.js new file mode 100644 index 000000000000..f991915d25cb --- /dev/null +++ b/packages/api-format/src/input.js @@ -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, values: Array): Array { + const types = inputs.map(({ type }) => type); + + return util.formatArray(formatters, types, values); +}; diff --git a/packages/api-format/src/input/index.spec.js b/packages/api-format/src/input.spec.js similarity index 82% rename from packages/api-format/src/input/index.spec.js rename to packages/api-format/src/input.spec.js index e7af4f1d4709..902f112f6701 100644 --- a/packages/api-format/src/input/index.spec.js +++ b/packages/api-format/src/input.spec.js @@ -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' } diff --git a/packages/api-format/src/input/address.js b/packages/api-format/src/input/address.js deleted file mode 100644 index 9809ffc21266..000000000000 --- a/packages/api-format/src/input/address.js +++ /dev/null @@ -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); -}; diff --git a/packages/api-format/src/input/address.spec.js b/packages/api-format/src/input/address.spec.js deleted file mode 100644 index 700459777b6f..000000000000 --- a/packages/api-format/src/input/address.spec.js +++ /dev/null @@ -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'); - }); - }); -}); diff --git a/packages/api-format/src/input/hex.js b/packages/api-format/src/input/hex.js deleted file mode 100644 index 4d72e946b8a1..000000000000 --- a/packages/api-format/src/input/hex.js +++ /dev/null @@ -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 -}; diff --git a/packages/api-format/src/input/hex.spec.js b/packages/api-format/src/input/hex.spec.js deleted file mode 100644 index cae7cc973580..000000000000 --- a/packages/api-format/src/input/hex.spec.js +++ /dev/null @@ -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,}$/); - }); - }); -}); diff --git a/packages/api-format/src/input/index.js b/packages/api-format/src/input/index.js deleted file mode 100644 index 8fcb6f52c11d..000000000000 --- a/packages/api-format/src/input/index.js +++ /dev/null @@ -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, values: Array): Array { - const types = inputs.map(({ type }) => type); - - return util.formatArray(formatters, types, values); -}; diff --git a/packages/api-format/src/output/index.js b/packages/api-format/src/output.js similarity index 50% rename from packages/api-format/src/output/index.js rename to packages/api-format/src/output.js index a0322c7a6e1d..4c9d3cf45df2 100644 --- a/packages/api-format/src/output/index.js +++ b/packages/api-format/src/output.js @@ -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); }; diff --git a/packages/api-format/src/output.spec.js b/packages/api-format/src/output.spec.js new file mode 100644 index 000000000000..152318eae3aa --- /dev/null +++ b/packages/api-format/src/output.spec.js @@ -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]) + ] + } + }) + ); + }); + }); +}); diff --git a/packages/api-format/src/output/index.spec.js b/packages/api-format/src/output/index.spec.js deleted file mode 100644 index e73c1fa7e925..000000000000 --- a/packages/api-format/src/output/index.spec.js +++ /dev/null @@ -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'); - }); - }); -}); diff --git a/packages/api-format/src/util.js b/packages/api-format/src/util.js index 0aaea0e20694..218b2a2a8b82 100644 --- a/packages/api-format/src/util.js +++ b/packages/api-format/src/util.js @@ -45,9 +45,7 @@ function format (formatters: { [any]: FormatterFunction }, type: string, value: function formatArray (formatters: { [any]: FormatterFunction }, types: Array, values: Array): Array { return types.map((type, index) => { - const value = values[index]; - - return format(formatters, type, value); + return format(formatters, type, values[index]); }); } diff --git a/yarn.lock b/yarn.lock index f2c10badce29..3476703c2ccf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"