|
1 | 1 | import asyncio |
| 2 | +import time |
2 | 3 | from typing import Tuple |
3 | 4 |
|
4 | 5 | import bolt11 |
5 | 6 | import pytest |
6 | 7 | from click.testing import CliRunner |
7 | 8 |
|
8 | 9 | from cashu.core.base import TokenV4 |
| 10 | +from cashu.core.p2pk import P2PKSecret |
9 | 11 | from cashu.core.settings import settings |
10 | 12 | from cashu.wallet.cli.cli import cli |
11 | 13 | from cashu.wallet.wallet import Wallet |
@@ -687,6 +689,74 @@ def test_send_with_lock_and_refund(mint, cli_prefix): |
687 | 689 | assert fake_refund_pubkey in token.token[0].proofs[0].secret |
688 | 690 |
|
689 | 691 |
|
| 692 | +def test_send_with_lock_and_timelock(mint, cli_prefix): |
| 693 | + runner = CliRunner() |
| 694 | + result = runner.invoke( |
| 695 | + cli, |
| 696 | + [*cli_prefix, "locks"], |
| 697 | + ) |
| 698 | + assert result.exception is None |
| 699 | + lock = None |
| 700 | + for word in result.output.split(" "): |
| 701 | + word = word.strip() |
| 702 | + if word.startswith("P2PK:"): |
| 703 | + lock = word |
| 704 | + break |
| 705 | + assert lock is not None, "no lock found" |
| 706 | + |
| 707 | + before = int(time.time()) |
| 708 | + result = runner.invoke( |
| 709 | + cli, |
| 710 | + [*cli_prefix, "send", "10", "--lock", lock, "--timelock", "5"], |
| 711 | + ) |
| 712 | + after = int(time.time()) |
| 713 | + assert result.exception is None |
| 714 | + print("test_send_with_lock_and_timelock", result.output) |
| 715 | + token_str = result.output.split("\n")[0] |
| 716 | + assert "cashuB" in token_str, "output does not have a token" |
| 717 | + token = TokenV4.deserialize(token_str).to_tokenv3() |
| 718 | + secret = P2PKSecret.deserialize(token.token[0].proofs[0].secret) |
| 719 | + assert secret.locktime is not None |
| 720 | + assert before + 5 <= secret.locktime <= after + 5 |
| 721 | + |
| 722 | + |
| 723 | +def test_send_with_lock_uses_locktime_delta_seconds_by_default(mint, cli_prefix): |
| 724 | + runner = CliRunner() |
| 725 | + result = runner.invoke( |
| 726 | + cli, |
| 727 | + [*cli_prefix, "locks"], |
| 728 | + ) |
| 729 | + assert result.exception is None |
| 730 | + lock = None |
| 731 | + for word in result.output.split(" "): |
| 732 | + word = word.strip() |
| 733 | + if word.startswith("P2PK:"): |
| 734 | + lock = word |
| 735 | + break |
| 736 | + assert lock is not None, "no lock found" |
| 737 | + |
| 738 | + before = int(time.time()) |
| 739 | + result = runner.invoke( |
| 740 | + cli, |
| 741 | + [*cli_prefix, "send", "10", "--lock", lock], |
| 742 | + ) |
| 743 | + after = int(time.time()) |
| 744 | + assert result.exception is None |
| 745 | + print( |
| 746 | + "test_send_with_lock_uses_locktime_delta_seconds_by_default", result.output |
| 747 | + ) |
| 748 | + token_str = result.output.split("\n")[0] |
| 749 | + assert "cashuB" in token_str, "output does not have a token" |
| 750 | + token = TokenV4.deserialize(token_str).to_tokenv3() |
| 751 | + secret = P2PKSecret.deserialize(token.token[0].proofs[0].secret) |
| 752 | + assert secret.locktime is not None |
| 753 | + assert ( |
| 754 | + before + settings.locktime_delta_seconds |
| 755 | + <= secret.locktime |
| 756 | + <= after + settings.locktime_delta_seconds |
| 757 | + ) |
| 758 | + |
| 759 | + |
690 | 760 | def mint_tokens(runner, cli_prefix, amount: str): |
691 | 761 | result = runner.invoke( |
692 | 762 | cli, |
|
0 commit comments