Skip to content

Commit 5421128

Browse files
committed
Changes to create fresh destination scripts
1 parent d29ae18 commit 5421128

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,6 +2922,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29222922
spendable_output = Some(SpendableOutputDescriptor::StaticOutput {
29232923
outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
29242924
output: outp.clone(),
2925+
channel_keys_id: self.channel_keys_id,
29252926
});
29262927
break;
29272928
}
@@ -2952,6 +2953,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
29522953
spendable_output = Some(SpendableOutputDescriptor::StaticOutput {
29532954
outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
29542955
output: outp.clone(),
2956+
channel_keys_id: self.channel_keys_id,
29552957
});
29562958
break;
29572959
}

lightning/src/chain/keysinterface.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ pub enum SpendableOutputDescriptor {
136136
outpoint: OutPoint,
137137
/// The output which is referenced by the given outpoint.
138138
output: TxOut,
139+
/// Key which is used to derive child keys
140+
channel_keys_id: [u8; 32]
139141
},
140142
/// An output to a P2WSH script which can be spent with a single signature after a CSV delay.
141143
///
@@ -180,6 +182,7 @@ impl_writeable_tlv_based_enum!(SpendableOutputDescriptor,
180182
(0, StaticOutput) => {
181183
(0, outpoint, required),
182184
(2, output, required),
185+
(4, channel_keys_id, required),
183186
},
184187
;
185188
(1, DelayedPaymentOutput),
@@ -814,8 +817,7 @@ pub struct KeysManager {
814817
secp_ctx: Secp256k1<secp256k1::All>,
815818
node_secret: SecretKey,
816819
inbound_payment_key: KeyMaterial,
817-
destination_script: Script,
818-
shutdown_pubkey: PublicKey,
820+
shutdown_pubkey: ExtendedPubKey,
819821
channel_master_key: ExtendedPrivKey,
820822
channel_child_index: AtomicUsize,
821823

@@ -854,17 +856,8 @@ impl KeysManager {
854856
match ExtendedPrivKey::new_master(Network::Testnet, seed) {
855857
Ok(master_key) => {
856858
let node_secret = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(0).unwrap()).expect("Your RNG is busted").private_key.key;
857-
let destination_script = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(1).unwrap()) {
858-
Ok(destination_key) => {
859-
let wpubkey_hash = WPubkeyHash::hash(&ExtendedPubKey::from_private(&secp_ctx, &destination_key).public_key.to_bytes());
860-
Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
861-
.push_slice(&wpubkey_hash.into_inner())
862-
.into_script()
863-
},
864-
Err(_) => panic!("Your RNG is busted"),
865-
};
866859
let shutdown_pubkey = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(2).unwrap()) {
867-
Ok(shutdown_key) => ExtendedPubKey::from_private(&secp_ctx, &shutdown_key).public_key.key,
860+
Ok(shutdown_key) => ExtendedPubKey::from_private(&secp_ctx, &shutdown_key),
868861
Err(_) => panic!("Your RNG is busted"),
869862
};
870863
let channel_master_key = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(3).unwrap()).expect("Your RNG is busted");
@@ -882,8 +875,6 @@ impl KeysManager {
882875
secp_ctx,
883876
node_secret,
884877
inbound_payment_key: KeyMaterial(inbound_pmt_key_bytes),
885-
886-
destination_script,
887878
shutdown_pubkey,
888879

889880
channel_master_key,
@@ -1000,7 +991,7 @@ impl KeysManager {
1000991
input_value += descriptor.output.value;
1001992
if !output_set.insert(descriptor.outpoint) { return Err(()); }
1002993
},
1003-
SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output } => {
994+
SpendableOutputDescriptor::StaticOutput { ref outpoint, ref output, .. } => {
1004995
input.push(TxIn {
1005996
previous_output: outpoint.into_bitcoin_outpoint(),
1006997
script_sig: Script::new(),
@@ -1043,12 +1034,25 @@ impl KeysManager {
10431034
}
10441035
spend_tx.input[input_idx].witness = keys_cache.as_ref().unwrap().0.sign_dynamic_p2wsh_input(&spend_tx, input_idx, &descriptor, &secp_ctx)?;
10451036
},
1046-
SpendableOutputDescriptor::StaticOutput { ref output, .. } => {
1047-
let derivation_idx = if output.script_pubkey == self.destination_script {
1037+
SpendableOutputDescriptor::StaticOutput { ref output, channel_keys_id,.. } => {
1038+
let derivation_idx = byte_utils::slice_to_be64(&channel_keys_id[0..8]);
1039+
let destination_script = match self.channel_master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(derivation_idx as u32).expect("key space exhausted")){
1040+
Ok(destination_key) => {
1041+
let wpubkey_hash = WPubkeyHash::hash(&ExtendedPubKey::from_private(&secp_ctx, &destination_key).public_key.to_bytes());
1042+
Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
1043+
.push_slice(&wpubkey_hash.into_inner())
1044+
.into_script()
1045+
},
1046+
Err(_) => panic!("Your RNG is busted"),
1047+
};
1048+
1049+
1050+
let derivation_idx = if output.script_pubkey == destination_script {
10481051
1
10491052
} else {
10501053
2
10511054
};
1055+
10521056
let secret = {
10531057
// Note that when we aren't serializing the key, network doesn't matter
10541058
match ExtendedPrivKey::new_master(Network::Testnet, &self.seed) {
@@ -1063,7 +1067,7 @@ impl KeysManager {
10631067
};
10641068
let pubkey = ExtendedPubKey::from_private(&secp_ctx, &secret).public_key;
10651069
if derivation_idx == 2 {
1066-
assert_eq!(pubkey.key, self.shutdown_pubkey);
1070+
assert_eq!(pubkey.key, self.shutdown_pubkey.public_key.key);
10671071
}
10681072
let witness_script = bitcoin::Address::p2pkh(&pubkey, Network::Testnet).script_pubkey();
10691073
let payment_script = bitcoin::Address::p2wpkh(&pubkey, Network::Testnet).expect("uncompressed key found").script_pubkey();
@@ -1101,11 +1105,11 @@ impl KeysInterface for KeysManager {
11011105
}
11021106

11031107
fn get_destination_script(&self) -> Script {
1104-
self.destination_script.clone()
1108+
bitcoin::Address::p2wpkh(&::bitcoin::PublicKey{compressed: true, key: self.shutdown_pubkey.public_key.key}, Network::Testnet).unwrap().script_pubkey()
11051109
}
11061110

11071111
fn get_shutdown_scriptpubkey(&self) -> ShutdownScript {
1108-
ShutdownScript::new_p2wpkh_from_pubkey(self.shutdown_pubkey.clone())
1112+
ShutdownScript::new_p2wpkh_from_pubkey(self.shutdown_pubkey.public_key.key.clone())
11091113
}
11101114

11111115
fn get_channel_signer(&self, _inbound: bool, channel_value_satoshis: u64) -> Self::Signer {

0 commit comments

Comments
 (0)