From a66439eea54ec09c16fa9c9ea5d56b922a048997 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Sun, 18 May 2025 14:31:49 +0200 Subject: [PATCH] CLI: add preimage to get_invoice This should make regtest extract_preimage more reliable --- electrum/wallet.py | 4 ++++ tests/regtest/regtest.sh | 30 ++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/electrum/wallet.py b/electrum/wallet.py index c232fd47f..6a805ffbc 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -2864,6 +2864,10 @@ class Abstract_Wallet(ABC, Logger, EventListener): d['lightning_invoice'] = x.lightning_invoice if self.lnworker and status == PR_UNPAID: d['can_pay'] = self.lnworker.can_pay_invoice(x) + if self.lnworker and status == PR_PAID: + payment_hash = bytes.fromhex(d['invoice_id']) + preimage = self.lnworker.get_preimage(payment_hash) + d['preimage'] = preimage.hex() if preimage else None else: amount_sat = x.get_amount_sat() assert isinstance(amount_sat, (int, str, type(None))) diff --git a/tests/regtest/regtest.sh b/tests/regtest/regtest.sh index d50212ac2..f0d1112e4 100755 --- a/tests/regtest/regtest.sh +++ b/tests/regtest/regtest.sh @@ -67,6 +67,18 @@ function wait_until_channel_closed() printf "\n" } +function wait_until_preimage() +{ + msg="wait until $1 has preimage for $2" + cmd="./run_electrum --regtest -D /tmp/$1" + while [[ $($cmd get_invoice $2 | jq '.preimage' | tr -d '"') == "null" ]]; do + sleep 1 + msg="$msg." + printf "$msg\r" + done + printf "\n" +} + function wait_until_spent() { msg="wait until $1:$2 is spent" @@ -311,7 +323,9 @@ if [[ $1 == "extract_preimage" ]]; then wait_until_channel_open alice chan_id=$($alice list_channels | jq -r ".[0].channel_point") # alice pays bob - invoice1=$($bob add_request 0.04 --lightning --memo "test1" | jq -r ".lightning_invoice") + request1=$($bob add_request 0.04 --lightning --memo "test1") + invoice1=$(echo $request1 | jq -r ".lightning_invoice") + rhash1=$(echo $request1 | jq -r ".rhash") screen -S alice_payment -dm -L -Logfile /tmp/alice/screen1.log $alice lnpay $invoice1 --timeout=600 sleep 1 unsettled=$($alice list_channels | jq '.[] | .local_unsettled_sent') @@ -320,7 +334,9 @@ if [[ $1 == "extract_preimage" ]]; then exit 1 fi # bob pays alice - invoice2=$($alice add_request 0.04 --lightning --memo "test2" | jq -r ".lightning_invoice") + request2=$($alice add_request 0.04 --lightning --memo "test2") + invoice2=$(echo $request2 | jq -r ".lightning_invoice") + rhash2=$(echo $request2 | jq -r ".rhash") screen -S bob_payment -dm -L -Logfile /tmp/bob/screen2.log $bob lnpay $invoice2 --timeout=600 sleep 1 unsettled=$($bob list_channels | jq '.[] | .local_unsettled_sent') @@ -331,14 +347,8 @@ if [[ $1 == "extract_preimage" ]]; then # bob force closes $bob close_channel $chan_id --force new_blocks 1 - wait_until_channel_closed bob - wait_until_channel_closed alice - sleep 5 - # check logs - alice_log_found=$(grep -rnw "/tmp/alice/regtest/logs/" -e "found preimage in witness of length 5" | wc -l) - bob_log_found=$(grep -rnw "/tmp/bob/regtest/logs/" -e "found preimage in witness of length 3" | wc -l) - if [[ "$alice_log_found" != "1" ]]; then exit 1; fi - if [[ "$bob_log_found" != "1" ]]; then exit 1; fi + wait_until_preimage alice $rhash1 + wait_until_preimage bob $rhash2 # check both "lnpay" commands succeeded success=$(cat /tmp/alice/screen1.log | jq -r ".success") if [[ "$success" != "true" ]]; then exit 1; fi