mirror of
https://github.com/aljazceru/nutshell.git
synced 2026-02-23 09:34:22 +01:00
Mint: Add LndRest and regtest tests (#359)
* update * working * test with lnd * update action * cache poetry * add lndrest * enable regtest * add regtests.yml * poetry version * add helpers * save * run legend regtest fork * actually start * use bash * give rights * remove cache? * change order * tests succeed with lndrestwallet * check if wallet is set * settings for regtest * fix fakewallet test * remove wacky balance check * adjust permissions * try with sudo * adjust example * remove eclair
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
@@ -28,7 +28,7 @@ settings.mint_listen_port = SERVER_PORT
|
||||
settings.mint_url = SERVER_ENDPOINT
|
||||
settings.lightning = True
|
||||
settings.tor = False
|
||||
settings.mint_lightning_backend = "FakeWallet"
|
||||
settings.mint_lightning_backend = settings.mint_lightning_backend or "FakeWallet"
|
||||
settings.mint_database = "./test_data/test_mint"
|
||||
settings.mint_derivation_path = "0/0/0/0"
|
||||
settings.mint_private_key = "TEST_PRIVATE_KEY"
|
||||
|
||||
184
tests/helpers.py
Normal file
184
tests/helpers.py
Normal file
@@ -0,0 +1,184 @@
|
||||
import hashlib
|
||||
import importlib
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import string
|
||||
import time
|
||||
from subprocess import PIPE, Popen, TimeoutExpired
|
||||
from typing import Tuple
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from cashu.core.settings import settings
|
||||
|
||||
|
||||
def get_random_string(N: int = 10):
|
||||
return "".join(
|
||||
random.SystemRandom().choice(string.ascii_uppercase + string.digits)
|
||||
for _ in range(N)
|
||||
)
|
||||
|
||||
|
||||
async def get_random_invoice_data():
|
||||
return {"out": False, "amount": 10, "memo": f"test_memo_{get_random_string(10)}"}
|
||||
|
||||
|
||||
wallets_module = importlib.import_module("cashu.lightning")
|
||||
wallet_class = getattr(wallets_module, settings.mint_lightning_backend)
|
||||
WALLET = wallet_class()
|
||||
is_fake: bool = WALLET.__class__.__name__ == "FakeWallet"
|
||||
is_regtest: bool = not is_fake
|
||||
|
||||
|
||||
docker_lightning_cli = [
|
||||
"docker",
|
||||
"exec",
|
||||
"cashu-lnd-1-1",
|
||||
"lncli",
|
||||
"--network",
|
||||
"regtest",
|
||||
"--rpcserver=lnd-1",
|
||||
]
|
||||
|
||||
docker_bitcoin_cli = [
|
||||
"docker",
|
||||
"exec",
|
||||
"cashu-bitcoind-1-1bitcoin-cli",
|
||||
"-rpcuser=lnbits",
|
||||
"-rpcpassword=lnbits",
|
||||
"-regtest",
|
||||
]
|
||||
|
||||
|
||||
docker_lightning_unconnected_cli = [
|
||||
"docker",
|
||||
"exec",
|
||||
"cashu-lnd-2-1",
|
||||
"lncli",
|
||||
"--network",
|
||||
"regtest",
|
||||
"--rpcserver=lnd-2",
|
||||
]
|
||||
|
||||
|
||||
def run_cmd(cmd: list) -> str:
|
||||
timeout = 20
|
||||
process = Popen(cmd, stdout=PIPE, stderr=PIPE)
|
||||
|
||||
def process_communication(comm):
|
||||
stdout, stderr = comm
|
||||
output = stdout.decode("utf-8").strip()
|
||||
error = stderr.decode("utf-8").strip()
|
||||
return output, error
|
||||
|
||||
try:
|
||||
now = time.time()
|
||||
output, error = process_communication(process.communicate(timeout=timeout))
|
||||
took = time.time() - now
|
||||
logger.debug(f"ran command output: {output}, error: {error}, took: {took}s")
|
||||
return output
|
||||
except TimeoutExpired:
|
||||
process.kill()
|
||||
output, error = process_communication(process.communicate())
|
||||
logger.error(f"timeout command: {cmd}, output: {output}, error: {error}")
|
||||
raise
|
||||
|
||||
|
||||
def run_cmd_json(cmd: list) -> dict:
|
||||
output = run_cmd(cmd)
|
||||
try:
|
||||
return json.loads(output) if output else {}
|
||||
except json.decoder.JSONDecodeError:
|
||||
logger.error(f"failed to decode json from cmd `{cmd}`: {output}")
|
||||
raise
|
||||
|
||||
|
||||
def get_hold_invoice(sats: int) -> Tuple[str, dict]:
|
||||
preimage = os.urandom(32)
|
||||
preimage_hash = hashlib.sha256(preimage).hexdigest()
|
||||
cmd = docker_lightning_cli.copy()
|
||||
cmd.extend(["addholdinvoice", preimage_hash, str(sats)])
|
||||
json = run_cmd_json(cmd)
|
||||
return preimage.hex(), json
|
||||
|
||||
|
||||
def settle_invoice(preimage: str) -> str:
|
||||
cmd = docker_lightning_cli.copy()
|
||||
cmd.extend(["settleinvoice", preimage])
|
||||
return run_cmd(cmd)
|
||||
|
||||
|
||||
def cancel_invoice(preimage_hash: str) -> str:
|
||||
cmd = docker_lightning_cli.copy()
|
||||
cmd.extend(["cancelinvoice", preimage_hash])
|
||||
return run_cmd(cmd)
|
||||
|
||||
|
||||
def get_real_invoice(sats: int) -> dict:
|
||||
cmd = docker_lightning_cli.copy()
|
||||
cmd.extend(["addinvoice", str(sats)])
|
||||
return run_cmd_json(cmd)
|
||||
|
||||
|
||||
def pay_real_invoice(invoice: str) -> str:
|
||||
cmd = docker_lightning_cli.copy()
|
||||
cmd.extend(["payinvoice", "--force", invoice])
|
||||
return run_cmd(cmd)
|
||||
|
||||
|
||||
def mine_blocks(blocks: int = 1) -> str:
|
||||
cmd = docker_bitcoin_cli.copy()
|
||||
cmd.extend(["-generate", str(blocks)])
|
||||
return run_cmd(cmd)
|
||||
|
||||
|
||||
def get_unconnected_node_uri() -> str:
|
||||
cmd = docker_lightning_unconnected_cli.copy()
|
||||
cmd.append("getinfo")
|
||||
info = run_cmd_json(cmd)
|
||||
pubkey = info["identity_pubkey"]
|
||||
return f"{pubkey}@lnd-2:9735"
|
||||
|
||||
|
||||
def create_onchain_address(address_type: str = "bech32") -> str:
|
||||
cmd = docker_bitcoin_cli.copy()
|
||||
cmd.extend(["getnewaddress", address_type])
|
||||
return run_cmd(cmd)
|
||||
|
||||
|
||||
def pay_onchain(address: str, sats: int) -> str:
|
||||
btc = sats * 0.00000001
|
||||
cmd = docker_bitcoin_cli.copy()
|
||||
cmd.extend(["sendtoaddress", address, str(btc)])
|
||||
return run_cmd(cmd)
|
||||
|
||||
|
||||
# def clean_database(settings):
|
||||
# if DB_TYPE == POSTGRES:
|
||||
# db_url = make_url(settings.lnbits_database_url)
|
||||
|
||||
# conn = psycopg2.connect(settings.lnbits_database_url)
|
||||
# conn.autocommit = True
|
||||
# with conn.cursor() as cur:
|
||||
# try:
|
||||
# cur.execute("DROP DATABASE lnbits_test")
|
||||
# except psycopg2.errors.InvalidCatalogName:
|
||||
# pass
|
||||
# cur.execute("CREATE DATABASE lnbits_test")
|
||||
|
||||
# db_url.database = "lnbits_test"
|
||||
# settings.lnbits_database_url = str(db_url)
|
||||
|
||||
# core.db.__init__("database")
|
||||
|
||||
# conn.close()
|
||||
# else:
|
||||
# # FIXME: do this once mock data is removed from test data folder
|
||||
# # os.remove(settings.lnbits_data_folder + "/database.sqlite3")
|
||||
# pass
|
||||
|
||||
|
||||
def pay_if_regtest(bolt11: str):
|
||||
if is_regtest:
|
||||
pay_real_invoice(bolt11)
|
||||
@@ -7,6 +7,7 @@ from cashu.core.base import TokenV3
|
||||
from cashu.core.settings import settings
|
||||
from cashu.wallet.cli.cli import cli
|
||||
from cashu.wallet.wallet import Wallet
|
||||
from tests.helpers import is_fake, pay_if_regtest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="session")
|
||||
@@ -14,6 +15,16 @@ def cli_prefix():
|
||||
yield ["--wallet", "test_cli_wallet", "--host", settings.mint_url, "--tests"]
|
||||
|
||||
|
||||
def get_bolt11_and_invoice_id_from_invoice_command(output: str) -> (str, str):
|
||||
invoice = [
|
||||
line.split(" ")[1] for line in output.split("\n") if line.startswith("Invoice")
|
||||
][0]
|
||||
invoice_id = [
|
||||
line.split(" ")[-1] for line in output.split("\n") if line.startswith("You can")
|
||||
][0]
|
||||
return invoice, invoice_id
|
||||
|
||||
|
||||
async def init_wallet():
|
||||
wallet = await Wallet.with_db(
|
||||
url=settings.mint_host,
|
||||
@@ -77,7 +88,8 @@ def test_balance(cli_prefix):
|
||||
assert result.exit_code == 0
|
||||
|
||||
|
||||
def test_invoice(mint, cli_prefix):
|
||||
@pytest.mark.skipif(not is_fake, reason="only on fakewallet")
|
||||
def test_invoice_automatic_fakewallet(mint, cli_prefix):
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
@@ -87,20 +99,60 @@ def test_invoice(mint, cli_prefix):
|
||||
print("INVOICE")
|
||||
print(result.output)
|
||||
wallet = asyncio.run(init_wallet())
|
||||
# assert wallet.available_balance >= 1000
|
||||
assert wallet.available_balance >= 1000
|
||||
assert f"Balance: {wallet.available_balance} sat" in result.output
|
||||
assert result.exit_code == 0
|
||||
|
||||
|
||||
def test_invoice(mint, cli_prefix):
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[*cli_prefix, "invoice", "-n", "1000"],
|
||||
)
|
||||
|
||||
assert result.exception is None
|
||||
|
||||
invoice, invoice_id = get_bolt11_and_invoice_id_from_invoice_command(result.output)
|
||||
pay_if_regtest(invoice)
|
||||
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[*cli_prefix, "invoice", "1000", "--id", invoice_id],
|
||||
)
|
||||
assert result.exception is None
|
||||
|
||||
wallet = asyncio.run(init_wallet())
|
||||
assert wallet.available_balance >= 1000
|
||||
assert result.exit_code == 0
|
||||
|
||||
|
||||
def test_invoice_with_split(mint, cli_prefix):
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[*cli_prefix, "invoice", "10", "-s", "1"],
|
||||
[
|
||||
*cli_prefix,
|
||||
"invoice",
|
||||
"10",
|
||||
"-s",
|
||||
"1",
|
||||
"-n",
|
||||
],
|
||||
)
|
||||
assert result.exception is None
|
||||
# wallet = asyncio.run(init_wallet())
|
||||
# assert wallet.proof_amounts.count(1) >= 10
|
||||
|
||||
invoice, invoice_id = get_bolt11_and_invoice_id_from_invoice_command(result.output)
|
||||
pay_if_regtest(invoice)
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[*cli_prefix, "invoice", "10", "-s", "1", "--id", invoice_id],
|
||||
)
|
||||
assert result.exception is None
|
||||
|
||||
assert result.exception is None
|
||||
wallet = asyncio.run(init_wallet())
|
||||
assert wallet.proof_amounts.count(1) >= 10
|
||||
|
||||
|
||||
def test_wallets(cli_prefix):
|
||||
|
||||
@@ -5,6 +5,7 @@ from cashu.mint.ledger import Ledger
|
||||
from cashu.wallet.wallet import Wallet
|
||||
from cashu.wallet.wallet import Wallet as Wallet1
|
||||
from tests.conftest import SERVER_ENDPOINT
|
||||
from tests.helpers import pay_if_regtest
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="function")
|
||||
@@ -23,8 +24,10 @@ async def wallet1(mint):
|
||||
async def test_melt(wallet1: Wallet, ledger: Ledger):
|
||||
# mint twice so we have enough to pay the second invoice back
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
assert wallet1.balance == 128
|
||||
total_amount, fee_reserve_sat = await wallet1.get_pay_amount_with_fees(
|
||||
@@ -41,6 +44,7 @@ async def test_melt(wallet1: Wallet, ledger: Ledger):
|
||||
@pytest.mark.asyncio
|
||||
async def test_split(wallet1: Wallet, ledger: Ledger):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
|
||||
keep_proofs, send_proofs = await wallet1.split_to_send(wallet1.proofs, 10)
|
||||
@@ -57,6 +61,7 @@ async def test_split(wallet1: Wallet, ledger: Ledger):
|
||||
@pytest.mark.asyncio
|
||||
async def test_check_proof_state(wallet1: Wallet, ledger: Ledger):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
|
||||
keep_proofs, send_proofs = await wallet1.split_to_send(wallet1.proofs, 10)
|
||||
|
||||
@@ -14,6 +14,7 @@ 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 get_real_invoice, is_regtest, pay_if_regtest
|
||||
|
||||
|
||||
async def assert_err(f, msg: Union[str, CashuError]):
|
||||
@@ -138,6 +139,7 @@ async def test_get_keyset_ids(wallet1: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_mint(wallet1: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
assert wallet1.balance == 64
|
||||
|
||||
@@ -158,6 +160,7 @@ async def test_mint(wallet1: Wallet):
|
||||
async def test_mint_amounts(wallet1: Wallet):
|
||||
"""Mint predefined amounts"""
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
amts = [1, 1, 1, 2, 2, 4, 16]
|
||||
await wallet1.mint(amount=sum(amts), split=amts, id=invoice.id)
|
||||
assert wallet1.balance == 27
|
||||
@@ -187,6 +190,7 @@ async def test_mint_amounts_wrong_order(wallet1: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_split(wallet1: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
assert wallet1.balance == 64
|
||||
p1, p2 = await wallet1.split(wallet1.proofs, 20)
|
||||
@@ -202,6 +206,7 @@ async def test_split(wallet1: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_split_to_send(wallet1: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
keep_proofs, spendable_proofs = await wallet1.split_to_send(
|
||||
wallet1.proofs, 32, set_reserved=True
|
||||
@@ -217,6 +222,7 @@ async def test_split_to_send(wallet1: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_split_more_than_balance(wallet1: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
await assert_err(
|
||||
wallet1.split(wallet1.proofs, 128),
|
||||
@@ -230,8 +236,10 @@ async def test_split_more_than_balance(wallet1: Wallet):
|
||||
async def test_melt(wallet1: Wallet):
|
||||
# mint twice so we have enough to pay the second invoice back
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
assert wallet1.balance == 128
|
||||
|
||||
@@ -243,38 +251,46 @@ async def test_melt(wallet1: Wallet):
|
||||
assert fee_reserve_sat == 2
|
||||
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, total_amount)
|
||||
|
||||
invoice_to_pay = invoice.bolt11
|
||||
invoice_payment_hash = str(invoice.payment_hash)
|
||||
if is_regtest:
|
||||
invoice_dict = get_real_invoice(64)
|
||||
invoice_to_pay = invoice_dict["payment_request"]
|
||||
invoice_payment_hash = str(invoice_dict["r_hash"])
|
||||
|
||||
melt_response = await wallet1.pay_lightning(
|
||||
send_proofs, invoice=invoice.bolt11, fee_reserve_sat=fee_reserve_sat
|
||||
send_proofs, invoice=invoice_to_pay, fee_reserve_sat=fee_reserve_sat
|
||||
)
|
||||
|
||||
assert melt_response.change
|
||||
assert len(melt_response.change) == 1
|
||||
assert melt_response.change, "No change returned"
|
||||
assert len(melt_response.change) == 1, "More than one change returned"
|
||||
# NOTE: we assume that we will get a token back from the same keyset as the ones we melted
|
||||
# this could be wrong if we melted tokens from an old keyset but the returned ones are
|
||||
# from a newer one.
|
||||
assert melt_response.change[0].id == send_proofs[0].id
|
||||
assert melt_response.change[0].id == send_proofs[0].id, "Wrong keyset returned"
|
||||
|
||||
# verify that proofs in proofs_used db have the same melt_id as the invoice in the db
|
||||
assert invoice.payment_hash
|
||||
assert invoice.payment_hash, "No payment hash in invoice"
|
||||
invoice_db = await get_lightning_invoice(
|
||||
db=wallet1.db, payment_hash=invoice.payment_hash, out=True
|
||||
db=wallet1.db, payment_hash=invoice_payment_hash, out=True
|
||||
)
|
||||
assert invoice_db
|
||||
assert invoice_db, "No invoice in db"
|
||||
proofs_used = await get_proofs(
|
||||
db=wallet1.db, melt_id=invoice_db.id, table="proofs_used"
|
||||
)
|
||||
|
||||
assert len(proofs_used) == len(send_proofs)
|
||||
assert all([p.melt_id == invoice_db.id for p in proofs_used])
|
||||
assert len(proofs_used) == len(send_proofs), "Not all proofs used"
|
||||
assert all([p.melt_id == invoice_db.id for p in proofs_used]), "Wrong melt_id"
|
||||
|
||||
# the payment was without fees so we need to remove it from the total amount
|
||||
assert wallet1.balance == 128 - (total_amount - fee_reserve_sat)
|
||||
assert wallet1.balance == 64
|
||||
assert wallet1.balance == 128 - (total_amount - fee_reserve_sat), "Wrong balance"
|
||||
assert wallet1.balance == 64, "Wrong balance"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_split_to_send_more_than_balance(wallet1: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
await assert_err(
|
||||
wallet1.split_to_send(wallet1.proofs, 128, set_reserved=True),
|
||||
@@ -287,6 +303,7 @@ async def test_split_to_send_more_than_balance(wallet1: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_double_spend(wallet1: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
doublespend = await wallet1.mint(64, id=invoice.id)
|
||||
await wallet1.split(wallet1.proofs, 20)
|
||||
await assert_err(
|
||||
@@ -300,6 +317,7 @@ async def test_double_spend(wallet1: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_duplicate_proofs_double_spent(wallet1: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
doublespend = await wallet1.mint(64, id=invoice.id)
|
||||
await assert_err(
|
||||
wallet1.split(wallet1.proofs + doublespend, 20),
|
||||
@@ -312,6 +330,7 @@ async def test_duplicate_proofs_double_spent(wallet1: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_and_redeem(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
_, spendable_proofs = await wallet1.split_to_send(
|
||||
wallet1.proofs, 32, set_reserved=True
|
||||
@@ -330,6 +349,7 @@ async def test_send_and_redeem(wallet1: Wallet, wallet2: Wallet):
|
||||
async def test_invalidate_unspent_proofs(wallet1: Wallet):
|
||||
"""Try to invalidate proofs that have not been spent yet. Should not work!"""
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
await wallet1.invalidate(wallet1.proofs)
|
||||
assert wallet1.balance == 64
|
||||
@@ -339,6 +359,7 @@ async def test_invalidate_unspent_proofs(wallet1: Wallet):
|
||||
async def test_invalidate_unspent_proofs_without_checking(wallet1: Wallet):
|
||||
"""Try to invalidate proofs that have not been spent yet but force no check."""
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
await wallet1.invalidate(wallet1.proofs, check_spendable=False)
|
||||
assert wallet1.balance == 0
|
||||
@@ -347,6 +368,7 @@ async def test_invalidate_unspent_proofs_without_checking(wallet1: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_split_invalid_amount(wallet1: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
await assert_err(
|
||||
wallet1.split(wallet1.proofs, -1),
|
||||
@@ -357,6 +379,7 @@ async def test_split_invalid_amount(wallet1: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_token_state(wallet1: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
assert wallet1.balance == 64
|
||||
resp = await wallet1.check_proof_state(wallet1.proofs)
|
||||
|
||||
@@ -8,6 +8,7 @@ from cashu.lightning.base import InvoiceResponse, PaymentStatus
|
||||
from cashu.wallet.api.app import app
|
||||
from cashu.wallet.wallet import Wallet
|
||||
from tests.conftest import SERVER_ENDPOINT
|
||||
from tests.helpers import is_regtest
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="function")
|
||||
@@ -22,6 +23,7 @@ async def wallet(mint):
|
||||
yield wallet
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_invoice(wallet: Wallet):
|
||||
with TestClient(app) as client:
|
||||
@@ -40,6 +42,7 @@ async def test_invoice(wallet: Wallet):
|
||||
print("paid")
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_balance():
|
||||
with TestClient(app) as client:
|
||||
@@ -50,6 +53,7 @@ async def test_balance():
|
||||
assert response.json()["mints"]
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_send(wallet: Wallet):
|
||||
with TestClient(app) as client:
|
||||
@@ -58,6 +62,7 @@ async def test_send(wallet: Wallet):
|
||||
assert response.json()["balance"]
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_without_split(wallet: Wallet):
|
||||
with TestClient(app) as client:
|
||||
@@ -66,6 +71,7 @@ async def test_send_without_split(wallet: Wallet):
|
||||
assert response.json()["balance"]
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_without_split_but_wrong_amount(wallet: Wallet):
|
||||
with TestClient(app) as client:
|
||||
@@ -73,6 +79,7 @@ async def test_send_without_split_but_wrong_amount(wallet: Wallet):
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_pending():
|
||||
with TestClient(app) as client:
|
||||
@@ -80,6 +87,7 @@ async def test_pending():
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_receive_all(wallet: Wallet):
|
||||
with TestClient(app) as client:
|
||||
@@ -89,6 +97,7 @@ async def test_receive_all(wallet: Wallet):
|
||||
assert response.json()["balance"]
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_burn_all(wallet: Wallet):
|
||||
with TestClient(app) as client:
|
||||
@@ -99,6 +108,7 @@ async def test_burn_all(wallet: Wallet):
|
||||
assert response.json()["balance"]
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_pay():
|
||||
with TestClient(app) as client:
|
||||
@@ -113,6 +123,7 @@ async def test_pay():
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_lock():
|
||||
with TestClient(app) as client:
|
||||
@@ -120,6 +131,7 @@ async def test_lock():
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_locks():
|
||||
with TestClient(app) as client:
|
||||
@@ -127,6 +139,7 @@ async def test_locks():
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_invoices():
|
||||
with TestClient(app) as client:
|
||||
@@ -134,6 +147,7 @@ async def test_invoices():
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_wallets():
|
||||
with TestClient(app) as client:
|
||||
@@ -141,6 +155,7 @@ async def test_wallets():
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_info():
|
||||
with TestClient(app) as client:
|
||||
@@ -149,6 +164,7 @@ async def test_info():
|
||||
assert response.json()["version"]
|
||||
|
||||
|
||||
@pytest.mark.skipif(is_regtest, reason="regtest")
|
||||
@pytest.mark.asyncio
|
||||
async def test_flow(wallet: Wallet):
|
||||
with TestClient(app) as client:
|
||||
|
||||
@@ -15,6 +15,7 @@ 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):
|
||||
@@ -59,6 +60,7 @@ async def wallet2(mint):
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_htlc_secret(wallet1: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
preimage = "00000000000000000000000000000000"
|
||||
preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
||||
@@ -69,6 +71,7 @@ async def test_create_htlc_secret(wallet1: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_htlc_split(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
preimage = "00000000000000000000000000000000"
|
||||
preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
||||
@@ -81,6 +84,7 @@ async def test_htlc_split(wallet1: Wallet, wallet2: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_htlc_redeem_with_preimage(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
preimage = "00000000000000000000000000000000"
|
||||
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
||||
@@ -94,6 +98,7 @@ async def test_htlc_redeem_with_preimage(wallet1: Wallet, wallet2: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_htlc_redeem_with_wrong_preimage(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
preimage = "00000000000000000000000000000000"
|
||||
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
|
||||
@@ -111,6 +116,7 @@ async def test_htlc_redeem_with_wrong_preimage(wallet1: Wallet, wallet2: Wallet)
|
||||
@pytest.mark.asyncio
|
||||
async def test_htlc_redeem_with_no_signature(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
preimage = "00000000000000000000000000000000"
|
||||
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
||||
@@ -130,6 +136,7 @@ async def test_htlc_redeem_with_no_signature(wallet1: Wallet, wallet2: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_htlc_redeem_with_wrong_signature(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
preimage = "00000000000000000000000000000000"
|
||||
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
||||
@@ -153,6 +160,7 @@ async def test_htlc_redeem_with_wrong_signature(wallet1: Wallet, wallet2: Wallet
|
||||
@pytest.mark.asyncio
|
||||
async def test_htlc_redeem_with_correct_signature(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
preimage = "00000000000000000000000000000000"
|
||||
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
||||
@@ -174,6 +182,7 @@ async def test_htlc_redeem_hashlock_wrong_signature_timelock_correct_signature(
|
||||
wallet1: Wallet, wallet2: Wallet
|
||||
):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
preimage = "00000000000000000000000000000000"
|
||||
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
||||
@@ -207,6 +216,7 @@ async def test_htlc_redeem_hashlock_wrong_signature_timelock_wrong_signature(
|
||||
wallet1: Wallet, wallet2: Wallet
|
||||
):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
preimage = "00000000000000000000000000000000"
|
||||
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
||||
|
||||
@@ -16,6 +16,7 @@ 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):
|
||||
@@ -60,6 +61,7 @@ async def wallet2(mint):
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_p2pk_pubkey(wallet1: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
pubkey = await wallet1.create_p2pk_pubkey()
|
||||
PublicKey(bytes.fromhex(pubkey), raw=True)
|
||||
@@ -68,6 +70,7 @@ async def test_create_p2pk_pubkey(wallet1: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_p2pk(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
pubkey_wallet2 = await wallet2.create_p2pk_pubkey()
|
||||
# p2pk test
|
||||
@@ -81,6 +84,7 @@ async def test_p2pk(wallet1: Wallet, wallet2: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_p2pk_sig_all(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
pubkey_wallet2 = await wallet2.create_p2pk_pubkey()
|
||||
# p2pk test
|
||||
@@ -96,6 +100,7 @@ async def test_p2pk_sig_all(wallet1: Wallet, wallet2: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_p2pk_receive_with_wrong_private_key(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
pubkey_wallet2 = await wallet2.create_p2pk_pubkey() # receiver side
|
||||
# sender side
|
||||
@@ -116,6 +121,7 @@ async def test_p2pk_short_locktime_receive_with_wrong_private_key(
|
||||
wallet1: Wallet, wallet2: Wallet
|
||||
):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
pubkey_wallet2 = await wallet2.create_p2pk_pubkey() # receiver side
|
||||
# sender side
|
||||
@@ -141,6 +147,7 @@ async def test_p2pk_short_locktime_receive_with_wrong_private_key(
|
||||
@pytest.mark.asyncio
|
||||
async def test_p2pk_locktime_with_refund_pubkey(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
pubkey_wallet2 = await wallet2.create_p2pk_pubkey() # receiver side
|
||||
# sender side
|
||||
@@ -169,6 +176,7 @@ async def test_p2pk_locktime_with_refund_pubkey(wallet1: Wallet, wallet2: Wallet
|
||||
@pytest.mark.asyncio
|
||||
async def test_p2pk_locktime_with_wrong_refund_pubkey(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
await wallet2.create_p2pk_pubkey() # receiver side
|
||||
# sender side
|
||||
@@ -204,6 +212,7 @@ async def test_p2pk_locktime_with_second_refund_pubkey(
|
||||
wallet1: Wallet, wallet2: Wallet
|
||||
):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
pubkey_wallet1 = await wallet1.create_p2pk_pubkey() # receiver side
|
||||
pubkey_wallet2 = await wallet2.create_p2pk_pubkey() # receiver side
|
||||
@@ -235,6 +244,7 @@ async def test_p2pk_locktime_with_second_refund_pubkey(
|
||||
@pytest.mark.asyncio
|
||||
async def test_p2pk_multisig_2_of_2(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
||||
pubkey_wallet2 = await wallet2.create_p2pk_pubkey()
|
||||
@@ -256,6 +266,7 @@ async def test_p2pk_multisig_2_of_2(wallet1: Wallet, wallet2: Wallet):
|
||||
@pytest.mark.asyncio
|
||||
async def test_p2pk_multisig_duplicate_signature(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
||||
pubkey_wallet2 = await wallet2.create_p2pk_pubkey()
|
||||
@@ -279,6 +290,7 @@ async def test_p2pk_multisig_duplicate_signature(wallet1: Wallet, wallet2: Walle
|
||||
@pytest.mark.asyncio
|
||||
async def test_p2pk_multisig_quorum_not_met_1_of_2(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
||||
pubkey_wallet2 = await wallet2.create_p2pk_pubkey()
|
||||
@@ -299,6 +311,7 @@ async def test_p2pk_multisig_quorum_not_met_1_of_2(wallet1: Wallet, wallet2: Wal
|
||||
@pytest.mark.asyncio
|
||||
async def test_p2pk_multisig_quorum_not_met_2_of_3(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
|
||||
pubkey_wallet2 = await wallet2.create_p2pk_pubkey()
|
||||
@@ -323,6 +336,7 @@ async def test_p2pk_multisig_quorum_not_met_2_of_3(wallet1: Wallet, wallet2: Wal
|
||||
@pytest.mark.asyncio
|
||||
async def test_p2pk_multisig_with_duplicate_publickey(wallet1: Wallet, wallet2: Wallet):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
pubkey_wallet2 = await wallet2.create_p2pk_pubkey()
|
||||
# p2pk test
|
||||
@@ -340,6 +354,7 @@ async def test_p2pk_multisig_with_wrong_first_private_key(
|
||||
wallet1: Wallet, wallet2: Wallet
|
||||
):
|
||||
invoice = await wallet1.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet1.mint(64, id=invoice.id)
|
||||
await wallet1.create_p2pk_pubkey()
|
||||
pubkey_wallet2 = await wallet2.create_p2pk_pubkey()
|
||||
|
||||
@@ -12,6 +12,7 @@ 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: Union[str, CashuError]):
|
||||
@@ -147,6 +148,7 @@ async def test_generate_secrets_from_to(wallet3: Wallet):
|
||||
async def test_restore_wallet_after_mint(wallet3: Wallet):
|
||||
await reset_wallet_db(wallet3)
|
||||
invoice = await wallet3.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet3.mint(64, id=invoice.id)
|
||||
assert wallet3.balance == 64
|
||||
await reset_wallet_db(wallet3)
|
||||
@@ -177,6 +179,7 @@ async def test_restore_wallet_after_split_to_send(wallet3: Wallet):
|
||||
await reset_wallet_db(wallet3)
|
||||
|
||||
invoice = await wallet3.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet3.mint(64, id=invoice.id)
|
||||
assert wallet3.balance == 64
|
||||
|
||||
@@ -199,6 +202,7 @@ async def test_restore_wallet_after_send_and_receive(wallet3: Wallet, wallet2: W
|
||||
)
|
||||
await reset_wallet_db(wallet3)
|
||||
invoice = await wallet3.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet3.mint(64, id=invoice.id)
|
||||
assert wallet3.balance == 64
|
||||
|
||||
@@ -239,6 +243,7 @@ async def test_restore_wallet_after_send_and_self_receive(wallet3: Wallet):
|
||||
await reset_wallet_db(wallet3)
|
||||
|
||||
invoice = await wallet3.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet3.mint(64, id=invoice.id)
|
||||
assert wallet3.balance == 64
|
||||
|
||||
@@ -265,6 +270,7 @@ async def test_restore_wallet_after_send_twice(
|
||||
await reset_wallet_db(wallet3)
|
||||
|
||||
invoice = await wallet3.request_mint(2)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet3.mint(2, id=invoice.id)
|
||||
box.add(wallet3.proofs)
|
||||
assert wallet3.balance == 2
|
||||
@@ -319,6 +325,7 @@ async def test_restore_wallet_after_send_and_self_receive_nonquadratic_value(
|
||||
await reset_wallet_db(wallet3)
|
||||
|
||||
invoice = await wallet3.request_mint(64)
|
||||
pay_if_regtest(invoice.bolt11)
|
||||
await wallet3.mint(64, id=invoice.id)
|
||||
box.add(wallet3.proofs)
|
||||
assert wallet3.balance == 64
|
||||
|
||||
Reference in New Issue
Block a user