Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hermes Tests #1158

Merged
merged 5 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 75 additions & 2 deletions mutiny-core/src/blindauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,54 @@ fn create_blind_auth_secret(
}

#[cfg(test)]
#[cfg(not(target_arch = "wasm32"))]
mod test {
use crate::blindauth::{ServicePlanIndex, SignedToken, TokenStorage};
use tbs::{BlindedMessage, BlindedSignature};
use crate::auth::MutinyAuthClient;
use crate::blindauth::{BlindAuthClient, ServicePlanIndex, SignedToken, TokenStorage};
use crate::generate_seed;
use crate::logging::MutinyLogger;
use crate::storage::MemoryStorage;
use crate::test_utils::create_manager;
use bitcoin::bip32::ExtendedPrivKey;
use bitcoin::Network;
use nostr::prelude::hex;
use std::sync::Arc;
use tbs::{
unblind_signature, AggregatePublicKey, BlindedMessage, BlindedSignature, PubKeyPoint,
};

const FREE_PK: &str = "a5596b43416c17dcd331a64d4a5f60ab3a470fc03f83a3d834910903ee78f5ade33da927b86481d6edd3de08e89455d6115f5c5e642f4a47d24c85a458369e736cfc410b984cf7e4bf97853a40632f26ca9ad65008bfbe27f40ab8aa7bdffe9f";

fn create_client() -> BlindAuthClient<MemoryStorage> {
let storage = MemoryStorage::default();
let logger = Arc::new(MutinyLogger::default());
let mnemonic = generate_seed(12).unwrap();
let xpriv = ExtendedPrivKey::new_master(Network::Regtest, &mnemonic.to_seed("")).unwrap();

// Set up test auth client
let auth_manager = create_manager();
let lnurl_client = Arc::new(
lnurl::Builder::default()
.build_async()
.expect("failed to make lnurl client"),
);
let auth_client = MutinyAuthClient::new(
auth_manager,
lnurl_client,
logger.clone(),
"https://auth-staging.mutinywallet.com".to_string(),
);

BlindAuthClient::new(
xpriv,
Arc::new(auth_client),
Network::Regtest,
"https://blind-auth-staging.mutinywallet.com".to_string(),
&storage,
logger,
)
.unwrap()
}

#[test]
fn test_token_storage_serialization() {
Expand Down Expand Up @@ -447,4 +492,32 @@ mod test {
let deserialized: TokenStorage = serde_json::from_str(string).unwrap();
assert_eq!(storage, deserialized);
}

#[tokio::test]
async fn test_blind_auth_client() {
let client = create_client();
assert!(client.available_tokens().await.is_empty());

client.redeem_available_tokens().await.unwrap();

let tokens = client.available_tokens().await;
assert!(!tokens.is_empty());

let token = tokens.first().unwrap();

// do the unblinding
let (nonce, blinding_key) = client.get_unblinded_info_from_token(token);
let unblinded_sig = unblind_signature(blinding_key, token.blind_sig);

// check that the signature is valid
let free_pk: AggregatePublicKey = AggregatePublicKey(
PubKeyPoint::from_compressed(
hex::decode(FREE_PK).expect("Invalid key hex")[..]
.try_into()
.expect("Invalid key byte key"),
)
.expect("Invalid FREE_PK"),
);
assert!(tbs::verify(nonce.to_message(), unblinded_sig, free_pk));
}
}
19 changes: 19 additions & 0 deletions mutiny-core/src/federation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ pub struct FedimintBalance {
pub amount: u64,
}

#[cfg_attr(test, mockall::automock)]
pub trait FedimintClient {
async fn claim_external_receive(
&self,
secret_key: &SecretKey,
tweaks: Vec<u64>,
) -> Result<(), MutinyError>;
}

pub(crate) struct FederationClient<S: MutinyStorage> {
pub(crate) uuid: String,
pub(crate) fedimint_client: ClientHandleArc,
Expand Down Expand Up @@ -740,6 +749,16 @@ fn maybe_update_after_checking_fedimint<S: MutinyStorage>(
Ok(())
}

impl<S: MutinyStorage> FedimintClient for FederationClient<S> {
async fn claim_external_receive(
&self,
secret_key: &SecretKey,
tweaks: Vec<u64>,
) -> Result<(), MutinyError> {
self.claim_external_receive(secret_key, tweaks).await
}
}

fn sats_round_up(amount: &Amount) -> u64 {
Amount::from_msats(amount.msats + 999).sats_round_down()
}
Expand Down
Loading
Loading