Skip to content

Commit

Permalink
test with lightning=true
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Sep 24, 2023
1 parent 449cb68 commit 1f586f5
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 51 deletions.
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
settings.mint_host = "0.0.0.0"
settings.mint_listen_port = SERVER_PORT
settings.mint_url = SERVER_ENDPOINT
settings.lightning = False
settings.lightning = True
settings.tor = False
settings.mint_lightning_backend = "FakeWallet"
settings.mint_database = "./test_data/test_mint"
Expand Down
7 changes: 5 additions & 2 deletions tests/test_mint.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ async def test_get_keyset(ledger: Ledger):

@pytest.mark.asyncio
async def test_mint(ledger: Ledger):
invoice, payment_hash = await ledger.request_mint(8)
blinded_messages_mock = [
BlindedMessage(
amount=8,
B_="02634a2c2b34bec9e8a4aba4361f6bf202d7fa2365379b0840afe249a7a9d71239",
)
]
promises = await ledger.mint(blinded_messages_mock)
promises = await ledger.mint(blinded_messages_mock, hash=payment_hash)
assert len(promises)
assert promises[0].amount == 8
assert (
Expand All @@ -83,14 +84,16 @@ async def test_mint(ledger: Ledger):

@pytest.mark.asyncio
async def test_mint_invalid_blinded_message(ledger: Ledger):
invoice, payment_hash = await ledger.request_mint(8)
blinded_messages_mock_invalid_key = [
BlindedMessage(
amount=8,
B_="02634a2c2b34bec9e8a4aba4361f6bff02d7fa2365379b0840afe249a7a9d71237",
)
]
await assert_err(
ledger.mint(blinded_messages_mock_invalid_key), "invalid public key"
ledger.mint(blinded_messages_mock_invalid_key, hash=payment_hash),
"invalid public key",
)


Expand Down
67 changes: 44 additions & 23 deletions tests/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,17 @@ async def test_get_keyset_ids(wallet1: Wallet):

@pytest.mark.asyncio
async def test_mint(wallet1: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
assert wallet1.balance == 64


@pytest.mark.asyncio
async def test_mint_amounts(wallet1: Wallet):
"""Mint predefined amounts"""
invoice = await wallet1.request_mint(64)
amts = [1, 1, 1, 2, 2, 4, 16]
await wallet1.mint(amount=sum(amts), split=amts)
await wallet1.mint(amount=sum(amts), split=amts, hash=invoice.hash)
assert wallet1.balance == 27
assert wallet1.proof_amounts == amts

Expand All @@ -171,7 +173,8 @@ async def test_mint_amounts_wrong_order(wallet1: Wallet):

@pytest.mark.asyncio
async def test_split(wallet1: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
assert wallet1.balance == 64
p1, p2 = await wallet1.split(wallet1.proofs, 20)
assert wallet1.balance == 64
Expand All @@ -185,7 +188,8 @@ async def test_split(wallet1: Wallet):

@pytest.mark.asyncio
async def test_split_to_send(wallet1: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
keep_proofs, spendable_proofs = await wallet1.split_to_send(
wallet1.proofs, 32, set_reserved=True
)
Expand All @@ -199,7 +203,8 @@ async def test_split_to_send(wallet1: Wallet):

@pytest.mark.asyncio
async def test_split_more_than_balance(wallet1: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
await assert_err(
wallet1.split(wallet1.proofs, 128),
# "Mint Error: inputs do not have same amount as outputs",
Expand All @@ -210,7 +215,8 @@ async def test_split_more_than_balance(wallet1: Wallet):

@pytest.mark.asyncio
async def test_split_to_send_more_than_balance(wallet1: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
await assert_err(
wallet1.split_to_send(wallet1.proofs, 128, set_reserved=True),
"balance too low.",
Expand All @@ -221,7 +227,8 @@ async def test_split_to_send_more_than_balance(wallet1: Wallet):

@pytest.mark.asyncio
async def test_double_spend(wallet1: Wallet):
doublespend = await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
doublespend = await wallet1.mint(64, hash=invoice.hash)
await wallet1.split(wallet1.proofs, 20)
await assert_err(
wallet1.split(doublespend, 20),
Expand All @@ -233,7 +240,8 @@ async def test_double_spend(wallet1: Wallet):

@pytest.mark.asyncio
async def test_duplicate_proofs_double_spent(wallet1: Wallet):
doublespend = await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
doublespend = await wallet1.mint(64, hash=invoice.hash)
await assert_err(
wallet1.split(wallet1.proofs + doublespend, 20),
"Mint Error: proofs already pending.",
Expand All @@ -244,7 +252,8 @@ async def test_duplicate_proofs_double_spent(wallet1: Wallet):

@pytest.mark.asyncio
async def test_send_and_redeem(wallet1: Wallet, wallet2: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
_, spendable_proofs = await wallet1.split_to_send(
wallet1.proofs, 32, set_reserved=True
)
Expand All @@ -261,22 +270,25 @@ async def test_send_and_redeem(wallet1: Wallet, wallet2: Wallet):
@pytest.mark.asyncio
async def test_invalidate_unspent_proofs(wallet1: Wallet):
"""Try to invalidate proofs that have not been spent yet. Should not work!"""
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
await wallet1.invalidate(wallet1.proofs)
assert wallet1.balance == 64


@pytest.mark.asyncio
async def test_invalidate_unspent_proofs_without_checking(wallet1: Wallet):
"""Try to invalidate proofs that have not been spent yet but force no check."""
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
await wallet1.invalidate(wallet1.proofs, check_spendable=False)
assert wallet1.balance == 0


@pytest.mark.asyncio
async def test_split_invalid_amount(wallet1: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
await assert_err(
wallet1.split(wallet1.proofs, -1),
"amount must be positive.",
Expand All @@ -285,14 +297,16 @@ async def test_split_invalid_amount(wallet1: Wallet):

@pytest.mark.asyncio
async def test_create_p2pk_pubkey(wallet1: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
pubkey = await wallet1.create_p2pk_pubkey()
PublicKey(bytes.fromhex(pubkey), raw=True)


@pytest.mark.asyncio
async def test_p2sh(wallet1: Wallet, wallet2: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
_ = await wallet1.create_p2sh_address_and_store() # receiver side
_, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8) # sender side

Expand All @@ -305,7 +319,8 @@ async def test_p2sh(wallet1: Wallet, wallet2: Wallet):

@pytest.mark.asyncio
async def test_p2sh_receive_with_wrong_wallet(wallet1: Wallet, wallet2: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
wallet1_address = await wallet1.create_p2sh_address_and_store() # receiver side
secret_lock = await wallet1.create_p2sh_lock(wallet1_address) # sender side
_, send_proofs = await wallet1.split_to_send(
Expand All @@ -316,7 +331,8 @@ async def test_p2sh_receive_with_wrong_wallet(wallet1: Wallet, wallet2: Wallet):

@pytest.mark.asyncio
async def test_token_state(wallet1: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
assert wallet1.balance == 64
resp = await wallet1.check_proof_state(wallet1.proofs)
assert resp.dict()["spendable"]
Expand Down Expand Up @@ -382,7 +398,8 @@ async def test_generate_secrets_from_to(wallet3: Wallet):
@pytest.mark.asyncio
async def test_restore_wallet_after_mint(wallet3: Wallet):
await reset_wallet_db(wallet3)
await wallet3.mint(64)
invoice = await wallet3.request_mint(64)
await wallet3.mint(64, hash=invoice.hash)
assert wallet3.balance == 64
await reset_wallet_db(wallet3)
await wallet3.load_proofs()
Expand Down Expand Up @@ -411,7 +428,8 @@ async def test_restore_wallet_after_split_to_send(wallet3: Wallet):
)
await reset_wallet_db(wallet3)

await wallet3.mint(64)
invoice = await wallet3.request_mint(64)
await wallet3.mint(64, hash=invoice.hash)
assert wallet3.balance == 64

_, spendable_proofs = await wallet3.split_to_send(wallet3.proofs, 32, set_reserved=True) # type: ignore
Expand All @@ -432,8 +450,8 @@ async def test_restore_wallet_after_send_and_receive(wallet3: Wallet, wallet2: W
"hello rug want adapt talent together lunar method bean expose beef position"
)
await reset_wallet_db(wallet3)

await wallet3.mint(64)
invoice = await wallet3.request_mint(64)
await wallet3.mint(64, hash=invoice.hash)
assert wallet3.balance == 64

_, spendable_proofs = await wallet3.split_to_send(wallet3.proofs, 32, set_reserved=True) # type: ignore
Expand Down Expand Up @@ -472,7 +490,8 @@ async def test_restore_wallet_after_send_and_self_receive(wallet3: Wallet):
)
await reset_wallet_db(wallet3)

await wallet3.mint(64)
invoice = await wallet3.request_mint(64)
await wallet3.mint(64, hash=invoice.hash)
assert wallet3.balance == 64

_, spendable_proofs = await wallet3.split_to_send(wallet3.proofs, 32, set_reserved=True) # type: ignore
Expand All @@ -497,7 +516,8 @@ async def test_restore_wallet_after_send_twice(
wallet3.private_key = PrivateKey()
await reset_wallet_db(wallet3)

await wallet3.mint(2)
invoice = await wallet3.request_mint(2)
await wallet3.mint(2, hash=invoice.hash)
box.add(wallet3.proofs)
assert wallet3.balance == 2

Expand Down Expand Up @@ -550,7 +570,8 @@ async def test_restore_wallet_after_send_and_self_receive_nonquadratic_value(
)
await reset_wallet_db(wallet3)

await wallet3.mint(64)
invoice = await wallet3.request_mint(64)
await wallet3.mint(64, hash=invoice.hash)
box.add(wallet3.proofs)
assert wallet3.balance == 64

Expand Down
27 changes: 18 additions & 9 deletions tests/test_wallet_htlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ async def wallet2(mint):

@pytest.mark.asyncio
async def test_create_htlc_secret(wallet1: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
preimage = "00000000000000000000000000000000"
preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
secret = await wallet1.create_htlc_lock(preimage=preimage)
Expand All @@ -67,7 +68,8 @@ async def test_create_htlc_secret(wallet1: Wallet):

@pytest.mark.asyncio
async def test_htlc_split(wallet1: Wallet, wallet2: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
preimage = "00000000000000000000000000000000"
preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
secret = await wallet1.create_htlc_lock(preimage=preimage)
Expand All @@ -79,7 +81,8 @@ async def test_htlc_split(wallet1: Wallet, wallet2: Wallet):

@pytest.mark.asyncio
async def test_htlc_redeem_with_preimage(wallet1: Wallet, wallet2: Wallet):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
preimage = "00000000000000000000000000000000"
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
secret = await wallet1.create_htlc_lock(preimage=preimage)
Expand All @@ -92,7 +95,8 @@ 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):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
preimage = "00000000000000000000000000000000"
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
secret = await wallet1.create_htlc_lock(preimage=preimage[:-1] + "1")
Expand All @@ -107,7 +111,8 @@ 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):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
preimage = "00000000000000000000000000000000"
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
Expand All @@ -126,7 +131,8 @@ 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):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
preimage = "00000000000000000000000000000000"
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
Expand All @@ -149,7 +155,8 @@ 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):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
preimage = "00000000000000000000000000000000"
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
# preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest()
Expand All @@ -171,7 +178,8 @@ async def test_htlc_redeem_with_correct_signature(wallet1: Wallet, wallet2: Wall
async def test_htlc_redeem_hashlock_wrong_signature_timelock_correct_signature(
wallet1: Wallet, wallet2: Wallet
):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
preimage = "00000000000000000000000000000000"
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
pubkey_wallet2 = await wallet2.create_p2pk_pubkey()
Expand Down Expand Up @@ -205,7 +213,8 @@ async def test_htlc_redeem_hashlock_wrong_signature_timelock_correct_signature(
async def test_htlc_redeem_hashlock_wrong_signature_timelock_wrong_signature(
wallet1: Wallet, wallet2: Wallet
):
await wallet1.mint(64)
invoice = await wallet1.request_mint(64)
await wallet1.mint(64, hash=invoice.hash)
preimage = "00000000000000000000000000000000"
pubkey_wallet1 = await wallet1.create_p2pk_pubkey()
pubkey_wallet2 = await wallet2.create_p2pk_pubkey()
Expand Down
Loading

0 comments on commit 1f586f5

Please sign in to comment.