Skip to content

Commit 749ebb8

Browse files
committed
Switch TestWalletSource to use P2WPKH script
We plan to reuse it for dual-funding/splicing, and those require standard SegWit inputs only.
1 parent f3c22a7 commit 749ebb8

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

lightning/src/util/test_utils.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ use bitcoin::constants::ChainHash;
6868
use bitcoin::hash_types::{BlockHash, Txid};
6969
use bitcoin::hashes::Hash;
7070
use bitcoin::network::Network;
71-
use bitcoin::opcodes;
7271
use bitcoin::script::{Builder, Script, ScriptBuf};
7372
use bitcoin::sighash::{EcdsaSighashType, SighashCache};
7473
use bitcoin::transaction::{Transaction, TxOut};
74+
use bitcoin::{opcodes, Witness};
7575

7676
use bitcoin::secp256k1::ecdh::SharedSecret;
7777
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
@@ -2001,7 +2001,7 @@ impl TestWalletSource {
20012001

20022002
pub fn add_utxo(&self, outpoint: bitcoin::OutPoint, value: Amount) -> TxOut {
20032003
let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp));
2004-
let utxo = Utxo::new_p2pkh(outpoint, value, &public_key.pubkey_hash());
2004+
let utxo = Utxo::new_v0_p2wpkh(outpoint, value, &public_key.wpubkey_hash().unwrap());
20052005
self.utxos.lock().unwrap().push(utxo.clone());
20062006
utxo.output
20072007
}
@@ -2015,44 +2015,47 @@ impl TestWalletSource {
20152015
pub fn remove_utxo(&self, outpoint: bitcoin::OutPoint) {
20162016
self.utxos.lock().unwrap().retain(|utxo| utxo.outpoint != outpoint);
20172017
}
2018-
}
2019-
2020-
impl WalletSourceSync for TestWalletSource {
2021-
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
2022-
Ok(self.utxos.lock().unwrap().clone())
2023-
}
20242018

2025-
fn get_change_script(&self) -> Result<ScriptBuf, ()> {
2026-
let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp));
2027-
Ok(ScriptBuf::new_p2pkh(&public_key.pubkey_hash()))
2028-
}
2029-
2030-
fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()> {
2031-
let mut tx = psbt.extract_tx_unchecked_fee_rate();
2019+
pub fn sign_tx(
2020+
&self, mut tx: Transaction,
2021+
) -> Result<Transaction, bitcoin::sighash::P2wpkhError> {
20322022
let utxos = self.utxos.lock().unwrap();
20332023
for i in 0..tx.input.len() {
20342024
if let Some(utxo) =
20352025
utxos.iter().find(|utxo| utxo.outpoint == tx.input[i].previous_output)
20362026
{
2037-
let sighash = SighashCache::new(&tx)
2038-
.legacy_signature_hash(
2039-
i,
2040-
&utxo.output.script_pubkey,
2041-
EcdsaSighashType::All as u32,
2042-
)
2043-
.map_err(|_| ())?;
2027+
let sighash = SighashCache::new(&tx).p2wpkh_signature_hash(
2028+
i,
2029+
&utxo.output.script_pubkey,
2030+
utxo.output.value,
2031+
EcdsaSighashType::All,
2032+
)?;
20442033
let signature = self.secp.sign_ecdsa(
20452034
&secp256k1::Message::from_digest(sighash.to_byte_array()),
20462035
&self.secret_key,
20472036
);
20482037
let bitcoin_sig =
20492038
bitcoin::ecdsa::Signature { signature, sighash_type: EcdsaSighashType::All };
2050-
tx.input[i].script_sig = Builder::new()
2051-
.push_slice(&bitcoin_sig.serialize())
2052-
.push_slice(&self.secret_key.public_key(&self.secp).serialize())
2053-
.into_script();
2039+
tx.input[i].witness =
2040+
Witness::p2wpkh(&bitcoin_sig, &self.secret_key.public_key(&self.secp));
20542041
}
20552042
}
20562043
Ok(tx)
20572044
}
20582045
}
2046+
2047+
impl WalletSourceSync for TestWalletSource {
2048+
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
2049+
Ok(self.utxos.lock().unwrap().clone())
2050+
}
2051+
2052+
fn get_change_script(&self) -> Result<ScriptBuf, ()> {
2053+
let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp));
2054+
Ok(ScriptBuf::new_p2wpkh(&public_key.wpubkey_hash().unwrap()))
2055+
}
2056+
2057+
fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()> {
2058+
let tx = psbt.extract_tx_unchecked_fee_rate();
2059+
self.sign_tx(tx).map_err(|_| ())
2060+
}
2061+
}

0 commit comments

Comments
 (0)