Mega-merge 2: Invoice/refactor description hash (WIP) (#814)

* description hashing in backend
This commit is contained in:
calle 2022-08-01 16:20:25 +02:00 committed by GitHub
parent c88e6b0e62
commit f1ec7e33f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 40 additions and 41 deletions

View File

@ -184,11 +184,13 @@ async def api_payments_create_invoice(data: CreateInvoiceData, wallet: Wallet):
lnurl_response: Union[None, bool, str] = None
if data.lnurl_callback:
if "lnurl_balance_check" in data:
assert (
data.lnurl_balance_check is not None
), "lnurl_balance_check is required"
if data.lnurl_balance_check is not None:
await save_balance_check(wallet.id, data.lnurl_balance_check)
else:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="lnurl_balance_check not set.",
)
async with httpx.AsyncClient() as client:
try:

View File

@ -73,11 +73,9 @@ async def lnurl_callback(
wallet_id=cp.wallet,
amount=int(amount_received / 1000),
memo=cp.lnurl_title,
description_hash=hashlib.sha256(
(
LnurlPayMetadata(json.dumps([["text/plain", str(cp.lnurl_title)]]))
).encode("utf-8")
).digest(),
description_hash=(
LnurlPayMetadata(json.dumps([["text/plain", str(cp.lnurl_title)]]))
).encode("utf-8"),
extra={"tag": "copilot", "copilotid": cp.id, "comment": comment},
)
payResponse = {"pr": payment_request, "routes": []}

View File

@ -90,9 +90,7 @@ async def lnurl_callback(
wallet_id=ls.wallet,
amount=int(amount_received / 1000),
memo=await track.fullname(),
description_hash=hashlib.sha256(
(await track.lnurlpay_metadata()).encode("utf-8")
).digest(),
description_hash=(await track.lnurlpay_metadata()).encode("utf-8"),
extra={"tag": "livestream", "track": track.id, "comment": comment},
)

View File

@ -70,11 +70,9 @@ async def lnurl_callback(address_id, amount: int = Query(...)):
json={
"out": False,
"amount": int(amount_received / 1000),
"description_hash": hashlib.sha256(
(await address.lnurlpay_metadata(domain=domain.domain)).encode(
"utf-8"
)
).hexdigest(),
"description_hash": (
await address.lnurlpay_metadata(domain=domain.domain)
).encode("utf-8"),
"extra": {"tag": f"Payment to {address.username}@{domain.domain}"},
},
timeout=40,

View File

@ -205,9 +205,7 @@ async def lnurl_callback(
wallet_id=device.wallet,
amount=lnurldevicepayment.sats / 1000,
memo=device.title,
description_hash=hashlib.sha256(
(await device.lnurlpay_metadata()).encode("utf-8")
).digest(),
description_hash=(await device.lnurlpay_metadata()).encode("utf-8"),
extra={"tag": "PoS"},
)
lnurldevicepayment = await update_lnurldevicepayment(

View File

@ -87,9 +87,7 @@ async def api_lnurl_callback(request: Request, link_id):
wallet_id=link.wallet,
amount=int(amount_received / 1000),
memo=link.description,
description_hash=hashlib.sha256(
link.lnurlpay_metadata.encode("utf-8")
).digest(),
description_hash=link.lnurlpay_metadata.encode("utf-8"),
extra={
"tag": "lnurlp",
"link": link.id,

View File

@ -73,9 +73,7 @@ async def lnurl_callback(request: Request, item_id: int):
wallet_id=shop.wallet,
amount=int(amount_received / 1000),
memo=item.name,
description_hash=hashlib.sha256(
(await item.lnurlpay_metadata()).encode("utf-8")
).digest(),
description_hash=(await item.lnurlpay_metadata()).encode("utf-8"),
extra={"tag": "offlineshop", "item": item.id},
)
except Exception as exc:

View File

@ -77,9 +77,7 @@ async def api_lnurlp_callback(
wallet_id=link.wallet,
amount=int(amount_received / 1000),
memo="Satsdice bet",
description_hash=hashlib.sha256(
link.lnurlpay_metadata.encode("utf-8")
).digest(),
description_hash=link.lnurlpay_metadata.encode("utf-8"),
extra={"tag": "satsdice", "link": link.id, "comment": "comment"},
)

View File

@ -1,4 +1,5 @@
import asyncio
import hashlib
import json
from os import getenv
from typing import AsyncGenerator, Dict, Optional
@ -47,9 +48,10 @@ class ClicheWallet(Wallet):
description_hash: Optional[bytes] = None,
) -> InvoiceResponse:
if description_hash:
description_hash_hashed = hashlib.sha256(description_hash).hexdigest()
ws = create_connection(self.endpoint)
ws.send(
f"create-invoice --msatoshi {amount*1000} --description_hash {description_hash.hex()}"
f"create-invoice --msatoshi {amount*1000} --description_hash {description_hash_hashed}"
)
r = ws.recv()
else:

View File

@ -4,6 +4,7 @@ except ImportError: # pragma: nocover
LightningRpc = None
import asyncio
import hashlib
import random
import time
from functools import partial, wraps
@ -94,7 +95,7 @@ class CLightningWallet(Wallet):
if not self.supports_description_hash:
raise Unsupported("description_hash")
params = [msat, label, description_hash.hex()]
params = [msat, label, hashlib.sha256(description_hash).hexdigest()]
r = self.ln.call("invoicewithdescriptionhash", params)
return InvoiceResponse(True, label, r["bolt11"], "")
else:

View File

@ -1,5 +1,6 @@
import asyncio
import base64
import hashlib
import json
import urllib.parse
from os import getenv
@ -72,7 +73,7 @@ class EclairWallet(Wallet):
data: Dict = {"amountMsat": amount * 1000}
if description_hash:
data["description_hash"] = description_hash.hex()
data["description_hash"] = hashlib.sha256(description_hash).hexdigest()
else:
data["description"] = memo or ""

View File

@ -61,7 +61,7 @@ class FakeWallet(Wallet):
data["timestamp"] = datetime.now().timestamp()
if description_hash:
data["tags_set"] = ["h"]
data["description_hash"] = description_hash.hex()
data["description_hash"] = description_hash.decode("utf-8")
else:
data["tags_set"] = ["d"]
data["memo"] = memo

View File

@ -1,4 +1,5 @@
import asyncio
import hashlib
import json
from os import getenv
from typing import AsyncGenerator, Dict, Optional
@ -59,7 +60,7 @@ class LNbitsWallet(Wallet):
) -> InvoiceResponse:
data: Dict = {"out": False, "amount": amount}
if description_hash:
data["description_hash"] = description_hash.hex()
data["description_hash"] = hashlib.sha256(description_hash).hexdigest()
else:
data["memo"] = memo or ""

View File

@ -132,7 +132,9 @@ class LndWallet(Wallet):
params: Dict = {"value": amount, "expiry": 600, "private": True}
if description_hash:
params["description_hash"] = description_hash # as bytes directly
params["description_hash"] = base64.b64encode(
hashlib.sha256(description_hash).digest()
) # as bytes directly
else:
params["memo"] = memo or ""

View File

@ -1,5 +1,6 @@
import asyncio
import base64
import hashlib
import json
from os import getenv
from pydoc import describe
@ -75,9 +76,9 @@ class LndRestWallet(Wallet):
) -> InvoiceResponse:
data: Dict = {"value": amount, "private": True}
if description_hash:
data["description_hash"] = base64.b64encode(description_hash).decode(
"ascii"
)
data["description_hash"] = base64.b64encode(
hashlib.sha256(description_hash).digest()
).decode("ascii")
else:
data["memo"] = memo or ""

View File

@ -1,4 +1,5 @@
import asyncio
import hashlib
import json
from http import HTTPStatus
from os import getenv
@ -54,7 +55,7 @@ class LNPayWallet(Wallet):
) -> InvoiceResponse:
data: Dict = {"num_satoshis": f"{amount}"}
if description_hash:
data["description_hash"] = description_hash.hex()
data["description_hash"] = hashlib.sha256(description_hash).hexdigest()
else:
data["memo"] = memo or ""

View File

@ -1,4 +1,5 @@
import asyncio
import hashlib
import json
from os import getenv
from typing import AsyncGenerator, Dict, Optional
@ -54,7 +55,7 @@ class LntxbotWallet(Wallet):
) -> InvoiceResponse:
data: Dict = {"amt": str(amount)}
if description_hash:
data["description_hash"] = description_hash.hex()
data["description_hash"] = hashlib.sha256(description_hash).hexdigest()
else:
data["memo"] = memo or ""

View File

@ -1,4 +1,5 @@
import asyncio
import hashlib
import json
import random
from os import getenv
@ -101,7 +102,7 @@ class SparkWallet(Wallet):
r = await self.invoicewithdescriptionhash(
msatoshi=amount * 1000,
label=label,
description_hash=description_hash.hex(),
description_hash=hashlib.sha256(description_hash).hexdigest(),
)
else:
r = await self.invoice(