From 14dc1e5acc9ad6c4b1e7a5765e13e22d57b1da1a Mon Sep 17 00:00:00 2001 From: ngutech21 Date: Tue, 9 Apr 2024 15:12:41 +0200 Subject: [PATCH] chore: remove legacy keysets --- moksha-core/src/keyset.rs | 119 ++-------------------- moksha-mint/src/mint.rs | 17 +--- moksha-mint/src/routes/default.rs | 8 +- moksha-mint/src/server.rs | 16 +-- moksha-wallet/src/client/crossplatform.rs | 4 +- moksha-wallet/src/client/mod.rs | 4 +- moksha-wallet/src/wallet.rs | 4 +- 7 files changed, 30 insertions(+), 142 deletions(-) diff --git a/moksha-core/src/keyset.rs b/moksha-core/src/keyset.rs index 46633849..727984f1 100644 --- a/moksha-core/src/keyset.rs +++ b/moksha-core/src/keyset.rs @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; use utoipa::ToSchema; -use base64::{engine::general_purpose, Engine as _}; + use bitcoin_hashes::{sha256, Hash}; use itertools::Itertools; @@ -42,17 +42,6 @@ pub struct MintKeyset { } impl MintKeyset { - pub fn legacy_new(seed: &str, derivation_path: &str) -> Self { - let priv_keys = derive_keys(seed, derivation_path); - let pub_keys = derive_pubkeys(&priv_keys); - Self { - private_keys: priv_keys, - keyset_id: legacy_derive_keyset_id(&pub_keys), - public_keys: pub_keys, - mint_pubkey: derive_pubkey(seed).expect("invalid seed"), - } - } - pub fn new(seed: &str, derivation_path: &str) -> Self { let priv_keys = derive_keys(seed, derivation_path); let pub_keys = derive_pubkeys(&priv_keys); @@ -65,47 +54,24 @@ impl MintKeyset { } } -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)] -pub struct Keysets { - pub keysets: Vec, -} - -impl Keysets { - pub fn new(keysets: Vec) -> Self { - Self { keysets } - } - - pub fn current_keyset( - &self, - mint_keys: &HashMap, - ) -> Result { - let computed_id = legacy_derive_keyset_id(mint_keys); - if self.keysets.contains(&computed_id) { - Ok(computed_id) - } else { - Err(MokshaCoreError::InvalidKeysetid) - } - } -} - // FIXME rename to keysets #[derive(Clone, Debug, Serialize, Deserialize, Default, ToSchema, PartialEq, Eq)] -pub struct V1Keysets { - pub keysets: Vec, +pub struct Keysets { + pub keysets: Vec, } // FIXME rename to keyset #[derive(Clone, Debug, Serialize, Deserialize, ToSchema, PartialEq, Eq)] -pub struct V1Keyset { +pub struct Keyset { pub id: String, // FIXME use KeysetId pub unit: CurrencyUnit, pub active: bool, } -impl V1Keysets { +impl Keysets { pub fn new(id: String, unit: CurrencyUnit, active: bool) -> Self { Self { - keysets: vec![V1Keyset { id, unit, active }], + keysets: vec![Keyset { id, unit, active }], } } @@ -212,25 +178,6 @@ pub fn derive_pubkeys(keys: &HashMap) -> HashMap .collect() } -/// Derives a keyset ID from a HashMap of public keys. -/// -/// # Arguments -/// -/// * `keys` - A HashMap of public keys. -/// -/// # Returns -/// -/// A string representing the derived keyset ID. -pub fn legacy_derive_keyset_id(keys: &HashMap) -> String { - let pubkeys_concat = keys - .iter() - .sorted_by(|(amt_a, _), (amt_b, _)| amt_a.cmp(amt_b)) - .map(|(_, pubkey)| pubkey) - .join(""); - let hashed_pubkeys = sha256::Hash::hash(pubkeys_concat.as_bytes()).to_byte_array(); - general_purpose::STANDARD.encode(hashed_pubkeys)[..12].to_string() -} - fn derive_keyset_id(keys: &HashMap) -> String { let pubkeys = keys .iter() @@ -263,11 +210,6 @@ mod tests { use secp256k1::PublicKey; use std::collections::HashMap; - fn public_key_from_hex(hex: &str) -> secp256k1::PublicKey { - use hex::FromHex; - let input_vec: Vec = Vec::from_hex(hex).expect("Invalid Hex String"); - secp256k1::PublicKey::from_slice(&input_vec).expect("Invalid Public Key") - } #[test] fn test_keyset_id() -> anyhow::Result<()> { @@ -292,18 +234,7 @@ mod tests { Ok(()) } - #[test] - fn test_derive_keys_master() -> anyhow::Result<()> { - let keys = super::derive_keys("master", "0/0/0/0"); - assert_eq!(keys.len(), 64); - - let pub_keys = super::derive_pubkeys(&keys); - let id = super::legacy_derive_keyset_id(&pub_keys); - assert_eq!("JHV8eUnoAln/", id); - assert_eq!(id.len(), 12); - Ok(()) - } - + #[test] fn test_derive_keys_master_v1() -> anyhow::Result<()> { let keys = super::derive_keys("supersecretprivatekey", ""); @@ -316,42 +247,6 @@ mod tests { Ok(()) } - // uses values from cashu test_mint.py - #[test] - fn test_derive_keys_cashu_py() -> anyhow::Result<()> { - let keys = super::derive_keys("TEST_PRIVATE_KEY", "0/0/0/0"); - assert_eq!(keys.len(), 64); - - let pub_keys = super::derive_pubkeys(&keys); - let id = super::legacy_derive_keyset_id(&pub_keys); - assert_eq!("1cCNIAZ2X/w1", id); - assert_eq!(id.len(), 12); - Ok(()) - } - - #[test] - fn test_legacy_derive_keyset_id() -> anyhow::Result<()> { - let mut pubs = HashMap::new(); - pubs.insert( - 1, - public_key_from_hex( - "02a9acc1e48c25eeeb9289b5031cc57da9fe72f3fe2861d264bdc074209b107ba2", - ), - ); - - pubs.insert( - 2, - public_key_from_hex( - "020000000000000000000000000000000000000000000000000000000000000001", - ), - ); - - let keyset_id = super::legacy_derive_keyset_id(&pubs); - - assert_eq!(keyset_id.len(), 12); - assert_eq!(keyset_id, "cNbjM0O6V/Kl"); - Ok(()) - } #[test] fn test_derive_keyset_id() -> anyhow::Result<()> { diff --git a/moksha-mint/src/mint.rs b/moksha-mint/src/mint.rs index 8a6c7a64..2adfb8d8 100644 --- a/moksha-mint/src/mint.rs +++ b/moksha-mint/src/mint.rs @@ -32,8 +32,6 @@ use crate::lightning::cln::ClnLightning; pub struct Mint { pub lightning: Arc, pub lightning_type: LightningType, - // FIXME remove after v1 api release - pub keyset_legacy: MintKeyset, pub keyset: MintKeyset, pub db: DB, pub dhke: Dhke, @@ -57,11 +55,6 @@ where Self { lightning, lightning_type, - keyset_legacy: MintKeyset::legacy_new( - // FIXME - &config.privatekey.clone(), - &config.derivation_path.clone().unwrap_or_default(), - ), keyset: MintKeyset::new( &config.privatekey.clone(), &config.derivation_path.clone().unwrap_or_default(), @@ -171,7 +164,7 @@ where let amount_promises = promises.total_amount(); if sum_proofs != amount_promises { return Err(MokshaMintError::SwapAmountMismatch(format!( - "Split amount mismatch: {sum_proofs} != {amount_promises}" + "Swap amount mismatch: {sum_proofs} != {amount_promises}" ))); } @@ -532,7 +525,7 @@ mod tests { moksha_core::primitives::PaymentMethod::Bolt11, "somehash".to_string(), &outputs, - &mint.keyset_legacy, + &mint.keyset, true, ) .await?; @@ -562,7 +555,7 @@ mod tests { moksha_core::primitives::PaymentMethod::Bolt11, "somehash".to_string(), &outputs, - &mint.keyset_legacy, + &mint.keyset, true, ) .await?; @@ -585,7 +578,7 @@ mod tests { let proofs = Proofs::empty(); let result = mint - .swap(&proofs, &blinded_messages, &mint.keyset_legacy) + .swap(&proofs, &blinded_messages, &mint.keyset) .await?; assert!(result.is_empty()); @@ -688,7 +681,7 @@ mod tests { 4, &tokens.proofs(), &change, - &mint.keyset_legacy, + &mint.keyset, ) .await?; diff --git a/moksha-mint/src/routes/default.rs b/moksha-mint/src/routes/default.rs index 666cc7d1..d063a0c2 100644 --- a/moksha-mint/src/routes/default.rs +++ b/moksha-mint/src/routes/default.rs @@ -3,7 +3,7 @@ use axum::{ Json, }; use moksha_core::{ - keyset::V1Keysets, + keyset::Keysets, primitives::{ Bolt11MeltQuote, Bolt11MintQuote, CurrencyUnit, KeyResponse, KeysResponse, MintInfoResponse, Nuts, PaymentMethod, PostMeltBolt11Request, PostMeltBolt11Response, @@ -96,12 +96,12 @@ pub async fn get_keys_by_id( get, path = "/v1/keysets", responses( - (status = 200, description = "get keysets", body = [V1Keysets]) + (status = 200, description = "get keysets", body = [Keysets]) ), )] #[instrument(skip(mint), err)] -pub async fn get_keysets(State(mint): State) -> Result, MokshaMintError> { - Ok(Json(V1Keysets::new( +pub async fn get_keysets(State(mint): State) -> Result, MokshaMintError> { + Ok(Json(Keysets::new( mint.keyset.keyset_id, CurrencyUnit::Sat, true, diff --git a/moksha-mint/src/server.rs b/moksha-mint/src/server.rs index a1758817..17831886 100644 --- a/moksha-mint/src/server.rs +++ b/moksha-mint/src/server.rs @@ -16,7 +16,7 @@ use axum::routing::{get_service, post, get}; use axum::{middleware, Router}; -use moksha_core::keyset::{V1Keyset, V1Keysets}; +use moksha_core::keyset::{Keyset, Keysets}; use moksha_core::proof::Proofs; use moksha_core::proof::{P2SHScript, Proof}; @@ -130,8 +130,8 @@ pub async fn run_server(mint: Mint) -> anyhow::Result<()> { PaymentMethod, KeysResponse, KeyResponse, - V1Keysets, - V1Keyset, + Keysets, + Keyset, BlindedMessage, BlindedSignature, Proof, @@ -283,7 +283,7 @@ mod tests { }; use http_body_util::BodyExt; use moksha_core::{ - keyset::V1Keysets, + keyset::Keysets, primitives::{CurrencyUnit, KeysResponse, MintInfoResponse}, }; @@ -328,8 +328,8 @@ mod tests { .await?; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let keysets = serde_json::from_slice::(&body)?; - assert_eq!(V1Keysets::new("00f545318e4fad2b".to_owned(), CurrencyUnit::Sat, true), keysets); + let keysets = serde_json::from_slice::(&body)?; + assert_eq!(Keysets::new("00f545318e4fad2b".to_owned(), CurrencyUnit::Sat, true), keysets); Ok(()) } @@ -408,7 +408,7 @@ mod tests { assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let keysets = serde_json::from_slice::(&body)?; + let keysets = serde_json::from_slice::(&body)?; assert_eq!(1, keysets.keysets.len()); assert_eq!(16, keysets.keysets[0].id.len()); Ok(()) @@ -500,7 +500,7 @@ mod tests { assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await?.to_bytes(); - let keys: V1Keysets = serde_json::from_slice(&body)?; + let keys: Keysets = serde_json::from_slice(&body)?; assert_eq!(1, keys.keysets.len()); let keyset = keys.keysets.first().expect("keyset not found"); assert!(keyset.active); diff --git a/moksha-wallet/src/client/crossplatform.rs b/moksha-wallet/src/client/crossplatform.rs index b3db8982..fda03676 100644 --- a/moksha-wallet/src/client/crossplatform.rs +++ b/moksha-wallet/src/client/crossplatform.rs @@ -2,7 +2,7 @@ use async_trait::async_trait; use moksha_core::{ blind::BlindedMessage, - keyset::V1Keysets, + keyset::Keysets, primitives::{ CurrencyUnit, GetMeltBtcOnchainResponse, KeysResponse, MintInfoResponse, PostMeltBolt11Request, PostMeltBolt11Response, PostMeltBtcOnchainRequest, @@ -36,7 +36,7 @@ impl CashuClient for CrossPlatformHttpClient { .await } - async fn get_keysets(&self, mint_url: &Url) -> Result { + async fn get_keysets(&self, mint_url: &Url) -> Result { self.do_get(&mint_url.join("v1/keysets")?).await } diff --git a/moksha-wallet/src/client/mod.rs b/moksha-wallet/src/client/mod.rs index 23482094..33d44b70 100644 --- a/moksha-wallet/src/client/mod.rs +++ b/moksha-wallet/src/client/mod.rs @@ -1,7 +1,7 @@ use async_trait::async_trait; use moksha_core::{ blind::BlindedMessage, - keyset::V1Keysets, + keyset::Keysets, primitives::{ CurrencyUnit, GetMeltBtcOnchainResponse, KeysResponse, MintInfoResponse, PostMeltBolt11Response, PostMeltBtcOnchainResponse, PostMeltQuoteBolt11Response, @@ -31,7 +31,7 @@ pub trait CashuClient { keyset_id: String, ) -> Result; - async fn get_keysets(&self, mint_url: &Url) -> Result; + async fn get_keysets(&self, mint_url: &Url) -> Result; async fn post_swap( &self, diff --git a/moksha-wallet/src/wallet.rs b/moksha-wallet/src/wallet.rs index 9f6e3d1c..73c9da5b 100644 --- a/moksha-wallet/src/wallet.rs +++ b/moksha-wallet/src/wallet.rs @@ -839,7 +839,7 @@ mod tests { use crate::wallet::WalletBuilder; use moksha_core::fixture::{read_fixture, read_fixture_as}; - use moksha_core::keyset::{KeysetId, MintKeyset, V1Keysets}; + use moksha_core::keyset::{KeysetId, MintKeyset, Keysets}; use moksha_core::primitives::{ CurrencyUnit, KeyResponse, KeysResponse, PaymentMethod, PostMeltBolt11Response, PostMeltQuoteBolt11Response, PostMintBolt11Response, PostSwapResponse, @@ -858,7 +858,7 @@ mod tests { }; let keys_response = KeysResponse::new(key_response.clone()); let keys_by_id_response = keys_response.clone(); - let keysets = V1Keysets::new(keys.keyset_id, CurrencyUnit::Sat, true); + let keysets = Keysets::new(keys.keyset_id, CurrencyUnit::Sat, true); let mut client = MockCashuClient::default(); client