Skip to content

Commit

Permalink
Add back vanilla channels and support for swaps
Browse files Browse the repository at this point in the history
Co-authored-by: Alekos Filini <alekos.filini@gmail.com>
  • Loading branch information
danielabrozzoni and afilini committed Sep 19, 2023
1 parent 8f9059d commit bfef26e
Show file tree
Hide file tree
Showing 15 changed files with 340 additions and 120 deletions.
4 changes: 2 additions & 2 deletions lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for ChannelMonitorImpl<Signe
writer.write_all(&$htlc_output.cltv_expiry.to_be_bytes())?;
writer.write_all(&$htlc_output.payment_hash.0[..])?;
$htlc_output.transaction_output_index.write(writer)?;
writer.write_all(&$htlc_output.amount_rgb.to_be_bytes())?;
$htlc_output.amount_rgb.write(writer)?;
}
}

Expand Down Expand Up @@ -3853,7 +3853,7 @@ impl<'a, 'b, ES: EntropySource, SP: SignerProvider> ReadableArgs<(&'a ES, &'b SP
let cltv_expiry: u32 = Readable::read(reader)?;
let payment_hash: PaymentHash = Readable::read(reader)?;
let transaction_output_index: Option<u32> = Readable::read(reader)?;
let amount_rgb: u64 = Readable::read(reader)?;
let amount_rgb: Option<u64> = Readable::read(reader)?;

HTLCOutputInCommitment {
offered, amount_msat, cltv_expiry, payment_hash, transaction_output_index, amount_rgb
Expand Down
8 changes: 5 additions & 3 deletions lightning/src/chain/keysinterface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,9 +824,11 @@ impl EcdsaChannelSigner for InMemorySigner {
for htlc in commitment_tx.htlcs() {
let channel_parameters = self.get_channel_parameters();
let mut htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_tx.feerate_per_kw(), self.holder_selected_contest_delay(), htlc, self.opt_anchors(), channel_parameters.opt_non_zero_fee_anchors.is_some(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
match color_htlc(&mut htlc_tx, &htlc, &self.ldk_data_dir) {
Err(_e) => return Err(()),
_ => {}
if commitment_tx.is_colored {
match color_htlc(&mut htlc_tx, &htlc, &self.ldk_data_dir) {
Err(_e) => return Err(()),
_ => {}
}
}
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, self.opt_anchors(), &keys);
let htlc_sighashtype = if self.opt_anchors() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
Expand Down
27 changes: 26 additions & 1 deletion lightning/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,15 @@ pub enum Event {
/// check that whatever fee you want has been included here or subtract it as required. Further,
/// LDK will not stop you from forwarding more than you received.
expected_outbound_amount_msat: u64,

/// Inbound assets, if any
inbound_rgb_amount: Option<u64>,
/// How much assets should be forwarded, if any
expected_outbound_rgb_amount: Option<u64>,
/// Whether the intercept is a swap
is_swap: bool,
/// Previous short channel id
prev_short_channel_id: u64,
},
/// Used to indicate that an output which you should know how to spend was confirmed on chain
/// and is now spendable.
Expand Down Expand Up @@ -903,7 +912,7 @@ impl Writeable for Event {
(0, WithoutLength(outputs), required),
});
},
&Event::HTLCIntercepted { requested_next_hop_scid, payment_hash, inbound_amount_msat, expected_outbound_amount_msat, intercept_id } => {
&Event::HTLCIntercepted { requested_next_hop_scid, payment_hash, inbound_amount_msat, expected_outbound_amount_msat, inbound_rgb_amount, expected_outbound_rgb_amount, intercept_id, is_swap, prev_short_channel_id } => {
6u8.write(writer)?;
let intercept_scid = InterceptNextHop::FakeScid { requested_next_hop_scid };
write_tlv_fields!(writer, {
Expand All @@ -912,6 +921,10 @@ impl Writeable for Event {
(4, payment_hash, required),
(6, inbound_amount_msat, required),
(8, expected_outbound_amount_msat, required),
(10, inbound_rgb_amount, option),
(12, expected_outbound_rgb_amount, option),
(14, is_swap, required),
(16, prev_short_channel_id, required),
});
}
&Event::PaymentForwarded {
Expand Down Expand Up @@ -1174,12 +1187,20 @@ impl MaybeReadable for Event {
let mut requested_next_hop_scid = InterceptNextHop::FakeScid { requested_next_hop_scid: 0 };
let mut inbound_amount_msat = 0;
let mut expected_outbound_amount_msat = 0;
let mut inbound_rgb_amount = None;
let mut expected_outbound_rgb_amount = None;
let mut is_swap = false;
let mut prev_short_channel_id = 0;
read_tlv_fields!(reader, {
(0, intercept_id, required),
(2, requested_next_hop_scid, required),
(4, payment_hash, required),
(6, inbound_amount_msat, required),
(8, expected_outbound_amount_msat, required),
(10, inbound_rgb_amount, option),
(12, expected_outbound_rgb_amount, option),
(14, is_swap, required),
(16, prev_short_channel_id, required),
});
let next_scid = match requested_next_hop_scid {
InterceptNextHop::FakeScid { requested_next_hop_scid: scid } => scid
Expand All @@ -1189,7 +1210,11 @@ impl MaybeReadable for Event {
requested_next_hop_scid: next_scid,
inbound_amount_msat,
expected_outbound_amount_msat,
inbound_rgb_amount,
expected_outbound_rgb_amount,
intercept_id,
is_swap,
prev_short_channel_id,
}))
},
7u8 => {
Expand Down
23 changes: 15 additions & 8 deletions lightning/src/ln/chan_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ pub struct HTLCOutputInCommitment {
/// value is spent to additional transaction fees).
pub transaction_output_index: Option<u32>,
/// The RGB amount allocated to the HTLC
pub amount_rgb: u64,
pub amount_rgb: Option<u64>,
}

impl_writeable_tlv_based!(HTLCOutputInCommitment, {
Expand All @@ -575,7 +575,7 @@ impl_writeable_tlv_based!(HTLCOutputInCommitment, {
(4, cltv_expiry, required),
(6, payment_hash, required),
(8, transaction_output_index, option),
(10, amount_rgb, required),
(10, amount_rgb, option),
});

#[inline]
Expand Down Expand Up @@ -1022,7 +1022,7 @@ impl HolderCommitmentTransaction {
for _ in 0..htlcs.len() {
counterparty_htlc_sigs.push(dummy_sig);
}
let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, 0, false, dummy_key.clone(), dummy_key.clone(), keys, 0, htlcs, &channel_parameters.as_counterparty_broadcastable());
let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, 0, false, dummy_key.clone(), dummy_key.clone(), keys, 0, htlcs, &channel_parameters.as_counterparty_broadcastable(), false);
htlcs.sort_by_key(|htlc| htlc.0.transaction_output_index);
HolderCommitmentTransaction {
inner,
Expand Down Expand Up @@ -1248,6 +1248,7 @@ pub struct CommitmentTransaction {
keys: TxCreationKeys,
// For access to the pre-built transaction, see doc for trust()
pub(crate) built: BuiltCommitmentTransaction,
pub(crate) is_colored: bool,
}

impl Eq for CommitmentTransaction {}
Expand Down Expand Up @@ -1278,6 +1279,7 @@ impl_writeable_tlv_based!(CommitmentTransaction, {
(12, htlcs, vec_type),
(14, opt_anchors, option),
(16, opt_non_zero_fee_anchors, option),
(18, is_colored, required),
});

impl CommitmentTransaction {
Expand All @@ -1291,7 +1293,7 @@ impl CommitmentTransaction {
/// Only include HTLCs that are above the dust limit for the channel.
///
/// This is not exported to bindings users due to the generic though we likely should expose a version without
pub fn new_with_auxiliary_htlc_data<T>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, opt_anchors: bool, broadcaster_funding_key: PublicKey, countersignatory_funding_key: PublicKey, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters) -> CommitmentTransaction {
pub fn new_with_auxiliary_htlc_data<T>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, opt_anchors: bool, broadcaster_funding_key: PublicKey, countersignatory_funding_key: PublicKey, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters, is_colored: bool) -> CommitmentTransaction {
// Sort outputs and populate output indices while keeping track of the auxiliary data
let (outputs, htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters, opt_anchors, &broadcaster_funding_key, &countersignatory_funding_key).unwrap();

Expand All @@ -1311,6 +1313,7 @@ impl CommitmentTransaction {
txid
},
opt_non_zero_fee_anchors: None,
is_colored,
}
}

Expand Down Expand Up @@ -1590,9 +1593,11 @@ impl<'a> TrustedCommitmentTransaction<'a> {
for this_htlc in inner.htlcs.iter() {
assert!(this_htlc.transaction_output_index.is_some());
let mut htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, self.opt_anchors(), self.opt_non_zero_fee_anchors.is_some(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
match color_htlc(&mut htlc_tx, &this_htlc, &ldk_data_dir) {
Err(_e) => return Err(()),
_ => {}
if inner.is_colored {
match color_htlc(&mut htlc_tx, &this_htlc, &ldk_data_dir) {
Err(_e) => return Err(()),
_ => {}
}
}

let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(&this_htlc, self.opt_anchors(), &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key);
Expand All @@ -1616,7 +1621,9 @@ impl<'a> TrustedCommitmentTransaction<'a> {
if this_htlc.offered && preimage.is_some() { unreachable!(); }

let mut htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, self.opt_anchors(), self.opt_non_zero_fee_anchors.is_some(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
color_htlc(&mut htlc_tx, &this_htlc, &ldk_data_dir).expect("successful htlc tx coloring");
if inner.is_colored {
color_htlc(&mut htlc_tx, &this_htlc, &ldk_data_dir).expect("successful htlc tx coloring");
}

let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(&this_htlc, self.opt_anchors(), &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key);

Expand Down
Loading

0 comments on commit bfef26e

Please sign in to comment.