Skip to content

Commit b410479

Browse files
committed
f return witness instead of modifying the transaction
1 parent 0297baa commit b410479

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
@@ -503,13 +503,11 @@ impl InMemoryChannelKeys {
503503
}
504504

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

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

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

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

0 commit comments

Comments
 (0)