From bffc15ba610de742e139c41f68d6d134f7d6467c Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Sun, 3 Nov 2024 10:34:51 -0800 Subject: [PATCH] fmt --- src/spendpolicy.rs | 105 ++++++++++++++++++++------------------- src/transactions.rs | 27 +++++----- src/unlock_conditions.rs | 33 ++++++------ 3 files changed, 85 insertions(+), 80 deletions(-) diff --git a/src/spendpolicy.rs b/src/spendpolicy.rs index 5916687..959c7e7 100644 --- a/src/spendpolicy.rs +++ b/src/spendpolicy.rs @@ -1,14 +1,14 @@ -use chrono::{DateTime, Utc}; -use serde_json::json; use crate::encoding::to_writer; use crate::signing::{PublicKey, Signature}; #[allow(deprecated)] use crate::unlock_conditions::UnlockConditions; use crate::{Address, Hash256}; use blake2b_simd::Params; +use chrono::{DateTime, Utc}; use core::fmt; -use serde::ser::{SerializeTuple, SerializeStruct}; +use serde::ser::{SerializeStruct, SerializeTuple}; use serde::Serialize; +use serde_json::json; use thiserror::Error; #[derive(Debug, PartialEq, Error)] @@ -69,18 +69,18 @@ impl SpendPolicy { } } - fn type_str(&self) -> &str { - match self { - SpendPolicy::Above(_) => "above", - SpendPolicy::After(_) => "after", - SpendPolicy::PublicKey(_) => "pk", - SpendPolicy::Hash(_) => "h", - SpendPolicy::Threshold(_, _) => "thresh", - SpendPolicy::Opaque(_) => "opaque", - #[allow(deprecated)] - SpendPolicy::UnlockConditions(_) => "uc", - } - } + fn type_str(&self) -> &str { + match self { + SpendPolicy::Above(_) => "above", + SpendPolicy::After(_) => "after", + SpendPolicy::PublicKey(_) => "pk", + SpendPolicy::Hash(_) => "h", + SpendPolicy::Threshold(_, _) => "thresh", + SpendPolicy::Opaque(_) => "opaque", + #[allow(deprecated)] + SpendPolicy::UnlockConditions(_) => "uc", + } + } /// Create a policy that is only valid after a certain block height pub fn above(height: u64) -> Self { @@ -151,11 +151,11 @@ impl SpendPolicy { /// threshold policies. The version byte is only written for the top-level /// policy. fn serialize_policy(&self, s: &mut S) -> Result<(), S::Error> { - s.serialize_element(&self.type_prefix())?; // type prefix + s.serialize_element(&self.type_prefix())?; // type prefix match self { SpendPolicy::Above(height) => s.serialize_element(height), SpendPolicy::After(time) => { - let ts = time.timestamp() as u64; + let ts = time.timestamp() as u64; s.serialize_element(&ts) } SpendPolicy::PublicKey(pk) => { @@ -181,21 +181,21 @@ impl SpendPolicy { impl Serialize for SpendPolicy { fn serialize(&self, serializer: S) -> Result { - if !serializer.is_human_readable() { - let mut s = serializer.serialize_tuple(0)?; - s.serialize_element(&1u8)?; // version - self.serialize_policy(&mut s)?; - return s.end(); - } - - let mut state = serializer.serialize_struct("SpendPolicy", 2)?; - state.serialize_field("type", self.type_str())?; + if !serializer.is_human_readable() { + let mut s = serializer.serialize_tuple(0)?; + s.serialize_element(&1u8)?; // version + self.serialize_policy(&mut s)?; + return s.end(); + } + + let mut state = serializer.serialize_struct("SpendPolicy", 2)?; + state.serialize_field("type", self.type_str())?; match self { SpendPolicy::Above(height) => { state.serialize_field("policy", height)?; } SpendPolicy::After(time) => { - let ts = time.timestamp() as u64; + let ts = time.timestamp() as u64; state.serialize_field("policy", &ts)?; } SpendPolicy::PublicKey(pk) => { @@ -205,21 +205,24 @@ impl Serialize for SpendPolicy { state.serialize_field("policy", &hash)?; } SpendPolicy::Threshold(n, policies) => { - state.serialize_field("policy", &json!({ - "n": n, - "of": policies, - }))?; - } - SpendPolicy::Opaque(addr) => { - state.serialize_field("policy", addr)?; - } - #[allow(deprecated)] - SpendPolicy::UnlockConditions(uc) => { - state.serialize_field("policy", uc)?; - } - } - state.end() - } + state.serialize_field( + "policy", + &json!({ + "n": n, + "of": policies, + }), + )?; + } + SpendPolicy::Opaque(addr) => { + state.serialize_field("policy", addr)?; + } + #[allow(deprecated)] + SpendPolicy::UnlockConditions(uc) => { + state.serialize_field("policy", uc)?; + } + } + state.end() + } } impl fmt::Display for SpendPolicy { @@ -277,8 +280,8 @@ impl SatisfiedPolicy { #[cfg(test)] mod tests { - use chrono::TimeZone; use super::*; + use chrono::TimeZone; #[test] fn test_address() { @@ -395,9 +398,9 @@ mod tests { } } - #[test] - fn test_policy_json() { - let test_cases = vec![ + #[test] + fn test_policy_json() { + let test_cases = vec![ (SpendPolicy::above(100), "{\"type\":\"above\",\"policy\":100}"), ( SpendPolicy::after(Utc.timestamp_opt(100, 0).unwrap()), "{\"type\":\"after\",\"policy\":100}"), ( SpendPolicy::public_key(PublicKey::new([1; 32])), "{\"type\":\"pk\",\"policy\":\"ed25519:0101010101010101010101010101010101010101010101010101010101010101\"}"), @@ -444,9 +447,9 @@ mod tests { ) ]; - for (policy, expected) in test_cases { - let json = serde_json::to_string(&policy).unwrap(); - assert_eq!(json, expected); - } - } + for (policy, expected) in test_cases { + let json = serde_json::to_string(&policy).unwrap(); + assert_eq!(json, expected); + } + } } diff --git a/src/transactions.rs b/src/transactions.rs index 346b014..4d57a04 100644 --- a/src/transactions.rs +++ b/src/transactions.rs @@ -538,10 +538,11 @@ mod tests { unlock_conditions: UnlockConditions::new( 123, vec![PublicKey::new([ - 0x9a, 0xac, 0x1f, 0xfb, 0x1c, 0xfd, 0x10, 0x79, 0xa8, 0xc6, 0xc8, 0x7b, - 0x47, 0xda, 0x1d, 0x56, 0x7e, 0x35, 0xb9, 0x72, 0x34, 0x99, 0x3c, 0x28, - 0x8c, 0x1a, 0xd0, 0xdb, 0x1d, 0x1c, 0xe1, 0xb6, - ]).into()], + 0x9a, 0xac, 0x1f, 0xfb, 0x1c, 0xfd, 0x10, 0x79, 0xa8, 0xc6, 0xc8, 0x7b, 0x47, + 0xda, 0x1d, 0x56, 0x7e, 0x35, 0xb9, 0x72, 0x34, 0x99, 0x3c, 0x28, 0x8c, 0x1a, + 0xd0, 0xdb, 0x1d, 0x1c, 0xe1, 0xb6, + ]) + .into()], 1, ), }; @@ -581,10 +582,11 @@ mod tests { unlock_conditions: UnlockConditions::new( 123, vec![PublicKey::new([ - 0x9a, 0xac, 0x1f, 0xfb, 0x1c, 0xfd, 0x10, 0x79, 0xa8, 0xc6, 0xc8, 0x7b, - 0x47, 0xda, 0x1d, 0x56, 0x7e, 0x35, 0xb9, 0x72, 0x34, 0x99, 0x3c, 0x28, - 0x8c, 0x1a, 0xd0, 0xdb, 0x1d, 0x1c, 0xe1, 0xb6, - ]).into()], + 0x9a, 0xac, 0x1f, 0xfb, 0x1c, 0xfd, 0x10, 0x79, 0xa8, 0xc6, 0xc8, 0x7b, 0x47, + 0xda, 0x1d, 0x56, 0x7e, 0x35, 0xb9, 0x72, 0x34, 0x99, 0x3c, 0x28, 0x8c, 0x1a, + 0xd0, 0xdb, 0x1d, 0x1c, 0xe1, 0xb6, + ]) + .into()], 1, ), claim_address: Address::new( @@ -828,10 +830,11 @@ mod tests { unlock_conditions: UnlockConditions::new( 123, vec![PublicKey::new([ - 0x9a, 0xac, 0x1f, 0xfb, 0x1c, 0xfd, 0x10, 0x79, 0xa8, 0xc6, 0xc8, 0x7b, - 0x47, 0xda, 0x1d, 0x56, 0x7e, 0x35, 0xb9, 0x72, 0x34, 0x99, 0x3c, 0x28, - 0x8c, 0x1a, 0xd0, 0xdb, 0x1d, 0x1c, 0xe1, 0xb6, - ]).into()], + 0x9a, 0xac, 0x1f, 0xfb, 0x1c, 0xfd, 0x10, 0x79, 0xa8, 0xc6, 0xc8, 0x7b, 0x47, + 0xda, 0x1d, 0x56, 0x7e, 0x35, 0xb9, 0x72, 0x34, 0x99, 0x3c, 0x28, 0x8c, 0x1a, + 0xd0, 0xdb, 0x1d, 0x1c, 0xe1, 0xb6, + ]) + .into()], 1, ), unlock_hash: Hash256::from([ diff --git a/src/unlock_conditions.rs b/src/unlock_conditions.rs index 2210bd7..f81bc76 100644 --- a/src/unlock_conditions.rs +++ b/src/unlock_conditions.rs @@ -40,10 +40,7 @@ impl<'de> Deserialize<'de> for UnlockKey { UnlockKey::parse_string(&s).map_err(|e| Error::custom(format!("{:?}", e))) } else { let (algorithm, key) = <(Algorithm, Vec)>::deserialize(deserializer)?; - Ok(Self { - algorithm, - key, - }) + Ok(Self { algorithm, key }) } } } @@ -134,10 +131,10 @@ impl UnlockKey { impl From for UnlockKey { fn from(val: PublicKey) -> Self { - UnlockKey{ - algorithm: Algorithm::ed25519(), - key: val.as_ref().to_vec(), - } + UnlockKey { + algorithm: Algorithm::ed25519(), + key: val.as_ref().to_vec(), + } } } @@ -237,11 +234,12 @@ mod tests { #[test] fn test_serialize_unlock_key() { - let unlock_key : UnlockKey = PublicKey::new([ - 0x9a, 0xac, 0x1f, 0xfb, 0x1c, 0xfd, 0x10, 0x79, 0xa8, 0xc6, 0xc8, 0x7b, 0x47, 0xda, - 0x1d, 0x56, 0x7e, 0x35, 0xb9, 0x72, 0x34, 0x99, 0x3c, 0x28, 0x8c, 0x1a, 0xd0, 0xdb, - 0x1d, 0x1c, 0xe1, 0xb6, - ]).into(); + let unlock_key: UnlockKey = PublicKey::new([ + 0x9a, 0xac, 0x1f, 0xfb, 0x1c, 0xfd, 0x10, 0x79, 0xa8, 0xc6, 0xc8, 0x7b, 0x47, 0xda, + 0x1d, 0x56, 0x7e, 0x35, 0xb9, 0x72, 0x34, 0x99, 0x3c, 0x28, 0x8c, 0x1a, 0xd0, 0xdb, + 0x1d, 0x1c, 0xe1, 0xb6, + ]) + .into(); // binary let unlock_key_serialized = to_bytes(&unlock_key).unwrap(); @@ -274,10 +272,11 @@ mod tests { let unlock_conditions = UnlockConditions::new( 123, vec![PublicKey::new([ - 0x9a, 0xac, 0x1f, 0xfb, 0x1c, 0xfd, 0x10, 0x79, 0xa8, 0xc6, 0xc8, 0x7b, 0x47, - 0xda, 0x1d, 0x56, 0x7e, 0x35, 0xb9, 0x72, 0x34, 0x99, 0x3c, 0x28, 0x8c, 0x1a, - 0xd0, 0xdb, 0x1d, 0x1c, 0xe1, 0xb6, - ]).into()], + 0x9a, 0xac, 0x1f, 0xfb, 0x1c, 0xfd, 0x10, 0x79, 0xa8, 0xc6, 0xc8, 0x7b, 0x47, 0xda, + 0x1d, 0x56, 0x7e, 0x35, 0xb9, 0x72, 0x34, 0x99, 0x3c, 0x28, 0x8c, 0x1a, 0xd0, 0xdb, + 0x1d, 0x1c, 0xe1, 0xb6, + ]) + .into()], 1, );