Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit ec98778

Browse files
committed
wip(feat): reissue ecash from OOBNotes
wip(feat): reissue ecash notes from `OOBNotes`` wip: working version 🚀
1 parent 9e1f35a commit ec98778

File tree

8 files changed

+113
-3
lines changed

8 files changed

+113
-3
lines changed

.editorconfig

Whitespace-only changes.

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mutiny-core/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ pub enum MutinyError {
162162
/// Payjoin configuration error
163163
#[error("Payjoin configuration failed.")]
164164
PayjoinConfigError,
165+
#[error("Fedimint external note reissuance failed.")]
166+
FedimintReissueFailed,
165167
#[error(transparent)]
166168
Other(#[from] anyhow::Error),
167169
}

mutiny-core/src/federation.rs

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
get_payment_info, list_payment_info, persist_payment_info, MutinyStorage, VersionedValue,
1010
},
1111
utils::sleep,
12-
HTLCStatus, MutinyInvoice, DEFAULT_PAYMENT_TIMEOUT,
12+
HTLCStatus, MutinyInvoice, DEFAULT_PAYMENT_TIMEOUT, DEFAULT_REISSUE_TIMEOUT,
1313
};
1414
use async_trait::async_trait;
1515
use bip39::Mnemonic;
@@ -52,7 +52,7 @@ use fedimint_ln_client::{
5252
};
5353
use fedimint_ln_common::lightning_invoice::RoutingFees;
5454
use fedimint_ln_common::LightningCommonInit;
55-
use fedimint_mint_client::MintClientInit;
55+
use fedimint_mint_client::{MintClientInit, MintClientModule, OOBNotes, ReissueExternalNotesState};
5656
use fedimint_wallet_client::{WalletClientInit, WalletClientModule};
5757
use futures::future::{self};
5858
use futures_util::{pin_mut, StreamExt};
@@ -603,6 +603,29 @@ impl<S: MutinyStorage> FederationClient<S> {
603603
}
604604
}
605605

606+
pub(crate) async fn reissue(&self, oob_notes: OOBNotes) -> Result<bool, MutinyError> {
607+
let logger = Arc::clone(&self.logger);
608+
609+
// Get the `MintClientModule`
610+
let mint_module = self.fedimint_client.get_first_module::<MintClientModule>();
611+
612+
// TODO: (@leonardo) Do we need any `extra_meta` ?
613+
// Reissue `OOBNotes`
614+
let operation_id = mint_module.reissue_external_notes(oob_notes, ()).await?;
615+
616+
// TODO: (@leonardo) re-think about the results and errors that we need/want
617+
match process_reissue_outcome(&mint_module, operation_id, logger.clone()).await? {
618+
ReissueExternalNotesState::Created | ReissueExternalNotesState::Failed(_) => {
619+
log_info!(logger, "re-issuance of OOBNotes failed!");
620+
Err(MutinyError::FedimintReissueFailed)
621+
}
622+
_ => {
623+
log_info!(logger, "re-issuance of OOBNotes was successful!");
624+
Ok(true)
625+
}
626+
}
627+
}
628+
606629
pub async fn get_mutiny_federation_identity(&self) -> FederationIdentity {
607630
let gateway_fees = self.gateway_fee().await.ok();
608631

@@ -860,6 +883,53 @@ where
860883
invoice
861884
}
862885

886+
async fn process_reissue_outcome(
887+
mint_module: &MintClientModule,
888+
operation_id: OperationId,
889+
logger: Arc<MutinyLogger>,
890+
) -> Result<ReissueExternalNotesState, MutinyError> {
891+
// Subscribe/Process the outcome based on `ReissueExternalNotesState`
892+
let stream_or_outcome = mint_module
893+
.subscribe_reissue_external_notes(operation_id)
894+
.await
895+
.map_err(|e| MutinyError::Other(e))?;
896+
897+
match stream_or_outcome {
898+
UpdateStreamOrOutcome::Outcome(outcome) => {
899+
log_trace!(logger, "outcome received {:?}", outcome);
900+
return Ok(outcome);
901+
}
902+
UpdateStreamOrOutcome::UpdateStream(mut stream) => {
903+
let timeout = DEFAULT_REISSUE_TIMEOUT * 1_000;
904+
let timeout_fut = sleep(timeout as i32);
905+
pin_mut!(timeout_fut);
906+
907+
log_trace!(logger, "started timeout future {:?}", timeout);
908+
909+
while let future::Either::Left((outcome_opt, _)) =
910+
future::select(stream.next(), &mut timeout_fut).await
911+
{
912+
if let Some(outcome) = outcome_opt {
913+
log_trace!(logger, "streamed outcome received {:?}", outcome);
914+
915+
match outcome {
916+
ReissueExternalNotesState::Failed(_) | ReissueExternalNotesState::Done => {
917+
log_trace!(
918+
logger,
919+
"streamed outcome received is final {:?}, returning",
920+
outcome
921+
);
922+
return Ok(outcome);
923+
}
924+
_ => { /* ignore and continue */ }
925+
}
926+
};
927+
}
928+
return Err(MutinyError::FedimintReissueFailed);
929+
}
930+
}
931+
}
932+
863933
#[derive(Clone)]
864934
pub struct FedimintStorage<S: MutinyStorage> {
865935
pub(crate) storage: S,

mutiny-core/src/lib.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
clippy::arc_with_non_send_sync,
88
type_alias_bounds
99
)]
10-
extern crate core;
1110

1211
pub mod auth;
1312
mod chain;
@@ -84,6 +83,7 @@ use bitcoin::hashes::Hash;
8483
use bitcoin::secp256k1::PublicKey;
8584
use bitcoin::{hashes::sha256, Network};
8685
use fedimint_core::{api::InviteCode, config::FederationId};
86+
use fedimint_mint_client::OOBNotes;
8787
use futures::{pin_mut, select, FutureExt};
8888
use futures_util::join;
8989
use hex_conservative::{DisplayHex, FromHex};
@@ -113,6 +113,7 @@ use crate::utils::parse_profile_metadata;
113113
use mockall::{automock, predicate::*};
114114

115115
const DEFAULT_PAYMENT_TIMEOUT: u64 = 30;
116+
const DEFAULT_REISSUE_TIMEOUT: u64 = 60;
116117
const MAX_FEDERATION_INVOICE_AMT: u64 = 200_000;
117118
const SWAP_LABEL: &str = "SWAP";
118119

@@ -1308,6 +1309,26 @@ impl<S: MutinyStorage> MutinyWallet<S> {
13081309
})
13091310
}
13101311

1312+
pub async fn reissue_oob_notes(&self, oob_notes: OOBNotes) -> Result<bool, MutinyError> {
1313+
let federation_lock = self.federations.read().await;
1314+
let federation_ids = self.list_federation_ids().await?;
1315+
1316+
let maybe_federation_id = federation_ids
1317+
.iter()
1318+
.find(|id| id.to_prefix() == oob_notes.federation_id_prefix());
1319+
1320+
if let Some(fed_id) = maybe_federation_id {
1321+
log_info!(self.logger, "found federation_id {:?}", fed_id);
1322+
let fedimint_client = federation_lock.get(&fed_id).ok_or(MutinyError::NotFound)?;
1323+
log_info!(self.logger, "got fedimint client for federation_id {:?}", fed_id);
1324+
let reissue = fedimint_client.reissue(oob_notes).await?;
1325+
log_info!(self.logger, "successfully reissued for federation_id {:?}", fed_id);
1326+
Ok(reissue)
1327+
} else {
1328+
return Err(MutinyError::NotFound);
1329+
}
1330+
}
1331+
13111332
/// Estimate the fee before trying to sweep from federation
13121333
pub async fn estimate_sweep_federation_fee(
13131334
&self,

mutiny-wasm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ once_cell = "1.18.0"
4545
hex-conservative = "0.1.1"
4646
payjoin = { version = "0.13.0", features = ["send", "base64"] }
4747
fedimint-core = { git = "https://github.com/fedimint/fedimint", rev = "6a923ee10c3a578cd835044e3fdd94aa5123735a" }
48+
fedimint-mint-client = { git = "https://github.com/fedimint/fedimint", rev = "6a923ee10c3a578cd835044e3fdd94aa5123735a" }
4849

4950
# The `console_error_panic_hook` crate provides better debugging of panics by
5051
# logging them with `console.error`. This is great for development, but requires

mutiny-wasm/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ pub enum MutinyJsError {
159159
/// Payjoin configuration error
160160
#[error("Payjoin configuration failed.")]
161161
PayjoinConfigError,
162+
#[error("Fedimint external note reissuance failed.")]
163+
FedimintReissueFailed,
162164
/// Unknown error.
163165
#[error("Unknown Error")]
164166
UnknownError,
@@ -226,6 +228,7 @@ impl From<MutinyError> for MutinyJsError {
226228
MutinyError::PayjoinConfigError => MutinyJsError::PayjoinConfigError,
227229
MutinyError::PayjoinCreateRequest => MutinyJsError::PayjoinCreateRequest,
228230
MutinyError::PayjoinResponse(e) => MutinyJsError::PayjoinResponse(e.to_string()),
231+
MutinyError::FedimintReissueFailed => MutinyJsError::FedimintReissueFailed,
229232
}
230233
}
231234
}

mutiny-wasm/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use bitcoin::hashes::sha256;
2323
use bitcoin::secp256k1::PublicKey;
2424
use bitcoin::{Address, Network, OutPoint, Transaction, Txid};
2525
use fedimint_core::{api::InviteCode, config::FederationId};
26+
use fedimint_mint_client::OOBNotes;
2627
use futures::lock::Mutex;
2728
use gloo_utils::format::JsValueSerdeExt;
2829
use hex_conservative::DisplayHex;
@@ -1023,6 +1024,17 @@ impl MutinyWallet {
10231024
Ok(self.inner.sweep_federation_balance(amount).await?.into())
10241025
}
10251026

1027+
pub async fn reissue_oob_notes(&self, oob_notes: String) -> Result<bool, MutinyJsError> {
1028+
let notes = OOBNotes::from_str(&oob_notes).map_err(|e| {
1029+
log_error!(
1030+
self.inner.logger,
1031+
"Error parsing federation `OOBNotes` ({oob_notes}): {e}"
1032+
);
1033+
MutinyJsError::InvalidArgumentsError
1034+
})?;
1035+
Ok(self.inner.reissue_oob_notes(notes).await?)
1036+
}
1037+
10261038
/// Estimate the fee before trying to sweep from federation
10271039
pub async fn estimate_sweep_federation_fee(
10281040
&self,

0 commit comments

Comments
 (0)