Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into pyup-update-pytest-4.5.0-to-5.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
willcl-ark authored Jul 25, 2019
2 parents 53f7aa4 + ea88803 commit 2363975
Show file tree
Hide file tree
Showing 27 changed files with 2,011 additions and 1,570 deletions.
4 changes: 2 additions & 2 deletions .travis/travis_before_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sudo cp ${TRAVIS_BUILD_DIR}/bitcoin-${CORE_VERSION}/bin/bitcoin-cli /usr/local/b
# Install LND
#############

export LND_VERSION="v0.6.1-beta"
export LND_VERSION="v0.7.1-beta-rc1"

# Install LND
wget https://github.com/lightningnetwork/lnd/releases/download/${LND_VERSION}/lnd-linux-amd64-${LND_VERSION}.tar.gz
Expand All @@ -32,7 +32,7 @@ sudo cp ${TRAVIS_BUILD_DIR}/lnd-linux-amd64-${LND_VERSION}/lncli /usr/local/bin/
# Install loop
##############

export LOOP_VERSION="v0.1.2-alpha"
export LOOP_VERSION="v0.2.1-alpha"

# Install Loop
wget https://github.com/lightninglabs/loop/releases/download/${LOOP_VERSION}/loop-linux-amd64-${LOOP_VERSION}.tar.gz
Expand Down
62 changes: 38 additions & 24 deletions lnd_grpc/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from lnd_grpc.utilities import get_lnd_dir

# tell gRPC which cypher suite to use
environ["GRPC_SSL_CIPHER_SUITES"] = 'HIGH+ECDSA'
environ["GRPC_SSL_CIPHER_SUITES"] = "HIGH+ECDSA"


class BaseClient:
Expand All @@ -21,13 +21,15 @@ class BaseClient:
Has some static helper methods for various applications.
"""

def __init__(self,
lnd_dir: str = None,
macaroon_path: str = None,
tls_cert_path: str = None,
network: str = defaultNetwork,
grpc_host: str = defaultRPCHost,
grpc_port: str = defaultRPCPort):
def __init__(
self,
lnd_dir: str = None,
macaroon_path: str = None,
tls_cert_path: str = None,
network: str = defaultNetwork,
grpc_host: str = defaultRPCHost,
grpc_port: str = defaultRPCPort,
):

self.lnd_dir = lnd_dir
self.macaroon_path = macaroon_path
Expand Down Expand Up @@ -75,17 +77,19 @@ def tls_cert(self) -> bytes:
:return: tls.cert as bytestring
"""
try:
with open(self.tls_cert_path, 'rb') as r:
with open(self.tls_cert_path, "rb") as r:
_tls_cert = r.read()
except FileNotFoundError:
sys.stderr.write("TLS cert not found at %s" % self.tls_cert_path)
raise
try:
assert _tls_cert.startswith(b'-----BEGIN CERTIFICATE-----')
assert _tls_cert.startswith(b"-----BEGIN CERTIFICATE-----")
return _tls_cert
except (AssertionError, AttributeError):
sys.stderr.write("TLS cert at %s did not start with b'-----BEGIN CERTIFICATE-----')"
% self.tls_cert_path)
sys.stderr.write(
"TLS cert at %s did not start with b'-----BEGIN CERTIFICATE-----')"
% self.tls_cert_path
)
raise

@property
Expand All @@ -94,8 +98,11 @@ def macaroon_path(self) -> str:
:return: macaroon path
"""
if not self._macaroon_path:
self._macaroon_path = Path(self.lnd_dir) / f'{defaultDataDirname}/{defaultChainSubDirname}/bitcoin/' \
f'{self.network}/{defaultAdminMacFilename}'
self._macaroon_path = (
Path(self.lnd_dir)
/ f"{defaultDataDirname}/{defaultChainSubDirname}/bitcoin/"
f"{self.network}/{defaultAdminMacFilename}"
)
return str(self._macaroon_path)
else:
return self._macaroon_path
Expand All @@ -110,29 +117,34 @@ def macaroon(self):
try to open the macaroon and return it as a byte string
"""
try:
with open(self.macaroon_path, 'rb') as f:
with open(self.macaroon_path, "rb") as f:
macaroon_bytes = f.read()
macaroon = codecs.encode(macaroon_bytes, 'hex')
macaroon = codecs.encode(macaroon_bytes, "hex")
return macaroon
except FileNotFoundError:
sys.stderr.write(f"Could not find macaroon in {self.macaroon_path}. This might happen"
f"in versions of lnd < v0.5-beta or those not using default"
f"installation path. Set client object's macaroon_path attribute"
f"manually.")
sys.stderr.write(
f"Could not find macaroon in {self.macaroon_path}. This might happen"
f"in versions of lnd < v0.5-beta or those not using default"
f"installation path. Set client object's macaroon_path attribute"
f"manually."
)

def metadata_callback(self, context, callback):
"""
automatically incorporate the macaroon into all requests
:return: macaroon callback
"""
callback([('macaroon', self.macaroon)], None)
callback([("macaroon", self.macaroon)], None)

def connectivity_event_logger(self, channel_connectivity):
"""
Channel connectivity callback logger
"""
self.connection_status = channel_connectivity._name_
if self.connection_status == 'SHUTDOWN' or self.connection_status == 'TRANSIENT_FAILURE':
if (
self.connection_status == "SHUTDOWN"
or self.connection_status == "TRANSIENT_FAILURE"
):
self.connection_status_change = True

@property
Expand All @@ -147,15 +159,17 @@ def combined_credentials(self) -> grpc.CallCredentials:

@property
def grpc_address(self) -> str:
return str(self.grpc_host + ':' + self.grpc_port)
return str(self.grpc_host + ":" + self.grpc_port)

@staticmethod
def channel_point_generator(funding_txid, output_index):
"""
Generate a ln.ChannelPoint object from a funding_txid and output_index
:return: ln.ChannelPoint
"""
return ln.ChannelPoint(funding_txid_str=funding_txid, output_index=int(output_index))
return ln.ChannelPoint(
funding_txid_str=funding_txid, output_index=int(output_index)
)

@staticmethod
def lightning_address(pubkey, host):
Expand Down
8 changes: 5 additions & 3 deletions lnd_grpc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
defaultRESTPort = 8080
defaultPeerPort = 9735
defaultRPCHost = "localhost"
defaultNetwork = 'mainnet'
defaultNetwork = "mainnet"
defaultNoSeedBackup = False
defaultTorSOCKSPort = 9050
defaultTorDNSHost = "soa.nodes.lightning.directory"
Expand All @@ -27,5 +27,7 @@
defaultTorV3PrivateKeyFilename = "v3_onion_private_key"

# lnd_grpc default params
GRPC_OPTIONS = [('grpc.max_receive_message_length', 33554432),
('grpc.max_send_message_length', 33554432)]
GRPC_OPTIONS = [
("grpc.max_receive_message_length", 33554432),
("grpc.max_send_message_length", 33554432),
]
77 changes: 48 additions & 29 deletions lnd_grpc/invoices.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from lnd_grpc.config import defaultNetwork, defaultRPCHost, defaultRPCPort

# tell gRPC which cypher suite to use
environ["GRPC_SSL_CIPHER_SUITES"] = 'HIGH+ECDSA'
environ["GRPC_SSL_CIPHER_SUITES"] = "HIGH+ECDSA"


class Invoices(BaseClient):
Expand All @@ -18,35 +18,41 @@ class Invoices(BaseClient):
for hold invoice applications.
"""

def __init__(self,
lnd_dir: str = None,
macaroon_path: str = None,
tls_cert_path: str = None,
network: str = defaultNetwork,
grpc_host: str = defaultRPCHost,
grpc_port: str = defaultRPCPort):
def __init__(
self,
lnd_dir: str = None,
macaroon_path: str = None,
tls_cert_path: str = None,
network: str = defaultNetwork,
grpc_host: str = defaultRPCHost,
grpc_port: str = defaultRPCPort,
):
self._inv_stub: invrpc.InvoicesStub = None

super().__init__(lnd_dir=lnd_dir,
macaroon_path=macaroon_path,
tls_cert_path=tls_cert_path,
network=network,
grpc_host=grpc_host,
grpc_port=grpc_port)
super().__init__(
lnd_dir=lnd_dir,
macaroon_path=macaroon_path,
tls_cert_path=tls_cert_path,
network=network,
grpc_host=grpc_host,
grpc_port=grpc_port,
)

@property
def invoice_stub(self) -> invrpc.InvoicesStub:
if self._inv_stub is None:
ssl_creds = grpc.ssl_channel_credentials(self.tls_cert)
_inv_channel = grpc.secure_channel(target=self.grpc_address,
credentials=self.combined_credentials,
options=self.grpc_options)
_inv_channel = grpc.secure_channel(
target=self.grpc_address,
credentials=self.combined_credentials,
options=self.grpc_options,
)
self._inv_stub = invrpc.InvoicesStub(_inv_channel)
return self._inv_stub

def subscribe_single_invoice(self,
r_hash: bytes = b'',
r_hash_str: str = '') -> ln.Invoice:
def subscribe_single_invoice(
self, r_hash: bytes = b"", r_hash_str: str = ""
) -> ln.Invoice:
"""
Returns a uni-directional stream (server -> client) for notifying the client of invoice
state changes.
Expand All @@ -61,7 +67,7 @@ def subscribe_single_invoice(self,
response = self.invoice_stub.SubscribeSingleInvoice(request)
return response

def cancel_invoice(self, payment_hash: bytes = b'') -> inv.CancelInvoiceResp:
def cancel_invoice(self, payment_hash: bytes = b"") -> inv.CancelInvoiceResp:
"""
Cancels a currently open invoice. If the invoice is already canceled, this call will
succeed. If the invoice is already settled, it will fail.
Expand All @@ -75,10 +81,17 @@ def cancel_invoice(self, payment_hash: bytes = b'') -> inv.CancelInvoiceResp:
response = self.invoice_stub.CancelInvoice(request)
return response

def add_hold_invoice(self, memo: str = '', hash: bytes = b'', value: int = 0,
expiry: int = 3600, fallback_addr: str = '', cltv_expiry: int = 36,
route_hints: ln.RouteHint = [], private: bool = 1) \
-> inv.AddHoldInvoiceResp:
def add_hold_invoice(
self,
memo: str = "",
hash: bytes = b"",
value: int = 0,
expiry: int = 3600,
fallback_addr: str = "",
cltv_expiry: int = 36,
route_hints: ln.RouteHint = [],
private: bool = 1,
) -> inv.AddHoldInvoiceResp:
"""
Attempts to add a new hold invoice to the invoice database. Any duplicated invoices are
rejected, therefore all invoices *must* have a unique payment hash.
Expand All @@ -92,13 +105,19 @@ def add_hold_invoice(self, memo: str = '', hash: bytes = b'', value: int = 0,
:return: AddHoldInvoiceResponse with 1 attribute: 'payment_request'
"""
request = inv.AddHoldInvoiceRequest(
memo=memo, hash=hash, value=value, expiry=expiry,
fallback_addr=fallback_addr, cltv_expiry=cltv_expiry,
route_hints=route_hints, private=private)
memo=memo,
hash=hash,
value=value,
expiry=expiry,
fallback_addr=fallback_addr,
cltv_expiry=cltv_expiry,
route_hints=route_hints,
private=private,
)
response = self.invoice_stub.AddHoldInvoice(request)
return response

def settle_invoice(self, preimage: bytes = b'') -> inv.SettleInvoiceResp:
def settle_invoice(self, preimage: bytes = b"") -> inv.SettleInvoiceResp:
"""
Settles an accepted invoice. If the invoice is already settled, this call will succeed.
Expand Down
Loading

0 comments on commit 2363975

Please sign in to comment.