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

Commit 03d499d

Browse files
committed
wip(feat): reissue ecash from OOBNotes
wip(feat): reissue ecash notes from `OOBNotes`` wip: working version 🚀 fix: clippy
1 parent 38ee362 commit 03d499d

File tree

8 files changed

+123
-3
lines changed

8 files changed

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

+71-2
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,28 @@ impl<S: MutinyStorage> FederationClient<S> {
603603
}
604604
}
605605

606+
pub(crate) async fn reissue(&self, oob_notes: OOBNotes) -> Result<(), 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+
// Reissue `OOBNotes`
613+
let operation_id = mint_module.reissue_external_notes(oob_notes, ()).await?;
614+
615+
// TODO: (@leonardo) re-think about the results and errors that we need/want
616+
match process_reissue_outcome(&mint_module, operation_id, logger.clone()).await? {
617+
ReissueExternalNotesState::Created | ReissueExternalNotesState::Failed(_) => {
618+
log_trace!(logger, "re-issuance of OOBNotes failed!");
619+
Err(MutinyError::FedimintReissueFailed)
620+
}
621+
_ => {
622+
log_trace!(logger, "re-issuance of OOBNotes was successful!");
623+
Ok(())
624+
}
625+
}
626+
}
627+
606628
pub async fn get_mutiny_federation_identity(&self) -> FederationIdentity {
607629
let gateway_fees = self.gateway_fee().await.ok();
608630

@@ -860,6 +882,53 @@ where
860882
invoice
861883
}
862884

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

mutiny-core/src/lib.rs

+33-1
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;
@@ -83,6 +82,7 @@ use bitcoin::hashes::Hash;
8382
use bitcoin::secp256k1::{PublicKey, ThirtyTwoByteHash};
8483
use bitcoin::{hashes::sha256, Network};
8584
use fedimint_core::{api::InviteCode, config::FederationId};
85+
use fedimint_mint_client::OOBNotes;
8686
use futures::{pin_mut, select, FutureExt};
8787
use futures_util::join;
8888
use hex_conservative::{DisplayHex, FromHex};
@@ -112,6 +112,7 @@ use crate::utils::parse_profile_metadata;
112112
use mockall::{automock, predicate::*};
113113

114114
const DEFAULT_PAYMENT_TIMEOUT: u64 = 30;
115+
const DEFAULT_REISSUE_TIMEOUT: u64 = 5;
115116
const MAX_FEDERATION_INVOICE_AMT: u64 = 200_000;
116117
const SWAP_LABEL: &str = "SWAP";
117118

@@ -1352,6 +1353,37 @@ impl<S: MutinyStorage> MutinyWallet<S> {
13521353
})
13531354
}
13541355

1356+
pub async fn reissue_oob_notes(&self, oob_notes: OOBNotes) -> Result<(), MutinyError> {
1357+
let federation_lock = self.federations.read().await;
1358+
let federation_ids = self.list_federation_ids().await?;
1359+
1360+
let maybe_federation_id = federation_ids
1361+
.iter()
1362+
.find(|id| id.to_prefix() == oob_notes.federation_id_prefix());
1363+
1364+
if let Some(fed_id) = maybe_federation_id {
1365+
log_trace!(self.logger, "found federation_id {:?}", fed_id);
1366+
1367+
let fedimint_client = federation_lock.get(fed_id).ok_or(MutinyError::NotFound)?;
1368+
log_trace!(
1369+
self.logger,
1370+
"got fedimint client for federation_id {:?}",
1371+
fed_id
1372+
);
1373+
1374+
fedimint_client.reissue(oob_notes).await?;
1375+
log_trace!(
1376+
self.logger,
1377+
"successfully reissued for federation_id {:?}",
1378+
fed_id
1379+
);
1380+
1381+
Ok(())
1382+
} else {
1383+
Err(MutinyError::NotFound)
1384+
}
1385+
}
1386+
13551387
/// Estimate the fee before trying to sweep from federation
13561388
pub async fn estimate_sweep_federation_fee(
13571389
&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 = "5ade2536015a12a7e003a42b159ccc4a431e1a32" }
48+
fedimint-mint-client = { git = "https://github.com/fedimint/fedimint", rev = "5ade2536015a12a7e003a42b159ccc4a431e1a32" }
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;
@@ -1021,6 +1022,17 @@ impl MutinyWallet {
10211022
Ok(self.inner.sweep_federation_balance(amount).await?.into())
10221023
}
10231024

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

0 commit comments

Comments
 (0)