subscriptions / sdk update work in progress

This commit is contained in:
Believethehype
2024-03-18 19:44:44 +01:00
parent 53658548e3
commit 41785313a4
45 changed files with 1312 additions and 307 deletions

View File

@@ -1,44 +1,84 @@
import json
import os
from datetime import timedelta
import requests
from nostr_sdk import Keys, PublicKey, Client, nip04_encrypt, EventBuilder, Tag, NostrSigner
from nostr_sdk import Keys, PublicKey, Client, nip04_encrypt, EventBuilder, Tag, NostrSigner, Filter, Timestamp, \
NostrWalletConnectUri, Nwc
from nostr_dvm.utils.dvmconfig import DVMConfig
from nostr_dvm.utils.nostr_utils import check_and_set_private_key
from nostr_dvm.utils.zap_utils import zaprequest
def nwc_zap(connectionstr, bolt11, keys):
target_pubkey, relay, secret = parse_connection_str(connectionstr)
SecretSK = Keys.parse(secret)
def nwc_zap(connectionstr, bolt11, keys, externalrelay=None):
uri = NostrWalletConnectUri.parse(connectionstr)
content = {
"method": "pay_invoice",
"params": {
"invoice": bolt11
}
}
# Initialize NWC client
nwc = Nwc(uri)
signer = NostrSigner.keys(keys)
client = Client(signer)
client.add_relay(relay)
client.connect()
info = nwc.get_info()
print(info)
client_public_key = PublicKey.from_hex(target_pubkey)
encrypted_content = nip04_encrypt(SecretSK.secret_key(), client_public_key, json.dumps(content))
balance = nwc.get_balance()
print(f"Balance: {balance} SAT")
pTag = Tag.parse(["p", client_public_key.to_hex()])
event = EventBuilder(23194, encrypted_content,
[pTag]).to_event(keys)
event_id = nwc.pay_invoice(bolt11)
print("NWC event: " + event_id)
event_id = client.send_event(event)
print(event_id.to_hex())
#target_pubkey, relay, secret = parse_connection_str(connectionstr)
#print(target_pubkey)
#print(relay)
#print(secret)
#SecretSK = Keys.parse(secret)
#content = {
# "method": "pay_invoice",
# "params": {
# "invoice": bolt11
# }
#}
#signer = NostrSigner.keys(keys)
#client = Client(signer)
#client.add_relay(relay)
#if externalrelay is not None:
# client.add_relay(externalrelay)
#client.connect()
#client_public_key = PublicKey.from_hex(target_pubkey)
#encrypted_content = nip04_encrypt(SecretSK.secret_key(), client_public_key, json.dumps(content))
#pTag = Tag.parse(["p", client_public_key.to_hex()])
#event = EventBuilder(23194, encrypted_content,
# [pTag]).to_event(keys)
#ts = Timestamp.now()
#event_id = client.send_event(event)
#nwc_response_filter = Filter().kind(23195).since(ts)
#events = client.get_events_of([nwc_response_filter], timedelta(seconds=5))
#if len(events) > 0:
# for evt in events:
# print(evt.as_json())
#else:
# print("No response found")
return event_id
def parse_connection_str(connectionstring):
split = connectionstring.split("?")
targetpubkey = split[0].split(":")[1]
targetpubkey = split[0].split(":")[1].replace("//", "")
split2 = split[1].split("&")
relay = split2[0].split("=")[1]
relay = relay.replace("%3A%2F%2F", "://")