diff --git a/Cargo.lock b/Cargo.lock index 7f815ed04..6332ddd6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2525,7 +2525,6 @@ dependencies = [ "getrandom", "gloo-net", "hex-conservative", - "instant", "itertools 0.11.0", "js-sys", "jwt-compact", @@ -2555,6 +2554,7 @@ dependencies = [ "wasm-bindgen-futures", "wasm-bindgen-test", "web-sys", + "web-time", ] [[package]] @@ -4597,6 +4597,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "webpki" version = "0.22.4" diff --git a/mutiny-core/Cargo.toml b/mutiny-core/Cargo.toml index 3a04d21df..c68c18c8d 100644 --- a/mutiny-core/Cargo.toml +++ b/mutiny-core/Cargo.toml @@ -48,6 +48,7 @@ payjoin = { version = "0.13.0", features = ["send", "base64"] } bincode = "1.3.3" hex-conservative = "0.1.1" async-lock = "3.2.0" +web-time = "1.1" fedimint-client = { git = "https://github.com/fedimint/fedimint", rev = "5ade2536015a12a7e003a42b159ccc4a431e1a32" } fedimint-core = { git = "https://github.com/fedimint/fedimint", rev = "5ade2536015a12a7e003a42b159ccc4a431e1a32" } @@ -81,7 +82,6 @@ wasm-bindgen-futures = { version = "0.4.38" } web-sys = { version = "0.3.65", features = ["console"] } js-sys = "0.3.65" gloo-net = { version = "0.4.0" } -instant = { version = "0.1", features = ["wasm-bindgen"] } getrandom = { version = "0.2", features = ["js"] } # add nip07 feature for wasm32 nostr = { version = "0.29.0", default-features = false, features = ["nip04", "nip05", "nip07", "nip47", "nip57"] } diff --git a/mutiny-core/src/federation.rs b/mutiny-core/src/federation.rs index 64a7cd786..a5abef304 100644 --- a/mutiny-core/src/federation.rs +++ b/mutiny-core/src/federation.rs @@ -57,20 +57,17 @@ use fedimint_wallet_client::{WalletClientInit, WalletClientModule}; use futures::future::{self}; use futures_util::{pin_mut, StreamExt}; use hex_conservative::{DisplayHex, FromHex}; -#[cfg(target_arch = "wasm32")] -use instant::Instant; use lightning::{ ln::PaymentHash, log_debug, log_error, log_info, log_trace, log_warn, util::logger::Logger, }; use lightning_invoice::Bolt11Invoice; use serde::{de::DeserializeOwned, Deserialize, Serialize}; -#[cfg(not(target_arch = "wasm32"))] -use std::time::Instant; use std::{collections::HashMap, fmt::Debug, sync::Arc}; use std::{ str::FromStr, sync::atomic::{AtomicU32, Ordering}, }; +use web_time::Instant; // The amount of time in milliseconds to wait for // checking the status of a fedimint payment. This diff --git a/mutiny-core/src/gossip.rs b/mutiny-core/src/gossip.rs index cfc52f1d8..6e984e5a2 100644 --- a/mutiny-core/src/gossip.rs +++ b/mutiny-core/src/gossip.rs @@ -5,8 +5,6 @@ use crate::{ use bitcoin::hashes::hex::FromHex; use bitcoin::Network; use hex_conservative::DisplayHex; -#[cfg(target_arch = "wasm32")] -use instant::Instant; use lightning::ln::msgs::NodeAnnouncement; use lightning::routing::gossip::NodeId; use lightning::util::logger::Logger; @@ -18,8 +16,7 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::str::FromStr; use std::sync::Arc; -#[cfg(not(target_arch = "wasm32"))] -use std::time::Instant; +use web_time::Instant; use crate::logging::MutinyLogger; use crate::node::{NetworkGraph, RapidGossipSync}; diff --git a/mutiny-core/src/lib.rs b/mutiny-core/src/lib.rs index 91a9a7743..d636acca9 100644 --- a/mutiny-core/src/lib.rs +++ b/mutiny-core/src/lib.rs @@ -96,8 +96,6 @@ use fedimint_core::{api::InviteCode, config::FederationId}; use futures::{pin_mut, select, FutureExt}; use futures_util::join; use hex_conservative::{DisplayHex, FromHex}; -#[cfg(target_arch = "wasm32")] -use instant::Instant; use itertools::Itertools; use lightning::chain::BestBlock; use lightning::ln::PaymentHash; @@ -115,11 +113,10 @@ use reqwest::multipart::{Form, Part}; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::sync::Arc; -#[cfg(not(target_arch = "wasm32"))] -use std::time::Instant; use std::{collections::HashMap, sync::atomic::AtomicBool}; use std::{str::FromStr, sync::atomic::Ordering}; use uuid::Uuid; +use web_time::Instant; use crate::labels::LabelItem; use crate::nostr::NostrKeySource; diff --git a/mutiny-core/src/node.rs b/mutiny-core/src/node.rs index 51a77fc22..a5743e7d8 100644 --- a/mutiny-core/src/node.rs +++ b/mutiny-core/src/node.rs @@ -39,8 +39,6 @@ use core::time::Duration; use esplora_client::AsyncClient; use futures_util::lock::Mutex; use hex_conservative::DisplayHex; -#[cfg(target_arch = "wasm32")] -use instant::Instant; use lightning::events::bump_transaction::{BumpTransactionEventHandler, Wallet}; use lightning::ln::channelmanager::ChannelDetails; use lightning::ln::PaymentSecret; @@ -83,8 +81,6 @@ use lightning_liquidity::{LiquidityClientConfig, LiquidityManager as LDKLSPLiqui #[cfg(test)] use mockall::predicate::*; use std::collections::HashMap; -#[cfg(not(target_arch = "wasm32"))] -use std::time::Instant; use std::{ str::FromStr, sync::{ @@ -92,6 +88,7 @@ use std::{ Arc, RwLock, }, }; +use web_time::Instant; const INITIAL_RECONNECTION_DELAY: u64 = 10; const MAX_RECONNECTION_DELAY: u64 = 60; diff --git a/mutiny-core/src/nodemanager.rs b/mutiny-core/src/nodemanager.rs index 98f480c3f..f3c3a3525 100644 --- a/mutiny-core/src/nodemanager.rs +++ b/mutiny-core/src/nodemanager.rs @@ -41,8 +41,6 @@ use core::time::Duration; use esplora_client::{AsyncClient, Builder}; use futures::{future::join_all, lock::Mutex}; use hex_conservative::DisplayHex; -#[cfg(target_arch = "wasm32")] -use instant::Instant; use lightning::chain::Confirm; use lightning::events::ClosureReason; use lightning::ln::channelmanager::{ChannelDetails, PhantomRouteHints}; @@ -62,9 +60,8 @@ use std::cmp::max; use std::io::Cursor; use std::str::FromStr; use std::sync::atomic::{AtomicBool, Ordering}; -#[cfg(not(target_arch = "wasm32"))] -use std::time::Instant; use std::{collections::HashMap, ops::Deref, sync::Arc}; +use web_time::Instant; const BITCOIN_PRICE_CACHE_SEC: u64 = 300; pub const DEVICE_LOCK_INTERVAL_SECS: u64 = 30; diff --git a/mutiny-core/src/utils.rs b/mutiny-core/src/utils.rs index 1b8cf4f9d..211108d13 100644 --- a/mutiny-core/src/utils.rs +++ b/mutiny-core/src/utils.rs @@ -48,15 +48,9 @@ pub async fn sleep(millis: i32) { } } pub fn now() -> Duration { - #[cfg(target_arch = "wasm32")] - return instant::SystemTime::now() - .duration_since(instant::SystemTime::UNIX_EPOCH) - .unwrap(); - - #[cfg(not(target_arch = "wasm32"))] - return std::time::SystemTime::now() - .duration_since(std::time::SystemTime::UNIX_EPOCH) - .unwrap(); + web_time::SystemTime::now() + .duration_since(web_time::SystemTime::UNIX_EPOCH) + .unwrap() } pub async fn fetch_with_timeout(