feat: send invoice back for order
This commit is contained in:
parent
bee52340a2
commit
35298a4f44
4 changed files with 60 additions and 12 deletions
|
|
@ -31,7 +31,7 @@ def decrypt_message(encoded_message: str, encryption_key) -> str:
|
||||||
return unpadded_data.decode()
|
return unpadded_data.decode()
|
||||||
|
|
||||||
|
|
||||||
def encrypt_message(message: str, encryption_key, iv: Optional[bytes]) -> str:
|
def encrypt_message(message: str, encryption_key, iv: Optional[bytes] = None) -> str:
|
||||||
padder = padding.PKCS7(128).padder()
|
padder = padding.PKCS7(128).padder()
|
||||||
padded_data = padder.update(message.encode()) + padder.finalize()
|
padded_data = padder.update(message.encode()) + padder.finalize()
|
||||||
|
|
||||||
|
|
|
||||||
40
models.py
40
models.py
|
|
@ -8,7 +8,12 @@ from pydantic import BaseModel
|
||||||
|
|
||||||
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis
|
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis
|
||||||
|
|
||||||
from .helpers import decrypt_message, get_shared_secret, sign_message_hash
|
from .helpers import (
|
||||||
|
decrypt_message,
|
||||||
|
encrypt_message,
|
||||||
|
get_shared_secret,
|
||||||
|
sign_message_hash,
|
||||||
|
)
|
||||||
from .nostr.event import NostrEvent
|
from .nostr.event import NostrEvent
|
||||||
|
|
||||||
######################################## NOSTR ########################################
|
######################################## NOSTR ########################################
|
||||||
|
|
@ -45,6 +50,24 @@ class Merchant(PartialMerchant):
|
||||||
encryption_key = get_shared_secret(self.private_key, public_key)
|
encryption_key = get_shared_secret(self.private_key, public_key)
|
||||||
return decrypt_message(encrypted_message, encryption_key)
|
return decrypt_message(encrypted_message, encryption_key)
|
||||||
|
|
||||||
|
def encrypt_message(self, clear_text_message: str, public_key: str) -> str:
|
||||||
|
encryption_key = get_shared_secret(self.private_key, public_key)
|
||||||
|
return encrypt_message(clear_text_message, encryption_key)
|
||||||
|
|
||||||
|
def build_dm_event(self, message: str, to_pubkey: str) -> NostrEvent:
|
||||||
|
content = self.encrypt_message(message, to_pubkey)
|
||||||
|
event = NostrEvent(
|
||||||
|
pubkey=self.public_key,
|
||||||
|
created_at=round(time.time()),
|
||||||
|
kind=4,
|
||||||
|
tags=[["p", to_pubkey]],
|
||||||
|
content=content,
|
||||||
|
)
|
||||||
|
event.id = event.event_id
|
||||||
|
event.sig = self.sign_hash(bytes.fromhex(event.id))
|
||||||
|
|
||||||
|
return event
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_row(cls, row: Row) -> "Merchant":
|
def from_row(cls, row: Row) -> "Merchant":
|
||||||
merchant = cls(**dict(row))
|
merchant = cls(**dict(row))
|
||||||
|
|
@ -242,6 +265,9 @@ class PartialOrder(BaseModel):
|
||||||
items: List[OrderItem]
|
items: List[OrderItem]
|
||||||
contact: Optional[OrderContact]
|
contact: Optional[OrderContact]
|
||||||
|
|
||||||
|
def validate_order(self):
|
||||||
|
assert len(self.items) != 0, f"Order has no items. Order: '{self.id}'"
|
||||||
|
|
||||||
async def total_sats(self, products: List[Product]) -> float:
|
async def total_sats(self, products: List[Product]) -> float:
|
||||||
product_prices = {}
|
product_prices = {}
|
||||||
for p in products:
|
for p in products:
|
||||||
|
|
@ -281,3 +307,15 @@ class PaymentRequest(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
message: Optional[str]
|
message: Optional[str]
|
||||||
payment_options: List[PaymentOption]
|
payment_options: List[PaymentOption]
|
||||||
|
|
||||||
|
def to_nostr_event(self, author_pubkey: str, to_pubkey: str) -> NostrEvent:
|
||||||
|
event = NostrEvent(
|
||||||
|
pubkey=author_pubkey,
|
||||||
|
created_at=round(time.time()),
|
||||||
|
kind=4,
|
||||||
|
tags=[["p", to_pubkey]],
|
||||||
|
content=json.dumps(self.dict(), separators=(",", ":"), ensure_ascii=False),
|
||||||
|
)
|
||||||
|
event.id = event.event_id
|
||||||
|
|
||||||
|
return event
|
||||||
|
|
|
||||||
29
tasks.py
29
tasks.py
|
|
@ -9,19 +9,18 @@ from websocket import WebSocketApp
|
||||||
|
|
||||||
from lnbits.core import get_wallet
|
from lnbits.core import get_wallet
|
||||||
from lnbits.core.models import Payment
|
from lnbits.core.models import Payment
|
||||||
from lnbits.extensions.nostrmarket.models import PartialOrder
|
from lnbits.helpers import Optional, url_for
|
||||||
from lnbits.helpers import url_for
|
|
||||||
from lnbits.tasks import register_invoice_listener
|
from lnbits.tasks import register_invoice_listener
|
||||||
|
|
||||||
from .crud import (
|
from .crud import (
|
||||||
get_merchant_by_pubkey,
|
get_merchant_by_pubkey,
|
||||||
get_product,
|
|
||||||
get_public_keys_for_merchants,
|
get_public_keys_for_merchants,
|
||||||
get_wallet_for_product,
|
get_wallet_for_product,
|
||||||
)
|
)
|
||||||
from .helpers import order_from_json
|
from .helpers import order_from_json
|
||||||
|
from .models import PartialOrder
|
||||||
from .nostr.event import NostrEvent
|
from .nostr.event import NostrEvent
|
||||||
from .nostr.nostr_client import connect_to_nostrclient_ws
|
from .nostr.nostr_client import connect_to_nostrclient_ws, publish_nostr_event
|
||||||
|
|
||||||
|
|
||||||
async def wait_for_paid_invoices():
|
async def wait_for_paid_invoices():
|
||||||
|
|
@ -94,21 +93,28 @@ async def handle_message(msg: str):
|
||||||
assert merchant, f"Merchant not found for public key '{public_key}'"
|
assert merchant, f"Merchant not found for public key '{public_key}'"
|
||||||
|
|
||||||
clear_text_msg = merchant.decrypt_message(event.content, event.pubkey)
|
clear_text_msg = merchant.decrypt_message(event.content, event.pubkey)
|
||||||
await handle_nip04_message(event.pubkey, event.id, clear_text_msg)
|
dm_resp = await handle_dirrect_message(
|
||||||
|
event.pubkey, event.id, clear_text_msg
|
||||||
|
)
|
||||||
|
if dm_resp:
|
||||||
|
dm_event = merchant.build_dm_event(dm_resp, event.pubkey)
|
||||||
|
await publish_nostr_event(dm_event)
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.warning(ex)
|
logger.warning(ex)
|
||||||
|
|
||||||
|
|
||||||
async def handle_nip04_message(from_pubkey: str, event_id: str, msg: str):
|
async def handle_dirrect_message(
|
||||||
|
from_pubkey: str, event_id: str, msg: str
|
||||||
|
) -> Optional[str]:
|
||||||
order, text_msg = order_from_json(msg)
|
order, text_msg = order_from_json(msg)
|
||||||
try:
|
try:
|
||||||
if order:
|
if order:
|
||||||
print("### order", from_pubkey, event_id, msg)
|
|
||||||
### check that event_id not parsed already
|
### check that event_id not parsed already
|
||||||
order["pubkey"] = from_pubkey
|
order["pubkey"] = from_pubkey
|
||||||
order["event_id"] = event_id
|
order["event_id"] = event_id
|
||||||
partial_order = PartialOrder(**order)
|
partial_order = PartialOrder(**order)
|
||||||
|
partial_order.validate_order()
|
||||||
assert len(partial_order.items) != 0, "Order has no items. Order: " + msg
|
assert len(partial_order.items) != 0, "Order has no items. Order: " + msg
|
||||||
|
|
||||||
first_product_id = partial_order.items[0].product_id
|
first_product_id = partial_order.items[0].product_id
|
||||||
|
|
@ -131,9 +137,14 @@ async def handle_nip04_message(from_pubkey: str, event_id: str, msg: str):
|
||||||
)
|
)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
data = resp.json()
|
data = resp.json()
|
||||||
|
return (
|
||||||
print("### payment request", data)
|
json.dumps(data, separators=(",", ":"), ensure_ascii=False)
|
||||||
|
if data
|
||||||
|
else None
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
print("### text_msg", text_msg)
|
print("### text_msg", text_msg)
|
||||||
|
return None
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.warning(ex)
|
logger.warning(ex)
|
||||||
|
return None
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import json
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue