mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-20 10:34:20 +01:00
* clean up db * db: table lock * db.table_with_schema * fix encrypt.py * postgres nowait * add timeout to lock * melt quote state in db * kinda working * kinda working with postgres * remove dispose * getting there * porperly clean up db for tests * faster tests * configure connection pooling * try github with connection pool * invoice dispatcher does not lock db * fakewallet: pay_if_regtest waits * pay fakewallet invoices * add more * faster * slower * pay_if_regtest async * do not lock the invoice dispatcher * test: do I get disk I/O errors if we disable the invoice_callback_dispatcher? * fix fake so it workss without a callback dispatchert * test on github * readd tasks * refactor * increase time for lock invoice disatcher * try avoiding a race * remove task * github actions: test regtest with postgres * mint per module * no connection pool for testing * enable pool * do not resend paid event * reuse connection * close db connections * sessions * enable debug * dispose engine * disable connection pool for tests * enable connection pool for postgres only * clean up shutdown routine * remove wait for lightning fakewallet lightning invoice * cancel invoice listener tasks on shutdown * fakewallet conftest: decrease outgoing delay * delay payment and set postgres only if needed * disable fail fast for regtest * clean up regtest.yml * change order of tests_db.py * row-specific mint_quote locking * refactor * fix lock statement * refactor swap * refactor * remove psycopg2 * add connection string example to .env.example * remove unnecessary pay * shorter sleep in test_wallet_subscription_swap
249 lines
9.2 KiB
Python
249 lines
9.2 KiB
Python
import asyncio
|
|
import hashlib
|
|
import secrets
|
|
from typing import List
|
|
|
|
import pytest
|
|
import pytest_asyncio
|
|
|
|
from cashu.core.base import HTLCWitness, Proof
|
|
from cashu.core.crypto.secp import PrivateKey
|
|
from cashu.core.htlc import HTLCSecret
|
|
from cashu.core.migrations import migrate_databases
|
|
from cashu.wallet import migrations
|
|
from cashu.wallet.wallet import Wallet
|
|
from cashu.wallet.wallet import Wallet as Wallet1
|
|
from cashu.wallet.wallet import Wallet as Wallet2
|
|
from tests.conftest import SERVER_ENDPOINT
|
|
from tests.helpers import pay_if_regtest
|
|
|
|
|
|
async def assert_err(f, msg):
|
|
"""Compute f() and expect an error message 'msg'."""
|
|
try:
|
|
await f
|
|
except Exception as exc:
|
|
if msg not in str(exc.args[0]):
|
|
raise Exception(f"Expected error: {msg}, got: {exc.args[0]}")
|
|
return
|
|
raise Exception(f"Expected error: {msg}, got no error")
|
|
|
|
|
|
def assert_amt(proofs: List[Proof], expected: int):
|
|
"""Assert amounts the proofs contain."""
|
|
assert [p.amount for p in proofs] == expected
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="function")
|
|
async def wallet1():
|
|
wallet1 = await Wallet1.with_db(
|
|
SERVER_ENDPOINT, "test_data/wallet_p2pk_1", "wallet1"
|
|
)
|
|
await migrate_databases(wallet1.db, migrations)
|
|
await wallet1.load_mint()
|
|
yield wallet1
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="function")
|
|
async def wallet2():
|
|
wallet2 = await Wallet2.with_db(
|
|
SERVER_ENDPOINT, "test_data/wallet_p2pk_2", "wallet2"
|
|
)
|
|
await migrate_databases(wallet2.db, migrations)
|
|
wallet2.private_key = PrivateKey(secrets.token_bytes(32), raw=True)
|
|
await wallet2.load_mint()
|
|
yield wallet2
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_htlc_secret(wallet1: Wallet):
|
|
invoice = await wallet1.request_mint(64)
|
|
await pay_if_regtest(invoice.bolt11)
|
|
await wallet1.mint(64, id=invoice.id)
|
|
preimage = "00000000000000000000000000000000"
|
|
preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
|
secret = await wallet1.create_htlc_lock(preimage=preimage)
|
|
assert secret.data == preimage_hash
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_htlc_split(wallet1: Wallet, wallet2: Wallet):
|
|
invoice = await wallet1.request_mint(64)
|
|
await pay_if_regtest(invoice.bolt11)
|
|
await wallet1.mint(64, id=invoice.id)
|
|
preimage = "00000000000000000000000000000000"
|
|
preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
|
secret = await wallet1.create_htlc_lock(preimage=preimage)
|
|
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
|
|
for p in send_proofs:
|
|
assert HTLCSecret.deserialize(p.secret).data == preimage_hash
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_htlc_redeem_with_preimage(wallet1: Wallet, wallet2: Wallet):
|
|
invoice = await wallet1.request_mint(64)
|
|
await pay_if_regtest(invoice.bolt11)
|
|
await wallet1.mint(64, id=invoice.id)
|
|
preimage = "00000000000000000000000000000000"
|
|
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
|
secret = await wallet1.create_htlc_lock(preimage=preimage)
|
|
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
|
|
for p in send_proofs:
|
|
p.witness = HTLCWitness(preimage=preimage).json()
|
|
await wallet2.redeem(send_proofs)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_htlc_redeem_with_wrong_preimage(wallet1: Wallet, wallet2: Wallet):
|
|
invoice = await wallet1.request_mint(64)
|
|
await pay_if_regtest(invoice.bolt11)
|
|
await wallet1.mint(64, id=invoice.id)
|
|
preimage = "00000000000000000000000000000000"
|
|
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
|
secret = await wallet1.create_htlc_lock(
|
|
preimage=preimage[:-5] + "11111"
|
|
) # wrong preimage
|
|
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
|
|
for p in send_proofs:
|
|
p.witness = HTLCWitness(preimage=preimage).json()
|
|
await assert_err(
|
|
wallet2.redeem(send_proofs), "Mint Error: HTLC preimage does not match"
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_htlc_redeem_with_no_signature(wallet1: Wallet, wallet2: Wallet):
|
|
invoice = await wallet1.request_mint(64)
|
|
await pay_if_regtest(invoice.bolt11)
|
|
await wallet1.mint(64, id=invoice.id)
|
|
preimage = "00000000000000000000000000000000"
|
|
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
|
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
|
secret = await wallet1.create_htlc_lock(
|
|
preimage=preimage, hashlock_pubkey=pubkey_wallet1
|
|
)
|
|
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
|
|
for p in send_proofs:
|
|
p.witness = HTLCWitness(preimage=preimage).json()
|
|
await assert_err(
|
|
wallet2.redeem(send_proofs),
|
|
"Mint Error: HTLC no hash lock signatures provided.",
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_htlc_redeem_with_wrong_signature(wallet1: Wallet, wallet2: Wallet):
|
|
invoice = await wallet1.request_mint(64)
|
|
await pay_if_regtest(invoice.bolt11)
|
|
await wallet1.mint(64, id=invoice.id)
|
|
preimage = "00000000000000000000000000000000"
|
|
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
|
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
|
secret = await wallet1.create_htlc_lock(
|
|
preimage=preimage, hashlock_pubkey=pubkey_wallet1
|
|
)
|
|
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
|
|
signatures = await wallet1.sign_p2pk_proofs(send_proofs)
|
|
for p, s in zip(send_proofs, signatures):
|
|
p.witness = HTLCWitness(
|
|
preimage=preimage, signature=s[:-5] + "11111"
|
|
).json() # wrong signature
|
|
|
|
await assert_err(
|
|
wallet2.redeem(send_proofs),
|
|
"Mint Error: HTLC hash lock signatures did not match.",
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_htlc_redeem_with_correct_signature(wallet1: Wallet, wallet2: Wallet):
|
|
invoice = await wallet1.request_mint(64)
|
|
await pay_if_regtest(invoice.bolt11)
|
|
await wallet1.mint(64, id=invoice.id)
|
|
preimage = "00000000000000000000000000000000"
|
|
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
|
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
|
secret = await wallet1.create_htlc_lock(
|
|
preimage=preimage, hashlock_pubkey=pubkey_wallet1
|
|
)
|
|
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
|
|
|
|
signatures = await wallet1.sign_p2pk_proofs(send_proofs)
|
|
for p, s in zip(send_proofs, signatures):
|
|
p.witness = HTLCWitness(preimage=preimage, signature=s).json()
|
|
|
|
await wallet2.redeem(send_proofs)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_htlc_redeem_hashlock_wrong_signature_timelock_correct_signature(
|
|
wallet1: Wallet, wallet2: Wallet
|
|
):
|
|
invoice = await wallet1.request_mint(64)
|
|
await pay_if_regtest(invoice.bolt11)
|
|
await wallet1.mint(64, id=invoice.id)
|
|
preimage = "00000000000000000000000000000000"
|
|
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
|
pubkey_wallet2 = await wallet2.create_p2pk_pubkey()
|
|
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
|
secret = await wallet1.create_htlc_lock(
|
|
preimage=preimage,
|
|
hashlock_pubkey=pubkey_wallet2,
|
|
locktime_seconds=2,
|
|
locktime_pubkey=pubkey_wallet1,
|
|
)
|
|
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
|
|
|
|
signatures = await wallet1.sign_p2pk_proofs(send_proofs)
|
|
for p, s in zip(send_proofs, signatures):
|
|
p.witness = HTLCWitness(preimage=preimage, signature=s).json()
|
|
|
|
# should error because we used wallet2 signatures for the hash lock
|
|
await assert_err(
|
|
wallet1.redeem(send_proofs),
|
|
"Mint Error: HTLC hash lock signatures did not match.",
|
|
)
|
|
|
|
await asyncio.sleep(2)
|
|
# should succeed since lock time has passed and we provided wallet1 signature for timelock
|
|
await wallet1.redeem(send_proofs)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_htlc_redeem_hashlock_wrong_signature_timelock_wrong_signature(
|
|
wallet1: Wallet, wallet2: Wallet
|
|
):
|
|
invoice = await wallet1.request_mint(64)
|
|
await pay_if_regtest(invoice.bolt11)
|
|
await wallet1.mint(64, id=invoice.id)
|
|
preimage = "00000000000000000000000000000000"
|
|
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
|
pubkey_wallet2 = await wallet2.create_p2pk_pubkey()
|
|
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
|
secret = await wallet1.create_htlc_lock(
|
|
preimage=preimage,
|
|
hashlock_pubkey=pubkey_wallet2,
|
|
locktime_seconds=2,
|
|
locktime_pubkey=pubkey_wallet1,
|
|
)
|
|
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret)
|
|
|
|
signatures = await wallet1.sign_p2pk_proofs(send_proofs)
|
|
for p, s in zip(send_proofs, signatures):
|
|
p.witness = HTLCWitness(
|
|
preimage=preimage, signature=s[:-5] + "11111"
|
|
).json() # wrong signature
|
|
|
|
# should error because we used wallet2 signatures for the hash lock
|
|
await assert_err(
|
|
wallet1.redeem(send_proofs),
|
|
"Mint Error: HTLC hash lock signatures did not match.",
|
|
)
|
|
|
|
await asyncio.sleep(2)
|
|
# should fail since lock time has passed and we provided a wrong signature for timelock
|
|
await assert_err(
|
|
wallet1.redeem(send_proofs),
|
|
"Mint Error: HTLC refund signatures did not match.",
|
|
)
|