add nostr

This commit is contained in:
callebtc
2022-12-26 16:12:31 +01:00
parent 6dc9937b9d
commit 01c6e12b5a
34 changed files with 174 additions and 2041 deletions

View File

@@ -15,6 +15,7 @@ from os.path import isdir, join
import click
from loguru import logger
import threading
from cashu.core.base import Proof
from cashu.core.helpers import sum_proofs
@@ -28,6 +29,7 @@ from cashu.core.settings import (
SOCKS_HOST,
SOCKS_PORT,
TOR,
NOSTR_PRIVATE_KEY,
VERSION,
)
from cashu.tor.tor import TorProxy
@@ -39,8 +41,9 @@ from cashu.wallet.crud import (
)
from cashu.wallet.wallet import Wallet as Wallet
from nostr.client.client import NostrClient
from nostr.key import PublicKey
from cashu.nostr.nostr.client.client import NostrClient
from cashu.nostr.nostr.key import PublicKey
from cashu.nostr.nostr.event import Event
async def init_wallet(wallet: Wallet):
@@ -438,29 +441,36 @@ async def info(ctx):
return
@cli.command("nostr", help="Receive tokens via nostr.")
@cli.command("nreceive", help="Receive tokens via nostr.")
@click.pass_context
@coro
async def nostr(ctx):
wallet: Wallet = ctx.obj["WALLET"]
await wallet.load_mint()
client = NostrClient(
privatekey_hex="bfc6e7b0b998645d45aa451a3b9a3174bfe696fba78e86a86637a16f43e6c683"
)
print(f"Your nostr public key: {client.public_key.hex()}")
if NOSTR_PRIVATE_KEY is None:
print(
"Warning!\n\nYou don't have a private key set in your .env file. I will create a random private key for this session but I will not remember it. If you lose this key, you will lose access to the DMs you receive on it."
)
print("")
client = NostrClient(privatekey_hex=NOSTR_PRIVATE_KEY)
await asyncio.sleep(2)
def get_token_callback(token):
def get_token_callback(event: Event, decrypted_content):
print(
f"From {event.public_key[:3]}..{event.public_key[-3:]}: {decrypted_content}"
)
try:
proofs = [Proof(**p) for p in json.loads(base64.urlsafe_b64decode(token))]
proofs = [
Proof(**p)
for p in json.loads(base64.urlsafe_b64decode(decrypted_content))
]
wallet: Wallet = ctx.obj["WALLET"]
asyncio.run(wallet.redeem(proofs))
wallet.status()
except Exception as e:
pass
import threading
t = threading.Thread(
target=client.get_dm,
args=(
@@ -472,7 +482,7 @@ async def nostr(ctx):
t.start()
@cli.command("nostrsend", help="Send tokens via nostr.")
@cli.command("nsend", help="Send tokens via nostr.")
@click.argument("amount", type=int)
@click.argument(
"pubkey",
@@ -490,16 +500,11 @@ async def nostrsend(ctx, amount: int, pubkey: str):
)
token = await wallet.serialize_proofs(send_proofs)
from random import randrange
# token = f"Token {randrange(1000)}"
print(token)
wallet.status()
client = NostrClient(
privatekey_hex="bfc6e7b0b598645d45aa451a3b9a3174bfe696fba78e86a86637a16f4ee6d683"
)
client = NostrClient(privatekey_hex=NOSTR_PRIVATE_KEY)
await asyncio.sleep(1)
client.dm(token, PublicKey(bytes.fromhex(pubkey)))
print("Sent")
print(f"Token sent to {pubkey}")
client.close()