Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use K2Error in existing k2 code #18

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions crates/api/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub trait Signer {
&self,
agent_info: &AgentInfo,
message: &[u8],
) -> BoxFut<'_, std::io::Result<bytes::Bytes>>;
) -> BoxFut<'_, K2Result<bytes::Bytes>>;
}

/// Defines a type capable of cryptographic verification.
Expand Down Expand Up @@ -197,9 +197,13 @@ impl AgentInfoSigned {
pub async fn sign<S: Signer>(
signer: &S,
agent_info: AgentInfo,
) -> std::io::Result<std::sync::Arc<Self>> {
let encoded = serde_json::to_string(&agent_info)?;
let signature = signer.sign(&agent_info, encoded.as_bytes()).await?;
) -> K2Result<std::sync::Arc<Self>> {
let encoded = serde_json::to_string(&agent_info)
.map_err(|e| K2Error::other_src("encoding agent_info", e))?;
let signature = signer
.sign(&agent_info, encoded.as_bytes())
.await
.map_err(|e| K2Error::other_src("signing agent_info", e))?;
Ok(std::sync::Arc::new(Self {
agent_info,
encoded,
Expand All @@ -211,29 +215,31 @@ impl AgentInfoSigned {
pub fn decode<V: Verifier>(
verifier: &V,
encoded: &[u8],
) -> std::io::Result<Self> {
) -> K2Result<std::sync::Arc<Self>> {
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct Ref {
agent_info: String,
#[serde(with = "crate::serde_bytes_base64")]
signature: bytes::Bytes,
}
let v: Ref = serde_json::from_slice(encoded)?;
let agent_info: AgentInfo = serde_json::from_str(&v.agent_info)?;
let v: Ref = serde_json::from_slice(encoded)
.map_err(|e| K2Error::other_src("decoding agent_info", e))?;
let agent_info: AgentInfo = serde_json::from_str(&v.agent_info)
.map_err(|e| K2Error::other_src("decoding inner agent_info", e))?;
if !verifier.verify(&agent_info, v.agent_info.as_bytes(), &v.signature)
{
return Err(std::io::Error::other("InvalidSignature"));
return Err(K2Error::other("InvalidSignature"));
}
Ok(AgentInfoSigned {
Ok(std::sync::Arc::new(Self {
agent_info,
encoded: v.agent_info,
signature: v.signature,
})
}))
}

/// Get the canonical json encoding of this signed agent info.
pub fn encode(&self) -> std::io::Result<String> {
pub fn encode(&self) -> K2Result<String> {
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct Ref<'a> {
Expand All @@ -245,7 +251,7 @@ impl AgentInfoSigned {
agent_info: &self.encoded,
signature: &self.signature,
})
.map_err(std::convert::Into::into)
.map_err(|e| K2Error::other_src("encoding agent_info", e))
}

/// Access the inner [AgentInfo] data. Note, you can instead just deref.
Expand Down Expand Up @@ -285,7 +291,7 @@ mod test {
&self,
_agent_info: &AgentInfo,
_encoded: &[u8],
) -> BoxFut<'_, std::io::Result<bytes::Bytes>> {
) -> BoxFut<'_, K2Result<bytes::Bytes>> {
Box::pin(async move { Ok(bytes::Bytes::from_static(SIG)) })
}
}
Expand Down