Lightning-Network

如何通過 Python 與 Eclair 互動?

  • October 15, 2019

我想為 Lightning 的實驗編寫一些腳本。Python 是我最熟悉的語言。從三大實現來看,LND 有python-grpc,c-lightning 有pylightning,但是 Eclair 呢?如何通過 Python 腳本與 Eclair 節點互動?

這是一個簡單的實現

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}

引用自:https://bitcoin.stackexchange.com/questions/90476