Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

Commit

Permalink
added support for the invoicesrpc subserver
Browse files Browse the repository at this point in the history
This commit adds support for hold invoices which
is a part of the invoicesrpc subserver
  • Loading branch information
takinbo committed Apr 9, 2019
1 parent 6d30b89 commit af8efae
Show file tree
Hide file tree
Showing 5 changed files with 545 additions and 4 deletions.
34 changes: 32 additions & 2 deletions lndgrpc/aio/async_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from lndgrpc.common import ln, BaseClient
from lndgrpc.common import invoices, ln, BaseClient
from lndgrpc.errors import handle_rpc_errors

MAJOR = sys.version_info[0]
Expand Down Expand Up @@ -64,14 +64,44 @@ async def list_invoices(self):

@handle_rpc_errors
async def subscribe_invoices(self):
await self._ln_stub.SubscribeInvoices(ln.InvoiceSubscription())
async for invoice in self._ln_stub.SubscribeInvoices(
ln.InvoiceSubscription()
):
yield invoice

@handle_rpc_errors
async def subscribe_single_invoice(self, r_hash):
request = ln.PaymentHash(r_hash=r_hash)
async for invoice in self._invoices_stub.SubscribeSingleInvoice(
request
):
yield invoice

@handle_rpc_errors
async def add_invoice(self, value, memo=''):
request = ln.Invoice(value=value, memo=memo)
response = await self._ln_stub.AddInvoice(request)
return response

@handle_rpc_errors
async def add_hold_invoice(self, hash, value=0, memo=''):
request = invoices.AddHoldInvoiceRequest(
hash=hash, value=value, memo=memo)
response = await self._invoices_stub.AddHoldInvoice(request)
return response

@handle_rpc_errors
async def settle_invoice(self, preimage):
request = invoices.SettleInvoiceMsg(preimage=preimage)
response = await self._invoices_stub.SettleInvoice(request)
return response

@handle_rpc_errors
async def cancel_invoice(self, payment_hash):
request = invoices.CancelInvoiceMsg(payment_hash=payment_hash)
response = await self._invoices_stub.CancelInvoice(request)
return response

@handle_rpc_errors
async def unlock(self, password):
"""Unlock encrypted wallet at lnd startup"""
Expand Down
27 changes: 26 additions & 1 deletion lndgrpc/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .common import ln, BaseClient
from .common import invoices, ln, BaseClient
from .errors import handle_rpc_errors


Expand Down Expand Up @@ -66,12 +66,37 @@ def subscribe_invoices(self):
for invoice in self._ln_stub.SubscribeInvoices(ln.InvoiceSubscription()):
yield invoice

@handle_rpc_errors
def subscribe_single_invoice(self, r_hash):
request = ln.PaymentHash(r_hash=r_hash)
for invoice in self._invoices_stub.SubscribeSingleInvoice(request):
yield invoice

@handle_rpc_errors
def add_invoice(self, value, memo=''):
request = ln.Invoice(value=value, memo=memo)
response = self._ln_stub.AddInvoice(request)
return response

@handle_rpc_errors
def add_hold_invoice(self, hash, value=0, memo=''):
request = invoices.AddHoldInvoiceRequest(
hash=hash, value=value, memo=memo)
response = self._invoices_stub.AddHoldInvoice(request)
return response

@handle_rpc_errors
def settle_invoice(self, preimage):
request = invoices.SettleInvoiceMsg(preimage=preimage)
response = self._invoices_stub.SettleInvoice(request)
return response

@handle_rpc_errors
def cancel_invoice(self, payment_hash):
request = invoices.CancelInvoiceMsg(payment_hash=payment_hash)
response = self._invoices_stub.CancelInvoice(request)
return response

@handle_rpc_errors
def new_address(self, address_type=0):
"""Generates a new witness address"""
Expand Down
16 changes: 15 additions & 1 deletion lndgrpc/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import platform
import os
import grpc
from . import rpc_pb2, rpc_pb2_grpc
from . import invoices_pb2, invoices_pb2_grpc, rpc_pb2, rpc_pb2_grpc

ln = rpc_pb2
lnrpc = rpc_pb2_grpc
invoices = invoices_pb2
invoicesrpc = invoices_pb2_grpc


system = platform.system().lower()
Expand Down Expand Up @@ -99,6 +101,18 @@ def __init__(self, ip_address='127.0.0.1:10009', network='mainnet', admin=False,
self._credentials = generate_credentials(cert, macaroon)
self.ip_address = ip_address

@property
def _invoices_stub(self):
"""Create a invoices_stub dynamically to ensure channel freshness
If we make a call to the Lightning RPC service when the wallet
is locked or the server is down we will get back an RPCError with
StatusCode.UNAVAILABLE which will make the channel unusable.
To ensure the channel is usable we create a new one for each request.
"""
channel = self.grpc_module.secure_channel(self.ip_address, self._credentials, options=[('grpc.max_receive_message_length',1024*1024*50)])
return invoicesrpc.InvoicesStub(channel)

@property
def _ln_stub(self):
"""Create a ln_stub dynamically to ensure channel freshness
Expand Down
Loading

0 comments on commit af8efae

Please sign in to comment.