-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support VPN tunnel + Send kind 9735 zap receipt (#4)
- Loading branch information
Showing
6 changed files
with
181 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,8 @@ VPN_HOST=''# <YOUR_HOST_IP> for you VPN client | |
LND_REST_PORT=8080 #Default LND REST port is 8080, in most cases you can leave this untouched | ||
LND_INVOICE_MACAROON_HEX="<YOUR_LND_INVOICE_HEX_HERE>" | ||
INTERNET_IDENTIFIER="<IDENTIFIER_HERE>" ## Add the value on the left side of your LNURL identifier for example if your LNURL identifier is "[email protected]" you would add "nabismo" here | ||
HEX_PUBKEY=# <YOUR_NOSTR_HEX_PUBKEY> | ||
HEX_PUBKEY='' # <YOUR_NOSTR_HEX_PUBKEY> | ||
HEX_PRIV_KEY='' #<YOUR_NOSTR_HEX_PRIV_KEY> | ||
DOMAIN="<YOUR_DOMAIN_HERE>" # For example nostpy.lol | ||
CONTACT=<YOUR_EMAIL_ADDRESS> #Enter your email address for the certbot command to get emails about your TLS certificate when it is near expiration | ||
NGINX_FILE_PATH=/etc/nginx/sites-available/default #Leave this untouched |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ Flask==3.0.0 | |
requests==2.31.0 | ||
PySocks==1.7.1 | ||
python-dotenv==0.19.2 | ||
ddtrace | ||
ddtrace | ||
secp256k1 | ||
websocket-client |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import hashlib | ||
import json | ||
import logging | ||
import secp256k1 | ||
import time | ||
from websocket import create_connection | ||
|
||
|
||
class NostpyClient: | ||
def __init__(self, relays, pubkey, privkey, nostr_event, response) -> None: | ||
self.relays = relays | ||
self.pubkey = pubkey | ||
self.privkey = privkey | ||
self.kind9734 = nostr_event | ||
self.created_at = response["settle_date"] | ||
self.zap_reciept_tags = [ | ||
["description", json.dumps(nostr_event)], | ||
["bolt11", response["payment_request"]], | ||
["preimage", response["r_preimage"]], | ||
] | ||
|
||
def sign_event_id(self, event_id: str, private_key_hex: str) -> str: | ||
private_key = secp256k1.PrivateKey(bytes.fromhex(private_key_hex)) | ||
sig = private_key.schnorr_sign( | ||
bytes.fromhex(event_id), bip340tag=None, raw=True | ||
) | ||
return sig.hex() | ||
|
||
def calc_event_id( | ||
self, | ||
public_key: str, | ||
created_at: int, | ||
kind_number: int, | ||
tags: list, | ||
content: str, | ||
) -> str: | ||
data = [0, public_key, created_at, kind_number, tags, content] | ||
data_str = json.dumps(data, separators=(",", ":"), ensure_ascii=False) | ||
return hashlib.sha256(data_str.encode("UTF-8")).hexdigest() | ||
|
||
def parse_tags(self, logger): | ||
try: | ||
tag_list = [tag_pair for tag_pair in self.kind9734["tags"]] | ||
tag_list.append(self.zap_reciept_tags) | ||
return tag_list | ||
except Exception as exc: | ||
logger.error(f"Error parsing kind 9735 tags: {exc}") | ||
|
||
def create_event(self, kind_number, logger): | ||
kind_9735_tags = self.parse_tags(logger) | ||
content = "" | ||
event_id = self.calc_event_id( | ||
self.pubkey, self.created_at, kind_number, kind_9735_tags, content | ||
) | ||
signature_hex = self.sign_event_id(event_id, self.privkey) | ||
event_data = { | ||
"id": event_id, | ||
"pubkey": self.pubkey, | ||
"kind": kind_number, | ||
"created_at": self.created_at, | ||
"tags": kind_9735_tags, | ||
"content": content, | ||
"sig": signature_hex, | ||
} | ||
|
||
return event_data | ||
|
||
def verify_signature(self, event_id: str, pubkey: str, sig: str, logger) -> bool: | ||
try: | ||
pub_key = secp256k1.PublicKey(bytes.fromhex("02" + pubkey), True) | ||
result = pub_key.schnorr_verify( | ||
bytes.fromhex(event_id), bytes.fromhex(sig), None, raw=True | ||
) | ||
if result: | ||
logger.info(f"Verification successful for event: {event_id}") | ||
else: | ||
logger.error(f"Verification failed for event: {event_id}") | ||
return result | ||
except (ValueError, TypeError) as e: | ||
logger.error(f"Error verifying signature for event {event_id}: {e}") | ||
return False | ||
|
||
def send_event(self, ws_relay, logger): | ||
try: | ||
ws = create_connection(ws_relay) | ||
logger.info("WebSocket connection created.") | ||
event_data = self.create_event(9735, logger) | ||
sig = event_data["sig"] | ||
id = event_data["id"] | ||
signature_valid = self.verify_signature(id, self.pubkey, sig, logger) | ||
if signature_valid: | ||
event_json = json.dumps(("EVENT", event_data)) | ||
ws.send(event_json) | ||
logger.debug(f"Event sent: {event_json}") | ||
else: | ||
logger.error("Invalid signature, event not sent.") | ||
ws.close() | ||
logger.info("WebSocket connection closed.") | ||
except Exception as exc: | ||
logger.error(f"Error sending ws event: {exc}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters