How can I interact with Eclair from Python?

2

I want to write some scripts for experiments with Lightning. Python is the language I'm most comfortable with. From the three major implementations, LND has python-grpc and c-lightning has pylightning, but what about Eclair? How can I interact with an Eclair node from a Python script?

Sergei Tikhomirov

Posted 2019-09-18T10:19:18.243

Reputation: 1 326

Answers

2

Here is a simple implementation that just works

import requests

class EclairClient():
    def __init__(self,host, port, password, service_name=None, session=None):
        self._host = host
        self._port = port
        self._password = password
        self._session = session
        if session is None:
            self._session = requests.Session()
        self._service_name = service_name
        self._url = "http://%s:%s/%s" % (self._host, self._port, self._service_name)

    def __getattr__(self, name):
        if name.startswith('__') and name.endswith('__'):
            # Python internal stuff
            raise AttributeError
        if self._service_name is not None:
            name = "%s.%s" % (self._service_name, name)
        return EclairClient(self._host, self._port, self._password, name, self._session )

    def __call__(self, *args, **kwargs):
        return self._session.post(self._url, data=kwargs,auth=('', self._password)).json()
password = "mypassword"
c = EclairClient("localhost", 8080, password)
c.getinfo()
>>> {u'alias': u'blinktrade DEV',
>>>  u'blockHeight': 1583023,
>>> u'chainHash': u'43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000',
>>> u'nodeId': u'038b3f00c4a535238b748f025038dcbb825c4e2a03f08ab1c391f0bfada4b31b45',
>>> u'publicAddresses': []}

c.createinvoice(description="testing", amountMsat=15000)
>>>{u'amount': 15000,
>>> u'description': u'testing',
>>> u'expiry': 3600,
>>> u'nodeId': u'038b3f00c4a535238b748f025038dcbb825c4e2a03f08ab1c391f0bfada4b31b45',
>>> u'paymentHash': u'0eb6ea2dcb36b99a0e7ab3e09bff97c0e32277a5017034cf2e39859de00f4a8e',
>>> u'prefix': u'lntb',
>>> u'serialized': u'lntb150n1pw62a8zpp5p6mw5twtx6ue5rn6k0sfhluhcr3jyaa9q9crfnew8xzemcq0f28qdqvw3jhxarfdensxqrrssdwfxdxrvkdtaetg5k8dz3j42hpupffvdzljsl3nls9faujuxt5e4elu0cnvszg0vhs3m2gw28d4h9c83saxfk0qs4ygwd6yaa5dt8uqpamka3f',
>>> u'timestamp': 1571124450}

Rodrigo Pinho Pereira de Souza

Posted 2019-09-18T10:19:18.243

Reputation: 136

Thanks a lot, very much appreciated! – Sergei Tikhomirov – 2019-12-11T15:55:01.513

This script provides strange results for sendtoroute command. Given a route, it seems that it tries to route to the first element of the list (i.e. sending node), and the payment gets stuck in pending state. An analogous command in CLI works as expected. What may be the reason?

– Sergei Tikhomirov – 2019-12-16T11:49:25.830

2

This might not be a satisfying answer but if you look at the eclair git repo and their Wiki you will find that the eclair API is a JSON API and is exposed via http and web sockets and can be queried with regular http requests.

If you look at the c-lightning python client it is a tool that talks via a unixDomainSocket to the exposed lightningd api. So I guess you could in a smilar way quickly create a python wrapper to be able to talk to eclair.

It seems to me that currently no one has bothered to do so.

Rene Pickhardt

Posted 2019-09-18T10:19:18.243

Reputation: 8 514

1This is a good idea, the wrapper python for eclair should be an good training for my skill with python – vincenzopalazzo – 2019-09-18T11:23:23.577