Skip to content

Commit

Permalink
chore: remove legacy api (#267)
Browse files Browse the repository at this point in the history
* chore: remove legacy router and primitives

* chore: remove legacy keysets

* chore: rename split to swap

* chore: remove rand
  • Loading branch information
ngutech21 authored Apr 9, 2024
1 parent 0d58a4d commit cfd6adc
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 531 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions moksha-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ anyhow = "1.0.79"
url = "2.5.0"
base64 = "0.22.0"
bitcoin_hashes = "0.13.0"
rand = "0.8.5"
secp256k1 = { version = "0.28.2", features = ["rand", "serde"] }
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
Expand All @@ -30,7 +29,7 @@ utoipa = { version = "4.2.0" }
# getrandom is transitive dependency of rand
# on wasm, we need to enable the js backend
# see https://docs.rs/getrandom/latest/getrandom/#indirect-dependencies and https://docs.rs/getrandom/latest/getrandom/#webassembly-support
getrandom = { version = "0.2.11", features = ["js"] }
getrandom = { version = "0.2.14", features = ["js"] }

[dev-dependencies]
anyhow = "1.0.79"
Expand Down
9 changes: 0 additions & 9 deletions moksha-core/src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
//! The `SplitAmount` struct represents a split amount, with a `Vec<u64>` field for the split amounts. The struct provides a `create_secrets` method that generates a vector of random strings for use as secrets in the split transaction. The struct also implements the `IntoIterator` trait, which allows it to be iterated over as a vector of `u64` values.
//!
//! Both the `Amount` and `SplitAmount` structs are serializable and deserializable using serde.
use rand::distributions::Alphanumeric;
use rand::Rng;
#[derive(Debug, Clone)]
pub struct Amount(pub u64);
Expand Down Expand Up @@ -66,13 +64,6 @@ fn split_amount(amount: u64) -> Vec<u64> {
.collect::<Vec<u64>>()
}

pub fn generate_random_string() -> String {
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(24)
.map(char::from)
.collect()
}

#[cfg(test)]
mod tests {
Expand Down
138 changes: 11 additions & 127 deletions moksha-core/src/keyset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,16 @@ 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;
use rand::RngCore;
use secp256k1::{PublicKey, Secp256k1, SecretKey};

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

const MAX_ORDER: u64 = 64;

pub fn generate_hash() -> String {
let mut rng = rand::thread_rng();
let mut random = [0u8; 32];
rng.fill_bytes(&mut random);
sha256::Hash::hash(&random).to_string()
}

#[derive(Debug, Clone)]
pub struct MintKeyset {
Expand All @@ -42,17 +35,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);
Expand All @@ -65,45 +47,24 @@ impl MintKeyset {
}
}

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

impl Keysets {
pub fn new(keysets: Vec<String>) -> Self {
Self { keysets }
}

pub fn current_keyset(
&self,
mint_keys: &HashMap<u64, PublicKey>,
) -> Result<String, MokshaCoreError> {
let computed_id = legacy_derive_keyset_id(mint_keys);
if self.keysets.contains(&computed_id) {
Ok(computed_id)
} else {
Err(MokshaCoreError::InvalidKeysetid)
}
}
}

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

#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct V1Keyset {
// FIXME rename to keyset
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, PartialEq, Eq)]
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 }],
}
}

Expand Down Expand Up @@ -210,25 +171,6 @@ pub fn derive_pubkeys(keys: &HashMap<u64, SecretKey>) -> HashMap<u64, PublicKey>
.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<u64, PublicKey>) -> 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<u64, PublicKey>) -> String {
let pubkeys = keys
.iter()
Expand Down Expand Up @@ -256,16 +198,11 @@ pub fn derive_pubkey(seed: &str) -> Result<PublicKey, MokshaCoreError> {

#[cfg(test)]
mod tests {
use crate::keyset::{derive_pubkey, generate_hash, KeysetId};
use crate::keyset::{derive_pubkey, KeysetId};
use pretty_assertions::assert_eq;
use secp256k1::PublicKey;
use std::collections::HashMap;

fn public_key_from_hex(hex: &str) -> secp256k1::PublicKey {
use hex::FromHex;
let input_vec: Vec<u8> = 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<()> {
Expand All @@ -274,12 +211,6 @@ mod tests {
Ok(())
}

#[test]
fn test_generate_hash() {
let hash = generate_hash();
assert_eq!(hash.len(), 64);
}

#[test]
fn test_derive_pubkey() -> anyhow::Result<()> {
let result = derive_pubkey("supersecretprivatekey")?;
Expand All @@ -290,18 +221,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", "");
Expand All @@ -314,42 +234,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<()> {
Expand Down
Loading

0 comments on commit cfd6adc

Please sign in to comment.