Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"packages": [
"packages/*"
],
"version": "0.7.1"
"version": "0.8.0"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"test": "jest --coverage"
},
"devDependencies": {
"@polkadot/dev": "^0.15.1",
"@polkadot/dev": "^0.16.17",
"lerna": "^2.5.1"
}
}
8 changes: 4 additions & 4 deletions packages/api-format/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-format",
"version": "0.7.1",
"version": "0.8.0",
"description": "Input/Output formatters for JsonRPC exchange",
"main": "index.js",
"keywords": [
Expand Down Expand Up @@ -30,11 +30,11 @@
"test": "echo \"Tests only available from root wrapper\""
},
"devDependencies": {
"@polkadot/api-jsonrpc": "^0.7.1"
"@polkadot/api-jsonrpc": "^0.8.0"
},
"dependencies": {
"@polkadot/primitives-json": "^0.4.10",
"@polkadot/util": "^0.13.1",
"@polkadot/primitives-json": "^0.9.4",
"@polkadot/util": "^0.17.2",
"babel-runtime": "^6.26.0"
}
}
7 changes: 3 additions & 4 deletions packages/api-format/src/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
// @flow

import type { InterfaceInputType } from '@polkadot/api-jsonrpc/types';
import type { Hash } from '@polkadot/primitives/base';

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 echo = require('./echo');
const util = require('./util');

const formatters = {
'Address': accountIdEncode,
'CallData': hashEncode,
'H256': h256Encode,
'HeaderHash': headerHashEncode,
'H256': (value: Hash) => hashEncode(value, 256),
'HeaderHash': hashEncode,
'String': echo
};

Expand Down
2 changes: 1 addition & 1 deletion packages/api-format/src/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('formatInputs', () => {
{ name: 'foo', type: 'Address' },
{ name: 'bar', type: 'H256' }
],
['0x1234', '0xabcd']
[new Uint8Array([0x12, 0x34]), new Uint8Array([0xab, 0xcd])]
)
).toEqual([
'0x1234',
Expand Down
16 changes: 7 additions & 9 deletions packages/api-format/src/output.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@ describe('formatOutput', () => {
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])
new Uint8Array([0x56, 0x78]),
new Uint8Array([0x78, 0x9a])
]
}
},
number: new BN(0x1234),
parentHash: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x12, 0x34]),
stateRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x56, 0x78]),
transactionRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd])
})
);
});
Expand Down
4 changes: 3 additions & 1 deletion packages/api-format/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type FormattersFunctionMap = {
};

const isUndefined = require('@polkadot/util/is/undefined');
const l = require('@polkadot/util/logger')('api-format');

const echo = require('./echo');

const arrayTypeRegex = /\[\]$/;
Expand All @@ -20,7 +22,7 @@ function formatSingleType (formatters: FormattersFunctionMap, type: FormattersFu
const formatter: FormatterFunction = formatters[type];

if (isUndefined(formatter)) {
console.warn(`Unable to find default formatter for '${type}', falling back to echo`);
l.warn(`Unable to find default formatter for '${type}', falling back to echo`);

return echo(value);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/api-format/src/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ 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 echo");
expect(warnSpy).toHaveBeenCalledWith(
expect.anything(), expect.anything(), "Unable to find default formatter for 'Unknown', falling back to echo"
);
});

it('wraps exceptions with the type', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/api-jsonrpc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-jsonrpc",
"version": "0.7.1",
"version": "0.8.0",
"description": "Method definitions for the Polkadot RPC layer",
"main": "index.js",
"engines": {
Expand Down
8 changes: 5 additions & 3 deletions packages/api-jsonrpc/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ export type InterfaceMethodDefinition = {

export type InterfaceTypes = 'chain' | 'state';

export type InterfaceDefinition$Methods = {
[string]: InterfaceMethodDefinition
};

export type InterfaceDefinition = {
methods: {
[string]: InterfaceMethodDefinition
}
methods: InterfaceDefinition$Methods
};

export type InterfaceDefinitions = {
Expand Down
4 changes: 2 additions & 2 deletions packages/api-provider/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-provider",
"version": "0.7.1",
"version": "0.8.0",
"description": "Transport providers for the API",
"main": "index.js",
"keywords": [
Expand Down Expand Up @@ -34,7 +34,7 @@
"nock": "^9.1.0"
},
"dependencies": {
"@polkadot/util": "^0.13.1",
"@polkadot/util": "^0.17.2",
"babel-runtime": "^6.26.0",
"isomorphic-fetch": "^2.2.1",
"websocket": "^1.0.25"
Expand Down
29 changes: 29 additions & 0 deletions packages/api-provider/src/coder/json/decodeResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow

import type { JsonRpcResponse } from '../../types';
import type { RpcCoderState } from './types';

const assert = require('@polkadot/util/assert');
const isNumber = require('@polkadot/util/is/number');
const isUndefined = require('@polkadot/util/is/undefined');

module.exports = function decodeResponse (self: RpcCoderState, response: JsonRpcResponse): mixed {
assert(response, 'Empty response object received');

assert(response.jsonrpc === '2.0', 'Invalid jsonrpc field in decoded object');

assert(isNumber(response.id), 'Invalid id field in decoded object');

if (response.error) {
const { code, message } = response.error;

throw new Error(`[${code}]: ${message}`);
}

assert(!isUndefined(response.result), 'No result found in JsonRpc response');

return response.result;
};
49 changes: 49 additions & 0 deletions packages/api-provider/src/coder/json/decodeResponse.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

const createCoder = require('./index');

describe('decodeResponse', () => {
let coder;

beforeEach(() => {
coder = createCoder();
});

it('expects a non-empty input object', () => {
expect(
() => coder.decodeResponse()
).toThrow(/Empty response/);
});

it('expects a valid jsonrpc field', () => {
expect(
() => coder.decodeResponse({})
).toThrow(/Invalid jsonrpc/);
});

it('expects a valid id field', () => {
expect(
() => coder.decodeResponse({ jsonrpc: '2.0' })
).toThrow(/Invalid id/);
});

it('expects a valid result field', () => {
expect(
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0' })
).toThrow(/No result/);
});

it('throws any error found', () => {
expect(
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0', error: { code: 123, message: 'test error' } })
).toThrow(/\[123\]: test error/);
});

it('returns the result', () => {
expect(
coder.decodeResponse({ id: 1, jsonrpc: '2.0', result: 'some result' })
).toEqual('some result');
});
});
14 changes: 14 additions & 0 deletions packages/api-provider/src/coder/json/encodeJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow

import type { RpcCoderState } from './types';

const encodeObject = require('./encodeObject');

module.exports = function encodeJson (self: RpcCoderState, method: string, params: Array<mixed>): string {
return JSON.stringify(
encodeObject(self, method, params)
);
};
19 changes: 19 additions & 0 deletions packages/api-provider/src/coder/json/encodeJson.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

const createCoder = require('./index');

describe('encodeJson', () => {
let coder;

beforeEach(() => {
coder = createCoder();
});

it('encodes a valid JsonRPC JSON string', () => {
expect(
coder.encodeJson('method', 'params')
).toEqual('{"id":1,"jsonrpc":"2.0","method":"method","params":"params"}');
});
});
16 changes: 16 additions & 0 deletions packages/api-provider/src/coder/json/encodeObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow

import type { JsonRpcRequest } from '../../types';
import type { RpcCoderState } from './types';

module.exports = function encodeObject (self: RpcCoderState, method: string, params: Array<mixed>): JsonRpcRequest {
return {
id: ++self.id,
jsonrpc: '2.0',
method,
params
};
};
29 changes: 29 additions & 0 deletions packages/api-provider/src/coder/json/encodeObject.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

const createCoder = require('./index');

describe('encodeObject', () => {
let coder;

beforeEach(() => {
coder = createCoder();
});

it('starts with id === 0 (nothing sent)', () => {
expect(coder.getId()).toEqual(0);
});

it('encodes a valid JsonRPC object', () => {
expect(
coder.encodeObject('method', 'params')
).toEqual({
id: 1,
jsonrpc: '2.0',
method: 'method',
params: 'params'
});
expect(coder.getId()).toEqual(1);
});
});
28 changes: 28 additions & 0 deletions packages/api-provider/src/coder/json/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow

import type { JsonRpcRequest, JsonRpcResponse } from '../../types';
import type { RpcCoder, RpcCoderState } from './types';

const decodeResponse = require('./decodeResponse');
const encodeJson = require('./encodeJson');
const encodeObject = require('./encodeObject');

module.exports = function rpcCoder (): RpcCoder {
const self: RpcCoderState = {
id: 0
};

return {
decodeResponse: (response: JsonRpcResponse): mixed =>
decodeResponse(self, response),
encodeJson: (method: string, params: Array<mixed>): string =>
encodeJson(self, method, params),
encodeObject: (method: string, params: Array<mixed>): JsonRpcRequest =>
encodeObject(self, method, params),
getId: (): number =>
self.id
};
};
17 changes: 17 additions & 0 deletions packages/api-provider/src/coder/json/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow

import type { JsonRpcRequest, JsonRpcResponse } from '../../types';

export type RpcCoder = {
decodeResponse: (response: JsonRpcResponse) => mixed,
encodeObject: (method: string, params: Array<mixed>) => JsonRpcRequest,
encodeJson: (method: string, params: Array<mixed>) => string,
getId: () => number
};

export type RpcCoderState = {
id: number
};
Loading