Skip to content

Commit

Permalink
fix(response): axum response
Browse files Browse the repository at this point in the history
Signed-off-by: mcatta <marcocatta@gmail.com>
  • Loading branch information
mcatta committed Oct 2, 2023
1 parent 885e3c2 commit 1276421
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async fn generate_seed() -> impl IntoResponse {
"mnemonic": words
};

create_json_response(response)
create_json_response(response).into_response()
}

/**
Expand All @@ -63,17 +63,15 @@ async fn sign_message(Json(payload): Json<SignMessagePayload>) -> impl IntoRespo
let hashed_signature = general_purpose::STANDARD_NO_PAD.encode(signature);
let public_key = general_purpose::STANDARD_NO_PAD.encode(key_pair.public.to_bytes());

let response = object! {
let response: JsonValue = object! {
"message": payload.message,
"signature": hashed_signature,
"public_key": public_key
};

create_json_response(response);
create_json_response(response).into_response()
},
Err(_) => {
(StatusCode::BAD_REQUEST, "".to_string());
}
Err(_) => bad_request().into_response(),
}
}

Expand Down Expand Up @@ -113,16 +111,12 @@ async fn verify_message(Json(payload): Json<VerifyMessagePayload>) -> impl IntoR
"match": matched_signature
};

create_json_response(response);
create_json_response(response).into_response()
},
Err(_) => {
(StatusCode::BAD_REQUEST, "".to_string());
},
};
},
Err(_) => {
(StatusCode::BAD_REQUEST, "".to_string());
Err(_) => bad_request().into_response(),
}
},
Err(_) => bad_request().into_response(),
}
}

Expand All @@ -132,6 +126,10 @@ fn create_json_response(json: JsonValue) -> impl IntoResponse {
(headers, json::stringify(json))
}

fn bad_request() -> impl IntoResponse {
(StatusCode::BAD_REQUEST, "".to_string())
}

fn decode_hash(str: &String) -> Result<Vec<u8>, Error> {
general_purpose::STANDARD_NO_PAD
.decode(str)
Expand Down Expand Up @@ -185,7 +183,7 @@ mod tests {

// When
let result = rt.block_on(sign_message(Json(payload)));
assert!(result.into_response().status().is_success());
assert!(!result.into_response().status().is_success());
}

#[test]
Expand Down Expand Up @@ -219,7 +217,7 @@ mod tests {
let result = rt.block_on(verify_message(Json(payload)));

// Then
assert!(result.into_response().status().is_success());
assert!(!result.into_response().status().is_success());
}

#[test]
Expand Down

0 comments on commit 1276421

Please sign in to comment.