Skip to content

Commit 5c07dd3

Browse files
committed
Sanity check for ChannelManager and KeysInterface
Fix build errors Create script using p2wsh for comparison Using p2wpkh for generating the payment script spendable_outputs sanity check
1 parent 34cdca9 commit 5c07dd3

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ impl InMemorySigner {
540540
let witness_script = bitcoin::Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: remotepubkey}, Network::Testnet).script_pubkey();
541541
let sighash = hash_to_message!(&bip143::SigHashCache::new(spend_tx).signature_hash(input_idx, &witness_script, descriptor.output.value, SigHashType::All)[..]);
542542
let remotesig = secp_ctx.sign(&sighash, &self.payment_key);
543+
let payment_script = bitcoin::Address::p2wpkh(&::bitcoin::PublicKey{compressed: true, key: remotepubkey}, Network::Bitcoin).unwrap().script_pubkey();
544+
545+
if payment_script != descriptor.output.script_pubkey { return Err(()); }
543546

544547
let mut witness = Vec::with_capacity(2);
545548
witness.push(remotesig.serialize_der().to_vec());
@@ -570,6 +573,9 @@ impl InMemorySigner {
570573
let witness_script = chan_utils::get_revokeable_redeemscript(&descriptor.revocation_pubkey, descriptor.to_self_delay, &delayed_payment_pubkey);
571574
let sighash = hash_to_message!(&bip143::SigHashCache::new(spend_tx).signature_hash(input_idx, &witness_script, descriptor.output.value, SigHashType::All)[..]);
572575
let local_delayedsig = secp_ctx.sign(&sighash, &delayed_payment_key);
576+
let payment_script = bitcoin::Address::p2wsh(&witness_script, Network::Bitcoin).script_pubkey();
577+
578+
if descriptor.output.script_pubkey != payment_script { return Err(()); }
573579

574580
let mut witness = Vec::with_capacity(3);
575581
witness.push(local_delayedsig.serialize_der().to_vec());
@@ -1029,6 +1035,8 @@ impl KeysManager {
10291035
assert_eq!(pubkey.key, self.shutdown_pubkey);
10301036
}
10311037
let witness_script = bitcoin::Address::p2pkh(&pubkey, Network::Testnet).script_pubkey();
1038+
let payment_script = bitcoin::Address::p2wpkh(&pubkey, Network::Testnet).expect("uncompressed key found").script_pubkey();
1039+
assert_eq!(payment_script, output.script_pubkey);
10321040
let sighash = hash_to_message!(&bip143::SigHashCache::new(&spend_tx).signature_hash(input_idx, &witness_script, output.value, SigHashType::All)[..]);
10331041
let sig = secp_ctx.sign(&sighash, &secret.private_key.key);
10341042
spend_tx.input[input_idx].witness.push(sig.serialize_der().to_vec());

lightning/src/ln/channelmanager.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6215,6 +6215,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
62156215
write_tlv_fields!(writer, {
62166216
(1, pending_outbound_payments_no_retry, required),
62176217
(3, pending_outbound_payments, required),
6218+
(5, self.our_network_pubkey, required)
62186219
});
62196220

62206221
Ok(())
@@ -6509,10 +6510,13 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
65096510
// pending_outbound_payments_no_retry is for compatibility with 0.0.101 clients.
65106511
let mut pending_outbound_payments_no_retry: Option<HashMap<PaymentId, HashSet<[u8; 32]>>> = None;
65116512
let mut pending_outbound_payments = None;
6513+
let mut received_network_pubkey: Option<PublicKey> = None;
65126514
read_tlv_fields!(reader, {
65136515
(1, pending_outbound_payments_no_retry, option),
65146516
(3, pending_outbound_payments, option),
6517+
(5, received_network_pubkey, option)
65156518
});
6519+
65166520
if pending_outbound_payments.is_none() && pending_outbound_payments_no_retry.is_none() {
65176521
pending_outbound_payments = Some(pending_outbound_payments_compat);
65186522
} else if pending_outbound_payments.is_none() {
@@ -6575,6 +6579,14 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
65756579
pending_events_read.append(&mut channel_closures);
65766580
}
65776581

6582+
let our_network_pubkey = PublicKey::from_secret_key(&secp_ctx, &args.keys_manager.get_node_secret());
6583+
if let Some(network_pubkey) = received_network_pubkey {
6584+
if network_pubkey != our_network_pubkey {
6585+
log_error!(args.logger, "Key that was generated does not match the existing key.");
6586+
return Err(DecodeError::InvalidValue);
6587+
}
6588+
}
6589+
65786590
let inbound_pmt_key_material = args.keys_manager.get_inbound_payment_key_material();
65796591
let expanded_inbound_key = inbound_payment::ExpandedKey::new(&inbound_pmt_key_material);
65806592
let channel_manager = ChannelManager {
@@ -6597,7 +6609,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
65976609
pending_outbound_payments: Mutex::new(pending_outbound_payments.unwrap()),
65986610

65996611
our_network_key: args.keys_manager.get_node_secret(),
6600-
our_network_pubkey: PublicKey::from_secret_key(&secp_ctx, &args.keys_manager.get_node_secret()),
6612+
our_network_pubkey,
66016613
secp_ctx,
66026614

66036615
last_node_announcement_serial: AtomicUsize::new(last_node_announcement_serial as usize),

0 commit comments

Comments
 (0)