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

Commit ef9545d

Browse files
committed
wip(feat): reissue ecash from OOBNotes
wip(feat): reissue ecash notes from `OOBNotes`` wip: working version 🚀 fix: clippy
1 parent 4c5cea4 commit ef9545d

File tree

8 files changed

+124
-4
lines changed

8 files changed

+124
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ debug/
33
target/
44
.vim/
55
.direnv
6+
.editorconfig
67

78
# These are backup files generated by rustfmt
89
**/*.rs.bk

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
@@ -171,6 +171,8 @@ pub enum MutinyError {
171171
/// Token already spent.
172172
#[error("Token has been already spent.")]
173173
TokenAlreadySpent,
174+
#[error("Fedimint external note reissuance failed.")]
175+
FedimintReissueFailed,
174176
#[error(transparent)]
175177
Other(#[from] anyhow::Error),
176178
}

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};
@@ -609,6 +609,28 @@ impl<S: MutinyStorage> FederationClient<S> {
609609
}
610610
}
611611

612+
pub(crate) async fn reissue(&self, oob_notes: OOBNotes) -> Result<(), MutinyError> {
613+
let logger = Arc::clone(&self.logger);
614+
615+
// Get the `MintClientModule`
616+
let mint_module = self.fedimint_client.get_first_module::<MintClientModule>();
617+
618+
// Reissue `OOBNotes`
619+
let operation_id = mint_module.reissue_external_notes(oob_notes, ()).await?;
620+
621+
// TODO: (@leonardo) re-think about the results and errors that we need/want
622+
match process_reissue_outcome(&mint_module, operation_id, logger.clone()).await? {
623+
ReissueExternalNotesState::Created | ReissueExternalNotesState::Failed(_) => {
624+
log_trace!(logger, "re-issuance of OOBNotes failed!");
625+
Err(MutinyError::FedimintReissueFailed)
626+
}
627+
_ => {
628+
log_trace!(logger, "re-issuance of OOBNotes was successful!");
629+
Ok(())
630+
}
631+
}
632+
}
633+
612634
pub async fn get_mutiny_federation_identity(&self) -> FederationIdentity {
613635
let gateway_fees = self.gateway_fee().await.ok();
614636

@@ -866,6 +888,53 @@ where
866888
invoice
867889
}
868890

891+
async fn process_reissue_outcome(
892+
mint_module: &MintClientModule,
893+
operation_id: OperationId,
894+
logger: Arc<MutinyLogger>,
895+
) -> Result<ReissueExternalNotesState, MutinyError> {
896+
// Subscribe/Process the outcome based on `ReissueExternalNotesState`
897+
let stream_or_outcome = mint_module
898+
.subscribe_reissue_external_notes(operation_id)
899+
.await
900+
.map_err(MutinyError::Other)?;
901+
902+
match stream_or_outcome {
903+
UpdateStreamOrOutcome::Outcome(outcome) => {
904+
log_trace!(logger, "outcome received {:?}", outcome);
905+
Ok(outcome)
906+
}
907+
UpdateStreamOrOutcome::UpdateStream(mut stream) => {
908+
let timeout = DEFAULT_REISSUE_TIMEOUT * 1_000;
909+
let timeout_fut = sleep(timeout as i32);
910+
pin_mut!(timeout_fut);
911+
912+
log_trace!(logger, "started timeout future {:?}", timeout);
913+
914+
while let future::Either::Left((outcome_opt, _)) =
915+
future::select(stream.next(), &mut timeout_fut).await
916+
{
917+
if let Some(outcome) = outcome_opt {
918+
log_trace!(logger, "streamed outcome received {:?}", outcome);
919+
920+
match outcome {
921+
ReissueExternalNotesState::Failed(_) | ReissueExternalNotesState::Done => {
922+
log_trace!(
923+
logger,
924+
"streamed outcome received is final {:?}, returning",
925+
outcome
926+
);
927+
return Ok(outcome);
928+
}
929+
_ => { /* ignore and continue */ }
930+
}
931+
};
932+
}
933+
Err(MutinyError::FedimintReissueFailed)
934+
}
935+
}
936+
}
937+
869938
#[derive(Clone)]
870939
pub struct FedimintStorage<S: MutinyStorage> {
871940
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 cashu;
@@ -85,6 +84,7 @@ use bitcoin::hashes::Hash;
8584
use bitcoin::secp256k1::{PublicKey, ThirtyTwoByteHash};
8685
use bitcoin::{hashes::sha256, Network};
8786
use fedimint_core::{api::InviteCode, config::FederationId};
87+
use fedimint_mint_client::OOBNotes;
8888
use futures::{pin_mut, select, FutureExt};
8989
use futures_util::join;
9090
use hex_conservative::{DisplayHex, FromHex};
@@ -119,6 +119,7 @@ use crate::utils::parse_profile_metadata;
119119
use mockall::{automock, predicate::*};
120120

121121
const DEFAULT_PAYMENT_TIMEOUT: u64 = 30;
122+
const DEFAULT_REISSUE_TIMEOUT: u64 = 5;
122123
const MAX_FEDERATION_INVOICE_AMT: u64 = 200_000;
123124
const SWAP_LABEL: &str = "SWAP";
124125
const MELT_CASHU_TOKEN: &str = "Cashu Token Melt";
@@ -1362,6 +1363,37 @@ impl<S: MutinyStorage> MutinyWallet<S> {
13621363
})
13631364
}
13641365

1366+
pub async fn reissue_oob_notes(&self, oob_notes: OOBNotes) -> Result<(), MutinyError> {
1367+
let federation_lock = self.federations.read().await;
1368+
let federation_ids = self.list_federation_ids().await?;
1369+
1370+
let maybe_federation_id = federation_ids
1371+
.iter()
1372+
.find(|id| id.to_prefix() == oob_notes.federation_id_prefix());
1373+
1374+
if let Some(fed_id) = maybe_federation_id {
1375+
log_trace!(self.logger, "found federation_id {:?}", fed_id);
1376+
1377+
let fedimint_client = federation_lock.get(fed_id).ok_or(MutinyError::NotFound)?;
1378+
log_trace!(
1379+
self.logger,
1380+
"got fedimint client for federation_id {:?}",
1381+
fed_id
1382+
);
1383+
1384+
fedimint_client.reissue(oob_notes).await?;
1385+
log_trace!(
1386+
self.logger,
1387+
"successfully reissued for federation_id {:?}",
1388+
fed_id
1389+
);
1390+
1391+
Ok(())
1392+
} else {
1393+
Err(MutinyError::NotFound)
1394+
}
1395+
}
1396+
13651397
/// Estimate the fee before trying to sweep from federation
13661398
pub async fn estimate_sweep_federation_fee(
13671399
&self,

mutiny-wasm/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ 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
moksha-core = { git = "https://github.com/ngutech21/moksha", rev = "18d99977965662d46ccec29fecdb0ce493745917" }
49-
5050
bitcoin-waila = { git = "https://github.com/mutinywallet/bitcoin-waila", rev = "b8b6a4d709e438fbadeb16bdf0c577c59be4a7f2" }
5151

5252
# The `console_error_panic_hook` crate provides better debugging of panics by

mutiny-wasm/src/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ pub enum MutinyJsError {
168168
/// Token already spent.
169169
#[error("Token has been already spent.")]
170170
TokenAlreadySpent,
171+
#[error("Fedimint external note reissuance failed.")]
172+
FedimintReissueFailed,
171173
/// Unknown error.
172174
#[error("Unknown Error")]
173175
UnknownError,
@@ -238,6 +240,7 @@ impl From<MutinyError> for MutinyJsError {
238240
MutinyError::PayjoinConfigError => MutinyJsError::PayjoinConfigError,
239241
MutinyError::PayjoinCreateRequest => MutinyJsError::PayjoinCreateRequest,
240242
MutinyError::PayjoinResponse(e) => MutinyJsError::PayjoinResponse(e.to_string()),
243+
MutinyError::FedimintReissueFailed => MutinyJsError::FedimintReissueFailed,
241244
}
242245
}
243246
}

mutiny-wasm/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use bitcoin::hashes::sha256;
2424
use bitcoin::secp256k1::PublicKey;
2525
use bitcoin::{Address, Network, OutPoint, Transaction, Txid};
2626
use fedimint_core::{api::InviteCode, config::FederationId};
27+
use fedimint_mint_client::OOBNotes;
2728
use futures::lock::Mutex;
2829
use gloo_utils::format::JsValueSerdeExt;
2930
use hex_conservative::DisplayHex;
@@ -1022,6 +1023,17 @@ impl MutinyWallet {
10221023
Ok(self.inner.sweep_federation_balance(amount).await?.into())
10231024
}
10241025

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

0 commit comments

Comments
 (0)