Skip to content

Commit

Permalink
chore: remove legacy router and primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Apr 9, 2024
1 parent 0d58a4d commit ac86d4c
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 338 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions moksha-core/src/keyset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ impl Keysets {
}
}

#[derive(Clone, Debug, Serialize, Deserialize, Default, ToSchema)]
// FIXME rename to keysets
#[derive(Clone, Debug, Serialize, Deserialize, Default, ToSchema, PartialEq, Eq)]
pub struct V1Keysets {
pub keysets: Vec<V1Keyset>,
}

#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
// FIXME rename to keyset
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, PartialEq, Eq)]
pub struct V1Keyset {
pub id: String, // FIXME use KeysetId
pub unit: CurrencyUnit,
Expand Down
90 changes: 2 additions & 88 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ pub struct PaymentRequest {
pub hash: String, // TODO use sha256::Hash
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct PostMintResponse {
pub promises: Vec<BlindedSignature>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PostMintRequest {
pub outputs: Vec<BlindedMessage>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CheckFeesRequest {
Expand All @@ -41,36 +32,6 @@ pub struct CheckFeesResponse {
pub fee: u64,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PostMeltRequest {
pub proofs: Proofs,
pub pr: String,
pub outputs: Vec<BlindedMessage>,
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct PostMeltResponse {
pub paid: bool,
pub preimage: String,
pub change: Vec<BlindedSignature>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PostSplitRequest {
pub proofs: Proofs,
pub outputs: Vec<BlindedMessage>,
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct PostSplitResponse {
pub promises: Vec<BlindedSignature>,
}

impl PostSplitResponse {
pub fn with_promises(promises: Vec<BlindedSignature>) -> Self {
Self { promises }
}
}

#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct PostSwapRequest {
Expand All @@ -89,24 +50,7 @@ pub struct CashuErrorResponse {
pub detail: String,
}

#[skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq)]
pub struct MintLegacyInfoResponse {
pub name: Option<String>,
pub pubkey: PublicKey,
pub version: Option<String>,
pub description: Option<String>,
pub description_long: Option<String>,
pub contact: Option<Vec<Vec<String>>>,
pub nuts: Vec<String>,
pub motd: Option<String>,
pub parameter: Parameter,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Default)]
pub struct Parameter {
pub peg_out_only: bool,
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Default, ToSchema)]
pub struct KeysResponse {
Expand Down Expand Up @@ -581,7 +525,7 @@ mod tests {
dhke::public_key_from_hex,
fixture::read_fixture,
primitives::{
KeyResponse, MintInfoResponse, MintLegacyInfoResponse, Nuts, Parameter,
KeyResponse, MintInfoResponse, Nuts,
PostSwapResponse,
},
};
Expand All @@ -606,37 +550,7 @@ mod tests {
Ok(())
}

#[test]
fn test_deserialize_legacy_mint_info() -> anyhow::Result<()> {
let mint_info = MintLegacyInfoResponse {
name: Some("Bob's Cashu mint".to_string()),
pubkey: public_key_from_hex(
"02a9acc1e48c25eeeb9289b5031cc57da9fe72f3fe2861d264bdc074209b107ba2",
),
version: Some("Nutshell/0.11.0".to_string()),
description: Some("The short mint description".to_string()),
description_long: Some("A description that can be a long piece of text.".to_string()),
contact: Some(vec![
vec!["email".to_string(), "[email protected]".to_string()],
vec!["twitter".to_string(), "@me".to_string()],
vec!["nostr".to_string(), "npub...".to_string()],
]),
nuts: vec![
"NUT-07".to_string(),
"NUT-08".to_string(),
"NUT-08".to_string(),
],
motd: Some("Message to display to users.".to_string()),
parameter: Parameter {
peg_out_only: false,
},
};
let out = serde_json::to_string_pretty(&mint_info)?;
println!("{}", out);
assert!(!out.is_empty());

Ok(())
}


#[test]
fn test_deserialize_mint_info() -> anyhow::Result<()> {
Expand Down
1 change: 1 addition & 0 deletions moksha-mint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ hex = "0.4.3"
http-body-util = "0.1.0"
testcontainers = "0.15.0"
testcontainers-modules = { version = "0.3.6", features = ["postgres"] }
pretty_assertions = "1.4.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"proofs": [
"inputs": [
{
"amount": 64,
"secret": "sYYrrhUD3IwJzGFCGsUqqXXa",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"proofs": [
"inputs": [
{
"amount": 64,
"secret": "sYYrrhUD3IwJzGFCGsUqqXXa",
Expand Down
42 changes: 15 additions & 27 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,14 +450,16 @@ mod tests {
use crate::model::{Invoice, PayInvoiceResult};
use moksha_core::blind::{BlindedMessage, TotalAmount};
use moksha_core::dhke;
use moksha_core::primitives::PostSplitRequest;
use moksha_core::fixture::read_fixture_as;
use moksha_core::primitives::PostSwapRequest;
use moksha_core::proof::Proofs;
use moksha_core::token::TokenV3;
use std::str::FromStr;
use std::sync::Arc;
use testcontainers::clients::Cli;
use testcontainers::RunnableImage;
use testcontainers_modules::postgres::Postgres;
use pretty_assertions::assert_eq;

#[tokio::test]
async fn test_fee_reserve() -> anyhow::Result<()> {
Expand Down Expand Up @@ -495,7 +497,7 @@ mod tests {
id: "00ffd48b8f5ecf80".to_owned(),
}];

let result = mint.create_blinded_signatures(&blinded_messages, &mint.keyset_legacy)?;
let result = mint.create_blinded_signatures(&blinded_messages, &mint.keyset)?;

assert_eq!(1, result.len());
assert_eq!(8, result[0].amount);
Expand Down Expand Up @@ -552,7 +554,7 @@ mod tests {
)
.await?;

let outputs = create_blinded_msgs_from_fixture("blinded_messages_40.json".to_string())?;
let outputs = read_fixture_as::<Vec<BlindedMessage>>("blinded_messages_40.json")?;
let mut tx = mint.db.begin_tx().await?;
let result = mint
.mint_tokens(
Expand Down Expand Up @@ -591,7 +593,7 @@ mod tests {
}

#[tokio::test]
async fn test_split_64_in_20() -> anyhow::Result<()> {
async fn test_swap_64_in_20() -> anyhow::Result<()> {
let docker = Cli::default();
let image = create_postgres_image();
let node = docker.run(image);
Expand All @@ -601,10 +603,10 @@ mod tests {
None,
)
.await?;
let request = create_request_from_fixture("post_split_request_64_20.json".to_string())?;
let request = read_fixture_as::<PostSwapRequest>("post_swap_request_64_20.json")?;

let result = mint
.swap(&request.proofs, &request.outputs, &mint.keyset_legacy)
.swap(&request.inputs, &request.outputs, &mint.keyset)
.await?;
assert_eq!(result.total_amount(), 64);

Expand All @@ -617,7 +619,7 @@ mod tests {
}

#[tokio::test]
async fn test_split_duplicate_key() -> anyhow::Result<()> {
async fn test_swap_duplicate_key() -> anyhow::Result<()> {
let docker = Cli::default();
let image = create_postgres_image();
let node = docker.run(image);
Expand All @@ -627,10 +629,10 @@ mod tests {
)
.await?;
let request =
create_request_from_fixture("post_split_request_duplicate_key.json".to_string())?;
read_fixture_as::<PostSwapRequest>("post_swap_request_duplicate_key.json")?;

let result = mint
.swap(&request.proofs, &request.outputs, &mint.keyset_legacy)
.swap(&request.inputs, &request.outputs, &mint.keyset)
.await;
assert!(result.is_err());
Ok(())
Expand Down Expand Up @@ -673,10 +675,10 @@ mod tests {
Some(Arc::new(MockBtcOnchain::default())),
);

let tokens = create_token_from_fixture("token_60.cashu".to_string())?;
let tokens = create_token_from_fixture("token_60.cashu").expect("can not read fixture");
let invoice = "some invoice".to_string();
let change =
create_blinded_msgs_from_fixture("blinded_messages_blank_4000.json".to_string())?;
read_fixture_as::<Vec<BlindedMessage>>("blinded_messages_blank_4000.json")?;

let mut tx = mint.db.begin_tx().await?;
let (paid, _payment_hash, change) = mint
Expand All @@ -695,26 +697,12 @@ mod tests {
Ok(())
}

// FIXME refactor helper functions
fn create_token_from_fixture(fixture: String) -> Result<TokenV3, anyhow::Error> {
fn create_token_from_fixture(fixture: &str) -> Result<TokenV3, anyhow::Error> {
let base_dir = std::env::var("CARGO_MANIFEST_DIR")?;
let raw_token = std::fs::read_to_string(format!("{base_dir}/src/fixtures/{fixture}"))?;
Ok(raw_token.trim().to_string().try_into()?)
}

fn create_request_from_fixture(fixture: String) -> Result<PostSplitRequest, anyhow::Error> {
let base_dir = std::env::var("CARGO_MANIFEST_DIR")?;
let raw_token = std::fs::read_to_string(format!("{base_dir}/src/fixtures/{fixture}"))?;
Ok(serde_json::from_str::<PostSplitRequest>(&raw_token)?)
}

fn create_blinded_msgs_from_fixture(
fixture: String,
) -> Result<Vec<BlindedMessage>, anyhow::Error> {
let base_dir = std::env::var("CARGO_MANIFEST_DIR")?;
let raw_token = std::fs::read_to_string(format!("{base_dir}/src/fixtures/{fixture}"))?;
Ok(serde_json::from_str::<Vec<BlindedMessage>>(&raw_token)?)
}


async fn create_mint_from_mocks(
mock_db: PostgresDB,
Expand Down
8 changes: 0 additions & 8 deletions moksha-mint/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct GetMintQuery {
pub amount: u64,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PostMintQuery {
pub hash: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Invoice {
Expand Down
Loading

0 comments on commit ac86d4c

Please sign in to comment.