Skip to content

Commit 056256f

Browse files
authored
Update JS API and publish 0.2.0 (#210)
1 parent 72233bb commit 056256f

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

bin/wasm-node/javascript/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Light client for Polkadot and Substrate-based chains
2+
3+
This JavaScript library provides a light client for
4+
[the Polkadot blockchain](https://polkadot.network/) and for chains built using
5+
[the Substrate blockchain framework](https://substrate.dev/).
6+
7+
It is an "actual" light client, in the sense that it is byzantine-resilient.
8+
It does rely on the presence of an RPC server, but directly connects to the
9+
full nodes of the network.
10+
11+
## Usage
12+
13+
```
14+
import * as substrate_lite from 'substrate-lite';
15+
16+
// Load a string chain specifications.
17+
const chain_spec = Buffer.from(fs.readFileSync('./westend.json')).toString('utf8');
18+
19+
substrate_lite
20+
.start({
21+
chain_spec: chain_spec,
22+
json_rpc_callback: (resp) => {
23+
// Called whenever the client emits a response to a JSON-RPC request,
24+
// or a JSON-RPC pub-sub notification.
25+
console.log(resp)
26+
}
27+
})
28+
.then((client) => {
29+
client.send_json_rpc('{"jsonrpc":"2.0","id":1,"method":"system_name","params":[]}');
30+
})
31+
```
32+
33+
The `start` function returns a `Promise`.

bin/wasm-node/javascript/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ import Websocket from 'websocket';
2222

2323
import { default as wasm_base64 } from './autogen/wasm.js';
2424

25-
export async function start(chain_specs, json_rpc_callback) {
25+
export async function start(config) {
26+
const chain_spec = config.chain_spec;
27+
const json_rpc_callback = config.json_rpc_callback;
28+
29+
if (Object.prototype.toString.call(chain_spec) !== '[object String]')
30+
throw 'config must include a string chain_spec';
31+
2632
var module;
2733

2834
// List of environment variables to feed to the Rust program. An array of strings.
@@ -222,12 +228,12 @@ export async function start(chain_specs, json_rpc_callback) {
222228

223229
module = result.instance;
224230

225-
let chain_specs_len = Buffer.byteLength(chain_specs, 'utf8');
226-
let chain_specs_ptr = module.exports.alloc(chain_specs_len);
231+
let chain_spec_len = Buffer.byteLength(chain_spec, 'utf8');
232+
let chain_spec_ptr = module.exports.alloc(chain_spec_len);
227233
Buffer.from(module.exports.memory.buffer)
228-
.write(chain_specs, chain_specs_ptr);
234+
.write(chain_spec, chain_spec_ptr);
229235

230-
module.exports.init(chain_specs_ptr, chain_specs_len, 0, 0);
236+
module.exports.init(chain_spec_ptr, chain_spec_len, 0, 0);
231237

232238
return {
233239
send_json_rpc: (request) => {

bin/wasm-node/javascript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "substrate-lite",
3-
"version": "0.1.3",
3+
"version": "0.2.0",
44
"description": "Light client that connects to Polkadot and Substrate-based blockchains",
55
"author": "Parity Technologies <[email protected]>",
66
"license": "GPL-3.0-or-later WITH Classpath-exception-2.0",

bin/wasm-node/javascript/test.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)