Skip to content

Commit

Permalink
feat: v1 keysets
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Nov 30, 2023
1 parent 583bff6 commit 3050e80
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
34 changes: 33 additions & 1 deletion moksha-core/src/keyset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use itertools::Itertools;
use rand::RngCore;
use secp256k1::{PublicKey, Secp256k1, SecretKey};

use crate::error::MokshaCoreError;
use crate::{error::MokshaCoreError, primitives::CurrencyUnit};

const MAX_ORDER: u64 = 64;

Expand Down Expand Up @@ -87,6 +87,38 @@ impl Keysets {
}
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct V1Keysets {
pub keysets: Vec<V1Keyset>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct V1Keyset {
pub id: String,
pub unit: CurrencyUnit,
pub active: bool,
}

impl V1Keysets {
pub fn new(id: String, unit: CurrencyUnit, active: bool) -> Self {
Self {
keysets: vec![V1Keyset { id, unit, active }],
}
}

pub fn current_keyset(
&self,
mint_keys: &HashMap<u64, PublicKey>,
) -> Result<String, MokshaCoreError> {
let computed_id = derive_keyset_id(mint_keys);
if self.keysets.iter().any(|x| x.id.eq(&computed_id)) {
Ok(computed_id)
} else {
Err(MokshaCoreError::InvalidKeysetid)
}
}
}

/// Derives a set of secret keys from a master key using a given derivation path.
///
/// # Arguments
Expand Down
2 changes: 1 addition & 1 deletion moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub struct KeyResponse {
pub keys: HashMap<u64, PublicKey>,
}

#[derive(serde::Deserialize, Serialize, Debug, PartialEq, Eq)]
#[derive(serde::Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
pub enum CurrencyUnit {
Sat,
}
Expand Down
12 changes: 10 additions & 2 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use axum::response::IntoResponse;
use axum::routing::{get_service, post};
use axum::{middleware, Router};
use axum::{routing::get, Json};
use moksha_core::keyset::Keysets;
use moksha_core::keyset::{Keysets, V1Keysets};

use crate::mint::Mint;
use crate::model::{GetMintQuery, PostMintQuery};
Expand Down Expand Up @@ -81,7 +81,7 @@ fn app(mint: Mint, serve_wallet_path: Option<PathBuf>, prefix: Option<String>) -

let routes = Router::new()
.route("/v1/keys", get(get_keys))
.route("/v1/keysets", get(get_legacy_keysets))
.route("/v1/keysets", get(get_keysets))
.route("/v1/mint", get(get_mint).post(post_legacy_mint))
.route("/v1/checkfees", post(post_legacy_check_fees))
.route("/v1/melt", post(post_legacy_melt))
Expand Down Expand Up @@ -255,6 +255,14 @@ async fn get_keys(State(mint): State<Mint>) -> Result<Json<KeysResponse>, Moksha
}))
}

async fn get_keysets(State(mint): State<Mint>) -> Result<Json<V1Keysets>, MokshaMintError> {
Ok(Json(V1Keysets::new(
mint.keyset.keyset_id,
CurrencyUnit::Sat,
true,
)))
}

#[cfg(test)]
mod tests {
use std::{collections::HashMap, sync::Arc};
Expand Down

0 comments on commit 3050e80

Please sign in to comment.