Use primitives-json encode & decode (#41)

This commit is contained in:
Jaco Greeff
2017-12-14 22:02:07 +01:00
committed by GitHub
parent c6513b9147
commit 2cde699476
14 changed files with 103 additions and 103 deletions
+1 -1
View File
@@ -10,7 +10,7 @@
"test": "jest --coverage"
},
"devDependencies": {
"@polkadot/dev": "^0.13.3",
"@polkadot/dev": "^0.13.4",
"lerna": "^2.5.1"
}
}
+2 -2
View File
@@ -33,8 +33,8 @@
"@polkadot/api-jsonrpc": "^0.6.5"
},
"dependencies": {
"@polkadot/primitives-json": "^0.1.5",
"@polkadot/util": "^0.6.7",
"@polkadot/primitives-json": "^0.3.1",
"@polkadot/util": "^0.9.1",
"babel-runtime": "^6.26.0"
}
}
@@ -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);
});
});
+10 -9
View File
@@ -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> {
+13 -15
View File
@@ -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'
]);
});
});
+5 -5
View File
@@ -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 {
+23 -25
View File
@@ -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])
]
}
})
);
});
});
+3 -3
View File
@@ -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 {
+10 -10
View File
@@ -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', () => {
+1 -1
View File
@@ -34,7 +34,7 @@
"nock": "^9.1.0"
},
"dependencies": {
"@polkadot/util": "^0.6.7",
"@polkadot/util": "^0.9.1",
"babel-runtime": "^6.26.0",
"isomorphic-fetch": "^2.2.1",
"websocket": "^1.0.25"
+3 -1
View File
@@ -70,7 +70,9 @@ module.exports = class WsProvider extends JsonRpcCoder implements ProviderInterf
Object.keys(this._queued).forEach((id: string) => {
try {
this._websocket.send(this._queued[((id: any): number)]);
this._websocket.send(
this._queued[((id: any): number)]
);
delete this._queued[((id: any): number)];
} catch (error) {
console.error(error);
+1 -1
View File
@@ -33,7 +33,7 @@
"@polkadot/api-format": "^0.6.5",
"@polkadot/api-jsonrpc": "^0.6.5",
"@polkadot/api-provider": "^0.6.5",
"@polkadot/util": "^0.6.7",
"@polkadot/util": "^0.9.1",
"babel-runtime": "^6.26.0"
}
}
+27 -26
View File
@@ -54,9 +54,9 @@
lodash "^4.2.0"
to-fast-properties "^2.0.0"
"@polkadot/dev@^0.13.3":
version "0.13.3"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.13.3.tgz#e90caf191134d233fb6321012914f951b8a3ec17"
"@polkadot/dev@^0.13.4":
version "0.13.4"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.13.4.tgz#beaa45da1655f8335b4a8294db3803f95cd3cc4e"
dependencies:
babel-cli "^6.26.0"
babel-core "^6.26.0"
@@ -80,7 +80,7 @@
eslint-plugin-promise "^3.6.0"
eslint-plugin-react "^7.4.0"
eslint-plugin-standard "^3.0.1"
flow-bin "^0.60.0"
flow-bin "^0.61.0"
flow-copy-source "^1.2.1"
jest "^21.2.1"
makeshift "^1.1.0"
@@ -91,31 +91,28 @@
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"
"@polkadot/primitives-json@^0.3.1":
version "0.3.1"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-json/-/primitives-json-0.3.1.tgz#7c18589b944d792019ed08155363a29504604d23"
dependencies:
"@polkadot/primitives" "^0.1.5"
"@polkadot/util" "^0.6.8"
"@polkadot/primitives" "^0.3.1"
"@polkadot/util" "^0.9.1"
babel-runtime "^6.26.0"
"@polkadot/primitives@^0.1.5":
version "0.1.5"
resolved "https://registry.yarnpkg.com/@polkadot/primitives/-/primitives-0.1.5.tgz#6eb2fbccb06bda8bd774d205528d9b3a1d3e0b21"
"@polkadot/primitives@^0.3.1":
version "0.3.1"
resolved "https://registry.yarnpkg.com/@polkadot/primitives/-/primitives-0.3.1.tgz#420220a8e9c3118f613e47218b038ca7c3724922"
dependencies:
"@polkadot/util" "^0.9.1"
babel-runtime "^6.26.0"
"@polkadot/util@^0.6.7":
version "0.6.7"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.6.7.tgz#eb9a6bd3717b69c962f6fb70830ebc60380c333f"
dependencies:
babel-runtime "^6.26.0"
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"
"@polkadot/util@^0.9.1":
version "0.9.1"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.9.1.tgz#222a31f0325468044736792bd5652e483b8336db"
dependencies:
babel-runtime "^6.26.0"
bn.js "^4.11.8"
ip-regex "^2.1.0"
keccak "^1.3.0"
JSONStream@^1.0.4:
@@ -2105,9 +2102,9 @@ flat-cache@^1.2.1:
graceful-fs "^4.1.2"
write "^0.2.1"
flow-bin@^0.60.0:
version "0.60.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.60.0.tgz#885d0d92a90f94c29445e0f9a0382f5b5debaaed"
flow-bin@^0.61.0:
version "0.61.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.61.0.tgz#d0473a8c35dbbf4de573823f4932124397d32d35"
flow-copy-source@^1.2.1:
version "1.2.1"
@@ -2601,6 +2598,10 @@ invert-kv@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-1.0.0.tgz#104a8e4aaca6d3d8cd157a8ef8bfab2d7a3ffdb6"
ip-regex@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-2.1.0.tgz#fa78bf5d2e6913c911ce9f819ee5146bb6d844e9"
is-alphabetical@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/is-alphabetical/-/is-alphabetical-1.0.1.tgz#c77079cc91d4efac775be1034bf2d243f95e6f08"