Use formatters from primitives-json (#38)

This commit is contained in:
Jaco Greeff
2017-12-08 09:20:03 +01:00
committed by GitHub
parent a08273f764
commit 5bbb46b3a0
13 changed files with 97 additions and 157 deletions
+1
View File
@@ -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"
}
+26
View File
@@ -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' }
-10
View File
@@ -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');
});
});
});
-33
View File
@@ -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
};
-53
View File
@@ -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,}$/);
});
});
});
-24
View File
@@ -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);
};
+40
View File
@@ -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');
});
});
});
+1 -3
View File
@@ -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]);
});
}