Skip to content

Commit

Permalink
transactions: reduce test duplication, fix encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Nov 6, 2024
1 parent 0a9687e commit 421f774
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 270 deletions.
2 changes: 1 addition & 1 deletion sia/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub type BlockID = Hash256;

pub struct ChainIndex {
pub height: u64,
pub id: [u8; 32],
pub id: BlockID,
}

impl fmt::Display for ChainIndex {
Expand Down
11 changes: 9 additions & 2 deletions sia/src/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl V1SiaDecodable for Currency {

impl SiaEncodable for Currency {
fn encode<W: Write>(&self, w: &mut W) -> encoding::Result<()> {
w.write_all(&self.0.to_be_bytes())?;
w.write_all(&self.0.to_le_bytes())?;
Ok(())
}
}
Expand All @@ -66,7 +66,7 @@ impl SiaDecodable for Currency {
fn decode<R: std::io::Read>(r: &mut R) -> encoding::Result<Self> {
let mut buf = [0u8; 16];
r.read_exact(&mut buf)?;
Ok(Currency(u128::from_be_bytes(buf)))
Ok(Currency(u128::from_le_bytes(buf)))
}
}

Expand All @@ -85,6 +85,13 @@ impl DerefMut for Currency {
}
}

impl TryInto<u64> for Currency {
type Error = core::num::TryFromIntError;
fn try_into(self) -> Result<u64, Self::Error> {
self.0.try_into()
}
}

// Implement AsRef as well to be able to implicitly obtain a &u128 from a Currency as well.
impl<T> AsRef<T> for Currency
where
Expand Down
30 changes: 7 additions & 23 deletions sia/src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::time::SystemTime;

use crate::encoding::{SiaDecodable, SiaDecode, SiaEncodable, SiaEncode};
use crate::{ChainIndex, Hash256, HexParseError};
use base64::prelude::*;
use ed25519_dalek::{Signature as ED25519Signature, Signer, SigningKey, Verifier, VerifyingKey};
use serde::de::Error;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -117,11 +116,7 @@ pub struct Signature([u8; 64]);

impl Serialize for Signature {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
String::serialize(&BASE64_STANDARD.encode(self.0), serializer)
} else {
<[u8]>::serialize(&self.0, serializer) // prefixed with length
}
String::serialize(&hex::encode(self.0), serializer)
}
}

Expand All @@ -130,22 +125,12 @@ impl<'de> Deserialize<'de> for Signature {
where
D: serde::Deserializer<'de>,
{
if deserializer.is_human_readable() {
let s = String::deserialize(deserializer)?;
let sig = BASE64_STANDARD
.decode(s)
.map_err(|e| D::Error::custom(format!("{:?}", e)))?;
if sig.len() != 64 {
return Err(D::Error::custom("Invalid signature length"));
}
Ok(Signature(sig.try_into().unwrap()))
} else {
let data = <Vec<u8>>::deserialize(deserializer)?;
if data.len() != 64 {
return Err(D::Error::custom("Invalid signature length"));
}
Ok(Signature(data.try_into().unwrap()))
let buf = hex::decode(String::deserialize(deserializer)?)
.map_err(|e| D::Error::custom(format!("{:?}", e)))?;
if buf.len() != 64 {
return Err(D::Error::custom("Invalid signature length"));
}
Ok(Signature(buf.try_into().unwrap()))
}
}

Expand Down Expand Up @@ -324,9 +309,8 @@ mod tests {
claim_address: Address::from([0u8; 32]),
}],
siafund_outputs: vec![SiafundOutput {
value: Currency::new(0),
value: 0,
address: Address::from([0u8; 32]),
claim_start: Currency::new(0),
}],
miner_fees: vec![Currency::new(0)],
arbitrary_data: vec![vec![1, 2, 3]],
Expand Down
Loading

0 comments on commit 421f774

Please sign in to comment.