fix nostr relay and refactor nostr_send and nostr_receive (#100)

* fix nostr relay and refactor nostr_send and nostr_receive

* bump to 0.8.4

* make format

* check test

* close nostr
This commit is contained in:
calle
2023-01-25 01:50:02 +01:00
committed by GitHub
parent 9acac156a7
commit 01d9cef407
8 changed files with 116 additions and 77 deletions

View File

@@ -36,8 +36,6 @@ from cashu.core.settings import (
VERSION,
)
from cashu.nostr.nostr.client.client import NostrClient
from cashu.nostr.nostr.event import Event
from cashu.nostr.nostr.key import PublicKey
from cashu.tor.tor import TorProxy
from cashu.wallet import migrations
from cashu.wallet.crud import (
@@ -48,11 +46,13 @@ from cashu.wallet.crud import (
)
from cashu.wallet.wallet import Wallet as Wallet
from .clihelpers import (
from .cli_helpers import (
get_mint_wallet,
print_mint_balances,
proofs_to_serialized_tokenv2,
receive_nostr,
redeem_multimint,
send_nostr,
token_from_lnbits_link,
verify_mints,
)
@@ -228,38 +228,6 @@ async def balance(ctx: Context, verbose):
print(f"Balance: {wallet.available_balance} sat")
async def nostr_send(ctx: Context, amount: int, pubkey: str, verbose: bool, yes: bool):
"""
Sends tokens via nostr.
"""
wallet = await get_mint_wallet(ctx)
await wallet.load_proofs()
_, send_proofs = await wallet.split_to_send(
wallet.proofs, amount, set_reserved=True
)
token = await wallet.serialize_proofs(send_proofs)
print("")
print(token)
if not yes:
print("")
click.confirm(
f"Send {amount} sat to nostr pubkey {pubkey}?",
abort=True,
default=True,
)
# we only use ephemeral private keys for sending
client = NostrClient(relays=NOSTR_RELAYS)
if verbose:
print(f"Your ephemeral nostr private key: {client.private_key.hex()}")
await asyncio.sleep(1)
client.dm(token, PublicKey(bytes.fromhex(pubkey)))
print(f"Token sent to {pubkey}")
client.close()
async def send(ctx: Context, amount: int, lock: str, legacy: bool):
"""
Prints token to send to stdout.
@@ -340,7 +308,7 @@ async def send_command(
if nostr is None:
await send(ctx, amount, lock, legacy)
else:
await nostr_send(ctx, amount, nostr, verbose, yes)
await send_nostr(ctx, amount, nostr, verbose, yes)
async def receive(ctx: Context, token: str, lock: str):
@@ -416,44 +384,11 @@ async def receive(ctx: Context, token: str, lock: str):
# no mint information present, we extract the proofs and use wallet's default mint
proofs = [Proof(**p) for p in dtoken["proofs"]]
_, _ = await wallet.redeem(proofs, script, signature)
print(f"Received {sum_proofs(proofs)} sats")
wallet.status()
async def receive_nostr(ctx: Context, verbose: bool):
if NOSTR_PRIVATE_KEY is None:
print(
"Warning: No nostr private key set! You don't have NOSTR_PRIVATE_KEY set in your .env file. I will create a random private key for this session but I will not remember it."
)
print("")
client = NostrClient(privatekey_hex=NOSTR_PRIVATE_KEY, relays=NOSTR_RELAYS)
print(f"Your nostr public key: {client.public_key.hex()}")
if verbose:
print(f"Your nostr private key (do not share!): {client.private_key.hex()}")
await asyncio.sleep(2)
def get_token_callback(event: Event, decrypted_content):
if verbose:
print(
f"From {event.public_key[:3]}..{event.public_key[-3:]}: {decrypted_content}"
)
try:
# call the receive method
asyncio.run(receive(ctx, decrypted_content, ""))
except Exception as e:
pass
t = threading.Thread(
target=client.get_dm,
args=(
client.public_key,
get_token_callback,
),
name="Nostr DM",
)
t.start()
@cli.command("receive", help="Receive tokens.")
@click.argument("token", type=str, default="")
@click.option("--lock", "-l", default=None, help="Unlock tokens.", type=str)