From dcd6237fb8c59bde6653fa962ae923da4471a213 Mon Sep 17 00:00:00 2001 From: Lightspark Eng Date: Mon, 18 Sep 2023 13:40:33 -0700 Subject: [PATCH] Project import generated by Copybara. GitOrigin-RevId: 377b0ab32b4de60755ef95439c0c0a1df060cabf --- CHANGELOG.md | 7 ++++ lightspark/lightspark_client.py | 44 ++++++++++++---------- lightspark/scripts/create_invoice.py | 2 + lightspark/scripts/create_lnurl_invoice.py | 2 + lightspark/scripts/create_uma_invoice.py | 2 + lightspark/version.py | 2 +- uma/uma.py | 2 +- uma/uma_invoice_creator.py | 5 ++- 8 files changed, 43 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e47be4..6072321 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +# v1.4.3 + +- Fix a few UMA bugs/ommisions: + - Add expiry_secs to invoice calls + - Parse create_uma_invoice correctly + - Add fees to the invoice amount + # v1.4.2 - Fix a packaging problem with UMA. diff --git a/lightspark/lightspark_client.py b/lightspark/lightspark_client.py index adf63ab..6f06bad 100644 --- a/lightspark/lightspark_client.py +++ b/lightspark/lightspark_client.py @@ -137,17 +137,18 @@ def create_invoice( amount_msats: int, memo: Optional[str] = None, invoice_type: Optional[InvoiceType] = None, + expiry_secs: Optional[int] = None, ) -> Invoice: logger.info("Creating an invoice for node %s.", node_id) - json = self._requester.execute_graphql( - CREATE_INVOICE_MUTATION, - { - "amount_msats": amount_msats, - "node_id": node_id, - "memo": memo, - "invoice_type": invoice_type, - }, - ) + variables = { + "amount_msats": amount_msats, + "node_id": node_id, + "memo": memo, + "invoice_type": invoice_type, + } + if expiry_secs is not None: + variables["expiry_secs"] = expiry_secs + json = self._requester.execute_graphql(CREATE_INVOICE_MUTATION, variables) return Invoice_from_json(self._requester, json["create_invoice"]["invoice"]) @@ -156,6 +157,7 @@ def create_lnurl_invoice( node_id: str, amount_msats: int, metadata: str, + expiry_secs: Optional[int] = None, ) -> Invoice: """Generates a Lightning Invoice (follows the Bolt 11 specification) to request a payment from another Lightning Node. This should only be used for generating invoices for LNURLs, @@ -168,19 +170,21 @@ def create_lnurl_invoice( metadata (str): The LNURL metadata payload field in the initial payreq response. This will be hashed and present in the h-tag (SHA256 purpose of payment) of the resulting Bolt 11 invoice. + expiry_secs (int, optional): The number of seconds after which the invoice will expire. + Defaults to 1 day. Returns: Invoice: An `Invoice` object representing the generated invoice. """ logger.info("Creating an lnurl invoice for node %s.", node_id) - json = self._requester.execute_graphql( - CREATE_LNURL_INVOICE_MUTATION, - { - "amount_msats": amount_msats, - "node_id": node_id, - "metadata_hash": sha256(metadata.encode("utf-8")).hexdigest(), - }, - ) + variables = { + "amount_msats": amount_msats, + "node_id": node_id, + "metadata_hash": sha256(metadata.encode("utf-8")).hexdigest(), + } + if expiry_secs is not None: + variables["expiry_secs"] = expiry_secs + json = self._requester.execute_graphql(CREATE_LNURL_INVOICE_MUTATION, variables) return Invoice_from_json( self._requester, json["create_lnurl_invoice"]["invoice"] @@ -244,6 +248,7 @@ def create_uma_invoice( node_id: str, amount_msats: int, metadata: str, + expiry_secs: Optional[int] = None, ) -> Invoice: logger.info("Creating an uma invoice for node %s.", node_id) json = self._requester.execute_graphql( @@ -252,12 +257,11 @@ def create_uma_invoice( "amount_msats": amount_msats, "node_id": node_id, "metadata_hash": sha256(metadata.encode("utf-8")).hexdigest(), + "expiry_secs": expiry_secs if expiry_secs is not None else 600, }, ) - return Invoice_from_json( - self._requester, json["create_lnurl_invoice"]["invoice"] - ) + return Invoice_from_json(self._requester, json["create_uma_invoice"]["invoice"]) def delete_api_token(self, api_token_id: str) -> None: logger.info("Deleting API token %s.", api_token_id) diff --git a/lightspark/scripts/create_invoice.py b/lightspark/scripts/create_invoice.py index 3504614..e8c4001 100644 --- a/lightspark/scripts/create_invoice.py +++ b/lightspark/scripts/create_invoice.py @@ -8,12 +8,14 @@ $amount_msats: Long! $memo: String $invoice_type: InvoiceType + $expiry_secs: Int ) {{ create_invoice(input: {{ node_id: $node_id amount_msats: $amount_msats memo: $memo invoice_type: $invoice_type + expiry_secs: $expiry_secs }}) {{ invoice {{ ...InvoiceFragment diff --git a/lightspark/scripts/create_lnurl_invoice.py b/lightspark/scripts/create_lnurl_invoice.py index aa6667f..c520e88 100644 --- a/lightspark/scripts/create_lnurl_invoice.py +++ b/lightspark/scripts/create_lnurl_invoice.py @@ -7,11 +7,13 @@ $node_id: ID! $amount_msats: Long! $metadata_hash: String! + $expiry_secs: Int ) {{ create_lnurl_invoice(input: {{ node_id: $node_id amount_msats: $amount_msats metadata_hash: $metadata_hash + expiry_secs: $expiry_secs }}) {{ invoice {{ ...InvoiceFragment diff --git a/lightspark/scripts/create_uma_invoice.py b/lightspark/scripts/create_uma_invoice.py index 46ce0e7..62d0af5 100644 --- a/lightspark/scripts/create_uma_invoice.py +++ b/lightspark/scripts/create_uma_invoice.py @@ -7,11 +7,13 @@ $node_id: ID! $amount_msats: Long! $metadata_hash: String! + $expiry_secs: Int ) {{ create_uma_invoice(input: {{ node_id: $node_id amount_msats: $amount_msats metadata_hash: $metadata_hash + expiry_secs: $expiry_secs }}) {{ invoice {{ ...InvoiceFragment diff --git a/lightspark/version.py b/lightspark/version.py index daa50c7..aa56ed4 100644 --- a/lightspark/version.py +++ b/lightspark/version.py @@ -1 +1 @@ -__version__ = "1.4.2" +__version__ = "1.4.3" diff --git a/uma/uma.py b/uma/uma.py index 013aa51..5aa8443 100644 --- a/uma/uma.py +++ b/uma/uma.py @@ -304,7 +304,7 @@ def create_pay_req_response( utxo_callback: the URL that the receiving VASP will call to send UTXOs of the channel that the receiver used to receive the payment once it completes. """ - amount_msats = request.amount * msats_per_currency_unit + amount_msats = request.amount * msats_per_currency_unit + receiver_fees_msats metadata += "{" + request.payer_data.to_json() + "}" encoded_invoice = invoice_creator.create_uma_invoice( amount_msats=amount_msats, diff --git a/uma/uma_invoice_creator.py b/uma/uma_invoice_creator.py index 6d78182..38024ed 100644 --- a/uma/uma_invoice_creator.py +++ b/uma/uma_invoice_creator.py @@ -33,6 +33,9 @@ def create_uma_invoice( metadata: str, ) -> str: invoice = self.lightspark_client.create_uma_invoice( - self.node_id, amount_msats=amount_msats, metadata=metadata + self.node_id, + amount_msats=amount_msats, + metadata=metadata, + expiry_secs=self.expiry_secs, ) return invoice.data.encoded_payment_request