lightningd客户端库
pyln-client的Python项目详细描述
pyln客户机:一个用于lightningd的python客户机库
这个包实现了基于Unix套接字的JSON-RPC协议
lightningd
向世界其他地方公开。它可以用来打电话
RPC接口上的任意函数,并作为插件的基础
用python编写。在
安装
pyln-client
在pip
上可用:
pip install pyln-client
或者,您也可以安装开发版本以访问 通过检查c-lightning源代码和 安装到python3环境中:
^{pr2}$这将向您的环境中添加指向库的链接,以便更改 签出的源代码也会导致环境获取这些代码 变化。但是请注意,未发布的版本可能会在没有 警告,请使用发布的版本进行彻底测试。在
示例
使用JSON-RPC客户端
"""Generate invoice on one daemon and pay it on the other"""frompyln.clientimportLightningRpcimportrandom# Create two instances of the LightningRpc object using two different c-lightning daemons on your computerl1=LightningRpc("/tmp/lightning1/lightning-rpc")l5=LightningRpc("/tmp/lightning5/lightning-rpc")info5=l5.getinfo()print(info5)# Create invoice for test paymentinvoice=l5.invoice(100,"lbl{}".format(random.random()),"testpayment")print(invoice)# Get route to l1route=l1.getroute(info5['id'],100,1)print(route)# Pay invoiceprint(l1.sendpay(route['route'],invoice['payment_hash']))
编写插件
插件是lightningd
可以配置为同时执行的程序
主守护程序。它们允许高级交互和自定义
守护进程。在
#!/usr/bin/env python3frompyln.clientimportPluginplugin=Plugin()@plugin.method("hello")defhello(plugin,name="world"):"""This is the documentation string for the hello-function. It gets reported as the description when registering the function as a method with `lightningd`. """greeting=plugin.get_option('greeting')s='{}{}'.format(greeting,name)plugin.log(s)returns@plugin.init()definit(options,configuration,plugin):plugin.log("Plugin helloworld.py initialized")@plugin.subscribe("connect")defon_connect(plugin,id,address):plugin.log("Received connect event for peer {}".format(id))plugin.add_option('greeting','Hello','The greeting I should use.')plugin.run()
- 项目
标签: