Skip to content

Commit

Permalink
Merge pull request #15 from iotaledger/chore/publify-fields
Browse files Browse the repository at this point in the history
chore: Make more fields public and refactor some signature code
  • Loading branch information
DaughterOfMars authored Oct 30, 2024
2 parents e8b8312 + d815f43 commit dd7b331
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 318 deletions.
2 changes: 1 addition & 1 deletion crates/iota-rust-sdk/src/types/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub struct SignedCheckpointSummary {
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
pub struct CheckpointContents(
#[cfg_attr(test, any(proptest::collection::size_range(0..=2).lift()))]
Vec<CheckpointTransactionInfo>,
pub Vec<CheckpointTransactionInfo>,
);

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
21 changes: 21 additions & 0 deletions crates/iota-rust-sdk/src/types/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ pub use zklogin::{
ZkLoginInputs, ZkLoginProof, ZkLoginPublicIdentifier,
};

#[cfg(feature = "serde")]
#[derive(Debug)]
pub struct SignatureFromBytesError(String);

#[cfg(feature = "serde")]
impl SignatureFromBytesError {
fn new(msg: impl core::fmt::Display) -> Self {
Self(msg.to_string())
}
}

#[cfg(feature = "serde")]
impl core::fmt::Display for SignatureFromBytesError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "error deserializing bytes: {}", self.0)
}
}

#[cfg(feature = "serde")]
impl std::error::Error for SignatureFromBytesError {}

// Implement various base64 fixed-size array helpers
//

Expand Down
28 changes: 15 additions & 13 deletions crates/iota-rust-sdk/src/types/crypto/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ mod serialization {
use serde_with::{Bytes, DeserializeAs};

use super::*;
use crate::types::{Ed25519PublicKey, Secp256k1PublicKey, Secp256r1PublicKey, SignatureScheme};
use crate::types::{
Ed25519PublicKey, Secp256k1PublicKey, Secp256r1PublicKey, SignatureScheme,
crypto::SignatureFromBytesError,
};

#[derive(serde_derive::Deserialize)]
pub struct Multisig {
Expand Down Expand Up @@ -203,24 +206,23 @@ mod serialization {
})
} else {
let bytes: Cow<'de, [u8]> = Bytes::deserialize_as(deserializer)?;
Self::from_serialized_bytes(bytes)
Self::from_serialized_bytes(bytes).map_err(serde::de::Error::custom)
}
}
}

impl MultisigAggregatedSignature {
pub(crate) fn from_serialized_bytes<T: AsRef<[u8]>, E: serde::de::Error>(
bytes: T,
) -> Result<Self, E> {
pub fn from_serialized_bytes(
bytes: impl AsRef<[u8]>,
) -> Result<Self, SignatureFromBytesError> {
let bytes = bytes.as_ref();
let flag = SignatureScheme::from_byte(
*bytes
.first()
.ok_or_else(|| serde::de::Error::custom("missing signature scheme flag"))?,
)
.map_err(serde::de::Error::custom)?;
let flag =
SignatureScheme::from_byte(*bytes.first().ok_or_else(|| {
SignatureFromBytesError::new("missing signature scheme flag")
})?)
.map_err(SignatureFromBytesError::new)?;
if flag != SignatureScheme::Multisig {
return Err(serde::de::Error::custom("invalid multisig flag"));
return Err(SignatureFromBytesError::new("invalid multisig flag"));
}
let bcs_bytes = &bytes[1..];

Expand All @@ -231,7 +233,7 @@ mod serialization {
committee: multisig.committee,
})
} else {
Err(serde::de::Error::custom("invalid multisig"))
Err(SignatureFromBytesError::new("invalid multisig"))
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions crates/iota-rust-sdk/src/types/crypto/passkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mod serialization {
use serde_with::{Bytes, DeserializeAs};

use super::*;
use crate::types::{SignatureScheme, SimpleSignature};
use crate::types::{SignatureScheme, SimpleSignature, crypto::SignatureFromBytesError};

#[derive(serde::Serialize)]
struct AuthenticatorRef<'a> {
Expand Down Expand Up @@ -121,23 +121,24 @@ mod serialization {
let bytes: Cow<'de, [u8]> = Bytes::deserialize_as(deserializer)?;
Self::from_serialized_bytes(bytes)
}
.map_err(serde::de::Error::custom)
}
}

impl PasskeyAuthenticator {
fn try_from_raw<E: serde::de::Error>(
fn try_from_raw(
Authenticator {
authenticator_data,
client_data_json,
signature,
}: Authenticator,
) -> Result<Self, E> {
) -> Result<Self, SignatureFromBytesError> {
let SimpleSignature::Secp256r1 {
signature,
public_key,
} = signature
else {
return Err(serde::de::Error::custom(
return Err(SignatureFromBytesError::new(
"expected passkey with secp256r1 signature",
));
};
Expand All @@ -146,7 +147,7 @@ mod serialization {
ty: _,
challenge,
origin: _,
} = serde_json::from_str(&client_data_json).map_err(serde::de::Error::custom)?;
} = serde_json::from_str(&client_data_json).map_err(SignatureFromBytesError::new)?;

// challenge is 3 byte intent | 32 byte hash
let mut challenge_buf = [0; 3 + Digest::LENGTH];
Expand All @@ -158,7 +159,7 @@ mod serialization {
&mut challenge_buf,
)
.map_err(|e| {
serde::de::Error::custom(format!(
SignatureFromBytesError::new(format!(
"unable to decode base64urlunpadded into 3-byte intent and 32-byte digest: {e}"
))
})?;
Expand All @@ -176,22 +177,21 @@ mod serialization {
})
}

pub(crate) fn from_serialized_bytes<T: AsRef<[u8]>, E: serde::de::Error>(
bytes: T,
) -> Result<Self, E> {
pub fn from_serialized_bytes(
bytes: impl AsRef<[u8]>,
) -> Result<Self, SignatureFromBytesError> {
let bytes = bytes.as_ref();
let flag = SignatureScheme::from_byte(
*bytes
.first()
.ok_or_else(|| serde::de::Error::custom("missing signature scheme flag"))?,
)
.map_err(serde::de::Error::custom)?;
let flag =
SignatureScheme::from_byte(*bytes.first().ok_or_else(|| {
SignatureFromBytesError::new("missing signature scheme flag")
})?)
.map_err(SignatureFromBytesError::new)?;
if flag != SignatureScheme::Passkey {
return Err(serde::de::Error::custom("invalid passkey flag"));
return Err(SignatureFromBytesError::new("invalid passkey flag"));
}
let bcs_bytes = &bytes[1..];

let authenticator = bcs::from_bytes(bcs_bytes).map_err(serde::de::Error::custom)?;
let authenticator = bcs::from_bytes(bcs_bytes).map_err(SignatureFromBytesError::new)?;

Self::try_from_raw(authenticator)
}
Expand Down
Loading

0 comments on commit dd7b331

Please sign in to comment.