1
0

add hold invoice cli functionality

This commit is contained in:
f321x
2025-03-31 13:08:09 +02:00
parent 2b68f65aac
commit 41b8b16a7c
4 changed files with 235 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
import unittest
from unittest import mock
from decimal import Decimal
from os import urandom
from electrum.commands import Commands, eval_bool
from electrum import storage, wallet
@@ -9,6 +10,8 @@ from electrum.address_synchronizer import TX_HEIGHT_UNCONFIRMED
from electrum.simple_config import SimpleConfig
from electrum.transaction import Transaction, TxOutput, tx_from_any
from electrum.util import UserFacingException, NotEnoughFunds
from electrum.crypto import sha256
from electrum.lnaddr import lndecode
from . import ElectrumTestCase
from .test_wallet_vertical import WalletIntegrityHelper
@@ -405,3 +408,78 @@ class TestCommandsTestnet(ElectrumTestCase):
self.assertEqual({"good_keys": 1, "bad_keys": 2},
await cmds.importprivkey(privkeys2_str, wallet=wallet))
self.assertEqual(10, len(wallet.get_addresses()))
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
async def test_hold_invoice_commands(self, mock_save_db):
wallet: Abstract_Wallet = restore_wallet_from_text(
'disagree rug lemon bean unaware square alone beach tennis exhibit fix mimic',
gap_limit=2,
path='if_this_exists_mocking_failed_648151893',
config=self.config)['wallet']
cmds = Commands(config=self.config)
preimage: str = sha256(urandom(32)).hex()
payment_hash: str = sha256(bytes.fromhex(preimage)).hex()
with (mock.patch.object(wallet.lnworker, 'num_sats_can_receive', return_value=1000000)):
result = await cmds.add_hold_invoice(
preimage=preimage,
amount=Decimal(0.0001),
memo="test",
expiry=3500,
wallet=wallet,
)
invoice = lndecode(invoice=result['invoice'])
assert invoice.paymenthash.hex() == payment_hash
assert payment_hash in wallet.lnworker.preimages
assert payment_hash in wallet.lnworker.payment_info
assert payment_hash in wallet.lnworker.dont_settle_htlcs
assert invoice.get_amount_sat() == 10000
cancel_result = await cmds.cancel_hold_invoice(
payment_hash=payment_hash,
wallet=wallet,
)
assert payment_hash not in wallet.lnworker.payment_info
assert payment_hash not in wallet.lnworker.dont_settle_htlcs
assert payment_hash not in wallet.lnworker.preimages
assert cancel_result['cancelled'] == payment_hash
with self.assertRaises(AssertionError):
# settling a cancelled invoice should raise
await cmds.settle_hold_invoice(
payment_hash=payment_hash,
wallet=wallet,
)
# cancelling an unknown invoice should raise
await cmds.cancel_hold_invoice(
payment_hash=sha256(urandom(32)).hex(),
wallet=wallet,
)
# add another hold invoice
preimage: bytes = sha256(urandom(32))
payment_hash: str = sha256(preimage).hex()
with mock.patch.object(wallet.lnworker, 'num_sats_can_receive', return_value=1000000):
await cmds.add_hold_invoice(
preimage=preimage.hex(),
amount=Decimal(0.0001),
wallet=wallet,
)
with mock.patch.object(wallet.lnworker, 'is_accepted_mpp', return_value=True), \
mock.patch.object(wallet.lnworker, 'get_payment_mpp_amount_msat', return_value=10_000 * 1000):
status: dict = await cmds.check_hold_invoice(payment_hash=payment_hash, wallet=wallet)
assert status['status'] == 'paid'
assert status['amount_sat'] == 10000
settle_result = await cmds.settle_hold_invoice(
payment_hash=payment_hash,
wallet=wallet,
)
assert settle_result['settled'] == payment_hash
assert wallet.lnworker.preimages[payment_hash] == preimage.hex()
assert payment_hash not in wallet.lnworker.dont_settle_htlcs
with self.assertRaises(AssertionError):
# cancelling a settled invoice should raise
await cmds.cancel_hold_invoice(payment_hash=payment_hash, wallet=wallet)

View File

@@ -331,6 +331,7 @@ class MockLNWallet(Logger, EventListener, NetworkRetryManager[LNPeerAddr]):
update_mpp_with_received_htlc = LNWallet.update_mpp_with_received_htlc
set_mpp_resolution = LNWallet.set_mpp_resolution
is_mpp_amount_reached = LNWallet.is_mpp_amount_reached
get_mpp_amounts = LNWallet.get_mpp_amounts
get_first_timestamp_of_mpp = LNWallet.get_first_timestamp_of_mpp
bundle_payments = LNWallet.bundle_payments
get_payment_bundle = LNWallet.get_payment_bundle