Skip to content

Commit

Permalink
feat: use hasher digest
Browse files Browse the repository at this point in the history
  • Loading branch information
YoussefAWasfy committed Sep 30, 2024
1 parent 59b5fbc commit 4f82cdc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions affinidi-did-resolver-cache-sdk/src/networking/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
use std::time::Duration;

use crate::{config::ClientConfig, errors::DIDCacheError, WSRequest};
use blake2::{Blake2s256, Digest};
use futures_util::{SinkExt, StreamExt};
use ssi::{crypto::hashes::sha256, dids::Document};
use ssi::dids::Document;
use tokio::{
net::TcpStream,
select,
Expand Down Expand Up @@ -101,7 +102,10 @@ impl NetworkTask {
if let Some(cmd) = value {
match cmd {
WSCommands::Send(channel, uid, request) => {
if network_task.cache.insert(String::from_utf8(sha256::sha256(request.did.as_bytes()).to_vec()).unwrap(), &uid, channel) {
let mut hasher = Blake2s256::new();
hasher.update(request.did.clone());
let did_hash = format!("{:x}", hasher.finalize());
if network_task.cache.insert(did_hash, &uid, channel) {
let _ = network_task.ws_send(&mut websocket, &request).await;
}
}
Expand Down
6 changes: 4 additions & 2 deletions affinidi-did-resolver-cache-server/src/handlers/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use axum::{
},
response::IntoResponse,
};
use ssi::crypto::hashes::sha256;
use blake2::{Blake2s256, Digest};
use tokio::select;
use tracing::{debug, info, span, warn, Instrument};

Expand Down Expand Up @@ -83,7 +83,9 @@ async fn handle_socket(mut socket: WebSocket, state: SharedData) {
}
Err(e) => {
// Couldn't resolve the DID, send an error back
let did_hash = String::from_utf8(sha256::sha256(request.did.as_bytes()).to_vec()).unwrap();
let mut hasher = Blake2s256::new();
hasher.update(request.did.clone());
let did_hash = format!("{:x}", hasher.finalize());
warn!("Couldn't resolve DID: ({}) Reason: {}", &request.did, e);
state.stats().await.increment_resolver_error();
if let Err(e) = socket.send(Message::Text(serde_json::to_string(&WSResponseType::Error(WSResponseError {did: request.did, hash: did_hash, error: e.to_string()})).unwrap())).await {
Expand Down

0 comments on commit 4f82cdc

Please sign in to comment.