Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Nov 3, 2024
1 parent f53ba13 commit bffc15b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 80 deletions.
105 changes: 54 additions & 51 deletions src/spendpolicy.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -151,11 +151,11 @@ impl SpendPolicy {
/// threshold policies. The version byte is only written for the top-level
/// policy.
fn serialize_policy<S: serde::ser::SerializeTuple>(&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) => {
Expand All @@ -181,21 +181,21 @@ impl SpendPolicy {

impl Serialize for SpendPolicy {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
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) => {
Expand All @@ -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 {
Expand Down Expand Up @@ -277,8 +280,8 @@ impl SatisfiedPolicy {

#[cfg(test)]
mod tests {
use chrono::TimeZone;
use super::*;
use chrono::TimeZone;

#[test]
fn test_address() {
Expand Down Expand Up @@ -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\"}"),
Expand Down Expand Up @@ -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);
}
}
}
27 changes: 15 additions & 12 deletions src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
};
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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([
Expand Down
33 changes: 16 additions & 17 deletions src/unlock_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>)>::deserialize(deserializer)?;
Ok(Self {
algorithm,
key,
})
Ok(Self { algorithm, key })
}
}
}
Expand Down Expand Up @@ -134,10 +131,10 @@ impl UnlockKey {

impl From<PublicKey> 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(),
}
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
);

Expand Down

0 comments on commit bffc15b

Please sign in to comment.