Skip to content

Commit

Permalink
Add endpoint giving public keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Oct 23, 2024
1 parent 7bd4358 commit 4fcfc30
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
3 changes: 2 additions & 1 deletion crates/threshold-signature-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ use crate::{
attestation::api::attest,
health::api::healthz,
launch::Configuration,
node_info::api::{hashes, version as get_version},
node_info::api::{hashes, info, version as get_version},
r#unsafe::api::{delete, put, remove_keys, unsafe_get},
signing_client::{api::*, ListenerState},
user::api::*,
Expand Down Expand Up @@ -217,6 +217,7 @@ pub fn app(app_state: AppState) -> Router {
.route("/healthz", get(healthz))
.route("/version", get(get_version))
.route("/hashes", get(hashes))
.route("/info", get(info))
.route("/ws", get(ws_handler));

// Unsafe routes are for testing purposes only
Expand Down
26 changes: 24 additions & 2 deletions crates/threshold-signature-server/src/node_info/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,40 @@
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use axum::Json;
use entropy_shared::types::HashingAlgorithm;
use crate::{get_signer_and_x25519_secret, node_info::errors::GetInfoError, AppState};
use axum::{extract::State, Json};
use entropy_shared::{types::HashingAlgorithm, X25519PublicKey};
use serde::{Deserialize, Serialize};
use sp_core::Pair;
use strum::IntoEnumIterator;
use subxt::utils::AccountId32;

/// Returns the version and commit data
#[tracing::instrument]
pub async fn version() -> String {
format!("{}-{}", env!("CARGO_PKG_VERSION"), env!("VERGEN_GIT_DESCRIBE"))
}

/// Lists the supported hashing algorithms
#[tracing::instrument]
pub async fn hashes() -> Json<Vec<HashingAlgorithm>> {
let hashing_algos = HashingAlgorithm::iter().collect::<Vec<_>>();
Json(hashing_algos)
}

/// Public signing and encryption keys associated with a TS server
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct TssPublicKeys {
pub tss_account: AccountId32,
pub x25519_public_key: X25519PublicKey,
}

/// Returns the TS server's public keys and HTTP endpoint
#[tracing::instrument(skip_all)]
pub async fn info(State(app_state): State<AppState>) -> Result<Json<TssPublicKeys>, GetInfoError> {
let (signer, x25519_secret) = get_signer_and_x25519_secret(&app_state.kv_store).await?;
let tss_account = AccountId32(signer.signer().public().0);
let x25519_public_key = x25519_dalek::PublicKey::from(&x25519_secret).as_bytes().clone();

Ok(Json(TssPublicKeys { x25519_public_key, tss_account }))
}
34 changes: 34 additions & 0 deletions crates/threshold-signature-server/src/node_info/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2023 Entropy Cryptography Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use thiserror::Error;

/// Errors for protocol execution
#[derive(Debug, Error)]
pub enum GetInfoError {
#[error("Could not get public keys: {0}")]
User(#[from] crate::user::errors::UserErr),
}

impl IntoResponse for GetInfoError {
fn into_response(self) -> Response {
tracing::error!("{:?}", format!("{self}"));
let body = format!("{self}").into_bytes();
(StatusCode::INTERNAL_SERVER_ERROR, body).into_response()
}
}
1 change: 1 addition & 0 deletions crates/threshold-signature-server/src/node_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

//! Provides information about this instance of `entropy-tss`
pub mod api;
mod errors;

#[cfg(test)]
mod tests;
25 changes: 24 additions & 1 deletion crates/threshold-signature-server/src/node_info/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::helpers::tests::{initialize_test_logger, setup_client};
use crate::{
helpers::tests::{initialize_test_logger, setup_client},
node_info::api::TssPublicKeys,
};
use entropy_kvdb::clean_tests;
use entropy_shared::types::HashingAlgorithm;
use entropy_testing_utils::constants::{TSS_ACCOUNTS, X25519_PUBLIC_KEYS};
use serial_test::serial;

#[tokio::test]
Expand Down Expand Up @@ -55,3 +59,22 @@ async fn hashes_test() {
);
clean_tests();
}

#[tokio::test]
#[serial]
async fn info_test() {
clean_tests();
initialize_test_logger().await;
setup_client().await;
let client = reqwest::Client::new();
let response = client.get("http://127.0.0.1:3001/info").send().await.unwrap();
let public_keys: TssPublicKeys = response.json().await.unwrap();
assert_eq!(
public_keys,
TssPublicKeys {
tss_account: TSS_ACCOUNTS[0].clone(),
x25519_public_key: X25519_PUBLIC_KEYS[0],
}
);
clean_tests();
}

0 comments on commit 4fcfc30

Please sign in to comment.