Skip to content

WebSocket API

Avvrik edited this page Mar 15, 2019 · 73 revisions

KeyChain contains a WebSocket API. This API can be used to stream information from a KeyChain instance to any client that implements WebSockets. Implementations in different languages:

  • NodeJS
  • Python
  • JavaScript/HTML
  • etc

Install KeyChain for macOS.

Demo pages in JavaScript

For testing the KeyChain commands

  • On this demo page you can test all the KeyChain commands.

  • Here you will find the code of the KeyChain demo page. It will be automatically installed together with the KeyChain.

For signing transactions

  • Here you can try out signing Ethereum transactions with KeyChain.

  • You can find an example of the code here.

Message format

Each WebSocket API message is a json serialized object containing a command with the corresponding parameters.

For example, here is a request for the current version:

{"command":"version"}

For full comprehensive descriptions of the commands, acceptable parameters and values, go to the Protocol.

WebSocket integration guide

NodeJS integration example

The following will show the usage of websocket connections. We make use of the wscat application available via npm:

$ npm install -g wscat

A simple call would take the form:

$ wscat -c ws://127.0.0.1:16384
> { "command": "version"}

Successful Calls

The API will return a properly JSON formated response carrying the same id as the request to distinguish subsequent calls.

{"result":  ..data..}

Errors

In case of an error, the resulting answer will carry an error attribute and a description in human-readable format:

{"error":"Error: keyfile could not found by keyname"}

JavaScript integration example

Before you proceed with the integration, you need to install KeyChain for macOS.

Test in the web

After installing KeyChain, open the browser and connect to the demo page via http://localhost:16384/.

When the connection is established, you will see the following KeyChain page. If you click on one of the commands on the left panel, you will see its json request and response in the white boxes below:

image_2019-02-15_13-24-52

Test from a terminal application:

You can see if the installation was successful by going to the terminal app, opening KeyChain and trying one of the commands that you can take from the Protocol:

c:\Users\User-1>cd "C: Program Files\WsKeychain"
c:\Program Files\WSKeychain>keychain 
{"command":"version"}

A successful response will take the following format:

Response format
Field name Type Description
result string current version number which has the form of "[major].[minor].[build number]".
Response example
{"result":"0.9.114"}

Build a page that connects to WebSocket

Here you will find an example of how to build a web-page that connects to WebSocket.

Save this to the folder where you will be running the websocket command.

Open this page via localhost: http://localhost:16384/


You can find the code for the demo page and the page itself up in the Demo pages in JavaScript section.