2017-01-14 20:30:37 +01:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import socket
|
|
|
|
|
2018-01-27 16:53:14 +01:00
|
|
|
|
2017-01-17 23:26:00 +01:00
|
|
|
class UnixDomainSocketRpc(object):
|
2018-02-14 14:12:29 +01:00
|
|
|
def __init__(self, socket_path, executor=None, logger=logging):
|
2017-01-14 20:30:37 +01:00
|
|
|
self.socket_path = socket_path
|
|
|
|
self.decoder = json.JSONDecoder()
|
|
|
|
self.executor = executor
|
2018-02-14 14:12:29 +01:00
|
|
|
self.logger = logger
|
2017-01-14 20:30:37 +01:00
|
|
|
|
2018-01-27 16:53:14 +01:00
|
|
|
@staticmethod
|
|
|
|
def _writeobj(sock, obj):
|
2017-01-14 20:30:37 +01:00
|
|
|
s = json.dumps(obj)
|
|
|
|
sock.sendall(bytearray(s, 'UTF-8'))
|
|
|
|
|
|
|
|
def _readobj(self, sock):
|
|
|
|
buff = b''
|
|
|
|
while True:
|
|
|
|
try:
|
2017-03-18 16:06:52 +01:00
|
|
|
b = sock.recv(1024)
|
|
|
|
buff += b
|
|
|
|
if len(b) == 0:
|
|
|
|
return {'error': 'Connection to RPC server lost.'}
|
2017-01-14 20:30:37 +01:00
|
|
|
# Convert late to UTF-8 so glyphs split across recvs do not
|
|
|
|
# impact us
|
|
|
|
objs, _ = self.decoder.raw_decode(buff.decode("UTF-8"))
|
|
|
|
return objs
|
|
|
|
except ValueError:
|
2017-12-10 13:41:10 +01:00
|
|
|
# Probably didn't read enough
|
2017-01-14 20:30:37 +01:00
|
|
|
pass
|
|
|
|
|
2017-04-12 20:08:48 +02:00
|
|
|
def __getattr__(self, name):
|
2018-02-14 14:17:31 +01:00
|
|
|
"""Intercept any call that is not explicitly defined and call @call
|
2017-04-12 20:08:48 +02:00
|
|
|
|
|
|
|
We might still want to define the actual methods in the subclasses for
|
|
|
|
documentation purposes.
|
|
|
|
"""
|
|
|
|
name = name.replace('_', '-')
|
2018-01-27 16:53:14 +01:00
|
|
|
|
2018-02-14 14:17:31 +01:00
|
|
|
def wrapper(**kwargs):
|
|
|
|
return self.call(name, payload=kwargs)
|
2017-04-12 20:08:48 +02:00
|
|
|
return wrapper
|
|
|
|
|
2018-02-14 14:17:31 +01:00
|
|
|
def call(self, method, payload=None):
|
2018-02-14 14:12:29 +01:00
|
|
|
self.logger.debug("Calling %s with payload %r", method, payload)
|
2017-01-14 20:30:37 +01:00
|
|
|
|
|
|
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
sock.connect(self.socket_path)
|
|
|
|
self._writeobj(sock, {
|
|
|
|
"method": method,
|
2018-02-14 14:17:31 +01:00
|
|
|
"params": payload or {},
|
2017-01-14 20:30:37 +01:00
|
|
|
"id": 0
|
|
|
|
})
|
|
|
|
resp = self._readobj(sock)
|
|
|
|
sock.close()
|
|
|
|
|
2018-02-14 14:12:29 +01:00
|
|
|
self.logger.debug("Received response for %s call: %r", method, resp)
|
2018-02-14 14:17:31 +01:00
|
|
|
if "error" in resp:
|
|
|
|
raise ValueError(
|
|
|
|
"RPC call failed: {}, method: {}, payload: {}".format(
|
|
|
|
resp["error"],
|
|
|
|
method,
|
|
|
|
payload
|
|
|
|
))
|
|
|
|
elif "result" not in resp:
|
|
|
|
raise ValueError("Malformed response, \"result\" missing.")
|
|
|
|
return resp["result"]
|
2017-01-14 20:30:37 +01:00
|
|
|
|
2017-01-17 23:26:00 +01:00
|
|
|
|
|
|
|
class LightningRpc(UnixDomainSocketRpc):
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
|
|
|
RPC client for the `lightningd` daemon.
|
2017-01-17 23:26:00 +01:00
|
|
|
|
|
|
|
This RPC client connects to the `lightningd` daemon through a unix
|
|
|
|
domain socket and passes calls through. Since some of the calls
|
|
|
|
are blocking, the corresponding python methods include an `async`
|
|
|
|
keyword argument. If `async` is set to true then the method
|
|
|
|
returns a future immediately, instead of blocking indefinitely.
|
|
|
|
|
|
|
|
This implementation is thread safe in that it locks the socket
|
|
|
|
between calls, but it does not (yet) support concurrent calls.
|
|
|
|
"""
|
|
|
|
|
2018-02-14 14:17:31 +01:00
|
|
|
def getpeer(self, peer_id, level=None):
|
2018-01-28 12:12:37 +01:00
|
|
|
"""
|
|
|
|
Show peer with {peer_id}, if {level} is set, include {log}s
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"id": peer_id,
|
|
|
|
"level": level
|
|
|
|
}
|
|
|
|
res = self.call("listpeers", payload)
|
|
|
|
return res.get("peers") and res["peers"][0] or None
|
2018-01-28 12:12:37 +01:00
|
|
|
|
2018-01-27 16:53:14 +01:00
|
|
|
def dev_blockheight(self):
|
|
|
|
"""
|
|
|
|
Show current block height
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
return self.call("dev-blockheight")
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def dev_setfees(self, immediate, normal=None, slow=None):
|
|
|
|
"""
|
|
|
|
Set feerate in satoshi-per-kw for {immediate}, {normal} and {slow}
|
|
|
|
(each is optional, when set, separate by spaces) and show the value of those three feerates
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"immediate": immediate,
|
|
|
|
"normal": normal,
|
|
|
|
"slow": slow
|
|
|
|
}
|
|
|
|
return self.call("dev-setfees", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
2018-01-28 12:12:37 +01:00
|
|
|
def listnodes(self, node_id=None):
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
Show all nodes in our local network view, filter on node {id} if provided
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"id": node_id
|
|
|
|
}
|
|
|
|
return self.call("listnodes", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
2018-02-14 14:17:31 +01:00
|
|
|
def getroute(self, peer_id, msatoshi, riskfactor, cltv=9):
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
Show route to {id} for {msatoshi}, using {riskfactor} and optional {cltv} (default 9)
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"id": peer_id,
|
|
|
|
"msatoshi": msatoshi,
|
|
|
|
"riskfactor": riskfactor,
|
|
|
|
"cltv": cltv
|
|
|
|
}
|
|
|
|
return self.call("getroute", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
2018-01-28 12:12:37 +01:00
|
|
|
def listchannels(self, short_channel_id=None):
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
Show all known channels, accept optional {short_channel_id}
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"short_channel_id": short_channel_id
|
|
|
|
}
|
|
|
|
return self.call("listchannels", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def invoice(self, msatoshi, label, description, expiry=None):
|
|
|
|
"""
|
|
|
|
Create an invoice for {msatoshi} with {label} and {description} with optional {expiry} seconds (default 1 hour)
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"msatoshi": msatoshi,
|
|
|
|
"label": label,
|
|
|
|
"description": description,
|
|
|
|
"expiry": expiry
|
|
|
|
}
|
|
|
|
return self.call("invoice", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def listinvoices(self, label=None):
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
Show invoice {label} (or all, if no {label))
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"label": label
|
|
|
|
}
|
|
|
|
return self.call("listinvoices", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def delinvoice(self, label, status):
|
2017-01-17 23:26:00 +01:00
|
|
|
"""
|
2018-01-27 16:53:14 +01:00
|
|
|
Delete unpaid invoice {label} with {status}
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"label": label,
|
|
|
|
"status": status
|
|
|
|
}
|
|
|
|
return self.call("delinvoice", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def waitanyinvoice(self, lastpay_index=None):
|
|
|
|
"""
|
|
|
|
Wait for the next invoice to be paid, after {lastpay_index} (if supplied)
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"lastpay_index": lastpay_index
|
|
|
|
}
|
|
|
|
return self.call("waitanyinvoice", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
2018-02-14 14:17:31 +01:00
|
|
|
def waitinvoice(self, label=None):
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
|
|
|
Wait for an incoming payment matching the invoice with {label}
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"label": label
|
|
|
|
}
|
|
|
|
return self.call("waitinvoice", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def decodepay(self, bolt11, description=None):
|
|
|
|
"""
|
|
|
|
Decode {bolt11}, using {description} if necessary
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"bolt11": bolt11,
|
|
|
|
"description": description
|
|
|
|
}
|
|
|
|
return self.call("decodepay", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def help(self):
|
|
|
|
"""
|
|
|
|
Show available commands
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
return self.call("help")
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"""
|
|
|
|
Shut down the lightningd process
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
return self.call("stop")
|
2017-01-17 23:26:00 +01:00
|
|
|
|
|
|
|
def getlog(self, level=None):
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
|
|
|
Show logs, with optional log {level} (info|unusual|debug|io)
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"level": level
|
|
|
|
}
|
|
|
|
return self.call("getlog", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def dev_rhash(self, secret):
|
|
|
|
"""
|
|
|
|
Show SHA256 of {secret}
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"secret": secret
|
|
|
|
}
|
|
|
|
return self.call("dev-rhash", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def dev_crash(self):
|
|
|
|
"""
|
|
|
|
Crash lightningd by calling fatal()
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
return self.call("dev-crash")
|
2017-01-17 23:26:00 +01:00
|
|
|
|
2018-01-27 16:53:14 +01:00
|
|
|
def getinfo(self):
|
|
|
|
"""
|
|
|
|
Show information about this node
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
return self.call("getinfo")
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def sendpay(self, route, rhash):
|
|
|
|
"""
|
|
|
|
Send along {route} in return for preimage of {rhash}
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"route": route,
|
|
|
|
"rhash": rhash
|
|
|
|
}
|
|
|
|
return self.call("sendpay", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def pay(self, bolt11, msatoshi=None, description=None, riskfactor=None):
|
|
|
|
"""
|
|
|
|
Send payment specified by {bolt11} with optional {msatoshi} (if and only if {bolt11} does not have amount),
|
|
|
|
{description} (required if {bolt11} uses description hash) and {riskfactor} (default 1.0)
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"bolt11": bolt11,
|
|
|
|
"msatoshi": msatoshi,
|
|
|
|
"description": description,
|
|
|
|
"riskfactor": riskfactor
|
|
|
|
}
|
|
|
|
return self.call("pay", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
2018-01-28 14:46:52 +01:00
|
|
|
def listpayments(self, bolt11=None, payment_hash=None):
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-01-28 14:46:52 +01:00
|
|
|
Show outgoing payments, regarding {bolt11} or {payment_hash} if set
|
|
|
|
Can only specify one of {bolt11} or {payment_hash}
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
assert not (bolt11 and payment_hash)
|
|
|
|
payload = {
|
|
|
|
"bolt11": bolt11,
|
|
|
|
"payment_hash": payment_hash
|
|
|
|
}
|
|
|
|
return self.call("listpayments", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
2018-01-27 20:49:13 +01:00
|
|
|
def connect(self, peer_id, host=None, port=None):
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-01-27 20:49:13 +01:00
|
|
|
Connect to {peer_id} at {host} and {port}
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"id": peer_id,
|
|
|
|
"host": host,
|
|
|
|
"port": port
|
|
|
|
}
|
|
|
|
return self.call("connect", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
2018-02-14 14:17:31 +01:00
|
|
|
def listpeers(self, peerid=None, level=None):
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
|
|
|
Show current peers, if {level} is set, include {log}s"
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"id": peerid,
|
|
|
|
"level": level,
|
|
|
|
}
|
|
|
|
return self.call("listpeers", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
2018-02-14 14:17:31 +01:00
|
|
|
def fundchannel(self, channel_id, satoshi):
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
|
|
|
Fund channel with {id} using {satoshi} satoshis"
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"id": channel_id,
|
|
|
|
"satoshi": satoshi
|
|
|
|
}
|
|
|
|
return self.call("fundchannel", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def close(self, peer_id):
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
Close the channel with peer {id}
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"id": peer_id
|
|
|
|
}
|
|
|
|
return self.call("close", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def dev_sign_last_tx(self, peer_id):
|
|
|
|
"""
|
|
|
|
Sign and show the last commitment transaction with peer {id}
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"id": peer_id
|
|
|
|
}
|
|
|
|
return self.call("dev-sign-last-tx", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def dev_fail(self, peer_id):
|
|
|
|
"""
|
|
|
|
Fail with peer {peer_id}
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"id": peer_id
|
|
|
|
}
|
|
|
|
return self.call("dev-fail", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def dev_reenable_commit(self, peer_id):
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
Re-enable the commit timer on peer {id}
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"id": peer_id
|
|
|
|
}
|
|
|
|
return self.call("dev-reenable-commit", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def dev_ping(self, peer_id, length, pongbytes):
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
Send {peer_id} a ping of length {len} asking for {pongbytes}"
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"id": peer_id,
|
|
|
|
"len": length,
|
|
|
|
"pongbytes": pongbytes
|
|
|
|
}
|
|
|
|
return self.call("dev-ping", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def dev_memdump(self):
|
|
|
|
"""
|
|
|
|
Show memory objects currently in use
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
return self.call("dev-memdump")
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def dev_memleak(self):
|
|
|
|
"""
|
|
|
|
Show unreferenced memory objects
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
return self.call("dev-memleak")
|
2018-01-27 16:53:14 +01:00
|
|
|
|
|
|
|
def withdraw(self, destination, satoshi):
|
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
Send to {destination} address {satoshi} (or "all") amount via Bitcoin transaction
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
payload = {
|
|
|
|
"destination": destination,
|
|
|
|
"satoshi": satoshi
|
|
|
|
}
|
|
|
|
return self.call("withdraw", payload)
|
2018-01-27 16:53:14 +01:00
|
|
|
|
2018-02-14 14:17:31 +01:00
|
|
|
def newaddr(self, addresstype=None):
|
|
|
|
"""Get a new address of type {addresstype} of the internal wallet.
|
2018-01-27 16:53:14 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
return self.call("newaddr", {"addresstype": addresstype})
|
2017-04-12 20:08:48 +02:00
|
|
|
|
2018-01-27 16:53:14 +01:00
|
|
|
def listfunds(self):
|
|
|
|
"""
|
|
|
|
Show funds available for opening channels
|
2017-01-17 23:26:00 +01:00
|
|
|
"""
|
2018-02-14 14:17:31 +01:00
|
|
|
return self.call("listfunds")
|
|
|
|
|
|
|
|
def dev_rescan_outputs(self):
|
|
|
|
"""
|
|
|
|
Synchronize the state of our funds with bitcoind
|
|
|
|
"""
|
|
|
|
return self.call("dev-rescan-outputs")
|
|
|
|
|
|
|
|
def dev_forget_channel(self, peerid, force=False):
|
|
|
|
""" Forget the channel with id=peerid
|
|
|
|
"""
|
|
|
|
return self.call("dev-forget-channel", payload={"id": peerid, "force": force})
|