mirror of
https://github.com/aljazceru/cdk.git
synced 2026-02-02 19:55:56 +01:00
Fix htlc witness deserialization (#1138)
* Add prior signatures if they exist
This commit is contained in:
@@ -281,12 +281,12 @@ impl PartialOrd for BlindSignature {
|
||||
#[serde(untagged)]
|
||||
#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
|
||||
pub enum Witness {
|
||||
/// P2PK Witness
|
||||
#[serde(with = "serde_p2pk_witness")]
|
||||
P2PKWitness(P2PKWitness),
|
||||
/// HTLC Witness
|
||||
#[serde(with = "serde_htlc_witness")]
|
||||
HTLCWitness(HTLCWitness),
|
||||
/// P2PK Witness
|
||||
#[serde(with = "serde_p2pk_witness")]
|
||||
P2PKWitness(P2PKWitness),
|
||||
}
|
||||
|
||||
impl From<P2PKWitness> for Witness {
|
||||
@@ -1042,4 +1042,28 @@ mod tests {
|
||||
assert_eq!(method, deserialized);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_witness_serialization() {
|
||||
let htlc_witness = HTLCWitness {
|
||||
preimage: "preimage".to_string(),
|
||||
signatures: Some(vec!["sig1".to_string()]),
|
||||
};
|
||||
let witness = Witness::HTLCWitness(htlc_witness);
|
||||
|
||||
let serialized = serde_json::to_string(&witness).unwrap();
|
||||
let deserialized: Witness = serde_json::from_str(&serialized).unwrap();
|
||||
|
||||
assert!(matches!(deserialized, Witness::HTLCWitness(_)));
|
||||
|
||||
let p2pk_witness = P2PKWitness {
|
||||
signatures: vec!["sig1".to_string(), "sig2".to_string()],
|
||||
};
|
||||
let witness = Witness::P2PKWitness(p2pk_witness);
|
||||
|
||||
let serialized = serde_json::to_string(&witness).unwrap();
|
||||
let deserialized: Witness = serde_json::from_str(&serialized).unwrap();
|
||||
|
||||
assert!(matches!(deserialized, Witness::P2PKWitness(_)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ pub enum Error {
|
||||
#[derive(Default, Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "swagger", derive(utoipa::ToSchema))]
|
||||
pub struct HTLCWitness {
|
||||
/// Primage
|
||||
/// Preimage
|
||||
pub preimage: String,
|
||||
/// Signatures
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
@@ -139,9 +139,15 @@ impl Proof {
|
||||
/// Add Preimage
|
||||
#[inline]
|
||||
pub fn add_preimage(&mut self, preimage: String) {
|
||||
let signatures = self
|
||||
.witness
|
||||
.as_ref()
|
||||
.map(|w| w.signatures())
|
||||
.unwrap_or_default();
|
||||
|
||||
self.witness = Some(Witness::HTLCWitness(HTLCWitness {
|
||||
preimage,
|
||||
signatures: None,
|
||||
signatures,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user