add nutzaps (beta)

This commit is contained in:
Believethehype
2024-08-19 13:25:03 +02:00
parent af49ee97c5
commit c22d0e5c79
10 changed files with 1091 additions and 53 deletions

81
tests/dalle.py Normal file
View File

@@ -0,0 +1,81 @@
import json
import os
from pathlib import Path
import dotenv
from nostr_sdk import Keys, LogLevel, init_logger
from nostr_dvm.tasks import search_users, advanced_search
from nostr_dvm.tasks.advanced_search import AdvancedSearch
from nostr_dvm.tasks.advanced_search_wine import AdvancedSearchWine
from nostr_dvm.tasks.imagegeneration_openai_dalle import ImageGenerationDALLE
from nostr_dvm.tasks.search_users import SearchUser
from nostr_dvm.utils.admin_utils import AdminConfig
from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config
from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag
from nostr_dvm.utils.nostr_utils import check_and_set_private_key
from nostr_dvm.utils.zap_utils import check_and_set_ln_bits_keys, get_price_per_sat
rebroadcast_NIP89 = False # Announce NIP89 on startup Only do this if you know what you're doing.
rebroadcast_NIP65_Relay_List = False
update_profile = False
#use_logger = True
log_level = LogLevel.ERROR
#if use_logger:
# init_logger(log_level)
def build_dalle(name, identifier):
dvm_config = build_default_config(identifier)
dvm_config.NEW_USER_BALANCE = 0
dvm_config.USE_OWN_VENV = False
dvm_config.ENABLE_NUTZAP = True
profit_in_sats = 10
dvm_config.FIX_COST = int(((4.0 / (get_price_per_sat("USD") * 100)) + profit_in_sats))
nip89info = {
"name": name,
"image": "https://image.nostr.build/22f2267ca9d4ee9d5e8a0c7818a9fa325bbbcdac5573a60a2d163e699bb69923.jpg",
"about": "I create Images bridging OpenAI's DALL·E 3",
"encryptionSupported": True,
"cashuAccepted": True,
"nip90Params": {
"size": {
"required": False,
"values": ["1024:1024", "1024x1792", "1792x1024"]
}
}
}
nip89config = NIP89Config()
nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY,
nip89info["image"])
nip89config.CONTENT = json.dumps(nip89info)
aconfig = AdminConfig()
aconfig.REBROADCAST_NIP89 = False # We add an optional AdminConfig for this one, and tell the dvm to rebroadcast its NIP89
aconfig.LUD16 = dvm_config.LN_ADDRESS
return ImageGenerationDALLE(name=name, dvm_config=dvm_config, nip89config=nip89config, admin_config=aconfig)
def playground():
if os.getenv("OPENAI_API_KEY") is not None and os.getenv("OPENAI_API_KEY") != "":
dalle = build_dalle("Dall-E 3", "dalle3")
dalle.run()
if __name__ == '__main__':
env_path = Path('.env')
if not env_path.is_file():
with open('.env', 'w') as f:
print("Writing new .env file")
f.write('')
if env_path.is_file():
print(f'loading environment from {env_path.resolve()}')
dotenv.load_dotenv(env_path, verbose=True, override=True)
else:
raise FileNotFoundError(f'.env file not found at {env_path} ')
playground()

View File

@@ -36,7 +36,7 @@ update_profile = False
global_update_rate = 120 # set this high on first sync so db can fully sync before another process trys to.
use_logger = True
log_level = LogLevel.ERROR
log_level = LogLevel.INFO

103
tests/nutzap_send.py Normal file
View File

@@ -0,0 +1,103 @@
from datetime import timedelta
from pathlib import Path
import dotenv
from nostr_sdk import PublicKey, Timestamp, Event, HandleNotification, Alphabet, Filter, SingleLetterTag, Kind
import asyncio
import argparse
from nostr_dvm.utils import dvmconfig
from nostr_dvm.utils.dvmconfig import DVMConfig
from nostr_dvm.utils.nut_wallet_utils import NutZapWallet
from nostr_dvm.utils.print import bcolors
# Run with params for test functions or set the default here
parser = argparse.ArgumentParser(description='Nutzaps')
parser.add_argument("--mint", type=bool, default=False)
parser.add_argument("--zap", type=bool, default=True)
parser.add_argument("--melt", type=bool, default=False)
args = parser.parse_args()
async def test(relays, mints):
nutzap_wallet = NutZapWallet()
update_wallet_info = False # leave this on false except when you manually changed relays/mints/keys
client, keys = await nutzap_wallet.client_connect(relays)
set_profile = False # Attention, this overwrites your current profile if on True, do not set if you use an non-test account
if set_profile:
lud16 = "hype@bitcoinfixesthis.org" #overwrite with your ln address
await nutzap_wallet.set_profile("Test", "I'm a nutsack test account", lud16, "https://i.nostr.build/V4FwExrV5aXHNm70.jpg", client, keys)
# Test 1 Config: Mint Tokens
mint_to_wallet = args.mint # Test function to mint 5 sats on the mint in your list with given index below
mint_index = 0 # Index of mint in mints list to mint a token
mint_amount = 10 # Amount to mint
# Test 2 Config: Send Nutzap
send_test = args.zap # Send a Nutzap
send_zap_amount = 3
send_zap_message = "From my nutsack"
send_reveiver = "npub139nfkqamy53j7vce9lw6w7uwxm3a8zrwnd2m836tj5y3aytv37vqygz42j" # keys.public_key().to_bech32() # This is ourself, for testing purposes, some other people to nutzap: #npub1nxa4tywfz9nqp7z9zp7nr7d4nchhclsf58lcqt5y782rmf2hefjquaa6q8 # dbth #npub1l2vyh47mk2p0qlsku7hg0vn29faehy9hy34ygaclpn66ukqp3afqutajft # pablof7z
send_zapped_event = None # None, or zap an event like this: Nip19Event.from_nostr_uri("nostr:nevent1qqsxq59mhz8s6aj9jzltcmqmmv3eutsfcpkeny2x755vdu5dtq44ldqpz3mhxw309ucnydewxqhrqt338g6rsd3e9upzp75cf0tahv5z7plpdeaws7ex52nmnwgtwfr2g3m37r844evqrr6jqvzqqqqqqyqtxyr6").event_id().to_hex()
# Test 3 Config: Melt to ln address
melt = args.melt
melt_amount = 6
print("PrivateKey: " + keys.secret_key().to_bech32() + " PublicKey: " + keys.public_key().to_bech32())
# See if we already have a wallet and fetch it
nut_wallet = await nutzap_wallet.get_nut_wallet(client, keys)
# If we have a wallet but want to maually update the info..
if nut_wallet is not None and update_wallet_info:
await nutzap_wallet.update_nut_wallet(nut_wallet, mints, client, keys)
await nutzap_wallet.announce_nutzap_info_event(nut_wallet, client, keys)
# If we don't have a wallet, we create one, fetch it and announce our info
if nut_wallet is None:
await nutzap_wallet.create_new_nut_wallet(mints, relays, client, keys, "Test", "My Nutsack")
nut_wallet = await nutzap_wallet.get_nut_wallet(client, keys)
if nut_wallet is not None:
await nutzap_wallet.announce_nutzap_info_event(nut_wallet, client, keys)
else:
print("Couldn't fetch wallet, please restart and see if it is there")
# Test 1: We mint to our own wallet
if mint_to_wallet:
await nutzap_wallet.mint_cashu(nut_wallet, mints[mint_index], client, keys, mint_amount)
nut_wallet = await nutzap_wallet.get_nut_wallet(client, keys)
# Test 2: We send a nutzap to someone (can be ourselves)
if send_test:
zapped_event_id_hex = send_zapped_event
zapped_user_hex = PublicKey.parse(send_reveiver).to_hex()
await nutzap_wallet.send_nut_zap(send_zap_amount, send_zap_message, nut_wallet, zapped_event_id_hex, zapped_user_hex, client,
keys)
#Test 3: Melt back to lightning:
if melt:
# you can overwrite the lu16 and/or npub, otherwise it's fetched from the profile (set it once by setting set_profile to True)
lud16 = None
npub = None
await nutzap_wallet.melt_cashu(nut_wallet, mints[mint_index], melt_amount, client, keys, lud16, npub)
await nutzap_wallet.get_nut_wallet(client, keys)
if __name__ == '__main__':
env_path = Path('.env')
if env_path.is_file():
print(f'loading environment from {env_path.resolve()}')
dotenv.load_dotenv(env_path, verbose=True, override=True)
else:
raise FileNotFoundError(f'.env file not found at {env_path} ')
show_history = True
asyncio.run(test(DVMConfig().NUTZAP_RELAYS, DVMConfig().NUZAP_MINTS))

View File

@@ -19,12 +19,12 @@ rebroadcast_NIP89 = False # Announce NIP89 on startup Only do this if you know
rebroadcast_NIP65_Relay_List = False
update_profile = False
use_logger = True
#use_logger = True
log_level = LogLevel.ERROR
if use_logger:
init_logger(log_level)
#if use_logger:
# init_logger(log_level)
RELAY_LIST = ["wss://relay.primal.net",
@@ -37,16 +37,17 @@ def build_advanced_search(name, identifier):
dvm_config.PRIVATE_KEY = check_and_set_private_key(identifier)
npub = Keys.parse(dvm_config.PRIVATE_KEY).public_key().to_bech32()
dvm_config.RELAY_LIST = RELAY_LIST
invoice_key, admin_key, wallet_id, user_id, lnaddress = check_and_set_ln_bits_keys(identifier, npub)
dvm_config.LNBITS_INVOICE_KEY = invoice_key
dvm_config.LNBITS_ADMIN_KEY = admin_key # The dvm might pay failed jobs back
dvm_config = build_default_config(identifier)
# dvm_config.LNBITS_URL = os.getenv("LNBITS_HOST")
dvm_config.ENABLE_NUTZAP = True
dvm_config.FIX_COST = 5
admin_config = AdminConfig()
admin_config.REBROADCAST_NIP89 = rebroadcast_NIP89
admin_config.REBROADCAST_NIP65_RELAY_LIST = rebroadcast_NIP65_Relay_List
admin_config.UPDATE_PROFILE = update_profile
admin_config.LUD16 = lnaddress
admin_config.LUD16 = dvm_config.LN_ADDRESS
# Add NIP89
nip89info = {

View File

@@ -4,6 +4,9 @@ import time
from pathlib import Path
from threading import Thread
from nostr_dvm.utils.nut_wallet_utils import NutZapWallet
from nostr_dvm.utils.print import bcolors
import dotenv
from nostr_sdk import Keys, Client, Tag, EventBuilder, Filter, HandleNotification, Timestamp, nip04_decrypt, \
nip04_encrypt, NostrSigner, PublicKey, Event, Kind, RelayOptions
@@ -47,12 +50,9 @@ async def nostr_client_test_search_profile(input):
keys = Keys.parse(check_and_set_private_key("test_client"))
iTag = Tag.parse(["i", input, "text"])
relaysTag = Tag.parse(['relays', "wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
"wss://nostr-pub.wellorder.net"])
alttag = Tag.parse(["alt", "This is a NIP90 DVM AI task to translate a given Input"])
event = EventBuilder(EventDefinitions.KIND_NIP90_USER_SEARCH, str("Search for user"),
[iTag, relaysTag, alttag]).to_event(keys)
[iTag, alttag]).to_event(keys)
relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
"wss://nostr-pub.wellorder.net"]
@@ -83,12 +83,9 @@ async def nostr_client_test_image(prompt):
event = EventBuilder(EventDefinitions.KIND_NIP90_GENERATE_IMAGE, str("Generate an Image."),
[iTag, outTag, tTag, paramTag1, bidTag, relaysTag, alttag]).to_event(keys)
relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
"wss://nostr-pub.wellorder.net"]
signer = NostrSigner.keys(keys)
client = Client(signer)
for relay in relay_list:
for relay in DVMConfig().RELAY_LIST:
await client.add_relay(relay)
await client.connect()
config = DVMConfig
@@ -154,7 +151,7 @@ async def nostr_client_test_inactive_filter(user):
async def nostr_client_test_tts(prompt):
keys = Keys.parse(check_and_set_private_key("test_client"))
iTag = Tag.parse(["i", "9d867cd3e868111a31c8acfa41ab7523b9940fc46c804d7db89d7f373c007fa6", "event"])
#iTag = Tag.parse(["i", prompt, "text"])
# iTag = Tag.parse(["i", prompt, "text"])
paramTag1 = Tag.parse(["param", "language", "en"])
bidTag = Tag.parse(['bid', str(1000 * 1000), str(1000 * 1000)])
@@ -230,6 +227,7 @@ async def nostr_client_test_discovery_user(user, ptag):
eventid = await send_event(event, client=client, dvm_config=config)
return event.as_json()
async def nostr_client_test_discovery_gallery(user, ptag):
keys = Keys.parse(check_and_set_private_key("test_client"))
@@ -311,25 +309,26 @@ async def nostr_client():
EventDefinitions.KIND_ZAP]).since(
Timestamp.now()) # events to us specific
kinds = [EventDefinitions.KIND_NIP90_GENERIC]
SUPPORTED_KINDS = [Kind(6301)]
SUPPORTED_KINDS = [Kind(6100), Kind(7000)]
for kind in SUPPORTED_KINDS:
if kind not in kinds:
kinds.append(kind)
dvm_filter = (Filter().kinds(kinds).since(Timestamp.now()))
dvm_filter = (Filter().kinds(kinds).since(Timestamp.now()).pubkey(pk))
await client.subscribe([dm_zap_filter, dvm_filter], None)
# await nostr_client_test_translation("This is the result of the DVM in spanish", "text", "es", 20, 20)
# await nostr_client_test_translation("note1p8cx2dz5ss5gnk7c59zjydcncx6a754c0hsyakjvnw8xwlm5hymsnc23rs", "event", "es", 20,20)
# await nostr_client_test_translation("44a0a8b395ade39d46b9d20038b3f0c8a11168e67c442e3ece95e4a1703e2beb", "event", "zh", 20, 20)
# await nostr_client_test_image("a beautiful purple ostrich watching the sunset")
# await nostr_client_test_search_profile("dontbelieve")
wot = ["99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64"]
#await nostr_client_test_discovery("99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64", "ab6cdf12ca3ae5109416295b8cd8a53fdec3a9d54beb7a9aee0ebfb67cb4edf7")
#await nostr_client_test_discovery_gallery("99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64", "4add3944eb596a27a650f9b954f5ed8dfefeec6ca50473605b0fbb058dd11306")
await nostr_client_test_discovery("99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64",
"2cf10ff849d2769b2b021bd93a0270d03eecfd14126d07f94c6ca2269cb3f3b1")
await nostr_client_test_image("a beautiful purple ostrich watching the sunset, eating a cashew nut")
# await nostr_client_test_search_profile("dontbelieve")
#wot = ["99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64"]
# await nostr_client_test_discovery("99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64", "ab6cdf12ca3ae5109416295b8cd8a53fdec3a9d54beb7a9aee0ebfb67cb4edf7")
# await nostr_client_test_discovery_gallery("99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64", "4add3944eb596a27a650f9b954f5ed8dfefeec6ca50473605b0fbb058dd11306")
# await nostr_client_test_discovery("99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64",
# "2cf10ff849d2769b2b021bd93a0270d03eecfd14126d07f94c6ca2269cb3f3b1")
# await nostr_client_test_censor_filter(wot)
# await nostr_client_test_inactive_filter("99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64")
@@ -338,11 +337,35 @@ async def nostr_client():
# cashutoken = "cashuAeyJ0b2tlbiI6W3sicHJvb2ZzIjpbeyJpZCI6InZxc1VRSVorb0sxOSIsImFtb3VudCI6MSwiQyI6IjAyNWU3ODZhOGFkMmExYTg0N2YxMzNiNGRhM2VhMGIyYWRhZGFkOTRiYzA4M2E2NWJjYjFlOTgwYTE1NGIyMDA2NCIsInNlY3JldCI6InQ1WnphMTZKMGY4UElQZ2FKTEg4V3pPck5rUjhESWhGa291LzVzZFd4S0U9In0seyJpZCI6InZxc1VRSVorb0sxOSIsImFtb3VudCI6NCwiQyI6IjAyOTQxNmZmMTY2MzU5ZWY5ZDc3MDc2MGNjZmY0YzliNTMzMzVmZTA2ZGI5YjBiZDg2Njg5Y2ZiZTIzMjVhYWUwYiIsInNlY3JldCI6IlRPNHB5WE43WlZqaFRQbnBkQ1BldWhncm44UHdUdE5WRUNYWk9MTzZtQXM9In0seyJpZCI6InZxc1VRSVorb0sxOSIsImFtb3VudCI6MTYsIkMiOiIwMmRiZTA3ZjgwYmMzNzE0N2YyMDJkNTZiMGI3ZTIzZTdiNWNkYTBhNmI3Yjg3NDExZWYyOGRiZDg2NjAzNzBlMWIiLCJzZWNyZXQiOiJHYUNIdHhzeG9HM3J2WWNCc0N3V0YxbU1NVXczK0dDN1RKRnVwOHg1cURzPSJ9XSwibWludCI6Imh0dHBzOi8vbG5iaXRzLmJpdGNvaW5maXhlc3RoaXMub3JnL2Nhc2h1L2FwaS92MS9ScDlXZGdKZjlxck51a3M1eVQ2SG5rIn1dfQ=="
# await nostr_client_test_image_private("a beautiful ostrich watching the sunset")
nutzap_wallet = NutZapWallet()
nut_wallet = await nutzap_wallet.get_nut_wallet(client, keys)
class NotificationHandler(HandleNotification):
async def handle(self, relay_url, subscription_id, event: Event):
print(f"Received new event from {relay_url}: {event.as_json()}")
print(
bcolors.BLUE + f"Received new event from {relay_url}: {event.as_json()}" + bcolors.ENDC)
if event.kind().as_u64() == 7000:
print("[Nostr Client]: " + event.as_json())
amount_sats = 0
for tag in event.tags():
if tag.as_vec()[0] == "amount":
amount_sats = int(int(tag.as_vec()[1]) / 1000) # millisats
# THIS IS FO TESTING
if event.author().to_hex() == "89669b03bb25232f33192fdda77b8e36e3d3886e9b55b3c74b95091e916c8f98":
nut_wallet = await nutzap_wallet.get_nut_wallet(client, keys)
if nut_wallet is None:
await nutzap_wallet.create_new_nut_wallet(dvmconfig.NUZAP_MINTS, dvmconfig.NUTZAP_RELAYS, client, keys, "Test", "My Nutsack")
nut_wallet = await nutzap_wallet.get_nut_wallet(client, keys)
if nut_wallet is not None:
await nutzap_wallet.announce_nutzap_info_event(nut_wallet, client, keys)
else:
print("Couldn't fetch wallet, please restart and see if it is there")
await nutzap_wallet.send_nut_zap(amount_sats, "From my nutsack lol", nut_wallet, event.id().to_hex(),
event.author().to_hex(), client,
keys)
elif 6000 < event.kind().as_u64() < 6999:
print("[Nostr Client]: " + event.as_json())
print("[Nostr Client]: " + event.content())
@@ -358,9 +381,10 @@ async def nostr_client():
async def handle_msg(self, relay_url, msg):
return
await client.handle_notifications(NotificationHandler())
asyncio.create_task(client.handle_notifications(NotificationHandler()))
# await client.handle_notifications(NotificationHandler())
while True:
await asyncio.sleep(5.0)
await asyncio.sleep(2)
if __name__ == '__main__':