Coder Social home page Coder Social logo

toyrpc's Introduction

toyrpc

Toychain JSON-RPC

Toyrpc lets you interact with all Toychain APIs via HTTP or TCP, using the JSON-RPC 2.0 spec (https://www.jsonrpc.org/specification).

poster


Why

  1. Programming Language Agnostic: No need to use JavaScript to program Toychain, you can simply set up a Toyrpc daemon and interact with it over JSON-RPC to clone, add, push, read, etc. --- everything you can do with Toychain.
  2. Container-friendly: Toyrpc and its Toychain can be containerized

Intall

You need to install both toychain AND toyrpc, because Toyrpc endpoints are instantiated from an existing Toychain object.

npm install --save toychain
npm install --save toyrpc

Setting up the server

Build or find a toychain, and then instantiate a Toyrpc from the toychain.

const Toychain = require('toychain')
const Toyrpc = require('toyrpc')

// Build a toychain
const toychain = new Toychain({ xpriv: <xpriv> })

// Instantiate RPC for the toychain
const rpc = new Toyrpc({
  chain: toychain,
  readonly: true,
  port: 3000,
  protocol: "http"
})

// Listen
rpc.listen()

Here's the full syntax for the constructor:

const rpc = new Toyrpc({
  chain: <toychain object>,
  readonly: <true|false (default false)>,
  port: <the port to serve rpc from>,
  protocol: <"http"|"tcp">
})
  • chain: you need to inject an existing toychain object to build an RPC endpoint
  • readonly: if set to true, you can make this RPC endpoint only readonly. All "write" actions (clone, add, push, and reset) are blocked. When readonly mode, the only allowed functions are count and get.
  • port: The port to serve RPC from
  • protocol: Choose to serve over HTTP or TCP. The default is HTTP.

Once initialized, you can listen:

rpc.listen()

API Usage

Once the endpoint is up and running, you can make JSON-RPC 2.0 compliant requests over HTTP or TCP.

Here's an example:

POST http://localhost:3012

{
  "jsonrpc": "2.0",
  "method": "add",
  "params": {
    "v": 1,
    "out": [{
      "o0": "OP_0", "o1": "OP_RETURN", "s2": "hello toy"
    }],
    "edge": { "in": 1, "out": 2 }
  },
  "id": 0
}

The curreently available RPC endpoints are:

See JSON-RPC 2.0 Spec for more details on the JSON-RPC syntax: https://www.jsonrpc.org/specification


Demo

You can try the demo under demo/http.

  1. The server.js starts a JSON-RPC server.
  2. The client.js lets you call various available JSON-RPC methods via HTTP

toyrpc's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.