Skip to content

Commit

Permalink
Project import generated by Copybara.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 377b0ab32b4de60755ef95439c0c0a1df060cabf
  • Loading branch information
Lightspark Eng authored and jklein24 committed Sep 18, 2023
1 parent cdca495 commit dcd6237
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 23 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
44 changes: 24 additions & 20 deletions lightspark/lightspark_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand All @@ -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,
Expand All @@ -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"]
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lightspark/scripts/create_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lightspark/scripts/create_lnurl_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lightspark/scripts/create_uma_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lightspark/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.4.2"
__version__ = "1.4.3"
2 changes: 1 addition & 1 deletion uma/uma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion uma/uma_invoice_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit dcd6237

Please sign in to comment.