Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.rst

Leverj OrderSigner Daemon

The daemon provides an interface for generating transaction signatures. Notably, Web3.py is only compatible with Python 3, so this provides a means by which clients written in Python 2.7 can take advantage of the transaction signing functionality without having to be rewritten in Python 3.

Setup

You will need Python 3.8 or later to run the daemon (earlier versions may work but are unsupported).

To install the library into your virtualenv:

pip install leverj-ordersigner-daemon

Running the Daemon

This project uses Twisted for event-driven networking functionality. To run the daemon, use the twistd executable:

twistd leverj-ordersigner

twistd will create the following files when it runs:

  • twistd.log: process logs.

  • twistd.pid: contains the process ID of the daemon process. You can use this to terminate the daemon like so:

    kill `cat twistd.pid`

Tip

Call twistd -n leverj-ordersigner to run the daemon process in the foreground.

Interacting with the Daemon

The daemon listens for new requests via a Unix Domain Socket located at /tmp/leverj-ordersigner-daemon.sock (can be seen in the output from the daemon when it starts up):

> twistd -n leverj-ordersigner
2020-09-17T17:02:44+1200 [twisted.scripts._twistd_unix.UnixAppLogger#info] twistd 20.3.0 (/bin/python 3.8.5) starting up.
2020-09-17T17:02:44+1200 [twisted.scripts._twistd_unix.UnixAppLogger#info] reactor class: twisted.internet.selectreactor.SelectReactor.
2020-09-17T17:02:44+1200 [-] SigningProtocolFactory starting on '/tmp/leverj-ordersigner-daemon.sock'
2020-09-17T17:02:44+1200 [ordersigner_daemon.protocol.SigningProtocolFactory#info] Starting factory <ordersigner_daemon.protocol.SigningProtocolFactory object at 0x10fcaca30>

To interact with the daemon manually, you can use netcat:

> nc -U /tmp/leverj-ordersigner-daemon.sock

Send a serialised order in JSON format, terminated with \r\n (e.g., press Ctrl+V, then Return — this should appear as ^M in the terminal). You will receive a JSON response with the corresponding transaction signature:

nc -U /tmp/leverj-ordersigner-daemon.sock
{"type": "spot", "instrument": {"symbol": "LEVETH", "quote": {"address": "0x0000000000000000000000000000000000000000", "decimals": 18}, "base": {"address": "0x167cdb1aC9979A6a694B368ED3D2bF9259Fa8282", "decimals": 9}}, "order": {"accountId": "0x167cdb1aC9979A6a694B368ED3D2bF9259Fa8282", "side": "buy", "quantity": 12.3343, "price": 23.44322, "orderType": "LMT", "instrument": "LEVETH", "timestamp": 12382173200872, "expiryTime": 1238217320021122}, "signer": "0xb98ea45b6515cbd6a5c39108612b2cd5ae184d5eb0d72b21389a1fe6db01fe0d"}
^M
{"ok": true, "signature": "0xaad62800f307299a33dae10908c559bd7cd4658a3803e6b587e0f5bf95a052c17783324ec07b629c30e3a41eb20b4ace2787304c50a00b5cdcbd6bc22dbbded11b"}

Important

Ensure that you escape all non-ASCII content and/or that your terminal uses UTF-8 encoding.

Note that, due to the way Unix Domain Sockets work, the daemon can handle connections from multiple clients simultaneously. For more information, see How do Unix Domain Sockets differentiate between multiple clients?

Tip

You can specify an alternative interface (e.g., TCP) like this:

twistd leverj-ordersigner -i tcp:12345:interface=127.0.0.1

The above will start a daemon listening for TCP connections on localhost port 12345.

See documentation for twisted.internet.endpoints.serverFromString for all possible options (note that some interfaces might not work properly; this project was designed explicitly to support Unix Domain Sockets).

Development

If you are working on the leverj-ordersigner-daemon project locally, you will need to install additional dependencies (only has to be done once):

pip install -e '.[dev]'

Documentation

This project uses Sphinx as its docs builder. To build documentation files, run the following command:

make html -C docs

Unit Tests

This project uses nose2 as its test runner. To execute unit tests in the current virtualenv, run the following command:

nose2

In addition, you can use Tox to run unit tests in each supported version of Python:

tox