Skip to content

Commit

Permalink
add list_transactions nwc command
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Apr 22, 2024
1 parent c4337d7 commit 4760ad1
Show file tree
Hide file tree
Showing 3 changed files with 379 additions and 159 deletions.
70 changes: 69 additions & 1 deletion mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ use ::nostr::nips::nip47::Method;
use ::nostr::nips::nip57;
#[cfg(target_arch = "wasm32")]
use ::nostr::prelude::rand::rngs::OsRng;
use ::nostr::prelude::ZapRequestData;
use ::nostr::prelude::{LookupInvoiceResponseResult, TransactionType, ZapRequestData};
#[cfg(target_arch = "wasm32")]
use ::nostr::Tag;
use ::nostr::{EventBuilder, EventId, JsonUtil, Keys, Kind};
Expand Down Expand Up @@ -146,6 +146,7 @@ pub trait InvoiceHandler {
fn get_network(&self) -> Network;
async fn get_best_block(&self) -> Result<BestBlock, MutinyError>;
async fn lookup_payment(&self, payment_hash: &[u8; 32]) -> Option<MutinyInvoice>;
async fn get_payments_by_label(&self, label: &str) -> Result<Vec<MutinyInvoice>, MutinyError>;
async fn pay_invoice(
&self,
invoice: &Bolt11Invoice,
Expand Down Expand Up @@ -427,6 +428,60 @@ impl From<Bolt11Invoice> for MutinyInvoice {
}
}

impl From<MutinyInvoice> for LookupInvoiceResponseResult {
fn from(invoice: MutinyInvoice) -> Self {
let transaction_type = if invoice.inbound {
Some(TransactionType::Incoming)
} else {
Some(TransactionType::Outgoing)
};

let (description, description_hash) = match invoice.bolt11.as_ref() {
None => (None, None),
Some(invoice) => match invoice.description() {
Bolt11InvoiceDescription::Direct(desc) => (Some(desc.to_string()), None),
Bolt11InvoiceDescription::Hash(hash) => (None, Some(hash.0.to_string())),
},
};

// try to get created_at from invoice,
// if it is not set, use last_updated as that's our closest approximation
let created_at = invoice
.bolt11
.as_ref()
.map(|b| b.duration_since_epoch().as_secs())
.unwrap_or(invoice.last_updated);

let settled_at = if invoice.status == HTLCStatus::Succeeded {
Some(invoice.last_updated)
} else {
None
};

// only reveal preimage if it is settled
let preimage = if invoice.status == HTLCStatus::Succeeded {
invoice.preimage
} else {
None
};

LookupInvoiceResponseResult {
transaction_type,
invoice: invoice.bolt11.map(|i| i.to_string()),
description,
description_hash,
preimage,
payment_hash: invoice.payment_hash.into_32().to_lower_hex_string(),
amount: invoice.amount_sats.map(|a| a * 1_000).unwrap_or(0),
fees_paid: invoice.fees_paid.map(|a| a * 1_000).unwrap_or(0),
created_at,
expires_at: invoice.expire,
settled_at,
metadata: Default::default(),
}
}
}

impl From<MutinyInvoice> for PaymentInfo {
fn from(invoice: MutinyInvoice) -> Self {
let preimage: Option<[u8; 32]> = invoice
Expand Down Expand Up @@ -3105,6 +3160,19 @@ impl<S: MutinyStorage> InvoiceHandler for MutinyWallet<S> {
.ok()
}

async fn get_payments_by_label(&self, label: &str) -> Result<Vec<MutinyInvoice>, MutinyError> {
let label_activity = self.get_label_activity(&label.to_string()).await?;
let mut invoices: Vec<MutinyInvoice> = Vec::with_capacity(label.len());

for item in label_activity {
if let ActivityItem::Lightning(mutiny_invoice) = item {
invoices.push(*mutiny_invoice);
}
}

Ok(invoices)
}

async fn pay_invoice(
&self,
invoice: &Bolt11Invoice,
Expand Down
1 change: 0 additions & 1 deletion mutiny-core/src/nostr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1541,7 +1541,6 @@ impl<S: MutinyStorage, P: PrimalApi, C: NostrClient> NostrManager<S, P, C> {

self.storage.set_nwc_sync_time(event.created_at.as_u64())?;

// TODO: handle nwc response here
if let Some(mut nwc) = nwc {
nwc.handle_nwc_request(event, invoice_handler, self).await?;
}
Expand Down
Loading

0 comments on commit 4760ad1

Please sign in to comment.