Switch TestWalletSource to use P2WPKH script
What changed, and why it matters
This commit changes a test-only helper in the Lightning Dev Kit codebase so that its fake wallet produces a more modern type of Bitcoin address (SegWit/P2WPKH) instead of the older P2PKH format. The change only affects internal test utilities and does not alter production code, network behavior, or cryptographic security. There is no indication this fixes or introduces a vulnerability.
No security action required. Treat as routine test-infrastructure refactoring. If reviewing for a release, verify that dependent dual-funding/splicing tests correctly exercise P2WPKH inputs.
Security signals we found
No security-relevant signals detected in the diff or commit metadata.
Change is confined to test utilities (`test_utils.rs`).
No change to production signing, transaction validation, or cryptographic code paths.
Commit message does not describe a bug, vulnerability, or security fix.
Evidence from the diff
The patch modifies TestWalletSource in lightning/src/util/test_utils.rs, a test fixture implementing the WalletSourceSync trait. It switches UTXO creation and change output from P2PKH (new_p2pkh/pubkey_hash) to native SegWit v0 P2WPKH (new_v0_p2wpkh/wpubkey_hash). It also refactors signing: sign_psbt now extracts the transaction and delegates to a new sign_tx method, which computes the P2WPKH sighash and attaches a Witness::p2wpkh instead of a legacy script_sig. The stated motivation is future reuse for dual-funding/splicing tests, which require standard SegWit inputs. No production wallet logic, consensus code, or cryptographic operations are changed.
Changed components
lightning/src/util/test_utils.rsTestWalletSource test fixtureInspect captured patch +29 / −26
diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs
index d28d0ab..50ae8f6 100644
--- a/lightning/src/util/test_utils.rs
+++ b/lightning/src/util/test_utils.rs
@@ -68,10 +68,10 @@ use bitcoin::constants::ChainHash;
use bitcoin::hash_types::{BlockHash, Txid};
use bitcoin::hashes::Hash;
use bitcoin::network::Network;
-use bitcoin::opcodes;
use bitcoin::script::{Builder, Script, ScriptBuf};
use bitcoin::sighash::{EcdsaSighashType, SighashCache};
use bitcoin::transaction::{Transaction, TxOut};
+use bitcoin::{opcodes, Witness};
use bitcoin::secp256k1::ecdh::SharedSecret;
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
@@ -2001,7 +2001,7 @@ impl TestWalletSource {
pub fn add_utxo(&self, outpoint: bitcoin::OutPoint, value: Amount) -> TxOut {
let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp));
- let utxo = Utxo::new_p2pkh(outpoint, value, &public_key.pubkey_hash());
+ let utxo = Utxo::new_v0_p2wpkh(outpoint, value, &public_key.wpubkey_hash().unwrap());
self.utxos.lock().unwrap().push(utxo.clone());
utxo.output
}
@@ -2015,44 +2015,47 @@ impl TestWalletSource {
pub fn remove_utxo(&self, outpoint: bitcoin::OutPoint) {
self.utxos.lock().unwrap().retain(|utxo| utxo.outpoint != outpoint);
}
-}
-
-impl WalletSourceSync for TestWalletSource {
- fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
- Ok(self.utxos.lock().unwrap().clone())
- }
- fn get_change_script(&self) -> Result<ScriptBuf, ()> {
- let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp));
- Ok(ScriptBuf::new_p2pkh(&public_key.pubkey_hash()))
- }
-
- fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()> {
- let mut tx = psbt.extract_tx_unchecked_fee_rate();
+ pub fn sign_tx(
+ &self, mut tx: Transaction,
+ ) -> Result<Transaction, bitcoin::sighash::P2wpkhError> {
let utxos = self.utxos.lock().unwrap();
for i in 0..tx.input.len() {
if let Some(utxo) =
utxos.iter().find(|utxo| utxo.outpoint == tx.input[i].previous_output)
{
- let sighash = SighashCache::new(&tx)
- .legacy_signature_hash(
- i,
- &utxo.output.script_pubkey,
- EcdsaSighashType::All as u32,
- )
- .map_err(|_| ())?;
+ let sighash = SighashCache::new(&tx).p2wpkh_signature_hash(
+ i,
+ &utxo.output.script_pubkey,
+ utxo.output.value,
+ EcdsaSighashType::All,
+ )?;
let signature = self.secp.sign_ecdsa(
&secp256k1::Message::from_digest(sighash.to_byte_array()),
&self.secret_key,
);
let bitcoin_sig =
bitcoin::ecdsa::Signature { signature, sighash_type: EcdsaSighashType::All };
- tx.input[i].script_sig = Builder::new()
- .push_slice(&bitcoin_sig.serialize())
- .push_slice(&self.secret_key.public_key(&self.secp).serialize())
- .into_script();
+ tx.input[i].witness =
+ Witness::p2wpkh(&bitcoin_sig, &self.secret_key.public_key(&self.secp));
}
}
Ok(tx)
}
}
+
+impl WalletSourceSync for TestWalletSource {
+ fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
+ Ok(self.utxos.lock().unwrap().clone())
+ }
+
+ fn get_change_script(&self) -> Result<ScriptBuf, ()> {
+ let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp));
+ Ok(ScriptBuf::new_p2wpkh(&public_key.wpubkey_hash().unwrap()))
+ }
+
+ fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()> {
+ let tx = psbt.extract_tx_unchecked_fee_rate();
+ self.sign_tx(tx).map_err(|_| ())
+ }
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.