Skip to content

Commit d422ff0

Browse files
committed
f return witness instead of modifying the transaction
1 parent a875465 commit d422ff0

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,11 @@ impl InMemoryChannelKeys {
505505
}
506506

507507
/// Sign the single input of spend_tx at index `input_idx` which spends the output
508-
/// described by descriptor.
508+
/// described by descriptor, returning the witness stack for the input.
509509
///
510510
/// Returns an Err if the input at input_idx does not exist, has a non-empty script_sig,
511511
/// or is not spending the outpoint described by `descriptor.outpoint`.
512-
///
513-
/// (C-not exported) as bindings don't support modifying a Transaction parameter
514-
pub fn sign_counterparty_payment_input<C: Signing>(&self, spend_tx: &mut Transaction, input_idx: usize, descriptor: &StaticCounterpartyPaymentOutputDescriptor, secp_ctx: &Secp256k1<C>) -> Result<(), ()> {
512+
pub fn sign_counterparty_payment_input<C: Signing>(&self, spend_tx: &Transaction, input_idx: usize, descriptor: &StaticCounterpartyPaymentOutputDescriptor, secp_ctx: &Secp256k1<C>) -> Result<Vec<Vec<u8>>, ()> {
515513
// TODO: We really should be taking the SigHashCache as a parameter here instead of
516514
// spend_tx, but ideally the SigHashCache would expose the transaction's inputs read-only
517515
// so that we can check them. This requires upstream rust-bitcoin changes (as well as
@@ -522,23 +520,23 @@ impl InMemoryChannelKeys {
522520

523521
let remotepubkey = self.pubkeys().payment_point;
524522
let witness_script = bitcoin::Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: remotepubkey}, Network::Testnet).script_pubkey();
525-
let sighash = hash_to_message!(&bip143::SigHashCache::new(&*spend_tx).signature_hash(input_idx, &witness_script, descriptor.output.value, SigHashType::All)[..]);
523+
let sighash = hash_to_message!(&bip143::SigHashCache::new(spend_tx).signature_hash(input_idx, &witness_script, descriptor.output.value, SigHashType::All)[..]);
526524
let remotesig = secp_ctx.sign(&sighash, &self.payment_key);
527-
spend_tx.input[input_idx].witness.push(remotesig.serialize_der().to_vec());
528-
spend_tx.input[input_idx].witness[0].push(SigHashType::All as u8);
529-
spend_tx.input[input_idx].witness.push(remotepubkey.serialize().to_vec());
530-
Ok(())
525+
526+
let mut witness = Vec::with_capacity(2);
527+
witness.push(remotesig.serialize_der().to_vec());
528+
witness[0].push(SigHashType::All as u8);
529+
witness.push(remotepubkey.serialize().to_vec());
530+
Ok(witness)
531531
}
532532

533533
/// Sign the single input of spend_tx at index `input_idx` which spends the output
534-
/// described by descriptor.
534+
/// described by descriptor, returning the witness stack for the input.
535535
///
536536
/// Returns an Err if the input at input_idx does not exist, has a non-empty script_sig,
537537
/// is not spending the outpoint described by `descriptor.outpoint`, or does not have a
538538
/// sequence set to `descriptor.to_self_delay`.
539-
///
540-
/// (C-not exported) as bindings don't support modifying a Transaction parameter
541-
pub fn sign_dynamic_p2wsh_input<C: Signing>(&self, spend_tx: &mut Transaction, input_idx: usize, descriptor: &DynamicP2WSHOutputDescriptor, secp_ctx: &Secp256k1<C>) -> Result<(), ()> {
539+
pub fn sign_dynamic_p2wsh_input<C: Signing>(&self, spend_tx: &Transaction, input_idx: usize, descriptor: &DynamicP2WSHOutputDescriptor, secp_ctx: &Secp256k1<C>) -> Result<Vec<Vec<u8>>, ()> {
542540
// TODO: We really should be taking the SigHashCache as a parameter here instead of
543541
// spend_tx, but ideally the SigHashCache would expose the transaction's inputs read-only
544542
// so that we can check them. This requires upstream rust-bitcoin changes (as well as
@@ -552,13 +550,15 @@ impl InMemoryChannelKeys {
552550
.expect("We constructed the payment_base_key, so we can only fail here if the RNG is busted.");
553551
let delayed_payment_pubkey = PublicKey::from_secret_key(&secp_ctx, &delayed_payment_key);
554552
let witness_script = chan_utils::get_revokeable_redeemscript(&descriptor.revocation_pubkey, descriptor.to_self_delay, &delayed_payment_pubkey);
555-
let sighash = hash_to_message!(&bip143::SigHashCache::new(&*spend_tx).signature_hash(input_idx, &witness_script, descriptor.output.value, SigHashType::All)[..]);
553+
let sighash = hash_to_message!(&bip143::SigHashCache::new(spend_tx).signature_hash(input_idx, &witness_script, descriptor.output.value, SigHashType::All)[..]);
556554
let local_delayedsig = secp_ctx.sign(&sighash, &delayed_payment_key);
557-
spend_tx.input[input_idx].witness.push(local_delayedsig.serialize_der().to_vec());
558-
spend_tx.input[input_idx].witness[0].push(SigHashType::All as u8);
559-
spend_tx.input[input_idx].witness.push(vec!()); //MINIMALIF
560-
spend_tx.input[input_idx].witness.push(witness_script.clone().into_bytes());
561-
Ok(())
555+
556+
let mut witness = Vec::with_capacity(3);
557+
witness.push(local_delayedsig.serialize_der().to_vec());
558+
witness[0].push(SigHashType::All as u8);
559+
witness.push(vec!()); //MINIMALIF
560+
witness.push(witness_script.clone().into_bytes());
561+
Ok(witness)
562562
}
563563
}
564564

@@ -981,15 +981,15 @@ impl KeysManager {
981981
self.derive_channel_keys(descriptor.channel_value_satoshis, descriptor.key_derivation_params.0, descriptor.key_derivation_params.1),
982982
descriptor.key_derivation_params));
983983
}
984-
keys_cache.as_ref().unwrap().0.sign_counterparty_payment_input(&mut spend_tx, input_idx, &descriptor, &secp_ctx).unwrap();
984+
spend_tx.input[input_idx].witness = keys_cache.as_ref().unwrap().0.sign_counterparty_payment_input(&spend_tx, input_idx, &descriptor, &secp_ctx).unwrap();
985985
},
986986
SpendableOutputDescriptor::DynamicOutputP2WSH(descriptor) => {
987987
if keys_cache.is_none() || keys_cache.as_ref().unwrap().1 != descriptor.key_derivation_params {
988988
keys_cache = Some((
989989
self.derive_channel_keys(descriptor.channel_value_satoshis, descriptor.key_derivation_params.0, descriptor.key_derivation_params.1),
990990
descriptor.key_derivation_params));
991991
}
992-
keys_cache.as_ref().unwrap().0.sign_dynamic_p2wsh_input(&mut spend_tx, input_idx, &descriptor, &secp_ctx).unwrap();
992+
spend_tx.input[input_idx].witness = keys_cache.as_ref().unwrap().0.sign_dynamic_p2wsh_input(&spend_tx, input_idx, &descriptor, &secp_ctx).unwrap();
993993
},
994994
SpendableOutputDescriptor::StaticOutput { ref output, .. } => {
995995
let derivation_idx = if output.script_pubkey == self.destination_script {

0 commit comments

Comments
 (0)