Skip to content

Commit 09f5e50

Browse files
authored
Merge pull request #2005 from arik-so/2023-01-taproot-message-types
Update messages for Taproot types.
2 parents 3b8bf93 + bc97b82 commit 09f5e50

File tree

7 files changed

+191
-8
lines changed

7 files changed

+191
-8
lines changed

.github/workflows/build.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ jobs:
180180
cargo check --no-default-features --features=futures --release
181181
cargo doc --release
182182
RUSTDOCFLAGS="--cfg=anchors" cargo doc --release
183+
- name: Run cargo check for Taproot build.
184+
run: |
185+
cargo check --release
186+
cargo check --no-default-features --features=no-std --release
187+
cargo check --no-default-features --features=futures --release
188+
cargo doc --release
189+
env:
190+
RUSTFLAGS: '--cfg=anchors --cfg=taproot'
191+
RUSTDOCFLAGS: '--cfg=anchors --cfg=taproot'
183192

184193
fuzz:
185194
runs-on: ubuntu-latest

ci/ci-tests.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,6 @@ fi
9494
echo -e "\n\nTest anchors builds"
9595
pushd lightning
9696
RUSTFLAGS="$RUSTFLAGS --cfg=anchors" cargo test --verbose --color always -p lightning
97+
echo -e "\n\nTest Taproot builds"
98+
RUSTFLAGS="$RUSTFLAGS --cfg=anchors --cfg=taproot" cargo test --verbose --color always -p lightning
9799
popd

lightning/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ regex = "1.5.6"
5656
version = "0.29.0"
5757
default-features = false
5858
features = ["bitcoinconsensus", "secp-recovery"]
59+
60+
[target.'cfg(taproot)'.dependencies]
61+
musig2 = { git = "https://github.com/arik-so/rust-musig2", rev = "27797d7" }

lightning/src/ln/channel.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,9 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
23622362

23632363
Ok((msgs::FundingSigned {
23642364
channel_id: self.channel_id,
2365-
signature
2365+
signature,
2366+
#[cfg(taproot)]
2367+
partial_signature_with_nonce: None,
23662368
}, channel_monitor))
23672369
}
23682370

@@ -3918,6 +3920,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
39183920
channel_id: self.channel_id,
39193921
per_commitment_secret,
39203922
next_per_commitment_point,
3923+
#[cfg(taproot)]
3924+
next_local_nonce: None,
39213925
}
39223926
}
39233927

@@ -5364,6 +5368,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
53645368
None => Builder::new().into_script(),
53655369
}),
53665370
channel_type: Some(self.channel_type.clone()),
5371+
#[cfg(taproot)]
5372+
next_local_nonce: None,
53675373
}
53685374
}
53695375

@@ -5428,7 +5434,11 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
54285434
temporary_channel_id,
54295435
funding_txid: funding_txo.txid,
54305436
funding_output_index: funding_txo.index,
5431-
signature
5437+
signature,
5438+
#[cfg(taproot)]
5439+
partial_signature_with_nonce: None,
5440+
#[cfg(taproot)]
5441+
next_local_nonce: None,
54325442
})
54335443
}
54345444

@@ -5947,6 +5957,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
59475957
channel_id: self.channel_id,
59485958
signature,
59495959
htlc_signatures,
5960+
#[cfg(taproot)]
5961+
partial_signature_with_nonce: None,
59505962
}, (counterparty_commitment_txid, commitment_stats.htlcs_included)))
59515963
}
59525964

lightning/src/ln/functional_tests.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,9 @@ fn test_update_fee_that_funder_cannot_afford() {
738738
let commit_signed_msg = msgs::CommitmentSigned {
739739
channel_id: chan.2,
740740
signature: res.0,
741-
htlc_signatures: res.1
741+
htlc_signatures: res.1,
742+
#[cfg(taproot)]
743+
partial_signature_with_nonce: None,
742744
};
743745

744746
let update_fee = msgs::UpdateFee {
@@ -1458,7 +1460,9 @@ fn test_fee_spike_violation_fails_htlc() {
14581460
let commit_signed_msg = msgs::CommitmentSigned {
14591461
channel_id: chan.2,
14601462
signature: res.0,
1461-
htlc_signatures: res.1
1463+
htlc_signatures: res.1,
1464+
#[cfg(taproot)]
1465+
partial_signature_with_nonce: None,
14621466
};
14631467

14641468
// Send the commitment_signed message to the nodes[1].
@@ -1469,7 +1473,9 @@ fn test_fee_spike_violation_fails_htlc() {
14691473
let raa_msg = msgs::RevokeAndACK {
14701474
channel_id: chan.2,
14711475
per_commitment_secret: local_secret,
1472-
next_per_commitment_point: next_local_point
1476+
next_per_commitment_point: next_local_point,
1477+
#[cfg(taproot)]
1478+
next_local_nonce: None,
14731479
};
14741480
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa_msg);
14751481

@@ -7497,7 +7503,13 @@ fn test_counterparty_raa_skip_no_crash() {
74977503
}
74987504

74997505
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(),
7500-
&msgs::RevokeAndACK { channel_id, per_commitment_secret, next_per_commitment_point });
7506+
&msgs::RevokeAndACK {
7507+
channel_id,
7508+
per_commitment_secret,
7509+
next_per_commitment_point,
7510+
#[cfg(taproot)]
7511+
next_local_nonce: None,
7512+
});
75017513
assert_eq!(check_closed_broadcast!(nodes[1], true).unwrap().data, "Received an unexpected revoke_and_ack");
75027514
check_added_monitors!(nodes[1], 1);
75037515
check_closed_event!(nodes[1], 1, ClosureReason::ProcessingError { err: "Received an unexpected revoke_and_ack".to_string() });

lightning/src/ln/msgs.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ use crate::routing::gossip::NodeId;
5151
/// 21 million * 10^8 * 1000
5252
pub(crate) const MAX_VALUE_MSAT: u64 = 21_000_000_0000_0000_000;
5353

54+
#[cfg(taproot)]
55+
/// A partial signature that also contains the Musig2 nonce its signer used
56+
#[derive(Clone, Debug, PartialEq, Eq)]
57+
pub struct PartialSignatureWithNonce(pub musig2::types::PartialSignature, pub musig2::types::PublicNonce);
58+
5459
/// An error in decoding a message or struct.
5560
#[derive(Clone, Debug, PartialEq, Eq)]
5661
pub enum DecodeError {
@@ -244,6 +249,9 @@ pub struct AcceptChannel {
244249
/// our feature bits with our counterparty's feature bits from the [`Init`] message.
245250
/// This is required to match the equivalent field in [`OpenChannel::channel_type`].
246251
pub channel_type: Option<ChannelTypeFeatures>,
252+
#[cfg(taproot)]
253+
/// Next nonce the channel initiator should use to create a funding output signature against
254+
pub next_local_nonce: Option<musig2::types::PublicNonce>,
247255
}
248256

249257
/// A [`funding_created`] message to be sent to or received from a peer.
@@ -259,6 +267,12 @@ pub struct FundingCreated {
259267
pub funding_output_index: u16,
260268
/// The signature of the channel initiator (funder) on the initial commitment transaction
261269
pub signature: Signature,
270+
#[cfg(taproot)]
271+
/// The partial signature of the channel initiator (funder)
272+
pub partial_signature_with_nonce: Option<PartialSignatureWithNonce>,
273+
#[cfg(taproot)]
274+
/// Next nonce the channel acceptor should use to finalize the funding output signature
275+
pub next_local_nonce: Option<musig2::types::PublicNonce>
262276
}
263277

264278
/// A [`funding_signed`] message to be sent to or received from a peer.
@@ -270,6 +284,9 @@ pub struct FundingSigned {
270284
pub channel_id: [u8; 32],
271285
/// The signature of the channel acceptor (fundee) on the initial commitment transaction
272286
pub signature: Signature,
287+
#[cfg(taproot)]
288+
/// The partial signature of the channel acceptor (fundee)
289+
pub partial_signature_with_nonce: Option<PartialSignatureWithNonce>,
273290
}
274291

275292
/// A [`channel_ready`] message to be sent to or received from a peer.
@@ -409,6 +426,9 @@ pub struct CommitmentSigned {
409426
pub signature: Signature,
410427
/// Signatures on the HTLC transactions
411428
pub htlc_signatures: Vec<Signature>,
429+
#[cfg(taproot)]
430+
/// The partial Taproot signature on the commitment transaction
431+
pub partial_signature_with_nonce: Option<PartialSignatureWithNonce>,
412432
}
413433

414434
/// A [`revoke_and_ack`] message to be sent to or received from a peer.
@@ -422,6 +442,9 @@ pub struct RevokeAndACK {
422442
pub per_commitment_secret: [u8; 32],
423443
/// The next sender-broadcast commitment transaction's per-commitment point
424444
pub next_per_commitment_point: PublicKey,
445+
#[cfg(taproot)]
446+
/// Musig nonce the recipient should use in their next commitment signature message
447+
pub next_local_nonce: Option<musig2::types::PublicNonce>
425448
}
426449

427450
/// An [`update_fee`] message to be sent to or received from a peer
@@ -1288,7 +1311,28 @@ impl Readable for OptionalField<u64> {
12881311
}
12891312
}
12901313

1314+
#[cfg(not(taproot))]
1315+
impl_writeable_msg!(AcceptChannel, {
1316+
temporary_channel_id,
1317+
dust_limit_satoshis,
1318+
max_htlc_value_in_flight_msat,
1319+
channel_reserve_satoshis,
1320+
htlc_minimum_msat,
1321+
minimum_depth,
1322+
to_self_delay,
1323+
max_accepted_htlcs,
1324+
funding_pubkey,
1325+
revocation_basepoint,
1326+
payment_point,
1327+
delayed_payment_basepoint,
1328+
htlc_basepoint,
1329+
first_per_commitment_point,
1330+
shutdown_scriptpubkey
1331+
}, {
1332+
(1, channel_type, option),
1333+
});
12911334

1335+
#[cfg(taproot)]
12921336
impl_writeable_msg!(AcceptChannel, {
12931337
temporary_channel_id,
12941338
dust_limit_satoshis,
@@ -1307,6 +1351,7 @@ impl_writeable_msg!(AcceptChannel, {
13071351
shutdown_scriptpubkey
13081352
}, {
13091353
(1, channel_type, option),
1354+
(4, next_local_nonce, option),
13101355
});
13111356

13121357
impl_writeable_msg!(AnnouncementSignatures, {
@@ -1363,30 +1408,60 @@ impl_writeable!(ClosingSignedFeeRange, {
13631408
max_fee_satoshis
13641409
});
13651410

1411+
#[cfg(not(taproot))]
13661412
impl_writeable_msg!(CommitmentSigned, {
13671413
channel_id,
13681414
signature,
13691415
htlc_signatures
13701416
}, {});
13711417

1418+
#[cfg(taproot)]
1419+
impl_writeable_msg!(CommitmentSigned, {
1420+
channel_id,
1421+
signature,
1422+
htlc_signatures
1423+
}, {
1424+
(2, partial_signature_with_nonce, option)
1425+
});
1426+
13721427
impl_writeable!(DecodedOnionErrorPacket, {
13731428
hmac,
13741429
failuremsg,
13751430
pad
13761431
});
13771432

1433+
#[cfg(not(taproot))]
13781434
impl_writeable_msg!(FundingCreated, {
13791435
temporary_channel_id,
13801436
funding_txid,
13811437
funding_output_index,
13821438
signature
13831439
}, {});
1440+
#[cfg(taproot)]
1441+
impl_writeable_msg!(FundingCreated, {
1442+
temporary_channel_id,
1443+
funding_txid,
1444+
funding_output_index,
1445+
signature
1446+
}, {
1447+
(2, partial_signature_with_nonce, option),
1448+
(4, next_local_nonce, option)
1449+
});
13841450

1451+
#[cfg(not(taproot))]
13851452
impl_writeable_msg!(FundingSigned, {
13861453
channel_id,
13871454
signature
13881455
}, {});
13891456

1457+
#[cfg(taproot)]
1458+
impl_writeable_msg!(FundingSigned, {
1459+
channel_id,
1460+
signature
1461+
}, {
1462+
(2, partial_signature_with_nonce, option)
1463+
});
1464+
13901465
impl_writeable_msg!(ChannelReady, {
13911466
channel_id,
13921467
next_per_commitment_point,
@@ -1446,12 +1521,22 @@ impl_writeable_msg!(OpenChannel, {
14461521
(1, channel_type, option),
14471522
});
14481523

1524+
#[cfg(not(taproot))]
14491525
impl_writeable_msg!(RevokeAndACK, {
14501526
channel_id,
14511527
per_commitment_secret,
14521528
next_per_commitment_point
14531529
}, {});
14541530

1531+
#[cfg(taproot)]
1532+
impl_writeable_msg!(RevokeAndACK, {
1533+
channel_id,
1534+
per_commitment_secret,
1535+
next_per_commitment_point
1536+
}, {
1537+
(4, next_local_nonce, option)
1538+
});
1539+
14551540
impl_writeable_msg!(Shutdown, {
14561541
channel_id,
14571542
scriptpubkey
@@ -2444,6 +2529,8 @@ mod tests {
24442529
first_per_commitment_point: pubkey_6,
24452530
shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, inner: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent },
24462531
channel_type: None,
2532+
#[cfg(taproot)]
2533+
next_local_nonce: None,
24472534
};
24482535
let encoded_value = accept_channel.encode();
24492536
let mut target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020212345678901234562334032891223698321446687011447600083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap();
@@ -2469,6 +2556,10 @@ mod tests {
24692556
funding_txid: Txid::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
24702557
funding_output_index: 255,
24712558
signature: sig_1,
2559+
#[cfg(taproot)]
2560+
partial_signature_with_nonce: None,
2561+
#[cfg(taproot)]
2562+
next_local_nonce: None,
24722563
};
24732564
let encoded_value = funding_created.encode();
24742565
let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202026e96fe9f8b0ddcd729ba03cfafa5a27b050b39d354dd980814268dfa9a44d4c200ffd977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
@@ -2483,6 +2574,8 @@ mod tests {
24832574
let funding_signed = msgs::FundingSigned {
24842575
channel_id: [2; 32],
24852576
signature: sig_1,
2577+
#[cfg(taproot)]
2578+
partial_signature_with_nonce: None,
24862579
};
24872580
let encoded_value = funding_signed.encode();
24882581
let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
@@ -2646,6 +2739,8 @@ mod tests {
26462739
channel_id: [2; 32],
26472740
signature: sig_1,
26482741
htlc_signatures: if htlcs { vec![sig_2, sig_3, sig_4] } else { Vec::new() },
2742+
#[cfg(taproot)]
2743+
partial_signature_with_nonce: None,
26492744
};
26502745
let encoded_value = commitment_signed.encode();
26512746
let mut target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a").unwrap();
@@ -2671,6 +2766,8 @@ mod tests {
26712766
channel_id: [2; 32],
26722767
per_commitment_secret: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
26732768
next_per_commitment_point: pubkey_1,
2769+
#[cfg(taproot)]
2770+
next_local_nonce: None,
26742771
};
26752772
let encoded_value = raa.encode();
26762773
let target_value = hex::decode("02020202020202020202020202020202020202020202020202020202020202020101010101010101010101010101010101010101010101010101010101010101031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();

0 commit comments

Comments
 (0)