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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/build/*
5 changes: 5 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
.*/build/.*

[include]
./node_modules/@polkadot/dev/flow-typed

[libs]

[options]
esproposal.decorators=ignore
module.ignore_non_literal_requires=true
unsafe.enable_getters_and_setters=true
module.name_mapper='^@polkadot/api-format\(.*\)$' -> '<PROJECT_ROOT>/packages/api-format/src\1'
module.name_mapper='^@polkadot/api-jsonrpc\(.*\)$' -> '<PROJECT_ROOT>/packages/api-jsonrpc/src\1'
module.name_mapper='^@polkadot/api-provider\(.*\)$' -> '<PROJECT_ROOT>/packages/api-provider/src\1'
module.name_mapper='^@polkadot/jsonrpc\(.*\)$' -> '<PROJECT_ROOT>/packages/api-jsonrpc/src\1'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ tmp/
.env.development.local
.env.test.local
.env.production.local
lerna-debug.log*
npm-debug.log*
yarn-debug.log*
yarn-error.log*
22 changes: 1 addition & 21 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
coverage
scripts
src
test
tmp
.babelrc
.coveralls.yml
.editorconfig
.eslintrc.json
.flowconfig
.gitignore
.istanbul.yml
.travis.yml
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*

8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ cache:
directories:
- node_modules
before_install:
- npm install -g yarn --cache-min 999999999
- npm install --global yarn --cache-min 999999999
- npm install --global lerna
install:
- yarn
- yarn install
- lerna bootstrap
script:
- yarn run polkadot-dev-build-travis
- yarn polkadot-dev-build-travis
44 changes: 13 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
![ISC](https://img.shields.io/badge/license-ISC-lightgrey.svg)
[![npm (scoped)](https://img.shields.io/npm/v/@polkadot/api.svg)](https://www.npmjs.com/package/@polkadot/api)
[![Build Status](https://travis-ci.org/polkadot-js/api.svg?branch=master)](https://travis-ci.org/polkadot-js/api)
[![Coverage Status](https://coveralls.io/repos/github/polkadot-js/api/badge.svg?branch=master)](https://coveralls.io/github/polkadot-js/api?branch=master)
[![Greenkeeper badge](https://badges.greenkeeper.io/polkadot-js/api.svg)](https://greenkeeper.io/)
[![Dependency Status](https://david-dm.org/polkadot-js/api.svg)](https://david-dm.org/polkadot-js/api)
[![devDependency Status](https://david-dm.org/polkadot-js/api/dev-status.svg)](https://david-dm.org/polkadot-js/api#info=devDependencies)

# @polkadot/api
# @polkadot/client

Warning - currently this does not actually do all that much, it is an attempt to put into code some thoughts about how to maintain the endpoints. This library provides a clean wrapper around all the methods exposed by a Polkadot network client. Methods are auto-generated for the [JsonRPC interface definitions](https://github.com/polkadot-js/jsonrpc).
This library provides a clean wrapper around all the methods exposed by a Polkadot network client.

## Usage
It is split up into a number of internal packages -

Installation -
- [@polkadot/api](packages/api/) The API library
- [@polkadot/api-format](packages/api-format/) Input and output formatters
- [@polkadot/api-jsonrpc](packages/api-jsonrpc/) Interface definitions for RPC
- [@polkadot/api-provider](packages/api-provider/) Transport providers

```
npm install --save @polkadot/api
```
## Contributing

Initialisation -

```js
import Api from '@polkadot/api';
import HttpProvider from '@polkadot/api-provider/http';

const provider = new HttpProvider('http://127.0.0.1:9933');
const api = new Api(provider);
```

Making calls -

```js
api.chain
.getHeader('0x1234567890')
.then((header) => console.log(header))
.catch((error) => console.error(error));
```

## Available methods

For a list of currently exposed methods, see the [@polkadot/jsonrpc](https://github.com/polkadot-js/jsonrpc#readme) repository.
- Make sure you have [Lerna](https://lernajs.io/) installed, `yarn install -g lerna`
- Bootstrap the dependencies, `lerna bootstrap`
- Make any changes in the relevant package, on master merges new versions will be published automatically
10 changes: 9 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
module.exports = require('@polkadot/dev/jest.config');
const config = require('@polkadot/dev/jest.config');

module.exports = Object.assign({}, config, {
moduleNameMapper: {
'@polkadot/api-format(.*)$': '<rootDir>/packages/api-format/src/$1',
'@polkadot/api-jsonrpc(.*)$': '<rootDir>/packages/api-jsonrpc/src/$1',
'@polkadot/api-provider(.*)$': '<rootDir>/packages/api-provider/src/$1'
}
});
14 changes: 14 additions & 0 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"lerna": "2.5.1",
"npmClient": "yarn",
"useWorkspaces": true,
"command": {
"publish": {
"allowBranch": "master"
}
},
"packages": [
"packages/*"
],
"version": "0.5.0"
}
39 changes: 15 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
{
"name": "@polkadot/api",
"version": "0.4.14",
"description": "A JavaScript wrapper for the Polkadot JsonRPC interface",
"main": "index.js",
"keywords": [
"Polkadot",
"JsonRPC"
],
"name": "@polkadot/api-wrapper",
"version": "0.5.0",
"description": "Non-published, see packages/",
"keywords": [],
"author": "Jaco Greeff <jacogr@gmail.com>",
"license": "ISC",
"private": true,
"engines": {
"node": ">=6.4"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
"node": ">=8.0"
},
"repository": {
"type": "git",
Expand All @@ -25,18 +18,16 @@
},
"homepage": "https://github.com/polkadot-js/api#readme",
"scripts": {
"build": "polkadot-dev-build-babel",
"check": "eslint src && flow check",
"test": "jest --coverage src"
"build": "yarn polkadot-dev-build-babel",
"check": "yarn eslint packages && yarn flow check",
"test": "yarn jest --coverage packages"
},
"devDependencies": {
"@polkadot/dev": "^0.8.1"
"@polkadot/dev": "^0.9.1",
"lerna": "^2.5.1"
},
"dependencies": {
"@polkadot/api-format": "^0.3.7",
"@polkadot/api-provider": "^0.1.6",
"@polkadot/jsonrpc": "^0.3.9",
"@polkadot/util": "^0.5.10",
"babel-runtime": "^6.26.0"
}
"dependencies": {},
"workspaces": [
"packages/*"
]
}
15 changes: 15 additions & 0 deletions packages/api-format/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License (ISC)

Copyright 2017 Jaco Greeff <jacogr@gmail.com>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
14 changes: 14 additions & 0 deletions packages/api-format/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[![Dependency Status](https://david-dm.org/polkadot-js/api.svg?path=packages/api-format)](https://david-dm.org/polkadot-js/api?path=packages/api-format)
[![devDependency Status](https://david-dm.org/polkadot-js/api/dev-status.svg?path=packages/api-format)](https://david-dm.org/polkadot-js/api?path=packages/api-format#info=devDependencies)

# @polkadot/api-format

Formatters that are used by the application API interface, taking care of transparently formatting parameters (inputs) and the results (output) for requests made over the client RPC interfaces.

## Usage

Installation -

```
npm install --save @polkadot/api-format
```
40 changes: 40 additions & 0 deletions packages/api-format/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@polkadot/api-format",
"version": "0.5.0",
"description": "Input/Output formatters for JsonRPC exchange",
"main": "index.js",
"keywords": [
"Polkadot",
"JsonRPC"
],
"author": "Jaco Greeff <jacogr@gmail.com>",
"license": "ISC",
"engines": {
"node": ">=6.4"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"repository": {
"type": "git",
"url": "git+https://github.com/polkadot-js/api.git"
},
"bugs": {
"url": "https://github.com/polkadot-js/api/issues"
},
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-format#readme",
"scripts": {
"build": "polkadot-dev-build-babel",
"check": "eslint src && flow check",
"test": "echo \"Tests only available from root wrapper\""
},
"devDependencies": {
"@polkadot/api-jsonrpc": "^0.5.0",
"@polkadot/dev": "^0.9.1"
},
"dependencies": {
"@polkadot/util": "^0.5.15",
"babel-runtime": "^6.26.0"
}
}
10 changes: 10 additions & 0 deletions packages/api-format/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow

const formatInputs = require('./input');
const formatOutput = require('./output');

module.exports = {
formatInputs,
formatOutput
};
10 changes: 10 additions & 0 deletions packages/api-format/src/input/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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);
};
13 changes: 13 additions & 0 deletions packages/api-format/src/input/address.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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 changes: 33 additions & 0 deletions packages/api-format/src/input/hex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 changes: 53 additions & 0 deletions packages/api-format/src/input/hex.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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,}$/);
});
});
});
Loading