|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use bitcoin::secp256k1::{Secp256k1, SecretKey}; |
| 4 | +use lightning::sign::{InMemorySigner, NodeSigner, OutputSpender, SignerProvider}; |
| 5 | + |
| 6 | +use lightning::sign::{EntropySource, KeysManager}; |
| 7 | +use lightning_invoice::RawBolt11Invoice; |
| 8 | + |
| 9 | +pub struct MyKeys { |
| 10 | + pub keys_manager: Arc<MyKeysManager>, |
| 11 | +} |
| 12 | + |
| 13 | +impl MyKeys { |
| 14 | + pub fn new(seed: [u8; 32], starting_time_secs: u64, starting_time_nanos: u32) -> Self { |
| 15 | + MyKeys { |
| 16 | + keys_manager: Arc::new(MyKeysManager::new( |
| 17 | + &seed, |
| 18 | + starting_time_secs, |
| 19 | + starting_time_nanos, |
| 20 | + )), |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub fn with_channel_keys(seed: [u8; 32], starting_time_secs: u64, starting_time_nanos: u32, channels_keys: String) -> Self { |
| 25 | + let keys = channels_keys.split('/').collect::<Vec<_>>(); |
| 26 | + |
| 27 | + let mut manager = |
| 28 | + MyKeysManager::new(&seed, starting_time_secs, starting_time_nanos); |
| 29 | + manager.set_channel_keys( |
| 30 | + keys[0].to_string(), |
| 31 | + keys[1].to_string(), |
| 32 | + keys[2].to_string(), |
| 33 | + keys[3].to_string(), |
| 34 | + keys[4].to_string(), |
| 35 | + keys[5].to_string(), |
| 36 | + ); |
| 37 | + MyKeys { |
| 38 | + keys_manager: Arc::new(manager), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + pub fn inner(&self) -> Arc<MyKeysManager> { |
| 43 | + self.keys_manager.clone() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// MyKeysManager is a custom keysmanager that allows the use of custom channel secrets. |
| 48 | +pub struct MyKeysManager { |
| 49 | + pub(crate) inner: KeysManager, |
| 50 | + |
| 51 | + funding_key: Option<SecretKey>, |
| 52 | + revocation_base_secret: Option<SecretKey>, |
| 53 | + payment_base_secret: Option<SecretKey>, |
| 54 | + delayed_payment_base_secret: Option<SecretKey>, |
| 55 | + htlc_base_secret: Option<SecretKey>, |
| 56 | + shachain_seed: Option<[u8; 32]>, |
| 57 | +} |
| 58 | + |
| 59 | +impl MyKeysManager { |
| 60 | + pub fn new(seed: &[u8; 32], starting_time_secs: u64, starting_time_nanos: u32) -> Self { |
| 61 | + let inner = KeysManager::new(seed, starting_time_secs, starting_time_nanos); |
| 62 | + Self { |
| 63 | + inner, |
| 64 | + funding_key: None, |
| 65 | + revocation_base_secret: None, |
| 66 | + payment_base_secret: None, |
| 67 | + delayed_payment_base_secret: None, |
| 68 | + htlc_base_secret: None, |
| 69 | + shachain_seed: None, |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + pub fn get_node_secret_key(&self) -> SecretKey { |
| 74 | + self.inner.get_node_secret_key() |
| 75 | + } |
| 76 | + |
| 77 | + pub fn set_channel_keys( |
| 78 | + &mut self, |
| 79 | + funding_key: String, |
| 80 | + revocation_base_secret: String, |
| 81 | + payment_base_secret: String, |
| 82 | + delayed_payment_base_secret: String, |
| 83 | + htlc_base_secret: String, |
| 84 | + _shachain_seed: String, |
| 85 | + ) { |
| 86 | + use std::str::FromStr; |
| 87 | + |
| 88 | + self.funding_key = Some(SecretKey::from_str(&funding_key).unwrap()); |
| 89 | + self.revocation_base_secret = Some(SecretKey::from_str(&revocation_base_secret).unwrap()); |
| 90 | + self.payment_base_secret = Some(SecretKey::from_str(&payment_base_secret).unwrap()); |
| 91 | + self.delayed_payment_base_secret = |
| 92 | + Some(SecretKey::from_str(&delayed_payment_base_secret).unwrap()); |
| 93 | + self.htlc_base_secret = Some(SecretKey::from_str(&htlc_base_secret).unwrap()); |
| 94 | + self.shachain_seed = Some(self.inner.get_secure_random_bytes()) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl EntropySource for MyKeysManager { |
| 99 | + fn get_secure_random_bytes(&self) -> [u8; 32] { |
| 100 | + self.inner.get_secure_random_bytes() |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl NodeSigner for MyKeysManager { |
| 105 | + fn ecdh( |
| 106 | + &self, |
| 107 | + recipient: lightning::sign::Recipient, |
| 108 | + other_key: &bitcoin::secp256k1::PublicKey, |
| 109 | + tweak: Option<&bitcoin::secp256k1::Scalar>, |
| 110 | + ) -> Result<bitcoin::secp256k1::ecdh::SharedSecret, ()> { |
| 111 | + self.inner.ecdh(recipient, other_key, tweak) |
| 112 | + } |
| 113 | + |
| 114 | + fn get_inbound_payment_key_material(&self) -> lightning::sign::KeyMaterial { |
| 115 | + self.inner.get_inbound_payment_key_material() |
| 116 | + } |
| 117 | + |
| 118 | + fn get_node_id( |
| 119 | + &self, |
| 120 | + recipient: lightning::sign::Recipient, |
| 121 | + ) -> Result<bitcoin::secp256k1::PublicKey, ()> { |
| 122 | + self.inner.get_node_id(recipient) |
| 123 | + } |
| 124 | + |
| 125 | + fn sign_bolt12_invoice( |
| 126 | + &self, |
| 127 | + invoice: &lightning::offers::invoice::UnsignedBolt12Invoice, |
| 128 | + ) -> Result<bitcoin::secp256k1::schnorr::Signature, ()> { |
| 129 | + self.inner.sign_bolt12_invoice(invoice) |
| 130 | + } |
| 131 | + |
| 132 | + fn sign_bolt12_invoice_request( |
| 133 | + &self, |
| 134 | + invoice_request: &lightning::offers::invoice_request::UnsignedInvoiceRequest, |
| 135 | + ) -> Result<bitcoin::secp256k1::schnorr::Signature, ()> { |
| 136 | + self.inner.sign_bolt12_invoice_request(invoice_request) |
| 137 | + } |
| 138 | + |
| 139 | + fn sign_gossip_message( |
| 140 | + &self, |
| 141 | + msg: lightning::ln::msgs::UnsignedGossipMessage, |
| 142 | + ) -> Result<bitcoin::secp256k1::ecdsa::Signature, ()> { |
| 143 | + self.inner.sign_gossip_message(msg) |
| 144 | + } |
| 145 | + |
| 146 | + fn sign_invoice( |
| 147 | + &self, |
| 148 | + invoice: &RawBolt11Invoice, |
| 149 | + recipient: lightning::sign::Recipient, |
| 150 | + ) -> Result<bitcoin::secp256k1::ecdsa::RecoverableSignature, ()> { |
| 151 | + self.inner.sign_invoice(invoice, recipient) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | +impl OutputSpender for MyKeysManager { |
| 157 | + fn spend_spendable_outputs<C: bitcoin::secp256k1::Signing>( |
| 158 | + &self, |
| 159 | + descriptors: &[&lightning::sign::SpendableOutputDescriptor], |
| 160 | + outputs: Vec<bitcoin::TxOut>, |
| 161 | + change_destination_script: bitcoin::ScriptBuf, |
| 162 | + feerate_sat_per_1000_weight: u32, |
| 163 | + locktime: Option<bitcoin::absolute::LockTime>, |
| 164 | + secp_ctx: &bitcoin::secp256k1::Secp256k1<C>, |
| 165 | + ) -> Result<bitcoin::Transaction, ()> { |
| 166 | + self.inner.spend_spendable_outputs( |
| 167 | + descriptors, |
| 168 | + outputs, |
| 169 | + change_destination_script, |
| 170 | + feerate_sat_per_1000_weight, |
| 171 | + locktime, |
| 172 | + secp_ctx, |
| 173 | + ) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +impl SignerProvider for MyKeysManager { |
| 178 | + type EcdsaSigner = InMemorySigner; |
| 179 | + |
| 180 | + fn derive_channel_signer( |
| 181 | + &self, |
| 182 | + channel_value_satoshis: u64, |
| 183 | + channel_keys_id: [u8; 32], |
| 184 | + ) -> Self::EcdsaSigner { |
| 185 | + if self.funding_key.is_some() { |
| 186 | + let commitment_seed = [ |
| 187 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 188 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 189 | + ]; |
| 190 | + return InMemorySigner::new( |
| 191 | + &Secp256k1::new(), |
| 192 | + self.funding_key.unwrap(), |
| 193 | + self.revocation_base_secret.unwrap(), |
| 194 | + self.payment_base_secret.unwrap(), |
| 195 | + self.delayed_payment_base_secret.unwrap(), |
| 196 | + self.htlc_base_secret.unwrap(), |
| 197 | + commitment_seed, |
| 198 | + channel_value_satoshis, |
| 199 | + channel_keys_id, |
| 200 | + self.shachain_seed.unwrap(), |
| 201 | + ); |
| 202 | + } |
| 203 | + self.inner |
| 204 | + .derive_channel_signer(channel_value_satoshis, channel_keys_id) |
| 205 | + } |
| 206 | + |
| 207 | + fn generate_channel_keys_id( |
| 208 | + &self, |
| 209 | + inbound: bool, |
| 210 | + channel_value_satoshis: u64, |
| 211 | + user_channel_id: u128, |
| 212 | + ) -> [u8; 32] { |
| 213 | + self.inner |
| 214 | + .generate_channel_keys_id(inbound, channel_value_satoshis, user_channel_id) |
| 215 | + } |
| 216 | + |
| 217 | + fn get_destination_script(&self, channel_keys_id: [u8; 32]) -> Result<bitcoin::ScriptBuf, ()> { |
| 218 | + self.inner.get_destination_script(channel_keys_id) |
| 219 | + } |
| 220 | + |
| 221 | + fn get_shutdown_scriptpubkey(&self) -> Result<lightning::ln::script::ShutdownScript, ()> { |
| 222 | + self.inner.get_shutdown_scriptpubkey() |
| 223 | + } |
| 224 | + |
| 225 | + fn read_chan_signer( |
| 226 | + &self, |
| 227 | + reader: &[u8], |
| 228 | + ) -> Result<Self::EcdsaSigner, lightning::ln::msgs::DecodeError> { |
| 229 | + self.inner.read_chan_signer(reader) |
| 230 | + } |
| 231 | +} |
0 commit comments