|
| 1 | +use bitcoin::{secp256k1::PublicKey, ScriptBuf, TxOut}; |
| 2 | + |
| 3 | +#[derive(Clone)] |
| 4 | +pub struct PayjoinChannelScheduler { |
| 5 | + channels: Vec<PayjoinChannel>, |
| 6 | +} |
| 7 | + |
| 8 | +impl PayjoinChannelScheduler { |
| 9 | + pub(crate) fn new() -> Self { |
| 10 | + Self { channels: vec![] } |
| 11 | + } |
| 12 | + |
| 13 | + pub(crate) fn schedule( |
| 14 | + &mut self, channel_value_satoshi: bitcoin::Amount, counterparty_node_id: PublicKey, |
| 15 | + channel_id: u128, |
| 16 | + ) { |
| 17 | + let channel = PayjoinChannel::new(channel_value_satoshi, counterparty_node_id, channel_id); |
| 18 | + match channel.state { |
| 19 | + ScheduledChannelState::ChannelCreated => { |
| 20 | + self.channels.push(channel); |
| 21 | + }, |
| 22 | + _ => {}, |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + pub(crate) fn set_channel_accepted( |
| 27 | + &mut self, channel_id: u128, output_script: &ScriptBuf, temporary_channel_id: [u8; 32], |
| 28 | + ) -> bool { |
| 29 | + for channel in &mut self.channels { |
| 30 | + if channel.channel_id() == channel_id { |
| 31 | + channel.state.set_channel_accepted(output_script, temporary_channel_id); |
| 32 | + return true; |
| 33 | + } |
| 34 | + } |
| 35 | + false |
| 36 | + } |
| 37 | + |
| 38 | + pub(crate) fn set_funding_tx_created( |
| 39 | + &mut self, channel_id: u128, url: &payjoin::Url, body: Vec<u8>, |
| 40 | + ) -> bool { |
| 41 | + for channel in &mut self.channels { |
| 42 | + if channel.channel_id() == channel_id { |
| 43 | + return channel.state.set_channel_funding_tx_created(url.clone(), body); |
| 44 | + } |
| 45 | + } |
| 46 | + false |
| 47 | + } |
| 48 | + |
| 49 | + pub(crate) fn set_funding_tx_signed( |
| 50 | + &mut self, tx: bitcoin::Transaction, |
| 51 | + ) -> Option<(payjoin::Url, Vec<u8>)> { |
| 52 | + for output in tx.output.iter() { |
| 53 | + if let Some(mut channel) = self.internal_find_by_tx_out(&output.clone()) { |
| 54 | + let info = channel.request_info(); |
| 55 | + if info.is_some() && channel.state.set_channel_funding_tx_signed(output.clone()) { |
| 56 | + return info; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + None |
| 61 | + } |
| 62 | + |
| 63 | + /// Get the next channel matching the given channel amount. |
| 64 | + /// |
| 65 | + /// The channel must be in accepted state. |
| 66 | + /// |
| 67 | + /// If more than one channel matches the given channel amount, the channel with the oldest |
| 68 | + /// creation date will be returned. |
| 69 | + pub(crate) fn get_next_channel( |
| 70 | + &self, channel_amount: bitcoin::Amount, |
| 71 | + ) -> Option<(u128, bitcoin::Address, [u8; 32], bitcoin::Amount, bitcoin::secp256k1::PublicKey)> |
| 72 | + { |
| 73 | + let channel = self |
| 74 | + .channels |
| 75 | + .iter() |
| 76 | + .filter(|channel| { |
| 77 | + channel.channel_value_satoshi() == channel_amount |
| 78 | + && channel.is_channel_accepted() |
| 79 | + && channel.output_script().is_some() |
| 80 | + && channel.temporary_channel_id().is_some() |
| 81 | + }) |
| 82 | + .min_by_key(|channel| channel.created_at()); |
| 83 | + |
| 84 | + if let Some(channel) = channel { |
| 85 | + let address = bitcoin::Address::from_script( |
| 86 | + &channel.output_script().unwrap(), |
| 87 | + bitcoin::Network::Regtest, // fixme |
| 88 | + ); |
| 89 | + if let Ok(address) = address { |
| 90 | + return Some(( |
| 91 | + channel.channel_id(), |
| 92 | + address, |
| 93 | + channel.temporary_channel_id().unwrap(), |
| 94 | + channel.channel_value_satoshi(), |
| 95 | + channel.counterparty_node_id(), |
| 96 | + )); |
| 97 | + } |
| 98 | + }; |
| 99 | + None |
| 100 | + } |
| 101 | + |
| 102 | + fn internal_find_by_tx_out(&self, txout: &TxOut) -> Option<PayjoinChannel> { |
| 103 | + let channel = self.channels.iter().find(|channel| { |
| 104 | + return Some(&txout.script_pubkey) == channel.output_script(); |
| 105 | + }); |
| 106 | + channel.cloned() |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +#[derive(Clone, Debug)] |
| 111 | +pub(crate) struct PayjoinChannel { |
| 112 | + state: ScheduledChannelState, |
| 113 | + channel_value_satoshi: bitcoin::Amount, |
| 114 | + channel_id: u128, |
| 115 | + counterparty_node_id: PublicKey, |
| 116 | + created_at: u64, |
| 117 | +} |
| 118 | + |
| 119 | +impl PayjoinChannel { |
| 120 | + pub(crate) fn new( |
| 121 | + channel_value_satoshi: bitcoin::Amount, counterparty_node_id: PublicKey, channel_id: u128, |
| 122 | + ) -> Self { |
| 123 | + Self { |
| 124 | + state: ScheduledChannelState::ChannelCreated, |
| 125 | + channel_value_satoshi, |
| 126 | + channel_id, |
| 127 | + counterparty_node_id, |
| 128 | + created_at: 0, |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + fn is_channel_accepted(&self) -> bool { |
| 133 | + match self.state { |
| 134 | + ScheduledChannelState::ChannelAccepted(..) => true, |
| 135 | + _ => false, |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + pub(crate) fn channel_value_satoshi(&self) -> bitcoin::Amount { |
| 140 | + self.channel_value_satoshi |
| 141 | + } |
| 142 | + |
| 143 | + pub(crate) fn channel_id(&self) -> u128 { |
| 144 | + self.channel_id |
| 145 | + } |
| 146 | + |
| 147 | + pub(crate) fn counterparty_node_id(&self) -> PublicKey { |
| 148 | + self.counterparty_node_id |
| 149 | + } |
| 150 | + |
| 151 | + pub(crate) fn output_script(&self) -> Option<&ScriptBuf> { |
| 152 | + self.state.output_script() |
| 153 | + } |
| 154 | + |
| 155 | + pub(crate) fn temporary_channel_id(&self) -> Option<[u8; 32]> { |
| 156 | + self.state.temporary_channel_id() |
| 157 | + } |
| 158 | + |
| 159 | + pub(crate) fn request_info(&self) -> Option<(payjoin::Url, Vec<u8>)> { |
| 160 | + match &self.state { |
| 161 | + ScheduledChannelState::FundingTxCreated(_, url, body) => { |
| 162 | + Some((url.clone(), body.clone())) |
| 163 | + }, |
| 164 | + _ => None, |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + fn created_at(&self) -> u64 { |
| 169 | + self.created_at |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +#[derive(Clone, Debug)] |
| 174 | +struct FundingTxParams { |
| 175 | + output_script: ScriptBuf, |
| 176 | + temporary_channel_id: [u8; 32], |
| 177 | +} |
| 178 | + |
| 179 | +impl FundingTxParams { |
| 180 | + fn new(output_script: ScriptBuf, temporary_channel_id: [u8; 32]) -> Self { |
| 181 | + Self { output_script, temporary_channel_id } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +#[derive(Clone, Debug)] |
| 186 | +enum ScheduledChannelState { |
| 187 | + ChannelCreated, |
| 188 | + ChannelAccepted(FundingTxParams), |
| 189 | + FundingTxCreated(FundingTxParams, payjoin::Url, Vec<u8>), |
| 190 | + FundingTxSigned(FundingTxParams, ()), |
| 191 | +} |
| 192 | + |
| 193 | +impl ScheduledChannelState { |
| 194 | + fn output_script(&self) -> Option<&ScriptBuf> { |
| 195 | + match self { |
| 196 | + ScheduledChannelState::ChannelAccepted(funding_tx_params) => { |
| 197 | + Some(&funding_tx_params.output_script) |
| 198 | + }, |
| 199 | + ScheduledChannelState::FundingTxCreated(funding_tx_params, _, _) => { |
| 200 | + Some(&funding_tx_params.output_script) |
| 201 | + }, |
| 202 | + ScheduledChannelState::FundingTxSigned(funding_tx_params, _) => { |
| 203 | + Some(&funding_tx_params.output_script) |
| 204 | + }, |
| 205 | + _ => None, |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + fn temporary_channel_id(&self) -> Option<[u8; 32]> { |
| 210 | + match self { |
| 211 | + ScheduledChannelState::ChannelAccepted(funding_tx_params) => { |
| 212 | + Some(funding_tx_params.temporary_channel_id) |
| 213 | + }, |
| 214 | + ScheduledChannelState::FundingTxCreated(funding_tx_params, _, _) => { |
| 215 | + Some(funding_tx_params.temporary_channel_id) |
| 216 | + }, |
| 217 | + ScheduledChannelState::FundingTxSigned(funding_tx_params, _) => { |
| 218 | + Some(funding_tx_params.temporary_channel_id) |
| 219 | + }, |
| 220 | + _ => None, |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + fn set_channel_accepted( |
| 225 | + &mut self, output_script: &ScriptBuf, temporary_channel_id: [u8; 32], |
| 226 | + ) -> bool { |
| 227 | + if let ScheduledChannelState::ChannelCreated = self { |
| 228 | + *self = ScheduledChannelState::ChannelAccepted(FundingTxParams::new( |
| 229 | + output_script.clone(), |
| 230 | + temporary_channel_id, |
| 231 | + )); |
| 232 | + return true; |
| 233 | + } |
| 234 | + return false; |
| 235 | + } |
| 236 | + |
| 237 | + fn set_channel_funding_tx_created(&mut self, url: payjoin::Url, body: Vec<u8>) -> bool { |
| 238 | + if let ScheduledChannelState::ChannelAccepted(funding_tx_params) = self { |
| 239 | + *self = ScheduledChannelState::FundingTxCreated(funding_tx_params.clone(), url, body); |
| 240 | + return true; |
| 241 | + } |
| 242 | + return false; |
| 243 | + } |
| 244 | + |
| 245 | + fn set_channel_funding_tx_signed(&mut self, output: TxOut) -> bool { |
| 246 | + let mut res = false; |
| 247 | + if let ScheduledChannelState::FundingTxCreated(funding_tx_params, _, _) = self { |
| 248 | + assert_eq!(funding_tx_params.output_script, output.script_pubkey); |
| 249 | + *self = ScheduledChannelState::FundingTxSigned(funding_tx_params.clone(), ()); |
| 250 | + res = true; |
| 251 | + } |
| 252 | + return res; |
| 253 | + } |
| 254 | +} |
0 commit comments