Skip to content

Commit e12517b

Browse files
committed
Replace Witness::from_vec with Witness::from_slice
1 parent c8ae81e commit e12517b

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9232,7 +9232,7 @@ fn test_invalid_funding_tx() {
92329232
},
92339233
script_sig: ScriptBuf::new(),
92349234
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
9235-
witness: Witness::from_vec(channelmonitor::deliberately_bogus_accepted_htlc_witness())
9235+
witness: Witness::from_slice(&channelmonitor::deliberately_bogus_accepted_htlc_witness())
92369236
}).collect(),
92379237
output: vec![TxOut {
92389238
value: 1000,
@@ -9884,7 +9884,7 @@ fn test_non_final_funding_tx() {
98849884

98859885
let chan_id = *nodes[0].network_chan_count.borrow();
98869886
let events = nodes[0].node.get_and_clear_pending_events();
9887-
let input = TxIn { previous_output: BitcoinOutPoint::null(), script_sig: bitcoin::ScriptBuf::new(), sequence: Sequence(1), witness: Witness::from_vec(vec!(vec!(1))) };
9887+
let input = TxIn { previous_output: BitcoinOutPoint::null(), script_sig: bitcoin::ScriptBuf::new(), sequence: Sequence(1), witness: Witness::from_slice(&[&[1]]) };
98889888
assert_eq!(events.len(), 1);
98899889
let mut tx = match events[0] {
98909890
Event::FundingGenerationReady { ref channel_value_satoshis, ref output_script, .. } => {
@@ -9929,7 +9929,7 @@ fn test_non_final_funding_tx_within_headroom() {
99299929

99309930
let chan_id = *nodes[0].network_chan_count.borrow();
99319931
let events = nodes[0].node.get_and_clear_pending_events();
9932-
let input = TxIn { previous_output: BitcoinOutPoint::null(), script_sig: bitcoin::ScriptBuf::new(), sequence: Sequence(1), witness: Witness::from_vec(vec!(vec!(1))) };
9932+
let input = TxIn { previous_output: BitcoinOutPoint::null(), script_sig: bitcoin::ScriptBuf::new(), sequence: Sequence(1), witness: Witness::from_slice(&[[1]]) };
99339933
assert_eq!(events.len(), 1);
99349934
let mut tx = match events[0] {
99359935
Event::FundingGenerationReady { ref channel_value_satoshis, ref output_script, .. } => {

lightning/src/ln/monitor_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2547,7 +2547,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
25472547
sig.push(EcdsaSighashType::All as u8);
25482548
sig
25492549
};
2550-
htlc_tx.input[0].witness = Witness::from_vec(vec![fee_utxo_sig, public_key.to_bytes()]);
2550+
htlc_tx.input[0].witness = Witness::from_slice(&[fee_utxo_sig, public_key.to_bytes()]);
25512551
check_spends!(htlc_tx, coinbase_tx, revoked_commitment_a, revoked_commitment_b);
25522552
htlc_tx
25532553
};

lightning/src/ln/msgs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,7 +3510,7 @@ mod tests {
35103510
previous_output: OutPoint { txid: Txid::from_hex("305bab643ee297b8b6b76b320792c8223d55082122cb606bf89382146ced9c77").unwrap(), index: 2 }.into_bitcoin_outpoint(),
35113511
script_sig: ScriptBuf::new(),
35123512
sequence: Sequence(0xfffffffd),
3513-
witness: Witness::from_vec(vec![
3513+
witness: Witness::from_slice(&vec![
35143514
hex::decode("304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701").unwrap(),
35153515
hex::decode("0301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944").unwrap()]),
35163516
}],
@@ -3584,10 +3584,10 @@ mod tests {
35843584
channel_id: ChannelId::from_bytes([2; 32]),
35853585
tx_hash: Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
35863586
witnesses: vec![
3587-
Witness::from_vec(vec![
3587+
Witness::from_slice(&vec![
35883588
hex::decode("304402206af85b7dd67450ad12c979302fac49dfacbc6a8620f49c5da2b5721cf9565ca502207002b32fed9ce1bf095f57aeb10c36928ac60b12e723d97d2964a54640ceefa701").unwrap(),
35893589
hex::decode("0301ab7dc16488303549bfcdd80f6ae5ee4c20bf97ab5410bbd6b1bfa85dcd6944").unwrap()]),
3590-
Witness::from_vec(vec![
3590+
Witness::from_slice(&vec![
35913591
hex::decode("3045022100ee00dbf4a862463e837d7c08509de814d620e4d9830fa84818713e0fa358f145022021c3c7060c4d53fe84fd165d60208451108a778c13b92ca4c6bad439236126cc01").unwrap(),
35923592
hex::decode("028fbbf0b16f5ba5bcb5dd37cd4047ce6f726a21c06682f9ec2f52b057de1dbdb5").unwrap()]),
35933593
],

lightning/src/sign/mod.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use bitcoin::blockdata::locktime::absolute::LockTime;
1616
use bitcoin::blockdata::transaction::{Transaction, TxOut, TxIn};
1717
use bitcoin::blockdata::script::{Script, ScriptBuf, Builder};
1818
use bitcoin::blockdata::opcodes;
19+
use bitcoin::ecdsa::Signature as EcdsaSignature;
1920
use bitcoin::network::constants::Network;
2021
use bitcoin::psbt::PartiallySignedTransaction;
2122
use bitcoin::bip32::{ExtendedPrivKey, ExtendedPubKey, ChildNumber};
@@ -1109,7 +1110,7 @@ impl InMemorySigner {
11091110
/// or if an output descriptor `script_pubkey` does not match the one we can spend.
11101111
///
11111112
/// [`descriptor.outpoint`]: StaticPaymentOutputDescriptor::outpoint
1112-
pub fn sign_counterparty_payment_input<C: Signing>(&self, spend_tx: &Transaction, input_idx: usize, descriptor: &StaticPaymentOutputDescriptor, secp_ctx: &Secp256k1<C>) -> Result<Vec<Vec<u8>>, ()> {
1113+
pub fn sign_counterparty_payment_input<C: Signing>(&self, spend_tx: &Transaction, input_idx: usize, descriptor: &StaticPaymentOutputDescriptor, secp_ctx: &Secp256k1<C>) -> Result<Witness, ()> {
11131114
// TODO: We really should be taking the SigHashCache as a parameter here instead of
11141115
// spend_tx, but ideally the SigHashCache would expose the transaction's inputs read-only
11151116
// so that we can check them. This requires upstream rust-bitcoin changes (as well as
@@ -1148,7 +1149,7 @@ impl InMemorySigner {
11481149
} else {
11491150
witness.push(remotepubkey.to_bytes());
11501151
}
1151-
Ok(witness)
1152+
Ok(witness.into())
11521153
}
11531154

11541155
/// Sign the single input of `spend_tx` at index `input_idx` which spends the output
@@ -1161,7 +1162,7 @@ impl InMemorySigner {
11611162
///
11621163
/// [`descriptor.outpoint`]: DelayedPaymentOutputDescriptor::outpoint
11631164
/// [`descriptor.to_self_delay`]: DelayedPaymentOutputDescriptor::to_self_delay
1164-
pub fn sign_dynamic_p2wsh_input<C: Signing>(&self, spend_tx: &Transaction, input_idx: usize, descriptor: &DelayedPaymentOutputDescriptor, secp_ctx: &Secp256k1<C>) -> Result<Vec<Vec<u8>>, ()> {
1165+
pub fn sign_dynamic_p2wsh_input<C: Signing>(&self, spend_tx: &Transaction, input_idx: usize, descriptor: &DelayedPaymentOutputDescriptor, secp_ctx: &Secp256k1<C>) -> Result<Witness, ()> {
11651166
// TODO: We really should be taking the SigHashCache as a parameter here instead of
11661167
// spend_tx, but ideally the SigHashCache would expose the transaction's inputs read-only
11671168
// so that we can check them. This requires upstream rust-bitcoin changes (as well as
@@ -1175,17 +1176,19 @@ impl InMemorySigner {
11751176
let delayed_payment_pubkey = PublicKey::from_secret_key(&secp_ctx, &delayed_payment_key);
11761177
let witness_script = chan_utils::get_revokeable_redeemscript(&descriptor.revocation_pubkey, descriptor.to_self_delay, &delayed_payment_pubkey);
11771178
let sighash = hash_to_message!(&sighash::SighashCache::new(spend_tx).segwit_signature_hash(input_idx, &witness_script, descriptor.output.value, EcdsaSighashType::All).unwrap()[..]);
1178-
let local_delayedsig = sign_with_aux_rand(secp_ctx, &sighash, &delayed_payment_key, &self);
1179+
let local_delayedsig = EcdsaSignature {
1180+
sig: sign_with_aux_rand(secp_ctx, &sighash, &delayed_payment_key, &self),
1181+
hash_ty: EcdsaSighashType::All,
1182+
};
11791183
let payment_script = bitcoin::Address::p2wsh(&witness_script, Network::Bitcoin).script_pubkey();
11801184

11811185
if descriptor.output.script_pubkey != payment_script { return Err(()); }
11821186

1183-
let mut witness = Vec::with_capacity(3);
1184-
witness.push(local_delayedsig.serialize_der().to_vec());
1185-
witness[0].push(EcdsaSighashType::All as u8);
1186-
witness.push(vec!()); //MINIMALIF
1187-
witness.push(witness_script.clone().into_bytes());
1188-
Ok(witness)
1187+
Ok(Witness::from_slice(&[
1188+
&local_delayedsig.serialize()[..],
1189+
&[], // MINIMALIF
1190+
witness_script.as_bytes(),
1191+
]))
11891192
}
11901193
}
11911194

@@ -1614,7 +1617,7 @@ impl KeysManager {
16141617
}
16151618
keys_cache = Some((signer, descriptor.channel_keys_id));
16161619
}
1617-
let witness = Witness::from_vec(keys_cache.as_ref().unwrap().0.sign_counterparty_payment_input(&psbt.unsigned_tx, input_idx, &descriptor, &secp_ctx)?);
1620+
let witness = keys_cache.as_ref().unwrap().0.sign_counterparty_payment_input(&psbt.unsigned_tx, input_idx, &descriptor, &secp_ctx)?;
16181621
psbt.inputs[input_idx].final_script_witness = Some(witness);
16191622
},
16201623
SpendableOutputDescriptor::DelayedPaymentOutput(descriptor) => {
@@ -1624,7 +1627,7 @@ impl KeysManager {
16241627
self.derive_channel_keys(descriptor.channel_value_satoshis, &descriptor.channel_keys_id),
16251628
descriptor.channel_keys_id));
16261629
}
1627-
let witness = Witness::from_vec(keys_cache.as_ref().unwrap().0.sign_dynamic_p2wsh_input(&psbt.unsigned_tx, input_idx, &descriptor, &secp_ctx)?);
1630+
let witness = keys_cache.as_ref().unwrap().0.sign_dynamic_p2wsh_input(&psbt.unsigned_tx, input_idx, &descriptor, &secp_ctx)?;
16281631
psbt.inputs[input_idx].final_script_witness = Some(witness);
16291632
},
16301633
SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output } => {
@@ -1659,7 +1662,7 @@ impl KeysManager {
16591662
let sig = sign_with_aux_rand(secp_ctx, &sighash, &secret.private_key, &self);
16601663
let mut sig_ser = sig.serialize_der().to_vec();
16611664
sig_ser.push(EcdsaSighashType::All as u8);
1662-
let witness = Witness::from_vec(vec![sig_ser, pubkey.inner.serialize().to_vec()]);
1665+
let witness = Witness::from_slice(&[&sig_ser, &pubkey.inner.serialize().to_vec()]);
16631666
psbt.inputs[input_idx].final_script_witness = Some(witness);
16641667
},
16651668
}

0 commit comments

Comments
 (0)