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

Commit 5866f3c

Browse files
committed
wip(feat): reissue ecash notes from OOBNotes`
1 parent 9e1f35a commit 5866f3c

File tree

8 files changed

+93
-1
lines changed

8 files changed

+93
-1
lines changed

.editorconfig

Whitespace-only changes.

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mutiny-core/src/error.rs

+2
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

+56-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::utils::{convert_from_fedimint_invoice, convert_to_fedimint_invoice, spawn};
2+
use crate::DEFAULT_REISSUE_TIMEOUT;
23
use crate::{
34
error::{MutinyError, MutinyStorageError},
45
event::PaymentInfo,
@@ -12,6 +13,7 @@ use crate::{
1213
HTLCStatus, MutinyInvoice, DEFAULT_PAYMENT_TIMEOUT,
1314
};
1415
use async_trait::async_trait;
16+
use bip39::rand_core::le;
1517
use bip39::Mnemonic;
1618
use bitcoin::secp256k1::ThirtyTwoByteHash;
1719
use bitcoin::{
@@ -52,7 +54,7 @@ use fedimint_ln_client::{
5254
};
5355
use fedimint_ln_common::lightning_invoice::RoutingFees;
5456
use fedimint_ln_common::LightningCommonInit;
55-
use fedimint_mint_client::MintClientInit;
57+
use fedimint_mint_client::{MintClientInit, MintClientModule, OOBNotes, ReissueExternalNotesState};
5658
use fedimint_wallet_client::{WalletClientInit, WalletClientModule};
5759
use futures::future::{self};
5860
use futures_util::{pin_mut, StreamExt};
@@ -603,6 +605,59 @@ impl<S: MutinyStorage> FederationClient<S> {
603605
}
604606
}
605607

608+
pub(crate) async fn reissue(&self, oob_notes: OOBNotes) -> Result<bool, MutinyError> {
609+
let logger = Arc::clone(&self.logger);
610+
611+
// Get the `MintClientModule`
612+
let mint_module = self.fedimint_client.get_first_module::<MintClientModule>();
613+
614+
// TODO: (@leonardo) Do we need any `extra_meta` ?
615+
// Reissue `OOBNotes`
616+
let operation_id = mint_module.reissue_external_notes(oob_notes, ()).await?;
617+
618+
// Subscribe/Process the outcome based on `ReissueExternalNotesState`
619+
let mut stream_or_outcome = mint_module
620+
.subscribe_reissue_external_notes(operation_id)
621+
.await
622+
.map_err(|e| MutinyError::Other(e))?;
623+
624+
let mut reissue_state = ReissueExternalNotesState::Created;
625+
626+
let _ = match stream_or_outcome {
627+
UpdateStreamOrOutcome::Outcome(outcome) => {
628+
reissue_state = outcome;
629+
log_trace!(logger, "Outcome received: {:?}", reissue_state);
630+
}
631+
UpdateStreamOrOutcome::UpdateStream(mut stream) => {
632+
let timeout = DEFAULT_REISSUE_TIMEOUT * 1_000;
633+
let timeout_future = sleep(timeout as i32);
634+
pin_mut!(timeout_future);
635+
636+
log_trace!(logger, "Start timeout stream features");
637+
while let future::Either::Left((outcome_opt, _)) =
638+
future::select(stream.next(), &mut timeout_future).await
639+
{
640+
if let Some(outcome) = outcome_opt {
641+
reissue_state = outcome;
642+
log_trace!(logger, "Streamed Outcome received: {:?}", reissue_state);
643+
if let ReissueExternalNotesState::Failed(_) = reissue_state {
644+
log_trace!(logger, "Streamed Outcome final, returning");
645+
break;
646+
}
647+
}
648+
}
649+
}
650+
};
651+
652+
// TODO: (@leonardo) re-think about the results and errors that we need/want
653+
match reissue_state {
654+
ReissueExternalNotesState::Created | ReissueExternalNotesState::Failed(_) => {
655+
Err(MutinyError::FedimintReissueFailed)
656+
}
657+
_ => Ok(true),
658+
}
659+
}
660+
606661
pub async fn get_mutiny_federation_identity(&self) -> FederationIdentity {
607662
let gateway_fees = self.gateway_fee().await.ok();
608663

mutiny-core/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ use bitcoin::hashes::Hash;
8484
use bitcoin::secp256k1::PublicKey;
8585
use bitcoin::{hashes::sha256, Network};
8686
use fedimint_core::{api::InviteCode, config::FederationId};
87+
use fedimint_mint_client::OOBNotes;
8788
use futures::{pin_mut, select, FutureExt};
8889
use futures_util::join;
8990
use hex_conservative::{DisplayHex, FromHex};
@@ -113,6 +114,7 @@ use crate::utils::parse_profile_metadata;
113114
use mockall::{automock, predicate::*};
114115

115116
const DEFAULT_PAYMENT_TIMEOUT: u64 = 30;
117+
const DEFAULT_REISSUE_TIMEOUT: u64 = 30;
116118
const MAX_FEDERATION_INVOICE_AMT: u64 = 200_000;
117119
const SWAP_LABEL: &str = "SWAP";
118120

@@ -1308,6 +1310,22 @@ impl<S: MutinyStorage> MutinyWallet<S> {
13081310
})
13091311
}
13101312

1313+
pub async fn reissue_oob_notes(&self, oob_notes: OOBNotes) -> Result<bool, MutinyError> {
1314+
let federation_lock = self.federations.read().await;
1315+
let federation_ids = self.list_federation_ids().await?;
1316+
1317+
let maybe_federation_id = federation_ids
1318+
.iter()
1319+
.find(|id| **id == oob_notes.federation_id_prefix());
1320+
1321+
if let Some(fed_id) = maybe_federation_id {
1322+
let fedimint_client = federation_lock.get(&fed_id).ok_or(MutinyError::NotFound)?;
1323+
Ok(fedimint_client.reissue(oob_notes).await?)
1324+
} else {
1325+
return Err(MutinyError::NotFound);
1326+
}
1327+
}
1328+
13111329
/// Estimate the fee before trying to sweep from federation
13121330
pub async fn estimate_sweep_federation_fee(
13131331
&self,

mutiny-wasm/Cargo.toml

+1
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

+3
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

+12
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)