smite: add closing_sig codec#109
Open
ekzyis wants to merge 2 commits into
Open
Conversation
ekzyis
commented
Jun 1, 2026
Contributor
Author
There was a problem hiding this comment.
This is basically the same as ClosingComplete. The only differences are naming and documentation:
diff -u {closing_complete,closing_sig}.rs
--- closing_complete.rs 2026-05-29 21:35:33.683302824 +0200
+++ closing_sig.rs 2026-05-29 21:35:10.695737395 +0200
@@ -1,4 +1,4 @@
-//! BOLT 2 `closing_complete` message.
+//! BOLT 2 `closing_sig` message.
use super::BoltError;
use super::tlv::TlvStream;
@@ -13,38 +13,39 @@
/// TLV type for closer and closee outputs.
const TLV_CLOSER_AND_CLOSEE_OUTPUTS: u64 = 3;
-// BOLT 2 `closing_complete` message (type 40).
+// BOLT 2 `closing_sig` message (type 41).
//
-// When closing a channel and shutdown is complete, each peer sends
-// `closing_complete` with the transaction details.
+// In response to `closing_complete`, the closee signs the proposed closing
+// transaction and sends back `closing_sig` with the signature in the same TLV
+// field that was used in `closing_complete`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
-pub struct ClosingComplete {
+pub struct ClosingSig {
// The ID of the channel to be closed.
pub channel_id: ChannelId,
- // Output script for local ("closer") output.
+ // Output script for remote ("closer") output.
pub closer_scriptpubkey: Vec<u8>,
- // Output script for remote ("closee") output.
+ // Output script for local ("closee") output.
pub closee_scriptpubkey: Vec<u8>,
// Suggested absolute fee for the closing tx.
pub fee_satoshis: u64,
// Suggested locktime for the closing tx.
pub locktime: u32,
// Optional TLV extensions.
- pub tlvs: ClosingCompleteTlvs,
+ pub tlvs: ClosingSigTlvs,
}
-/// TLV extensions for the `closing_complete` message.
+/// TLV extensions for the `closing_sig` message.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
-pub struct ClosingCompleteTlvs {
- // Signature if closing tx only has local output.
+pub struct ClosingSigTlvs {
+ // Signature if closing tx only has closer output.
pub closer_output_only: Option<Signature>,
- // Signature if closing tx only has remote output.
+ // Signature if closing tx only has closee output.
pub closee_output_only: Option<Signature>,
- // Signature if closing tx has local and remote output.
+ // Signature if closing tx has closer and closee output.
pub closer_and_closee_outputs: Option<Signature>,
}
-impl ClosingComplete {
+impl ClosingSig {
/// Encodes to wire format (without message type prefix).
#[must_use]
pub fn encode(&self) -> Vec<u8> {
@@ -99,7 +100,7 @@
// Decode TLVs (remaining bytes)
let tlv_stream = TlvStream::decode_with_known(cursor, &[TLV_CLOSEE_OUTPUT_ONLY])?;
- let tlvs = ClosingCompleteTlvs::from_stream(&tlv_stream)?;
+ let tlvs = ClosingSigTlvs::from_stream(&tlv_stream)?;
Ok(Self {
channel_id,
@@ -112,8 +113,8 @@
}
}
-impl ClosingCompleteTlvs {
- /// Extracts `closing_complete` TLVs from a parsed TLV stream.
+impl ClosingSigTlvs {
+ /// Extracts `closing_sig` TLVs from a parsed TLV stream.
///
/// # Errors
///
@@ -162,16 +163,16 @@
#[test]
fn roundtrip() {
- let original = ClosingComplete::default();
+ let original = ClosingSig::default();
let encoded = original.encode();
- let decoded = ClosingComplete::decode(&encoded).unwrap();
+ let decoded = ClosingSig::decode(&encoded).unwrap();
assert_eq!(original, decoded);
}
#[test]
fn decode_truncated_channel_id() {
assert_eq!(
- ClosingComplete::decode(&[0x11; 20]),
+ ClosingSig::decode(&[0x11; 20]),
Err(BoltError::Truncated {
expected: CHANNEL_ID_SIZE,
actual: 20
@@ -185,7 +186,7 @@
// one byte intead of two for length prefix
data.extend_from_slice(&[0x00]);
assert_eq!(
- ClosingComplete::decode(&data),
+ ClosingSig::decode(&data),
Err(BoltError::Truncated {
expected: 2,
actual: 1
@@ -200,7 +201,7 @@
data.extend_from_slice(&[0x00, 0x0a]);
data.extend_from_slice(&[0x22; 0x05]);
assert_eq!(
- ClosingComplete::decode(&data),
+ ClosingSig::decode(&data),
Err(BoltError::Truncated {
expected: 10,
actual: 5
@@ -216,7 +217,7 @@
// one byte instead of two for length prefix
data.extend_from_slice(&[0x00]);
assert_eq!(
- ClosingComplete::decode(&data),
+ ClosingSig::decode(&data),
Err(BoltError::Truncated {
expected: 2,
actual: 1
@@ -233,7 +234,7 @@
data.extend_from_slice(&[0x00, 0x0b]);
data.extend_from_slice(&[0x33; 0x0a]);
assert_eq!(
- ClosingComplete::decode(&data),
+ ClosingSig::decode(&data),
Err(BoltError::Truncated {
expected: 11,
actual: 10
@@ -251,7 +252,7 @@
// only three of eight bytes for fee_satoshis
data.extend_from_slice(&[0x44; 0x03]);
assert_eq!(
- ClosingComplete::decode(&data),
+ ClosingSig::decode(&data),
Err(BoltError::Truncated {
expected: 8,
actual: 3
@@ -270,7 +271,7 @@
// only 1 of four bytes for locktime
data.extend_from_slice(&[0x55]);
assert_eq!(
- ClosingComplete::decode(&data),
+ ClosingSig::decode(&data),
Err(BoltError::Truncated {
expected: 4,
actual: 1
@@ -281,8 +282,8 @@
#[test]
fn roundtrip_with_tlvs() {
let sig = Signature::from_compact(&[1u8; 64]).expect("valid signature");
- let original = ClosingComplete {
- tlvs: ClosingCompleteTlvs {
+ let original = ClosingSig {
+ tlvs: ClosingSigTlvs {
closer_output_only: Some(sig),
closee_output_only: Some(sig),
closer_and_closee_outputs: Some(sig),
@@ -291,14 +292,14 @@
};
let encoded = original.encode();
- let decoded = ClosingComplete::decode(&encoded).unwrap();
+ let decoded = ClosingSig::decode(&encoded).unwrap();
assert_eq!(original, decoded);
}
#[test]
fn decode_empty_tlv_values() {
- let msg = ClosingComplete::default();
- let decoded = ClosingComplete::decode(&msg.encode()).unwrap();
+ let msg = ClosingSig::default();
+ let decoded = ClosingSig::decode(&msg.encode()).unwrap();
assert!(decoded.tlvs.closer_output_only.is_none());
assert!(decoded.tlvs.closee_output_only.is_none());
assert!(decoded.tlvs.closer_and_closee_outputs.is_none());
@@ -306,7 +307,7 @@
#[test]
fn default_tlvs_are_none() {
- let tlvs = ClosingCompleteTlvs::default();
+ let tlvs = ClosingSigTlvs::default();
assert!(tlvs.closer_output_only.is_none());
assert!(tlvs.closee_output_only.is_none());
assert!(tlvs.closer_and_closee_outputs.is_none());
@@ -315,20 +316,20 @@
#[test]
fn decode_invalid_signature_tlv() {
let sig = Signature::from_compact(&[1u8; 64]).expect("valid signature");
- let cc = ClosingComplete {
- tlvs: ClosingCompleteTlvs {
+ let cs = ClosingSig {
+ tlvs: ClosingSigTlvs {
closer_output_only: Some(sig),
..Default::default()
},
..Default::default()
};
- let mut encoded = cc.encode();
+ let mut encoded = cs.encode();
let sig_start = encoded.len() - COMPACT_SIGNATURE_SIZE;
encoded[sig_start..].copy_from_slice(&[0xff; COMPACT_SIGNATURE_SIZE]);
assert!(matches!(
- ClosingComplete::decode(&encoded),
+ ClosingSig::decode(&encoded),
Err(BoltError::InvalidSignature(_))
));
}
Other similar codecs are TxRemoveInput and TxRemoveOutput.
I think it's okay to keep them as-is. I don't expect many changes that we'd have to duplicate.
The slightly different documentation might also be valuable.
I'm curious what others think, though.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
part of #98 | based on #105
This is basically the same as #105.