Skip to content

Commit

Permalink
chore: upgrade to Fedimint 0.2.0-rc2
Browse files Browse the repository at this point in the history
  • Loading branch information
elsirion committed Nov 26, 2023
1 parent b0dd885 commit 8bf93e8
Show file tree
Hide file tree
Showing 9 changed files with 560 additions and 266 deletions.
615 changes: 438 additions & 177 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ opt-level = 'z'
async-trait = "0.1.68"
anyhow = "1.0.71"
base64 = "0.21.3"
fedimint-client = { git = "https://github.com/fedimint/fedimint", tag = "v0.1.1" }
fedimint-core = { git = "https://github.com/fedimint/fedimint", tag = "v0.1.1" }
fedimint-wallet-client = { git = "https://github.com/fedimint/fedimint", tag = "v0.1.1" }
fedimint-mint-client = { git = "https://github.com/fedimint/fedimint", tag = "v0.1.1" }
fedimint-ln-client = { git = "https://github.com/fedimint/fedimint", tag = "v0.1.1" }
fedimint-client = "0.2.0-rc2"
fedimint-core = "0.2.0-rc2"
fedimint-wallet-client = "0.2.0-rc2"
fedimint-mint-client = "0.2.0-rc2"
fedimint-ln-client = "0.2.0-rc2"
futures = "0.3.28"
hex = "0.4.3"
leptos = { version = "0.4.8", features = ["csr"] }
leptos-qr-scanner = { git = "https://github.com/elsirion/leptos-qr-scanner", rev = "75e976e99d9c1ed64921081a23f7da823d2a0b6d" }
leptos_icons = { version = "0.0.15", features = ["macros", "BsLightningCharge", "FaCoinsSolid"] }
leptos_meta = { version = "0.4.8", features = ["csr"] }
lightning-invoice = { version = "0.21.0", features = [ "serde" ] }
lightning-invoice = { version = "0.26.0", features = [ "serde" ] }
qrcode-generator = "4.1.8"

console_error_panic_hook = "0.1.7"
Expand All @@ -40,6 +40,7 @@ wasm-bindgen = "0.2.87"
wasm-bindgen-futures = "0.4.37"
web-sys = { version = "0.3.65", features = [ "Navigator", "Window", "ServiceWorkerContainer" ] }
gloo-storage = "0.3.0"
rand = "0.8.5"

[patch.crates-io]
secp256k1-zkp = { git = "https://github.com/dpc/rust-secp256k1-zkp/", branch = "sanket-pr" }
Expand Down
115 changes: 75 additions & 40 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
use crate::db::PersistentMemDb;
use anyhow;
use fedimint_client::secret::PlainRootSecretStrategy;
use fedimint_client::sm::OperationId;
use fedimint_client::Client;
use fedimint_client::secret::{PlainRootSecretStrategy, RootSecretStrategy};
use fedimint_core::core::OperationId;
use fedimint_client::{Client, FederationInfo};
use fedimint_core::api::InviteCode;
use fedimint_core::db::IDatabase;
use fedimint_core::db::{IRawDatabase};
use fedimint_core::task::spawn;
use fedimint_core::util::BoxStream;
use fedimint_core::Amount;
use fedimint_ln_client::{LightningClientExt, LightningClientGen, LightningMeta};
use fedimint_mint_client::{MintClientExt, OOBNotes};
use fedimint_mint_client::{MintClientGen, MintMeta, MintMetaVariants};
use fedimint_wallet_client::WalletClientGen;
use fedimint_ln_client::{LightningClientInit, LightningClientModule, LightningOperationMeta, LightningOperationMetaPay, LightningOperationMetaVariant};
use fedimint_wallet_client::WalletClientInit;
use futures::StreamExt;
use leptos::warn;
use lightning_invoice::{Invoice, InvoiceDescription};
use lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription};
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter};
use std::str::FromStr;
use std::time::SystemTime;
use fedimint_mint_client::{MintClientInit, MintClientModule, MintOperationMeta, MintOperationMetaVariants, OOBNotes};
use thiserror::Error as ThisError;
use tokio::sync::{mpsc, oneshot, watch};
use fedimint_core::db::IDatabaseTransactionOpsCore;
use rand::thread_rng;
use tracing::{debug, info};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -157,13 +157,30 @@ async fn run_client(mut rpc: mpsc::Receiver<RpcCall>) {
};

let mut client_builder = fedimint_client::Client::builder();
client_builder.with_module(WalletClientGen(None));
client_builder.with_module(MintClientGen);
client_builder.with_module(LightningClientGen);
client_builder.with_database(wallet_db.clone());
client_builder.with_database(wallet_db.clone().into());

let client_secret = match client_builder
.load_decodable_client_secret::<[u8; 64]>()
.await
{
Ok(secret) => secret,
Err(_) => {
info!("Generating secret and writing to client storage");
let secret = PlainRootSecretStrategy::random(&mut thread_rng());
client_builder
.store_encodable_client_secret(secret)
.await
.expect("Failed to store client secret");
secret
}
};

client_builder.with_module(WalletClientInit(None));
client_builder.with_module(MintClientInit);
client_builder.with_module(LightningClientInit);
client_builder.with_primary_module(1);
client_builder.with_invite_code(invite_code);
let client_res = client_builder.build::<PlainRootSecretStrategy>().await;
client_builder.with_federation_info(FederationInfo::from_invite_code(invite_code).await.unwrap());
let client_res = client_builder.build(PlainRootSecretStrategy::to_root_secret(&client_secret)).await;

match client_res {
Ok(client) => {
Expand All @@ -183,13 +200,30 @@ async fn run_client(mut rpc: mpsc::Receiver<RpcCall>) {
} else {
// TODO: dedup
let mut client_builder = fedimint_client::Client::builder();
client_builder.with_module(WalletClientGen(None));
client_builder.with_module(MintClientGen);
client_builder.with_module(LightningClientGen);
client_builder.with_database(wallet_db);
client_builder.with_database(wallet_db.into());

let client_secret = match client_builder
.load_decodable_client_secret::<[u8; 64]>()
.await
{
Ok(secret) => secret,
Err(_) => {
info!("Generating secret and writing to client storage");
let secret = PlainRootSecretStrategy::random(&mut thread_rng());
client_builder
.store_encodable_client_secret(secret)
.await
.expect("Failed to store client secret");
secret
}
};

client_builder.with_module(WalletClientInit(None));
client_builder.with_module(MintClientInit);
client_builder.with_module(LightningClientInit);
client_builder.with_primary_module(1);
client_builder
.build::<PlainRootSecretStrategy>()
.build(PlainRootSecretStrategy::to_root_secret(&client_secret))
.await
.unwrap()
};
Expand Down Expand Up @@ -222,15 +256,15 @@ async fn run_client(mut rpc: mpsc::Receiver<RpcCall>) {
info!("Receiving notes: \"{notes}\"");
let notes: OOBNotes = notes.parse()?;
let amount = notes.total_amount();
client.reissue_external_notes(notes, ()).await?;
client.get_first_module::<MintClientModule>().reissue_external_notes(notes, ()).await?;
Ok(RpcResponse::Receive(amount))
}
let _ = response_sender
.send(receive_inner(client, &notes).await)
.map_err(|_| warn!("RPC receiver dropped before response was sent"));
}
RpcRequest::LnSend(invoice) => {
let invoice = match Invoice::from_str(&invoice) {
let invoice = match Bolt11Invoice::from_str(&invoice) {
Ok(invoice) => invoice,
Err(e) => {
let _ = response_sender
Expand All @@ -243,7 +277,8 @@ async fn run_client(mut rpc: mpsc::Receiver<RpcCall>) {
let _ = response_sender
.send(
client
.pay_bolt11_invoice(invoice)
.get_first_module::<LightningClientModule>()
.pay_bolt11_invoice(invoice, ())
.await
.map(|_| RpcResponse::LnSend),
)
Expand All @@ -253,8 +288,8 @@ async fn run_client(mut rpc: mpsc::Receiver<RpcCall>) {
amount,
description,
} => {
let (operation_id, invoice) = match client
.create_bolt11_invoice(amount, description, None)
let (operation_id, invoice) = match client.get_first_module::<LightningClientModule>()
.create_bolt11_invoice(amount, description, None, ())
.await
{
Ok(res) => res,
Expand All @@ -267,12 +302,12 @@ async fn run_client(mut rpc: mpsc::Receiver<RpcCall>) {
};

let (await_paid_sender, await_paid_receiver) = watch::channel(false);
let mut subscription = client
let mut subscription = client.get_first_module::<LightningClientModule>()
.subscribe_ln_receive(operation_id)
.await
.expect("subscribing to a just created operation can't fail")
.into_stream();
spawn(async move {
spawn("waiting for invoice being paid", async move {
while let Some(state) = subscription.next().await {
if state == fedimint_ln_client::LnReceiveState::Funded {
let _ = await_paid_sender.send(true);
Expand All @@ -294,44 +329,44 @@ async fn run_client(mut rpc: mpsc::Receiver<RpcCall>) {
.await
.into_iter()
.map(|(key, op_log)| {
let (amount_msat, description) = match op_log.operation_type() {
let (amount_msat, description) = match op_log.operation_module_kind() {
"mint" => {
let meta = op_log.meta::<MintMeta>();
let meta = op_log.meta::<MintOperationMeta>();
match meta.variant {
MintMetaVariants::Reissuance { .. } => {
MintOperationMetaVariants::Reissuance { .. } => {
(meta.amount.msats as i64, None)
}
MintMetaVariants::SpendOOB { .. } => {
MintOperationMetaVariants::SpendOOB { .. } => {
(-(meta.amount.msats as i64), None)
}
}
}
"ln" => match op_log.meta::<LightningMeta>() {
LightningMeta::Receive { invoice, .. } => {
"ln" => match op_log.meta::<LightningOperationMeta>().variant {
LightningOperationMetaVariant::Receive { invoice, .. } => {
let amount = invoice
.amount_milli_satoshis()
.expect("We don't create 0 amount invoices")
as i64;
let description = match invoice.description() {
InvoiceDescription::Direct(description) => {
Bolt11InvoiceDescription::Direct(description) => {
Some(description.to_string())
}
InvoiceDescription::Hash(_) => None,
Bolt11InvoiceDescription::Hash(_) => None,
};

(amount, description)
}
LightningMeta::Pay { invoice, .. } => {
LightningOperationMetaVariant::Pay(LightningOperationMetaPay {invoice, ..}) => {
// TODO: add fee
let amount = -(invoice
.amount_milli_satoshis()
.expect("Can't pay 0 amount invoices")
as i64);
let description = match invoice.description() {
InvoiceDescription::Direct(description) => {
Bolt11InvoiceDescription::Direct(description) => {
Some(description.to_string())
}
InvoiceDescription::Hash(_) => None,
Bolt11InvoiceDescription::Hash(_) => None,
};

(amount, description)
Expand All @@ -343,7 +378,7 @@ async fn run_client(mut rpc: mpsc::Receiver<RpcCall>) {
Transaction {
timestamp: key.creation_time,
operation_id: key.operation_id,
operation_kind: op_log.operation_type().to_owned(),
operation_kind: op_log.operation_module_kind().to_owned(),
amount_msat,
description,
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/qrcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn QrCode(
let qr_image_size = qr_image_size.unwrap_or(1024);
let qr_data_url = move || {
let png_bytes = qrcode_generator::to_png_to_vec_from_str(
&data.get(),
data.get(),
qrcode_generator::QrCodeEcc::Medium,
qr_image_size,
)
Expand Down
1 change: 0 additions & 1 deletion src/components/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::context::ClientContext;
pub fn Receive(cx: Scope) -> impl IntoView {
let ClientContext { client, .. } = expect_context::<ClientContext>(cx);

let client = client.clone();
let submit_action = create_action(cx, move |invoice: &String| {
let invoice = invoice.clone();
async move { client.get_value().receive(invoice).await }
Expand Down
1 change: 0 additions & 1 deletion src/components/receive_ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use leptos::*;
pub fn ReceiveLn(cx: Scope) -> impl IntoView {
let ClientContext { client, .. } = expect_context::<ClientContext>(cx);

let client = client.clone();
let submit_action = create_action(cx, move |(amount_msat, description): &(u64, String)| {
let description = description.clone();
let amount_msat = *amount_msat;
Expand Down
1 change: 0 additions & 1 deletion src/components/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::context::ClientContext;
pub fn Send(cx: Scope) -> impl IntoView {
let ClientContext { client, .. } = expect_context::<ClientContext>(cx);

let client = client.clone();
let submit_action = create_action(cx, move |invoice: &String| {
let invoice = invoice.clone();
async move { client.get_value().ln_send(invoice).await }
Expand Down
2 changes: 1 addition & 1 deletion src/components/tx_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::context::ClientContext;
#[component]
pub fn TxList<F>(cx: Scope, update_signal: F) -> impl IntoView
where
F: Fn() -> () + Copy + 'static,
F: Fn() + Copy + 'static,
{
let ClientContext { client, .. } = expect_context::<ClientContext>(cx);

Expand Down
Loading

0 comments on commit 8bf93e8

Please sign in to comment.