From f9307a559dcc146e3629fde2a7b93fb00d64bc3e Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 24 Sep 2023 17:46:56 +0200 Subject: [PATCH 1/5] mint operations --- tests/test_mint_operations.py | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/test_mint_operations.py diff --git a/tests/test_mint_operations.py b/tests/test_mint_operations.py new file mode 100644 index 00000000..c8e8606c --- /dev/null +++ b/tests/test_mint_operations.py @@ -0,0 +1,70 @@ +import pytest +import pytest_asyncio + +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 + + +@pytest_asyncio.fixture(scope="function") +async def wallet1(mint): + wallet1 = await Wallet1.with_db( + url=SERVER_ENDPOINT, + db="test_data/wallet1", + name="wallet1", + ) + await wallet1.load_mint() + wallet1.status() + yield wallet1 + + +@pytest.mark.asyncio +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) + await wallet1.mint(64, hash=invoice.hash) + invoice = await wallet1.request_mint(64) + await wallet1.mint(64, hash=invoice.hash) + assert wallet1.balance == 128 + total_amount, fee_reserve_sat = await wallet1.get_pay_amount_with_fees(invoice.pr) + mint_fees = await ledger.get_melt_fees(invoice.pr) + assert mint_fees == fee_reserve_sat + + keep_proofs, send_proofs = await wallet1.split_to_send(wallet1.proofs, total_amount) + + await ledger.melt(send_proofs, invoice.pr, outputs=None) + + +@pytest.mark.asyncio +async def test_split(wallet1: Wallet, ledger: Ledger): + # mint twice so we have enough to pay the second invoice back + invoice = await wallet1.request_mint(64) + await wallet1.mint(64, hash=invoice.hash) + + keep_proofs, send_proofs = await wallet1.split_to_send(wallet1.proofs, 10) + secrets, rs, derivation_paths = await wallet1.generate_n_secrets(len(send_proofs)) + outputs, rs = wallet1._construct_outputs( + [p.amount for p in send_proofs], secrets, rs + ) + + promises = await ledger.split(proofs=send_proofs, outputs=outputs) + assert len(promises) == len(outputs) + assert [p.amount for p in promises] == [p.amount for p in outputs] + + +@pytest.mark.asyncio +async def test_check_proof_state(wallet1: Wallet, ledger: Ledger): + # mint twice so we have enough to pay the second invoice back + invoice = await wallet1.request_mint(64) + await wallet1.mint(64, hash=invoice.hash) + + keep_proofs, send_proofs = await wallet1.split_to_send(wallet1.proofs, 10) + secrets, rs, derivation_paths = await wallet1.generate_n_secrets(len(send_proofs)) + outputs, rs = wallet1._construct_outputs( + [p.amount for p in send_proofs], secrets, rs + ) + + spendable, pending = await ledger.check_proof_state(proofs=send_proofs) + assert sum(spendable) == len(send_proofs) + assert sum(pending) == 0 From b558c68193eef91bbccbe4049608316e209472fe Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:10:41 +0200 Subject: [PATCH 2/5] fix spelling --- cashu/core/secret.py | 7 ++++++- cashu/wallet/htlc.py | 6 +++--- tests/test_wallet_htlc.py | 12 ++++++------ tests/test_wallet_p2pk.py | 6 ++++++ 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cashu/core/secret.py b/cashu/core/secret.py index 72bbd1f9..cca5723c 100644 --- a/cashu/core/secret.py +++ b/cashu/core/secret.py @@ -25,7 +25,12 @@ def __init__(self, tags: Optional[List[List[str]]] = None, **kwargs): self.__root__ = tags or [] def __setitem__(self, key: str, value: str) -> None: - self.__root__.append([key, value]) + if self.get_tag(key) is None: + self.__root__.append([key, value]) + else: + for tag in self.__root__: + if tag[0] == key: + tag.append(value) def __getitem__(self, key: str) -> Union[str, None]: return self.get_tag(key) diff --git a/cashu/wallet/htlc.py b/cashu/wallet/htlc.py index 9ed5dc68..dae24571 100644 --- a/cashu/wallet/htlc.py +++ b/cashu/wallet/htlc.py @@ -22,7 +22,7 @@ async def create_htlc_lock( *, preimage: Optional[str] = None, preimage_hash: Optional[str] = None, - hacklock_pubkey: Optional[str] = None, + hashlock_pubkey: Optional[str] = None, locktime_seconds: Optional[int] = None, locktime_pubkey: Optional[str] = None, ) -> HTLCSecret: @@ -39,8 +39,8 @@ async def create_htlc_lock( assert preimage_hash, "preimage_hash or preimage must be provided" - if hacklock_pubkey: - tags["pubkeys"] = hacklock_pubkey + if hashlock_pubkey: + tags["pubkeys"] = hashlock_pubkey return HTLCSecret( kind=SecretKind.HTLC, diff --git a/tests/test_wallet_htlc.py b/tests/test_wallet_htlc.py index 5128b3ab..37833672 100644 --- a/tests/test_wallet_htlc.py +++ b/tests/test_wallet_htlc.py @@ -117,7 +117,7 @@ async def test_htlc_redeem_with_no_signature(wallet1: Wallet, wallet2: Wallet): pubkey_wallet1 = await wallet1.create_p2pk_pubkey() # preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest() secret = await wallet1.create_htlc_lock( - preimage=preimage, hacklock_pubkey=pubkey_wallet1 + preimage=preimage, hashlock_pubkey=pubkey_wallet1 ) # p2pk test _, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret) @@ -137,11 +137,11 @@ async def test_htlc_redeem_with_wrong_signature(wallet1: Wallet, wallet2: Wallet pubkey_wallet1 = await wallet1.create_p2pk_pubkey() # preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest() secret = await wallet1.create_htlc_lock( - preimage=preimage, hacklock_pubkey=pubkey_wallet1 + preimage=preimage, hashlock_pubkey=pubkey_wallet1 ) + # p2pk test _, 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.htlcpreimage = preimage @@ -161,7 +161,7 @@ async def test_htlc_redeem_with_correct_signature(wallet1: Wallet, wallet2: Wall pubkey_wallet1 = await wallet1.create_p2pk_pubkey() # preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest() secret = await wallet1.create_htlc_lock( - preimage=preimage, hacklock_pubkey=pubkey_wallet1 + preimage=preimage, hashlock_pubkey=pubkey_wallet1 ) # p2pk test _, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret) @@ -186,7 +186,7 @@ async def test_htlc_redeem_hashlock_wrong_signature_timelock_correct_signature( # preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest() secret = await wallet1.create_htlc_lock( preimage=preimage, - hacklock_pubkey=pubkey_wallet2, + hashlock_pubkey=pubkey_wallet2, locktime_seconds=5, locktime_pubkey=pubkey_wallet1, ) @@ -221,7 +221,7 @@ async def test_htlc_redeem_hashlock_wrong_signature_timelock_wrong_signature( # preimage_hash = hashlib.sha256(bytes.fromhex(preimage)).hexdigest() secret = await wallet1.create_htlc_lock( preimage=preimage, - hacklock_pubkey=pubkey_wallet2, + hashlock_pubkey=pubkey_wallet2, locktime_seconds=5, locktime_pubkey=pubkey_wallet1, ) diff --git a/tests/test_wallet_p2pk.py b/tests/test_wallet_p2pk.py index 883e64bd..feade986 100644 --- a/tests/test_wallet_p2pk.py +++ b/tests/test_wallet_p2pk.py @@ -360,6 +360,12 @@ def test_tags(): assert tags["key3"] is None assert tags.get_tag_all("key2") == ["value2", "value2_1", "value3"] + # set multiple values of the same key + tags["key3"] = "value3" + assert tags.get_tag_all("key3") == ["value3"] + tags["key3"] = "value4" + assert tags.get_tag_all("key3") == ["value3", "value4"] + @pytest.mark.asyncio async def test_secret_initialized_with_tags(wallet1: Wallet): From 60fc842348080e813dc44bddfc441e5b0b582b3c Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:45:50 +0200 Subject: [PATCH 3/5] add pending test --- cashu/lightning/fake.py | 3 +++ tests/test_mint_operations.py | 6 ------ tests/test_wallet_p2pk.py | 5 ----- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/cashu/lightning/fake.py b/cashu/lightning/fake.py index dfeed100..d2c617a5 100644 --- a/cashu/lightning/fake.py +++ b/cashu/lightning/fake.py @@ -77,6 +77,7 @@ async def create_invoice( async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse: invoice = decode(bolt11) + # await asyncio.sleep(5) if invoice.payment_hash[:6] == self.privkey[:6] or BRR: await self.queue.put(invoice) @@ -88,6 +89,8 @@ async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse ) async def get_invoice_status(self, checking_id: str) -> PaymentStatus: + # paid = random.random() > 0.7 + # return PaymentStatus(paid) paid = checking_id in self.paid_invoices or BRR return PaymentStatus(paid or None) diff --git a/tests/test_mint_operations.py b/tests/test_mint_operations.py index c8e8606c..916d8f56 100644 --- a/tests/test_mint_operations.py +++ b/tests/test_mint_operations.py @@ -38,7 +38,6 @@ async def test_melt(wallet1: Wallet, ledger: Ledger): @pytest.mark.asyncio async def test_split(wallet1: Wallet, ledger: Ledger): - # mint twice so we have enough to pay the second invoice back invoice = await wallet1.request_mint(64) await wallet1.mint(64, hash=invoice.hash) @@ -55,15 +54,10 @@ async def test_split(wallet1: Wallet, ledger: Ledger): @pytest.mark.asyncio async def test_check_proof_state(wallet1: Wallet, ledger: Ledger): - # mint twice so we have enough to pay the second invoice back invoice = await wallet1.request_mint(64) await wallet1.mint(64, hash=invoice.hash) keep_proofs, send_proofs = await wallet1.split_to_send(wallet1.proofs, 10) - secrets, rs, derivation_paths = await wallet1.generate_n_secrets(len(send_proofs)) - outputs, rs = wallet1._construct_outputs( - [p.amount for p in send_proofs], secrets, rs - ) spendable, pending = await ledger.check_proof_state(proofs=send_proofs) assert sum(spendable) == len(send_proofs) diff --git a/tests/test_wallet_p2pk.py b/tests/test_wallet_p2pk.py index feade986..7a27f9f5 100644 --- a/tests/test_wallet_p2pk.py +++ b/tests/test_wallet_p2pk.py @@ -376,11 +376,8 @@ async def test_secret_initialized_with_tags(wallet1: Wallet): pubkey=pubkey.serialize().hex(), tags=tags, ) - assert secret.locktime assert secret.locktime == 100 - assert secret.n_sigs assert secret.n_sigs == 3 - assert secret.sigflag assert secret.sigflag == SigFlags.SIG_ALL @@ -396,7 +393,5 @@ async def test_secret_initialized_with_arguments(wallet1: Wallet): ) assert secret.locktime assert secret.locktime > 1689000000 - assert secret.n_sigs assert secret.n_sigs == 3 - assert secret.sigflag assert secret.sigflag == SigFlags.SIG_ALL From 0255f04811ef5f733a1b6b944c756e3cdb6f7b8d Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:49:52 +0200 Subject: [PATCH 4/5] fix tag --- cashu/core/secret.py | 10 ++++------ tests/test_wallet_p2pk.py | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/cashu/core/secret.py b/cashu/core/secret.py index cca5723c..783cb72d 100644 --- a/cashu/core/secret.py +++ b/cashu/core/secret.py @@ -24,13 +24,11 @@ def __init__(self, tags: Optional[List[List[str]]] = None, **kwargs): super().__init__(**kwargs) self.__root__ = tags or [] - def __setitem__(self, key: str, value: str) -> None: - if self.get_tag(key) is None: + def __setitem__(self, key: str, value: Union[str, List[str]]) -> None: + if isinstance(value, str): self.__root__.append([key, value]) - else: - for tag in self.__root__: - if tag[0] == key: - tag.append(value) + elif isinstance(value, list): + self.__root__.append([key, *value]) def __getitem__(self, key: str) -> Union[str, None]: return self.get_tag(key) diff --git a/tests/test_wallet_p2pk.py b/tests/test_wallet_p2pk.py index 7a27f9f5..8985f0ad 100644 --- a/tests/test_wallet_p2pk.py +++ b/tests/test_wallet_p2pk.py @@ -363,8 +363,8 @@ def test_tags(): # set multiple values of the same key tags["key3"] = "value3" assert tags.get_tag_all("key3") == ["value3"] - tags["key3"] = "value4" - assert tags.get_tag_all("key3") == ["value3", "value4"] + tags["key4"] = ["value4", "value4_2"] + assert tags.get_tag_all("key4") == ["value4", "value4_2"] @pytest.mark.asyncio From a6d99c147c37aa9e88a23ec5c65d7e801e198601 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:52:46 +0200 Subject: [PATCH 5/5] make htlc tests less likelyto fail --- tests/test_wallet_htlc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_wallet_htlc.py b/tests/test_wallet_htlc.py index 37833672..6171aa56 100644 --- a/tests/test_wallet_htlc.py +++ b/tests/test_wallet_htlc.py @@ -99,7 +99,7 @@ async def test_htlc_redeem_with_wrong_preimage(wallet1: Wallet, wallet2: Wallet) 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") + secret = await wallet1.create_htlc_lock(preimage=preimage[:-5] + "11111") # p2pk test _, send_proofs = await wallet1.split_to_send(wallet1.proofs, 8, secret_lock=secret) for p in send_proofs: @@ -145,7 +145,7 @@ async def test_htlc_redeem_with_wrong_signature(wallet1: Wallet, wallet2: Wallet signatures = await wallet1.sign_p2pk_proofs(send_proofs) for p, s in zip(send_proofs, signatures): p.htlcpreimage = preimage - p.htlcsignature = s[:-1] + "1" # wrong signature + p.htlcsignature = s[:-5] + "11111" # wrong signature await assert_err( wallet2.redeem(send_proofs), @@ -231,7 +231,7 @@ async def test_htlc_redeem_hashlock_wrong_signature_timelock_wrong_signature( signatures = await wallet1.sign_p2pk_proofs(send_proofs) for p, s in zip(send_proofs, signatures): p.htlcpreimage = preimage - p.htlcsignature = s[:-1] + "1" # wrong signature + p.htlcsignature = s[:-5] + "11111" # wrong signature # should error because we used wallet2 signatures for the hash lock await assert_err(