use std::fmt;
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct SwapRoleKeys<Pk> {
pub alice: Pk,
pub bob: Pk,
}
impl<Pk> SwapRoleKeys<Pk> {
pub fn new(alice: Pk, bob: Pk) -> Self {
Self { alice, bob }
}
}
impl<Pk> fmt::Display for SwapRoleKeys<Pk>
where
Pk: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Alice: {}, Bob: {}", self.alice, self.bob)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Display, Serialize, Deserialize)]
#[display(Debug)]
pub enum ScriptPath {
Success,
Failure,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct DataLock<Ti, Pk> {
pub timelock: Ti,
pub success: SwapRoleKeys<Pk>,
pub failure: SwapRoleKeys<Pk>,
}
impl<Ti, Pk> fmt::Display for DataLock<Ti, Pk>
where
Ti: fmt::Display,
Pk: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Timelock: {}, Success: <{}>, Failure: <{}>",
self.timelock, self.success, self.failure
)
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct DataPunishableLock<Ti, Pk> {
pub timelock: Ti,
pub success: SwapRoleKeys<Pk>,
pub failure: Pk,
}
impl<Ti, Pk> fmt::Display for DataPunishableLock<Ti, Pk>
where
Ti: fmt::Display,
Pk: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Timelock: {}, Success: <{}>, Failure: {}",
self.timelock, self.success, self.failure
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use bitcoin::secp256k1::PublicKey;
#[test]
fn serde_serialize_double_keys() {
let public_key = PublicKey::from_slice(&[
0x02, 0xc6, 0x6e, 0x7d, 0x89, 0x66, 0xb5, 0xc5, 0x55, 0xaf, 0x58, 0x05, 0x98, 0x9d,
0xa9, 0xfb, 0xf8, 0xdb, 0x95, 0xe1, 0x56, 0x31, 0xce, 0x35, 0x8c, 0x3a, 0x17, 0x10,
0xc9, 0x62, 0x67, 0x90, 0x63,
])
.expect("public keys must be 33 or 65 bytes, serialized according to SEC 2");
let double_key = SwapRoleKeys::<PublicKey>::new(public_key, public_key);
let s = serde_yaml::to_string(&double_key).unwrap();
let yml = r#"---
alice: 02c66e7d8966b5c555af5805989da9fbf8db95e15631ce358c3a1710c962679063
bob: 02c66e7d8966b5c555af5805989da9fbf8db95e15631ce358c3a1710c962679063
"#;
assert_eq!(yml, s);
}
}