Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fedimint to 0.4.2 #90

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,145 changes: 608 additions & 537 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ default = []
vendored = ["rusqlite/bundled-sqlcipher-vendored-openssl"]

[dependencies]
anyhow = "1"
anyhow = "1.0.86"
log = "0.4"
pretty_env_logger = "0.5" # todo swap to a file logger
iced = { git = "https://github.com/iced-rs/iced", rev = "b30d34f", features = ["debug", "tokio", "svg", "qr_code", "advanced"] }
Expand All @@ -26,16 +26,18 @@ diesel_migrations = { version = "2.1.0", features = ["sqlite"] }
uuid = { version = "1.8", features = ["v4"] }
async-trait = "0.1.77"
bincode = "1.3.3"
hex = "0.4.3"

bitcoin = { version = "0.29.2", features = ["base64"] }
bitcoin = { version = "0.30.2", features = ["base64"] }
bip39 = "2.0.0"
fedimint-client = "0.3.1"
fedimint-core = "0.3.1"
fedimint-wallet-client = "0.3.1"
fedimint-mint-client = "0.3.1"
fedimint-ln-client = "0.3.1"
fedimint-bip39 = "0.3.1"
fedimint-ln-common = "0.3.1"
fedimint-api-client = "0.4.2"
fedimint-client = "0.4.2"
fedimint-core = "0.4.2"
fedimint-wallet-client = "0.4.2"
fedimint-mint-client = "0.4.2"
fedimint-ln-client = "0.4.2"
fedimint-bip39 = "0.4.2"
fedimint-ln-common = "0.4.2"

[dev-dependencies]
tempdir = "0.3.7"
12 changes: 9 additions & 3 deletions src/bridge.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::components::{FederationItem, TransactionItem};
use bitcoin::address::NetworkUnchecked;
use bitcoin::{Address, Txid};
use fedimint_core::api::InviteCode;
use fedimint_core::config::ClientConfig;
use fedimint_core::invite_code::InviteCode;
use fedimint_core::Amount;
use fedimint_ln_common::lightning_invoice::Bolt11Invoice;
use tokio::sync::mpsc;
Expand All @@ -18,7 +19,7 @@ pub enum UICoreMsg {
SendLightning(Bolt11Invoice),
ReceiveLightning(Amount),
SendOnChain {
address: Address,
address: Address<NetworkUnchecked>,
amount_sats: Option<u64>,
},
ReceiveOnChain,
Expand Down Expand Up @@ -102,7 +103,12 @@ impl UIHandle {
.await;
}

pub async fn send_onchain(&self, id: Uuid, address: Address, amount_sats: Option<u64>) {
pub async fn send_onchain(
&self,
id: Uuid,
address: Address<NetworkUnchecked>,
amount_sats: Option<u64>,
) {
self.msg_send(UICoreMsgPacket {
msg: UICoreMsg::SendOnChain {
address,
Expand Down
1 change: 1 addition & 0 deletions src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn data_dir(network: Network) -> PathBuf {
Network::Testnet => default.join("testnet3"),
Network::Regtest => default.join("regtest"),
Network::Signet => default.join("signet"),
_ => panic!("Invalid network"),
}
}

Expand Down
17 changes: 7 additions & 10 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anyhow::anyhow;
use bip39::Mnemonic;
use bitcoin::address::NetworkUnchecked;
use bitcoin::{Address, Network};
use fedimint_core::api::InviteCode;
use fedimint_core::config::{ClientConfig, FederationId};
use fedimint_core::invite_code::InviteCode;
use fedimint_core::Amount;
use fedimint_ln_client::{LightningClientModule, PayType};
use fedimint_ln_common::config::FeeToAmount;
Expand All @@ -13,7 +14,7 @@ use std::path::PathBuf;
use std::str::FromStr;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime};
use std::time::{Duration, Instant};

use iced::{
futures::{channel::mpsc::Sender, SinkExt},
Expand Down Expand Up @@ -43,7 +44,6 @@ use crate::{
},
};

const PEG_IN_TIMEOUT_YEAR: Duration = Duration::from_secs(86400 * 365);
const HARBOR_FILE_NAME: &str = "harbor.sqlite";

#[derive(Clone)]
Expand Down Expand Up @@ -206,7 +206,7 @@ impl HarborCore {
async fn send_onchain(
&self,
msg_id: Uuid,
address: Address,
address: Address<NetworkUnchecked>,
sats: Option<u64>,
) -> anyhow::Result<()> {
// todo go through all clients and select the first one that has enough balance
Expand Down Expand Up @@ -276,15 +276,12 @@ impl HarborCore {
let client = self.get_client().await.fedimint_client;
let onchain = client.get_first_module::<WalletClientModule>();

// expire the address in 1 year
let valid_until = SystemTime::now() + PEG_IN_TIMEOUT_YEAR;

let (op_id, address) = onchain.get_deposit_address(valid_until, ()).await?;
let (op_id, address, _) = onchain.allocate_deposit_address_expert_only(()).await?;

self.storage
.create_onchain_receive(op_id, client.federation_id(), address.clone())?;

let sub = onchain.subscribe_deposit_updates(op_id).await?;
let sub = onchain.subscribe_deposit(op_id).await?;

spawn_onchain_receive_subscription(
self.tx.clone(),
Expand All @@ -301,7 +298,7 @@ impl HarborCore {

async fn get_federation_info(&self, invite_code: InviteCode) -> anyhow::Result<ClientConfig> {
let download = Instant::now();
let config = ClientConfig::download_from_invite_code(&invite_code)
let config = fedimint_api_client::download_from_invite_code(&invite_code)
.await
.map_err(|e| {
error!("Could not download federation info: {e}");
Expand Down
12 changes: 8 additions & 4 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::db_models::{
Fedimint, LightningPayment, LightningReceive, NewFedimint, NewProfile, OnChainPayment,
OnChainReceive, Profile,
};
use bitcoin::address::NetworkUnchecked;
use bitcoin::{Address, Txid};
use diesel::{
connection::SimpleConnection,
Expand All @@ -19,6 +20,7 @@ use std::{sync::Arc, time::Duration};

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();

#[allow(dead_code)]
pub(crate) fn check_password(url: &str, password: &str) -> anyhow::Result<()> {
let conn = Connection::open_with_flags(
url,
Expand Down Expand Up @@ -114,7 +116,7 @@ pub trait DBConnection {
&self,
operation_id: OperationId,
fedimint_id: FederationId,
address: Address,
address: Address<NetworkUnchecked>,
amount_sats: u64,
fee_sats: u64,
) -> anyhow::Result<()>;
Expand Down Expand Up @@ -277,7 +279,7 @@ impl DBConnection for SQLConnection {
&self,
operation_id: OperationId,
fedimint_id: FederationId,
address: Address,
address: Address<NetworkUnchecked>,
amount_sats: u64,
fee_sats: u64,
) -> anyhow::Result<()> {
Expand Down Expand Up @@ -675,7 +677,9 @@ mod tests {
let mut conn = pool.get().unwrap();

let operation_id = OperationId::new_random();
let address = Address::from_str("tb1qd28npep0s8frcm3y7dxqajkcy2m40eysplyr9v").unwrap();
let address = Address::from_str("tb1qd28npep0s8frcm3y7dxqajkcy2m40eysplyr9v")
.unwrap()
.assume_checked();

let amount: u64 = 10_000;
let fee: u64 = 200;
Expand All @@ -697,7 +701,7 @@ mod tests {
payment.fedimint_id(),
FederationId::from_str(FEDERATION_ID).unwrap()
);
assert_eq!(payment.address(), address);
assert_eq!(payment.address().assume_checked(), address);
assert!(payment.amount_sats.is_none());
assert!(payment.fee_sats.is_none());
assert_eq!(payment.txid(), None);
Expand Down
14 changes: 7 additions & 7 deletions src/db_models/lightning_payment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::components::{TransactionDirection, TransactionItem, TransactionItemKind};
use crate::db_models::schema::lightning_payments;
use crate::db_models::PaymentStatus;
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::hashes::hex::FromHex;
use diesel::prelude::*;
use fedimint_core::config::FederationId;
use fedimint_core::core::OperationId;
Expand Down Expand Up @@ -87,9 +87,9 @@ impl LightningPayment {
return Err(anyhow::anyhow!("Internal error: amount mismatch"));
}

let payment_hash = bolt11.payment_hash().to_hex();
let payment_hash = bolt11.payment_hash().to_string();
let new = NewLightningPayment {
operation_id: operation_id.to_string(),
operation_id: operation_id.fmt_full().to_string(),
fedimint_id: fedimint_id.to_string(),
payment_hash,
bolt11: bolt11.to_string(),
Expand All @@ -110,7 +110,7 @@ impl LightningPayment {
operation_id: OperationId,
) -> anyhow::Result<Option<Self>> {
Ok(lightning_payments::table
.filter(lightning_payments::operation_id.eq(operation_id.to_string()))
.filter(lightning_payments::operation_id.eq(operation_id.fmt_full().to_string()))
.first::<Self>(conn)
.optional()?)
}
Expand All @@ -122,10 +122,10 @@ impl LightningPayment {
) -> anyhow::Result<()> {
diesel::update(
lightning_payments::table
.filter(lightning_payments::operation_id.eq(operation_id.to_string())),
.filter(lightning_payments::operation_id.eq(operation_id.fmt_full().to_string())),
)
.set((
lightning_payments::preimage.eq(Some(preimage.to_hex())),
lightning_payments::preimage.eq(Some(hex::encode(preimage))),
lightning_payments::status.eq(PaymentStatus::Success as i32),
))
.execute(conn)?;
Expand All @@ -139,7 +139,7 @@ impl LightningPayment {
) -> anyhow::Result<()> {
diesel::update(
lightning_payments::table
.filter(lightning_payments::operation_id.eq(operation_id.to_string())),
.filter(lightning_payments::operation_id.eq(operation_id.fmt_full().to_string())),
)
.set(lightning_payments::status.eq(PaymentStatus::Failed as i32))
.execute(conn)?;
Expand Down
14 changes: 7 additions & 7 deletions src/db_models/lightning_receive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::components::{TransactionDirection, TransactionItem, TransactionItemKind};
use crate::db_models::schema::lightning_receives;
use crate::db_models::PaymentStatus;
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::hashes::hex::FromHex;
use diesel::prelude::*;
use fedimint_core::config::FederationId;
use fedimint_core::core::OperationId;
Expand Down Expand Up @@ -87,15 +87,15 @@ impl LightningReceive {
return Err(anyhow::anyhow!("Internal error: amount mismatch"));
}

let payment_hash = bolt11.payment_hash().to_hex();
let payment_hash = bolt11.payment_hash().to_string();
let new = NewLightningReceive {
operation_id: operation_id.to_string(),
operation_id: operation_id.fmt_full().to_string(),
fedimint_id: fedimint_id.to_string(),
payment_hash,
bolt11: bolt11.to_string(),
amount_msats: amount.msats as i64,
fee_msats: fee.msats as i64,
preimage: preimage.to_hex(),
preimage: hex::encode(preimage),
status: PaymentStatus::Pending as i32,
};

Expand All @@ -111,7 +111,7 @@ impl LightningReceive {
operation_id: OperationId,
) -> anyhow::Result<Option<Self>> {
Ok(lightning_receives::table
.filter(lightning_receives::operation_id.eq(operation_id.to_string()))
.filter(lightning_receives::operation_id.eq(operation_id.fmt_full().to_string()))
.first::<Self>(conn)
.optional()?)
}
Expand All @@ -122,7 +122,7 @@ impl LightningReceive {
) -> anyhow::Result<()> {
diesel::update(
lightning_receives::table
.filter(lightning_receives::operation_id.eq(operation_id.to_string())),
.filter(lightning_receives::operation_id.eq(operation_id.fmt_full().to_string())),
)
.set(lightning_receives::status.eq(PaymentStatus::Success as i32))
.execute(conn)?;
Expand All @@ -136,7 +136,7 @@ impl LightningReceive {
) -> anyhow::Result<()> {
diesel::update(
lightning_receives::table
.filter(lightning_receives::operation_id.eq(operation_id.to_string())),
.filter(lightning_receives::operation_id.eq(operation_id.fmt_full().to_string())),
)
.set(lightning_receives::status.eq(PaymentStatus::Failed as i32))
.execute(conn)?;
Expand Down
20 changes: 10 additions & 10 deletions src/db_models/onchain_payment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::components::{TransactionDirection, TransactionItem, TransactionItemKind};
use crate::db_models::schema::on_chain_payments;
use crate::db_models::PaymentStatus;
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::address::NetworkUnchecked;
use bitcoin::{Address, Txid};
use diesel::prelude::*;
use fedimint_core::config::FederationId;
Expand Down Expand Up @@ -42,14 +42,14 @@ impl OnChainPayment {
FederationId::from_str(&self.fedimint_id).expect("invalid fedimint id")
}

pub fn address(&self) -> Address {
pub fn address(&self) -> Address<NetworkUnchecked> {
Address::from_str(&self.address).expect("invalid address")
}

pub fn txid(&self) -> Option<Txid> {
self.txid
.as_ref()
.map(|p| FromHex::from_hex(p).expect("invalid txid"))
.map(|p| Txid::from_str(p).expect("invalid txid"))
}

pub fn status(&self) -> PaymentStatus {
Expand All @@ -60,14 +60,14 @@ impl OnChainPayment {
conn: &mut SqliteConnection,
operation_id: OperationId,
fedimint_id: FederationId,
address: Address,
address: Address<NetworkUnchecked>,
amount_sats: u64,
fee_sats: u64,
) -> anyhow::Result<()> {
let new = NewOnChainPayment {
operation_id: operation_id.to_string(),
operation_id: operation_id.fmt_full().to_string(),
fedimint_id: fedimint_id.to_string(),
address: address.to_string(),
address: address.assume_checked().to_string(),
amount_sats: amount_sats as i64,
fee_sats: fee_sats as i64,
status: PaymentStatus::Pending as i32,
Expand All @@ -85,7 +85,7 @@ impl OnChainPayment {
operation_id: OperationId,
) -> anyhow::Result<Option<Self>> {
Ok(on_chain_payments::table
.filter(on_chain_payments::operation_id.eq(operation_id.to_string()))
.filter(on_chain_payments::operation_id.eq(operation_id.fmt_full().to_string()))
.first::<Self>(conn)
.optional()?)
}
Expand All @@ -97,10 +97,10 @@ impl OnChainPayment {
) -> anyhow::Result<()> {
diesel::update(
on_chain_payments::table
.filter(on_chain_payments::operation_id.eq(operation_id.to_string())),
.filter(on_chain_payments::operation_id.eq(operation_id.fmt_full().to_string())),
)
.set((
on_chain_payments::txid.eq(Some(txid.to_hex())),
on_chain_payments::txid.eq(Some(txid.to_string())),
// fedimint doesn't tell us when the tx is confirmed so just jump to success
on_chain_payments::status.eq(PaymentStatus::Success as i32),
))
Expand All @@ -115,7 +115,7 @@ impl OnChainPayment {
) -> anyhow::Result<()> {
diesel::update(
on_chain_payments::table
.filter(on_chain_payments::operation_id.eq(operation_id.to_string())),
.filter(on_chain_payments::operation_id.eq(operation_id.fmt_full().to_string())),
)
.set(on_chain_payments::status.eq(PaymentStatus::Failed as i32))
.execute(conn)?;
Expand Down
Loading