diff --git a/CHANGELOG.md b/CHANGELOG.md index 60924ddac..16c2c3f68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,14 @@ What a version number promises before 1.0 is described in [docs/VERSIONING.md](d ## [Unreleased] +### Fixed + +- **InstantSend proofs on testnet.** Creating or topping up an identity, in the web app or with `dg auth new`, waited on `getislocks` at `trpc.digitalcash.dev`, which no longer follows testnet, so every lock fell back to a slower chain-lock proof. The InstantSend lock now comes off DAPI `subscribeToTransactionsWithProofs` feeds on three evonodes, opened before the asset lock is broadcast, as in govhub and mainnet-bridge. `--islock-rpc` and `islock_rpc` still add an RPC of your choice, but none is set by default. + +### Changed + +- **The web app asks only the Dash network's evonodes** for an identity deposit: the Insight fallback and the **Fallback block explorer** setting are gone. It watches the deposit on two nodes at once, and moves a feed that fails or goes silent to another node. + ## [0.2.0] - TODO(owner): the tagging date Dash Forge moves to **testnet**: its four contracts are registered there, and testnet is now the supported network for `dg`, `git-remote-dash`, the web app and the Actions. Devnet sakura, which v0.1.0 targeted, is down, and nothing on it was carried over. See the [release notes](docs/releases/v0.2.0.md) for networks, upgrading and known limitations. diff --git a/crates/dg/src/auth/new.rs b/crates/dg/src/auth/new.rs index 4f8cebdcf..1865a5f34 100644 --- a/crates/dg/src/auth/new.rs +++ b/crates/dg/src/auth/new.rs @@ -9,9 +9,12 @@ //! only when the feed is idle or unavailable, and its outputs are re-read from their raw //! transactions too. //! 4. A type-8 asset lock spends the deposit to a credit output; it is saved to the journal -//! before it is broadcast (DAPI first, the explorer as fallback). -//! 5. The lock is proven: an InstantSend lock where the network offers one, else a chain lock -//! once DAPI reports the transaction mined and Platform's chain-locked height reaches it. +//! before it is broadcast (DAPI first, the explorer as fallback). Before the broadcast, a +//! `subscribeToTransactionsWithProofs` stream filtered on the lock's txid and inputs opens +//! on up to three evonodes: they send its InstantSend lock once, live. +//! 5. The lock is proven: with the InstantSend lock when one arrives within a minute of the +//! broadcast, else (or when it was mined already) with a chain lock once DAPI reports the +//! transaction mined and Platform's chain-locked height reaches it. //! 6. One IdentityCreate registers the canonical keys (MASTER, HIGH, CRITICAL, TRANSFER, //! ENCRYPTION — the same DIP-13 keys the bridge and the web app derive) plus this computer's //! limited key, which is stored **before** the identity exists, so an interruption can never @@ -28,7 +31,7 @@ use anyhow::{Context as _, Result}; use serde::{Deserialize, Serialize}; use serde_json::json; -use forge_core::funding::{self, fetch_islock, CoreChain as _, CoreEndpoints, Insight}; +use forge_core::funding::{self, CoreChain as _, CoreEndpoints, Insight, LockRoute}; use forge_core::keystore::{BridgeIdentity, Secret}; use forge_core::platform::identity::{ self, build_asset_lock, identity_id_for, FreshKey, LimitedKeySpec, LockProof, NewIdentityKeys, @@ -50,8 +53,9 @@ use crate::secret_out::{Stream, Surroundings}; const DEPOSIT_WAIT: Duration = Duration::from_mins(30); /// How long to wait for the asset lock to become provable. const PROOF_WAIT: Duration = Duration::from_mins(20); -/// How long to try for an InstantSend lock before falling back to a chain lock. -const ISLOCK_WAIT: Duration = Duration::from_secs(90); +/// How long after the broadcast to wait for an InstantSend lock before falling back to a +/// chain lock (one normally arrives within seconds). +const ISLOCK_WAIT: Duration = Duration::from_secs(60); /// `dg auth new` arguments. #[derive(Debug, clap::Args)] @@ -67,9 +71,9 @@ pub struct NewArgs { /// Fallback block explorer (Insight API base URL), asked only when DAPI cannot answer. #[arg(long, value_name = "URL")] pub explorer: Option, - /// JSON-RPC (`getislocks`) to ask for an InstantSend proof of the asset lock, or `off` to - /// prove with a chain lock only. Default: `islock_rpc` in config.toml, else the network's - /// (testnet: the third-party https://trpc.digitalcash.dev, which sees the asset-lock txid). + /// An extra JSON-RPC (`getislocks`) asked for the asset lock's InstantSend lock beside the + /// DAPI stream (it sees the asset-lock txid), or `off` for the stream only. Default: + /// `islock_rpc` in config.toml, else none. #[arg(long, value_name = "URL")] pub islock_rpc: Option, /// Resume an interrupted creation (asks for the words again). @@ -342,26 +346,14 @@ async fn wait_for_deposit( }) } -/// Broadcast the asset lock: DAPI first, the explorer as fallback; an "already known" answer -/// from either is success. -async fn broadcast( - client: &PlatformClient, - insight: &Insight, - lock: &SignedAssetLock, -) -> Result<()> { - funding::broadcast(client, Some(insight), &lock.raw, &lock.txid) - .await - .context("broadcasting the asset lock") -} - /// The funding endpoints for this run: the network's, with the explorer from `--explorer` and /// the InstantSend RPC from `--islock-rpc`, else `islock_rpc` in config.toml. /// A value that is not an https URL (or `off`) is a usage error, before anything is done. fn endpoints_for(ctx: &Ctx, args: &NewArgs) -> Result { let (islock, from) = match &args.islock_rpc { Some(v) => (Some(v.clone()), "--islock-rpc"), - // An unreadable config.toml is an error, never the default RPC: `islock_rpc = "off"` - // must not quietly turn back into a third-party endpoint. + // An unreadable config.toml is an error, never the default: the user's `islock_rpc` + // must not be quietly dropped. None => ( crate::config::Config::load()?.islock_rpc, "islock_rpc in config.toml", @@ -381,8 +373,10 @@ fn instant_proof_refused(error: &str) -> bool { e.contains("instant lock proof signature") || e.contains("try chain asset lock proof") } -/// Wait until the lock is provable: InstantSend where available, else a chain lock. -async fn prove( +/// Broadcast the lock and prove it: with its InstantSend lock when one arrives within +/// [`ISLOCK_WAIT`] (from the DAPI stream, watched from before the broadcast, or the +/// `--islock-rpc` endpoint when one is set), else with a chain lock. +async fn send_and_prove( ctx: &Ctx, client: &PlatformClient, endpoints: &CoreEndpoints, @@ -390,21 +384,34 @@ async fn prove( lock: &SignedAssetLock, ) -> Result { let start = Instant::now(); - if let Some(rpc) = &endpoints.islock_rpc { - say(ctx, " waiting for InstantSend…"); - while start.elapsed() < ISLOCK_WAIT { - if let Ok(Some(islock)) = fetch_islock(rpc, &lock.txid).await { - return Ok(LockProof::Instant { - raw_tx: lock.raw.clone(), - islock, - }); - } - tokio::time::sleep(Duration::from_secs(3)).await; + let route = funding::send_and_await_instant_lock( + client, + Some(insight), + endpoints.islock_rpc.as_deref(), + &lock.raw, + &lock.txid, + ISLOCK_WAIT, + &mut || say(ctx, " waiting for InstantSend…"), + ) + .await + .context("broadcasting the asset lock")?; + match route { + LockRoute::Instant(islock) => { + return Ok(LockProof::Instant { + raw_tx: lock.raw.clone(), + islock, + }) } - say( + LockRoute::Mined => say(ctx, " the asset lock is mined already"), + LockRoute::AlreadyInstantLocked => say( + ctx, + " the asset lock is InstantSend-locked already; its lock is not sent again, \ + proving with a chain lock", + ), + LockRoute::NoInstantLock => say( ctx, " no InstantSend lock; proving with a chain lock instead", - ); + ), } prove_chain(ctx, client, insight, lock, start).await } @@ -731,7 +738,8 @@ fn show_deposit(ctx: &Ctx, endpoints: &CoreEndpoints, address: &str, duffs: u64) } } -/// Deposit → asset lock (saved to the journal before broadcast) → broadcast → proof. +/// Deposit → asset lock (saved to the journal before broadcast) → lock watch → broadcast → +/// proof. async fn fund_and_lock( ctx: &Ctx, client: &PlatformClient, @@ -775,8 +783,7 @@ async fn fund_and_lock( ); lock }; - broadcast(client, &insight, &lock).await?; - let proof = prove(ctx, client, &endpoints, &insight, &lock).await?; + let proof = send_and_prove(ctx, client, &endpoints, &insight, &lock).await?; Ok((lock, proof)) } @@ -878,7 +885,7 @@ async fn register( if let Err(e) = client.create_identity(keys, proof, &limited, spec).await { // An InstantSend proof Platform will not take (a stale lock on a resume, as much as a // fresh one) is not the end: the same asset lock proves with a chain lock. Chain locks - // need no InstantSend source, so this also works when the RPC is down. + // need no InstantSend source, so this works whatever delivered the refused lock. if !(matches!(proof, LockProof::Instant { .. }) && instant_proof_refused(&e.to_string())) { return Err(anyhow::Error::from(e).context("registering the identity")); } diff --git a/crates/dg/src/config.rs b/crates/dg/src/config.rs index 2502fb1c1..9218f66aa 100644 --- a/crates/dg/src/config.rs +++ b/crates/dg/src/config.rs @@ -10,8 +10,8 @@ //! dapi_addresses = "68.67.122.86,68.67.122.87" # testnet/devnet; default: deployments file //! fiat = "EUR" # optional fiat equivalents; "off" disables requests //! trending = false # `dg repo star` does not count toward Trending (default: true) -//! islock_rpc = "https://trpc.digitalcash.dev" # InstantSend lock source for `dg auth new`; -//! # "off" proves with chain locks only +//! islock_rpc = "https://rpc.example" # optional extra InstantSend lock source for +//! # `dg auth new`, beside the DAPI stream (default: none) //! ``` use std::path::{Path, PathBuf}; @@ -51,9 +51,9 @@ pub struct Config { /// `--no-trending` overrides it for one star. #[serde(skip_serializing_if = "Option::is_none")] pub trending: Option, - /// The JSON-RPC (`getislocks`) `dg auth new` asks for an InstantSend proof of its asset - /// lock, or `off` for chain locks only. Unset: the network's default (testnet's is the - /// third-party `https://trpc.digitalcash.dev`). Overridden by `dg auth new --islock-rpc`. + /// An extra JSON-RPC (`getislocks`) `dg auth new` asks for its asset lock's InstantSend + /// lock, beside the DAPI stream that delivers it; unset or `off`: the stream only (no + /// network has a default RPC). Overridden by `dg auth new --islock-rpc`. #[serde(skip_serializing_if = "Option::is_none")] pub islock_rpc: Option, } diff --git a/crates/forge-core/src/funding.rs b/crates/forge-core/src/funding.rs index 8d02e83e2..cbe87462f 100644 --- a/crates/forge-core/src/funding.rs +++ b/crates/forge-core/src/funding.rs @@ -3,15 +3,23 @@ //! [`CoreChain`] is the DAPI Core service every evonode serves next to Platform (dapi-grpc //! `core.proto`): `broadcastTransaction`, `getTransaction` (raw bytes, height, lock status), //! `getBlockchainStatus`, and `subscribeToTransactionsWithProofs`, a bloom-filtered stream of -//! the transactions that pay or spend an address. [`crate::platform::PlatformClient`] -//! implements it over the SDK's DAPI client, so funding needs no service besides the -//! evonodes. The [`Insight`] block explorer is only a fallback for when DAPI cannot answer, -//! and `getislocks` JSON-RPC recovers an InstantSend lock by txid where one exists. +//! the transactions that pay or spend an address, and of the InstantSend locks of the ones +//! it matched. [`crate::platform::PlatformClient`] implements it over the SDK's DAPI client, +//! so funding needs no service besides the evonodes. The [`Insight`] block explorer is only a +//! fallback for when DAPI cannot answer. //! -//! Neither is trusted with amounts or keys: every output a deposit is built from is re-read -//! from its raw funding transaction, which must hash to its txid -//! (`platform::identity::DepositWatch`, `verify_deposit`), and neither sees a private key. -//! A node or explorer can delay the user or hide funds, not take them. +//! An asset lock's InstantSend lock comes from that same stream, filtered on the lock's txid +//! and the outpoints it spends ([`CoreChain::watch_instant_lock`]). Nodes send a lock once, +//! live, and never replay it, so the watch is opened before the asset lock is broadcast +//! ([`send_and_await_instant_lock`]); without a lock in time the asset lock is proven with a +//! chain lock instead. A `getislocks` JSON-RPC endpoint, when the user names one, is asked as +//! well (no network has one by default: the public one stopped serving locks). +//! +//! None of them is trusted with amounts or keys: every output a deposit is built from is +//! re-read from its raw funding transaction, which must hash to its txid +//! (`platform::identity::DepositWatch`, `verify_deposit`), an InstantSend lock is used only +//! when it names the asset lock's txid (Platform checks its signature), and none sees a +//! private key. A node or explorer can delay the user or hide funds, not take them. use std::time::Duration; @@ -47,6 +55,13 @@ pub trait CoreTxFeed: Send { } } +/// A live feed of the InstantSend locks a lock watch's nodes send. +#[async_trait] +pub trait InstantLockFeed: Send { + /// The next batch of raw InstantSend lock messages; `None` once every stream has ended. + async fn next_locks(&mut self) -> Result>>>; +} + /// The DAPI Core calls funding needs (implemented by [`crate::platform::PlatformClient`]). #[async_trait] pub trait CoreChain: Send + Sync { @@ -65,41 +80,43 @@ pub trait CoreChain: Send + Sync { from_height: u32, history_only: bool, ) -> Result>; + /// Watch for the InstantSend lock of the transaction `raw`: a feed filtered on its txid + /// and the outpoints it spends, from just below the tip. Nodes send a lock only live, so + /// open the watch before broadcasting. + async fn watch_instant_lock(&self, raw: &[u8]) -> Result>; } -/// The fallback explorer and the lock-proof endpoint, per network. +/// The fallback explorer and the optional extra InstantSend source, per network. #[derive(Debug, Clone, PartialEq, Eq)] pub struct CoreEndpoints { /// Insight API base URL (`…/insight-api`), consulted only when DAPI cannot answer. pub insight: String, - /// JSON-RPC with `getislocks` (InstantSend proofs), or `None` to use chain-lock proofs. + /// A JSON-RPC with `getislocks`, asked for an InstantSend lock beside the DAPI stream; + /// `None` (every network's default) asks only the stream. pub islock_rpc: Option, } impl CoreEndpoints { /// The defaults for `network`, with the explorer overridable (ux-dx-spec §2.2: the - /// explorer is configurable). + /// explorer is configurable). No network has an InstantSend RPC by default: the DAPI + /// stream delivers the locks (testnet's `trpc.digitalcash.dev` stopped serving them). pub fn for_network(network: &Network, insight_override: Option<&str>) -> Self { - let (insight, islock_rpc) = match network { - Network::Testnet => ( - "https://insight.testnet.networks.dash.org/insight-api".to_string(), - Some("https://trpc.digitalcash.dev".to_string()), - ), - Network::Mainnet => ("https://insight.dash.org/insight-api".to_string(), None), - Network::Devnet { name, .. } => ( - format!("https://insight.{name}.networks.dash.org/insight-api"), - None, - ), + let insight = match network { + Network::Testnet => "https://insight.testnet.networks.dash.org/insight-api".to_string(), + Network::Mainnet => "https://insight.dash.org/insight-api".to_string(), + Network::Devnet { name, .. } => { + format!("https://insight.{name}.networks.dash.org/insight-api") + } }; Self { insight: insight_override.map_or(insight, |s| s.trim_end_matches('/').to_string()), - islock_rpc, + islock_rpc: None, } } - /// With the InstantSend RPC chosen by the user (`--islock-rpc`, `config.toml`): a URL, or - /// `off` / `none` to prove with chain locks only. `None` or blank keeps the network's - /// default (testnet's is a third-party service, `trpc.digitalcash.dev`). + /// With the InstantSend RPC chosen by the user (`--islock-rpc`, `config.toml`): a URL + /// asked beside the DAPI stream, or `off` / `none` for the stream only (the default, also + /// kept for `None` or blank). /// /// A URL must be https (plain http only to a loopback host: the RPC sees the asset-lock /// txid, so it must not travel in clear over the network). Anything else is an error. @@ -527,6 +544,131 @@ pub async fn fetch_islock(rpc: &str, txid: &str) -> Result>> { .and_then(|h| hex::decode(h).ok())) } +/// The txid (display hex) an InstantSend lock message locks, or `None` when it is not one +/// (DIP-22 `ISDLOCK`: version, the locked outpoints, txid, cycle hash, BLS signature). +pub fn instant_lock_txid(islock: &[u8]) -> Option { + use dash_sdk::dpp::dashcore::consensus::encode::deserialize; + deserialize::(islock) + .ok() + .map(|l| l.txid.to_string()) +} + +/// How long the RPC, when one is configured, is left between two `getislocks` questions. +const ISLOCK_RPC_POLL: Duration = Duration::from_secs(3); + +/// How an asset lock reached the network, and so how it can be proven. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum LockRoute { + /// Its InstantSend lock (raw message) arrived. + Instant(Vec), + /// It was mined already: its InstantSend lock is gone for good, prove with a chain lock. + Mined, + /// The network had InstantSend-locked it already (a resumed creation): nodes do not send + /// that lock again, and no RPC had it, so prove with a chain lock. + AlreadyInstantLocked, + /// No InstantSend lock arrived in time: prove with a chain lock. + NoInstantLock, +} + +/// Broadcast the asset lock `raw` (id `txid`) and wait up to `wait` for its InstantSend lock. +/// +/// The lock watch ([`CoreChain::watch_instant_lock`]) is opened first, because nodes send a +/// lock once, live: one that arrived before the watch is never seen. A lock the network has +/// already mined is not sent again ([`LockRoute::Mined`]), and one it knows is not broadcast +/// again; one it has already InstantSend-locked (a resumed creation) can no longer arrive +/// through the stream, so only `rpc` is asked for it ([`LockRoute::AlreadyInstantLocked`]). +/// `rpc`, a `getislocks` endpoint, is asked every few seconds, each call bounded by the time +/// left. Only a lock that decodes and names `txid` counts. A watch that cannot be opened does +/// not stop the broadcast. `on_waiting` is called once the broadcast is done and the wait +/// begins. Fails only when the broadcast does. +pub async fn send_and_await_instant_lock( + chain: &dyn CoreChain, + explorer: Option<&Insight>, + rpc: Option<&str>, + raw: &[u8], + txid: &str, + wait: Duration, + on_waiting: &mut (dyn FnMut() + Send), +) -> Result { + let status = chain + .transaction(txid) + .await + .inspect_err(|e| tracing::debug!("reading {txid} before broadcasting it failed: {e}")) + .ok() + .flatten(); + if status.as_ref().is_some_and(|s| s.height.is_some()) { + return Ok(LockRoute::Mined); + } + let locked_before = status.as_ref().is_some_and(|s| s.instant_locked); + if locked_before && rpc.is_none() { + return Ok(LockRoute::AlreadyInstantLocked); + } + let mut feed = if locked_before { + None + } else { + chain + .watch_instant_lock(raw) + .await + .inspect_err(|e| tracing::warn!("watching {txid} for InstantSend failed: {e}")) + .ok() + }; + if status.is_none() { + broadcast(chain, explorer, raw, txid).await?; + } + on_waiting(); + let is_ours = |l: &Vec| instant_lock_txid(l).as_deref() == Some(txid); + let none = if locked_before { + LockRoute::AlreadyInstantLocked + } else { + LockRoute::NoInstantLock + }; + let deadline = tokio::time::Instant::now() + wait; + let mut next_rpc = tokio::time::Instant::now(); + loop { + let now = tokio::time::Instant::now(); + if now >= deadline { + return Ok(none); + } + if let Some(rpc) = rpc.filter(|_| now >= next_rpc) { + match tokio::time::timeout_at(deadline, fetch_islock(rpc, txid)).await { + Ok(Ok(Some(l))) if is_ours(&l) => return Ok(LockRoute::Instant(l)), + Ok(Ok(Some(_))) => tracing::warn!("{rpc} sent a lock that is not {txid}'s"), + Ok(Ok(None)) | Err(_) => {} + Ok(Err(e)) => tracing::debug!("{e}"), + } + next_rpc = tokio::time::Instant::now() + ISLOCK_RPC_POLL; + } + let until = if rpc.is_some() { + deadline.min(next_rpc) + } else { + deadline + }; + let Some(f) = feed.as_mut() else { + if rpc.is_none() { + return Ok(none); + } + tokio::time::sleep_until(until).await; + continue; + }; + match tokio::time::timeout_at(until, f.next_locks()).await { + Ok(Ok(Some(locks))) => { + if let Some(l) = locks.into_iter().find(is_ours) { + return Ok(LockRoute::Instant(l)); + } + } + Ok(Ok(None)) => { + tracing::debug!("every InstantSend stream for {txid} ended"); + feed = None; + } + Ok(Err(e)) => { + tracing::warn!("the InstantSend stream for {txid} failed: {e}"); + feed = None; + } + Err(_waited) => {} + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -568,13 +710,31 @@ mod tests { (Insight::new(&endpoints), hits) } - /// DAPI Core in memory: transactions by id, a feed per watch, and a broadcast log. + /// DAPI Core in memory: transactions by id, a feed per watch, the InstantSend locks a + /// lock watch delivers, and a broadcast log. #[derive(Default)] struct FakeDapi { txs: Mutex>, feed: Mutex>>>, + islocks: Mutex>>>, broadcasts: Mutex>>, watched_from: Mutex>, + /// For each lock watch, how many broadcasts had happened when it was opened. + lock_watches: Mutex>, + /// A lock watch cannot be opened. + lock_watch_fails: bool, + /// A lock watch stays open without sending anything. + lock_watch_silent: bool, + } + + /// A lock watch that is open and never sends anything. + struct SilentFeed; + + #[async_trait] + impl InstantLockFeed for SilentFeed { + async fn next_locks(&mut self) -> Result>>> { + std::future::pending().await + } } struct FakeFeed(Vec>>); @@ -589,6 +749,13 @@ mod tests { } } + #[async_trait] + impl InstantLockFeed for FakeFeed { + async fn next_locks(&mut self) -> Result>>> { + self.next_transactions().await + } + } + #[async_trait] impl CoreChain for FakeDapi { async fn broadcast(&self, raw: &[u8]) -> Result<()> { @@ -610,6 +777,28 @@ mod tests { self.watched_from.lock().unwrap().push(from_height); Ok(Box::new(FakeFeed(self.feed.lock().unwrap().clone()))) } + async fn watch_instant_lock(&self, _raw: &[u8]) -> Result> { + let sent = self.broadcasts.lock().unwrap().len(); + self.lock_watches.lock().unwrap().push(sent); + if self.lock_watch_fails { + return Err(Error::Platform("no DAPI node opened a watch".into())); + } + if self.lock_watch_silent { + return Ok(Box::new(SilentFeed)); + } + Ok(Box::new(FakeFeed(self.islocks.lock().unwrap().clone()))) + } + } + + /// An `ISDLOCK` message locking `txid` (one input), its signature zeros. + fn islock_for(txid: Txid) -> Vec { + let mut m = vec![1u8, 1]; + m.extend_from_slice(Txid::all_zeros().as_byte_array()); + m.extend_from_slice(&3u32.to_le_bytes()); + m.extend_from_slice(txid.as_byte_array()); + m.extend_from_slice(&[0x11; 32]); + m.extend_from_slice(&[0; 96]); + m } fn payment(to: &str, duffs: u64) -> Transaction { @@ -739,7 +928,10 @@ mod tests { fn endpoints_follow_the_network_and_the_override() { let t = CoreEndpoints::for_network(&Network::Testnet, None); assert!(t.insight.contains("testnet")); - assert!(t.islock_rpc.is_some(), "testnet proves with InstantSend"); + assert!( + t.islock_rpc.is_none(), + "testnet's InstantSend locks come from the DAPI stream" + ); let m = CoreEndpoints::for_network(&Network::Mainnet, Some("https://my.explorer/api/")); assert_eq!(m.insight, "https://my.explorer/api"); assert_eq!(m.insight_host(), "my.explorer"); @@ -761,15 +953,12 @@ mod tests { #[test] fn the_islock_rpc_is_the_users_choice() { let default = CoreEndpoints::for_network(&Network::Testnet, None); - assert_eq!( - default.islock_rpc.as_deref(), - Some("https://trpc.digitalcash.dev") - ); + assert_eq!(default.islock_rpc, None, "no third-party RPC by default"); // Unset or blank: the network's default. let with = |c: Option<&str>| default.clone().with_islock_rpc(c); assert_eq!(with(None).unwrap(), default); assert_eq!(with(Some(" ")).unwrap(), default); - // A URL replaces it (trailing slash dropped); `off` means chain locks only. + // A URL adds one (trailing slash dropped); `off` means the DAPI stream only. let own = with(Some("https://rpc.example/")).unwrap(); assert_eq!(own.islock_rpc.as_deref(), Some("https://rpc.example")); assert!(with(Some("OFF")).unwrap().islock_rpc.is_none()); @@ -788,10 +977,257 @@ mod tests { let e = with(Some(bad)).unwrap_err().to_string(); assert!(e.contains("InstantSend RPC"), "{bad}: {e}"); } - // It also turns InstantSend on where the network has no default. + // Any network takes one. let m = CoreEndpoints::for_network(&Network::Mainnet, None) .with_islock_rpc(Some("https://rpc.example")) .unwrap(); assert!(m.islock_rpc.is_some()); } + + #[test] + fn an_instant_lock_names_the_txid_it_locks() { + let pay = payment(ADDR, 5_000); + let islock = islock_for(pay.txid()); + // The wire bytes are reversed into the display hex every txid is compared in. + assert_eq!(instant_lock_txid(&islock), Some(pay.txid().to_string())); + assert_ne!( + hex::encode(pay.txid().as_byte_array()), + pay.txid().to_string() + ); + // Truncated, padded or empty: not a lock. + assert_eq!(instant_lock_txid(&islock[..islock.len() - 1]), None); + assert_eq!(instant_lock_txid(&[islock.as_slice(), &[0]].concat()), None); + assert_eq!(instant_lock_txid(&[]), None); + } + + #[tokio::test] + async fn a_matching_instant_lock_from_the_stream_proves_the_asset_lock() { + let dapi = FakeDapi::default(); + let lock = payment(ADDR, 2_999_000); + let txid = lock.txid().to_string(); + let ours = islock_for(lock.txid()); + // Another transaction's lock first (a bloom false positive), then ours. + dapi.islocks.lock().unwrap().extend([ + vec![islock_for(payment(ADDR, 1).txid())], + vec![vec![9, 9], ours.clone()], + ]); + let mut waited = 0; + let route = send_and_await_instant_lock( + &dapi, + None, + None, + &serialize(&lock), + &txid, + Duration::from_secs(5), + &mut || waited += 1, + ) + .await + .unwrap(); + assert_eq!(route, LockRoute::Instant(ours)); + assert_eq!(waited, 1); + // The watch was open before the one broadcast. + assert_eq!(*dapi.lock_watches.lock().unwrap(), vec![0]); + assert_eq!(dapi.broadcasts.lock().unwrap().len(), 1); + } + + #[tokio::test] + async fn without_a_matching_instant_lock_the_asset_lock_proves_by_chain() { + let dapi = FakeDapi::default(); + let lock = payment(ADDR, 2_999_000); + let txid = lock.txid().to_string(); + dapi.islocks + .lock() + .unwrap() + .push(vec![islock_for(payment(ADDR, 1).txid())]); + let route = send_and_await_instant_lock( + &dapi, + None, + None, + &serialize(&lock), + &txid, + Duration::from_secs(5), + &mut || {}, + ) + .await + .unwrap(); + assert_eq!(route, LockRoute::NoInstantLock); + assert_eq!(dapi.broadcasts.lock().unwrap().len(), 1); + + // Mined already: nothing is watched or sent, straight to the chain proof. + let mined = FakeDapi::default(); + mined.txs.lock().unwrap().insert( + txid.clone(), + CoreTxStatus { + raw: serialize(&lock), + height: Some(88_861), + chain_locked: false, + instant_locked: true, + }, + ); + mined + .islocks + .lock() + .unwrap() + .push(vec![islock_for(lock.txid())]); + let route = send_and_await_instant_lock( + &mined, + None, + None, + &serialize(&lock), + &txid, + Duration::from_secs(5), + &mut || {}, + ) + .await + .unwrap(); + assert_eq!(route, LockRoute::Mined); + assert!(mined.lock_watches.lock().unwrap().is_empty()); + assert!(mined.broadcasts.lock().unwrap().is_empty()); + } + + /// The lock `txid` a pending creation sent, as DAPI reports it before a resume. + fn known(dapi: &FakeDapi, lock: &Transaction, instant_locked: bool) { + dapi.txs.lock().unwrap().insert( + lock.txid().to_string(), + CoreTxStatus { + raw: serialize(lock), + height: None, + chain_locked: false, + instant_locked, + }, + ); + } + + async fn route( + dapi: &FakeDapi, + rpc: Option<&str>, + lock: &Transaction, + wait: Duration, + ) -> (LockRoute, usize) { + let mut waited = 0; + let route = send_and_await_instant_lock( + dapi, + None, + rpc, + &serialize(lock), + &lock.txid().to_string(), + wait, + &mut || waited += 1, + ) + .await + .unwrap(); + (route, waited) + } + + #[tokio::test(start_paused = true)] + async fn a_silent_stream_is_given_up_on_after_the_wait() { + let dapi = FakeDapi { + lock_watch_silent: true, + ..FakeDapi::default() + }; + let lock = payment(ADDR, 2_999_000); + let start = tokio::time::Instant::now(); + let (r, waited) = route(&dapi, None, &lock, Duration::from_secs(60)).await; + assert_eq!(r, LockRoute::NoInstantLock); + assert_eq!(waited, 1); + assert_eq!(start.elapsed(), Duration::from_secs(60)); + assert_eq!(dapi.broadcasts.lock().unwrap().len(), 1); + } + + #[tokio::test] + async fn a_resumed_lock_already_instant_locked_is_neither_watched_nor_sent() { + let dapi = FakeDapi::default(); + let lock = payment(ADDR, 2_999_000); + known(&dapi, &lock, true); + dapi.islocks + .lock() + .unwrap() + .push(vec![islock_for(lock.txid())]); + let (r, waited) = route(&dapi, None, &lock, Duration::from_secs(5)).await; + assert_eq!(r, LockRoute::AlreadyInstantLocked); + assert_eq!(waited, 0, "no wait is announced"); + assert!(dapi.lock_watches.lock().unwrap().is_empty()); + assert!(dapi.broadcasts.lock().unwrap().is_empty()); + } + + #[tokio::test] + async fn a_resumed_lock_not_yet_instant_locked_is_watched_but_not_sent_again() { + let dapi = FakeDapi::default(); + let lock = payment(ADDR, 2_999_000); + known(&dapi, &lock, false); + let ours = islock_for(lock.txid()); + dapi.islocks.lock().unwrap().push(vec![ours.clone()]); + let (r, _) = route(&dapi, None, &lock, Duration::from_secs(5)).await; + assert_eq!(r, LockRoute::Instant(ours)); + assert_eq!(*dapi.lock_watches.lock().unwrap(), vec![0]); + assert!(dapi.broadcasts.lock().unwrap().is_empty()); + } + + #[tokio::test] + async fn a_failed_watch_still_broadcasts_and_falls_back_to_chain() { + let dapi = FakeDapi { + lock_watch_fails: true, + ..FakeDapi::default() + }; + let lock = payment(ADDR, 2_999_000); + let (r, _) = route(&dapi, None, &lock, Duration::from_secs(5)).await; + assert_eq!(r, LockRoute::NoInstantLock); + assert_eq!(dapi.lock_watches.lock().unwrap().len(), 1); + assert_eq!(dapi.broadcasts.lock().unwrap().len(), 1); + } + + /// A `getislocks` RPC that answers every call with `body`; counts the calls. + fn rpc_answering(body: String) -> (String, Arc) { + use std::io::{Read as _, Write as _}; + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("http://{}", listener.local_addr().unwrap()); + let hits = Arc::new(AtomicUsize::new(0)); + let counter = hits.clone(); + std::thread::spawn(move || { + for stream in listener.incoming() { + let Ok(mut stream) = stream else { return }; + counter.fetch_add(1, Ordering::SeqCst); + let mut buf = [0u8; 4096]; + let _ = stream.read(&mut buf); + let _ = write!( + stream, + "HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{body}", + body.len() + ); + } + }); + (url, hits) + } + + #[tokio::test] + async fn an_rpc_lock_for_another_transaction_is_refused() { + let dapi = FakeDapi::default(); + let lock = payment(ADDR, 2_999_000); + // The entry claims our txid, but the lock it carries locks another transaction. + let other = islock_for(payment(ADDR, 1).txid()); + let body = serde_json::json!({ + "result": [{ "txid": lock.txid().to_string(), "hex": hex::encode(&other) }] + }) + .to_string(); + let (url, hits) = rpc_answering(body); + let (r, _) = route(&dapi, Some(&url), &lock, Duration::from_millis(1_500)).await; + assert_eq!(r, LockRoute::NoInstantLock); + assert!(hits.load(Ordering::SeqCst) >= 1, "the RPC was asked"); + + // The same RPC with our own lock proves it. + let ours = islock_for(lock.txid()); + let body = serde_json::json!({ + "result": [{ "txid": lock.txid().to_string(), "hex": hex::encode(&ours) }] + }) + .to_string(); + let (url, _) = rpc_answering(body); + let (r, _) = route( + &FakeDapi::default(), + Some(&url), + &lock, + Duration::from_secs(5), + ) + .await; + assert_eq!(r, LockRoute::Instant(ours)); + } } diff --git a/crates/forge-core/src/platform/core_chain.rs b/crates/forge-core/src/platform/core_chain.rs index 5498cb23a..e52323e24 100644 --- a/crates/forge-core/src/platform/core_chain.rs +++ b/crates/forge-core/src/platform/core_chain.rs @@ -1,12 +1,15 @@ //! DAPI Core for funding: [`CoreChain`] on [`PlatformClient`] (broadcast, transaction status, -//! chain tip, and a bloom-filtered address feed), plus [`DepositTracker`], which turns that -//! feed into the verified unspent outputs of a deposit address. +//! chain tip, a bloom-filtered address feed, and a watch for an asset lock's InstantSend +//! lock), plus [`DepositTracker`], which turns the address feed into the verified unspent +//! outputs of a deposit address. //! //! All of it runs over the SDK's DAPI client against the same evonodes Platform reads use, -//! so creating an identity depends on no block explorer. +//! so creating an identity depends on no block explorer and no InstantSend service. use std::collections::BTreeMap; use std::str::FromStr; +use std::sync::{Arc, Mutex}; +use std::time::Duration; use async_trait::async_trait; use dapi_grpc::core::v0::{ @@ -16,15 +19,19 @@ use dapi_grpc::core::v0::{ }; use dapi_grpc::tonic::{Code, Streaming}; use dash_sdk::dapi_client::transport::TransportError; -use dash_sdk::dapi_client::{DapiClientError, DapiRequestExecutor, IntoInner, RequestSettings}; +use dash_sdk::dapi_client::{ + Address as DapiAddress, DapiClient, DapiClientError, DapiRequestExecutor, IntoInner, + RequestSettings, +}; use dash_sdk::dpp::dashcore::bloom::{BloomFilter, BloomFlags}; -use dash_sdk::dpp::dashcore::consensus::encode::deserialize; +use dash_sdk::dpp::dashcore::consensus::encode::{deserialize, serialize}; +use dash_sdk::dpp::dashcore::hashes::Hash as _; use dash_sdk::dpp::dashcore::{Address, ScriptBuf, Transaction}; use super::identity::VerifiedUtxo; use super::PlatformClient; use crate::error::{Error, Result}; -use crate::funding::{CoreChain, CoreTxFeed, CoreTxStatus}; +use crate::funding::{CoreChain, CoreTxFeed, CoreTxStatus, InstantLockFeed}; /// The bloom filter's false-positive rate. A false positive only costs bandwidth: every /// delivered transaction is checked against the address's script. @@ -35,6 +42,19 @@ const BLOOM_FP_RATE: f64 = 0.0001; const BLOOM_CAPACITY: u32 = 50; /// BIP37 `nFlags`: add every matched output to the filter. const BLOOM_UPDATE_ALL: u32 = 1; +/// Elements a lock watch's filter is sized for, at least: enough for 200+ bytes, as DAPI +/// nodes stream nearly everything to smaller filters (govhub's finding). +const LOCK_BLOOM_MIN_CAPACITY: u32 = 100; +/// Nodes a lock watch listens on at once: the first to send the lock wins, and a stream that +/// cannot open, ends or fails moves on to a node not tried yet (as mainnet-bridge and govhub +/// do). +const LOCK_STREAMS: usize = 3; +/// How long opening a lock stream on one node may take. +const LOCK_STREAM_OPEN: Duration = Duration::from_secs(10); +/// How long to wait for a lock watch's first stream to open before broadcasting anyway. +const LOCK_WATCH_OPEN: Duration = Duration::from_secs(15); +/// Blocks below the tip a lock watch starts at: nodes' tips differ by a block or two. +const LOCK_WATCH_LOOKBACK: u32 = 2; fn dapi_err(what: &str, e: impl std::fmt::Display) -> Error { Error::Platform(format!("{what} through DAPI: {e}")) @@ -83,6 +103,129 @@ pub(crate) fn address_bloom_filter(address: &str) -> Result { }) } +/// A BIP37 filter matching the transaction `tx` by its txid and by each outpoint it spends, +/// each in both byte orders: rs-dapi matches Core's internal (wire) order, the older JS DAPI +/// the display order. A node sends a transaction's InstantSend lock only when the filter +/// matches the transaction. +pub(crate) fn lock_bloom_filter(tx: &Transaction) -> Result { + let wire_txid = tx.txid().to_byte_array(); + let mut elements = vec![wire_txid.to_vec(), reversed(&wire_txid)]; + for input in &tx.input { + let outpoint = serialize(&input.previous_output); + let (txid, vout) = outpoint.split_at(32); + elements.push(outpoint.clone()); + elements.push([reversed(txid).as_slice(), vout].concat()); + } + let capacity = u32::try_from(elements.len()) + .unwrap_or(u32::MAX) + .max(LOCK_BLOOM_MIN_CAPACITY); + let mut filter = BloomFilter::new(capacity, BLOOM_FP_RATE, rand::random(), BloomFlags::None) + .map_err(|e| Error::Config(format!("bloom filter: {e}")))?; + for e in &elements { + filter.insert(e); + } + Ok(ProtoBloomFilter { + v_data: filter.to_bytes(), + n_hash_funcs: filter.hash_funcs(), + n_tweak: filter.tweak(), + n_flags: 0, + }) +} + +fn reversed(bytes: &[u8]) -> Vec { + bytes.iter().rev().copied().collect() +} + +/// Open the lock stream `request` on the one node `address` (no retries elsewhere, and the +/// node is not banned for its answer). +async fn open_lock_stream( + address: DapiAddress, + request: TransactionsWithProofsRequest, +) -> Result> { + let node = address.uri().to_string(); + let client = DapiClient::new( + std::iter::once(address).collect(), + RequestSettings { + connect_timeout: Some(super::DAPI_CONNECT_TIMEOUT), + retries: Some(0), + ban_failed_address: Some(false), + ..RequestSettings::default() + }, + ); + tokio::time::timeout( + LOCK_STREAM_OPEN, + client.execute(request, RequestSettings::default()), + ) + .await + .map_err(|_| dapi_err(&format!("opening a lock watch on {node}"), "timed out"))? + .into_inner() + .map_err(|e| dapi_err(&format!("opening a lock watch on {node}"), e)) +} + +/// One of a lock watch's streams, run as its own task: open `request` on the next untried node +/// in `spare` (telling `opened` when it does), forward its InstantSend locks to `locks`, and +/// once the stream fails to open, ends or fails, move on to the next node, until the nodes +/// run out or the watch is dropped. +async fn lock_stream_slot( + spare: Arc>>, + request: TransactionsWithProofsRequest, + locks: tokio::sync::mpsc::Sender>>, + opened: tokio::sync::mpsc::UnboundedSender<()>, +) { + loop { + let Some(node) = spare.lock().ok().and_then(|mut s| s.pop()) else { + return; + }; + let mut stream = match open_lock_stream(node, request.clone()).await { + Ok(stream) => stream, + Err(e) => { + tracing::debug!("{e}"); + continue; + } + }; + let _ = opened.send(()); + loop { + match stream.message().await { + Ok(Some(TransactionsWithProofsResponse { + responses: Some(Responses::InstantSendLockMessages(m)), + })) => { + if locks.send(m.messages).await.is_err() { + return; + } + } + Ok(Some(_)) => {} + Ok(None) => break, + Err(e) => { + tracing::debug!("a lock watch stream failed: {e}"); + break; + } + } + } + } +} + +/// The InstantSend locks every stream of a lock watch sends, merged. Dropping it stops the +/// streams. +struct DapiLockFeed { + locks: tokio::sync::mpsc::Receiver>>, + slots: Vec>, +} + +impl Drop for DapiLockFeed { + fn drop(&mut self) { + for slot in &self.slots { + slot.abort(); + } + } +} + +#[async_trait] +impl InstantLockFeed for DapiLockFeed { + async fn next_locks(&mut self) -> Result>>> { + Ok(self.locks.recv().await) + } +} + struct DapiFeed { stream: Streaming, /// Merkle blocks delivered so far: one per block replayed from the start height. @@ -187,6 +330,82 @@ impl CoreChain for PlatformClient { .map_err(|e| dapi_err(&format!("watching {address}"), e))?; Ok(Box::new(DapiFeed { stream, blocks: 0 })) } + + /// Streams on up to [`LOCK_STREAMS`] distinct live nodes ([`open_lock_watch`]); one is + /// open before this returns, so a lock sent after it is seen. + async fn watch_instant_lock(&self, raw: &[u8]) -> Result> { + let tx: Transaction = deserialize(raw) + .map_err(|e| Error::Config(format!("the asset-lock transaction: {e}")))?; + let from = self + .best_height() + .await? + .saturating_sub(LOCK_WATCH_LOOKBACK) + .max(1); + let request = TransactionsWithProofsRequest { + bloom_filter: Some(lock_bloom_filter(&tx)?), + from_block: Some(FromBlock::FromBlockHeight(from)), + count: 0, + send_transaction_hashes: false, + }; + tracing::debug!(from, "watching {} for InstantSend", tx.txid()); + let list = self.sdk().address_list(); + let mut nodes = list.get_live_addresses(); + if nodes.is_empty() { + // Every node is banned for now (a ban is the SDK's guess, often about Platform + // and not Core): try them all rather than not watching. + nodes = list + .ban_info() + .iter() + .filter_map(|b| b.uri.parse().ok()) + .collect(); + } + Ok(Box::new(open_lock_watch(nodes, request).await?)) + } +} + +/// A lock watch: `request` open on up to [`LOCK_STREAMS`] of `nodes` at once, picked at +/// random, each stream replaced by one on an untried node the moment it fails to open, ends +/// or fails. Returns once the first stream is open (the others keep connecting meanwhile), +/// or after [`LOCK_WATCH_OPEN`] with whatever is open by then; fails only when every node +/// was tried and none opened. +async fn open_lock_watch( + mut nodes: Vec, + request: TransactionsWithProofsRequest, +) -> Result { + nodes.sort_by_cached_key(|_| rand::random::()); + let slots = LOCK_STREAMS.min(nodes.len()); + let spare = Arc::new(Mutex::new(nodes)); + let (locks_tx, locks) = tokio::sync::mpsc::channel(16); + let (opened_tx, mut opened) = tokio::sync::mpsc::unbounded_channel(); + let feed = DapiLockFeed { + locks, + slots: (0..slots) + .map(|_| { + tokio::spawn(lock_stream_slot( + spare.clone(), + request.clone(), + locks_tx.clone(), + opened_tx.clone(), + )) + }) + .collect(), + }; + drop(opened_tx); + match tokio::time::timeout(LOCK_WATCH_OPEN, opened.recv()).await { + Ok(Some(())) => tracing::debug!("lock watch open"), + // Every slot ran out of nodes without opening a stream. + Ok(None) => { + return Err(Error::Platform( + "no DAPI node opened an InstantSend watch".into(), + )) + } + Err(_slow) => { + tracing::warn!( + "no DAPI node has opened the InstantSend watch yet; broadcasting anyway" + ); + } + } + Ok(feed) } /// The unspent outputs paying one address, built from the raw transactions a DAPI feed @@ -257,7 +476,6 @@ impl DepositTracker { mod tests { use super::*; use dash_sdk::dpp::dashcore::consensus::encode::serialize; - use dash_sdk::dpp::dashcore::hashes::Hash as _; use dash_sdk::dpp::dashcore::{OutPoint, TxIn, TxOut, Txid}; const ADDR: &str = "yhJHMkBAT2TF6D8GHc4v9bMfBh3V2Z6meg"; @@ -329,4 +547,52 @@ mod tests { assert!(!proto.v_data.is_empty() && proto.v_data.len() <= 36_000); assert!(address_bloom_filter("not-an-address").is_err()); } + + #[test] + fn the_lock_filter_matches_the_txid_and_outpoints_in_both_byte_orders() { + let funding = tx(&[(Txid::all_zeros(), 0)], &[(ADDR, 2_000_000)]); + let lock = tx(&[(funding.txid(), 0), (funding.txid(), 7)], &[(OTHER, 1)]); + let proto = lock_bloom_filter(&lock).unwrap(); + assert!( + proto.v_data.len() >= 200, + "{} bytes saturate on DAPI nodes", + proto.v_data.len() + ); + assert_eq!(proto.n_flags, 0); + let filter = BloomFilter::from_bytes( + proto.v_data.clone(), + proto.n_hash_funcs, + proto.n_tweak, + BloomFlags::None, + ) + .unwrap(); + // The txid as Core keeps it (rs-dapi) and as it is displayed (the JS DAPI). + let wire = lock.txid().to_byte_array(); + assert!(filter.contains(&wire)); + assert!(filter.contains(&hex::decode(lock.txid().to_string()).unwrap())); + for vout in [0u32, 7] { + let le = vout.to_le_bytes(); + let wire_outpoint = [funding.txid().to_byte_array().as_slice(), &le].concat(); + assert_eq!( + wire_outpoint, + serialize(&OutPoint::new(funding.txid(), vout)) + ); + let display_outpoint = [ + hex::decode(funding.txid().to_string()).unwrap().as_slice(), + &le, + ] + .concat(); + assert!(filter.contains(&wire_outpoint)); + assert!(filter.contains(&display_outpoint)); + } + // Not the funding transaction itself, nor an outpoint the lock does not spend. + assert!(!filter.contains(&funding.txid().to_byte_array())); + assert!(!filter.contains( + &[ + funding.txid().to_byte_array().as_slice(), + &1u32.to_le_bytes() + ] + .concat() + )); + } } diff --git a/crates/forge-core/src/platform/identity.rs b/crates/forge-core/src/platform/identity.rs index 78463dfee..cbeb994e3 100644 --- a/crates/forge-core/src/platform/identity.rs +++ b/crates/forge-core/src/platform/identity.rs @@ -609,7 +609,7 @@ pub fn build_asset_lock(keys: &NewIdentityKeys, utxos: &[VerifiedUtxo]) -> Resul /// How an asset lock is proven to Platform. #[derive(Debug, Clone, PartialEq, Eq)] pub enum LockProof { - /// An InstantSend lock (testnet, mainnet with an islock source). + /// An InstantSend lock (from the DAPI stream, on any network). Instant { /// The raw asset-lock transaction. raw_tx: Vec, diff --git a/docs/design/ux-dx-spec.md b/docs/design/ux-dx-spec.md index b4e388fe1..9d67ad44f 100644 --- a/docs/design/ux-dx-spec.md +++ b/docs/design/ux-dx-spec.md @@ -74,7 +74,7 @@ With no storage profile the command stops before creating anything: *"No storage Sign-in sheet (replaces `login-modal.tsx`): three tiles plus an "Advanced" disclosure. 1. **Use my Dash wallet** (App Connect v2 where the chain runs PV14 and Forge is deployed; first where a wallet can answer, else last with the reason). App creates an ephemeral keypair → QR (and, on a phone, a deep link that also carries a return address `/connect/`) with the request (group `dash-forge`; one AUTHENTICATION HIGH key, budget 0.05 DASH, expiry 90 days; no encryption key, since no Forge contract allows a wallet-issued one) → polls `loginKeyResponse` by `appEphemeralPubKeyHash`, at once when the tab regains focus or the wallet returns → decrypts the grant → **verifies on-chain** that the key is live on the responder with the requested bounds, budget and expiry (the budget may be higher: wallets offer presets from 0.1 DASH) → confirm step ("A wallet answered for this identity": compare the username and id with the wallet) → vault. One approval covers all four Forge contracts, so no guided grants follow. Waiting copy: *"Approve in your wallet. One approval covers all of Forge, spending up to 0.05 DASH until . Your wallet may offer other limits."* A link under the code, *"Using a Dash Wallet build without App Connect v2? Show the older code"*, switches to the older exchange (one approval per contract, guided) and back. If no wallet supports either yet the tile says so. Details: [wallet-login.md](wallet-login.md#app-connect-v2-one-approval-for-all-of-forge). -2. **Create a new identity** (funds from any Dash wallet). (i) 12-word mnemonic generated in-browser; the user confirms three words before continuing; (ii) deposit QR with amount (default 0.05 DASH, min 0.02; *"This becomes your Platform credits. ~0.0005 DASH per issue or push."*); (iii) watch the address, build and broadcast the asset lock, wait for InstantSend, register the identity **with the limited key inside the IdentityCreate** so no second signature is needed; (iv) the master key exists only in memory during this step. Note under the QR: *"This page watches for your deposit through the Dash network's own nodes, and asks a block explorer (`insight.dash.org`, changeable in Settings) only if they cannot answer. Neither can take funds or keys."* +2. **Create a new identity** (funds from any Dash wallet). (i) 12-word mnemonic generated in-browser; the user confirms three words before continuing; (ii) deposit QR with amount (default 0.05 DASH, min 0.02; *"This becomes your Platform credits. ~0.0005 DASH per issue or push."*); (iii) watch the address, build and broadcast the asset lock, wait for InstantSend, register the identity **with the limited key inside the IdentityCreate** so no second signature is needed; (iv) the master key exists only in memory during this step. Note under the QR: *"This page watches for your deposit through the Dash network's own nodes and checks the amounts itself."* The InstantSend lock comes off the same nodes' transaction feeds, opened before the asset lock is broadcast; no block explorer or RPC service is asked. 3. **Import an identity file** (bridge JSON, `dg auth export`, or mnemonic): the master key signs one `IdentityUpdate` registering a limited key and is discarded. *"Your master key is used once, right now, to create a limited key for this browser. It is not stored."* **Advanced**: paste a raw key (developer path; red warning; tab-scoped unless opted into the vault). @@ -414,7 +414,7 @@ jobs: ## 10. Accessibility, performance, mobile - **A11y**: WCAG 2.1 AA, axe 0 serious; every state color paired with an icon and a word; `forge-400` focus ring; Radix dialogs with focus trap and Escape; blue/orange diff palette; `prefers-reduced-motion` disables motion; QR codes carry the address as text; CLI honors `NO_COLOR` and never relies on color alone. -- **Performance budgets** (CI: Lighthouse + request interception): bundle < 1.5 MiB gz before WASM; WASM after first paint; cold repo home < 3 s and < 500 KB at any size (`flatIndex` never on home); warm reload < 1.5 s; **100 MB repo: README < 3 s, first blob < 2 s, deep tree < 4 s** (flatIndex ≈ 470 KB at 10k files, cached in IndexedDB); issues first page < 2 s; PR diff ≤ 50 files < 3 s; 100 MB search index ≤ 60 s in a worker with progress. Zero requests to origins other than DAPI, the repo's storage, and user-configured, off-able helpers (price source, block explorer). +- **Performance budgets** (CI: Lighthouse + request interception): bundle < 1.5 MiB gz before WASM; WASM after first paint; cold repo home < 3 s and < 500 KB at any size (`flatIndex` never on home); warm reload < 1.5 s; **100 MB repo: README < 3 s, first blob < 2 s, deep tree < 4 s** (flatIndex ≈ 470 KB at 10k files, cached in IndexedDB); issues first page < 2 s; PR diff ≤ 50 files < 3 s; 100 MB search index ≤ 60 s in a worker with progress. Zero requests to origins other than DAPI, the repo's storage, and user-configured, off-able helpers (price source). - **Mobile** (≥ 360 px): read everything, write the small things. Tabs scroll; the rail collapses under content with Verification first; the commit column hides; diffs unified; issue/comment composer works; merge and storage wizards say *"Use a desktop browser for this step"*. Clone box shows only the `dash://` URL and zip. Wallet sign-in (QR → deep link) is primary. --- @@ -425,7 +425,7 @@ Areas: `core` = `crates/forge-core`, `helper` = `crates/git-remote-dash`, `dg` = ### P0 — launch-blocking 1. Limited-key login: vault (passkey PRF / Argon2id), import-file ceremony, app-connect with on-chain grant verification, budget/expiry pill, renew flow — `web` -2. In-browser identity creation (QR asset lock, forced mnemonic backup, configurable explorer) — `web` +2. In-browser identity creation (QR asset lock, forced mnemonic backup) — `web` 3. `dg auth new/login/status/keys/name/export` on keychain with encrypted fallback; `dfk1:` env format — `dg`, `core` 4. `dg storage add` interactive + CORS fix blocks; web storage wizard with browser-side tests and PUT CORS template; credential vault — `dg`, `web`, `core` 5. `git push` honors `dash.storage`; plan/progress/summary lines; cost guard tty prompt and non-tty failure — `helper`, `core` diff --git a/docs/guides/identity-and-keys.md b/docs/guides/identity-and-keys.md index ba0a85476..b360509a0 100644 --- a/docs/guides/identity-and-keys.md +++ b/docs/guides/identity-and-keys.md @@ -329,8 +329,8 @@ What the vault does **not** protect against: ## Trust roots - **The contract group id** comes from `forge-contracts/deployments/.json`, which is built into the app and `dg`. So is **the group's owner**, which the same file pins. The owner (and any admins, of which Forge's group has none) is the only identity that can **add** contracts to the group. Every group-bound key can then sign for those contracts too, including keys registered earlier. Binding a key to the group therefore means trusting its owner. On testnet it is the deployer that registered Forge's contracts there, `3pjHaytsQJg5aKZj3VeqwBPccN7uN3bCJiUNkT8KUBwM`. Before binding a key, the app and `dg` check on chain, with proofs, that the group's owner is the pinned one and that it has no admins. They also cross-check that every member belongs to a contract that owner owns. Members they do not know are listed, not refused. `dg --strict-group` refuses them ([forge-v2 § Contract group trust](../contracts/forge-v2.md#contract-group-trust)). The owner cannot change, so this pin holds for the life of the group. -- **Watching the deposit** goes through DAPI, the Dash network's own evonodes: a bloom-filtered `subscribeToTransactionsWithProofs` feed of the deposit address from the block the creation started at, `broadcastTransaction` for the asset lock and `getTransaction` for its height. The block explorer (Insight, changeable in Settings, `dg auth new --explorer `) is only a fallback, asked when DAPI cannot answer or the feed is idle. Neither is trusted with amounts: each deposit output is read from a raw transaction whose txid is computed locally (an explorer's is fetched and hashed against the txid it named) before the asset lock is signed. A lying node or explorer can delay you or hide funds, but it cannot redirect or burn them. If a broadcast is dropped, the signed bytes are kept and sent again. -- **The InstantSend proof on testnet** comes from a third-party JSON-RPC, `https://trpc.digitalcash.dev` (`getislocks`), which sees the asset-lock txid of every `dg auth new`. It is not run by Dash or by Forge, and it cannot take funds or keys: Platform checks the lock's signature itself. Change it with `dg auth new --islock-rpc ` or `islock_rpc` in `config.toml`; `off` proves with a chain lock only (a few minutes slower). If Platform refuses an InstantSend proof, `dg` retries with a chain lock, on a resumed creation too. +- **Watching the deposit** goes through DAPI, the Dash network's own evonodes: a bloom-filtered `subscribeToTransactionsWithProofs` feed of the deposit address from the block the creation started at, `broadcastTransaction` for the asset lock and `getTransaction` for its height. The web app asks nothing else: it watches on two nodes at once and moves a feed that fails or goes silent to another node. `dg` falls back to a block explorer (Insight, `dg auth new --explorer `) when DAPI cannot answer or the feed is idle. No source is trusted with amounts: each deposit output is read from a raw transaction whose txid is computed locally (an explorer's is fetched and hashed against the txid it named) before the asset lock is signed. A lying node or explorer can delay you or hide funds, but it cannot redirect or burn them. If a broadcast is dropped, the signed bytes are kept and sent again. +- **The InstantSend proof** comes from the same evonodes. Before the asset lock is broadcast, the web app and `dg` open `subscribeToTransactionsWithProofs` feeds on three nodes, filtered on the lock's txid and the outpoints it spends. An InstantSend lock is sent once, live, and never replayed, so the feeds have to be open first. The first lock that names the lock's txid makes the proof. Platform checks its signature itself, so a node cannot fake one. With no lock within a minute, the proof is a chain lock instead, a few minutes slower. So is it for a lock already mined, and for a resumed creation whose lock was already InstantSend-locked, since that lock is not sent again. `dg` can also poll a `getislocks` JSON-RPC of your choice alongside the feeds (`dg auth new --islock-rpc ` or `islock_rpc` in `config.toml`). None is set by default: the public testnet one, `trpc.digitalcash.dev`, no longer follows the chain. If Platform refuses an InstantSend proof, `dg` retries with a chain lock, on a resumed creation too. - **The quorum keys** every proof is checked against come from `quorums..networks.dash.org`, as for every read. The web app compares them with a second source on each repository page ([Verify Forge](verify-forge.md#the-web-app-cross-checks-the-keys-with-a-second-source)). - **The code doing the checking**: the web app you loaded, or the `dg` you built. If you do not trust forge.dashhq.org, [serve the app yourself](verify-forge.md#run-your-own-copy-of-the-web-app). diff --git a/forge-web/components/auth/create-identity-flow.tsx b/forge-web/components/auth/create-identity-flow.tsx index 4c9071b50..57fa5e97a 100644 --- a/forge-web/components/auth/create-identity-flow.tsx +++ b/forge-web/components/auth/create-identity-flow.tsx @@ -28,13 +28,13 @@ import { ErrorBox, GroupNotice, useProtection } from '@/components/auth/protecti import { StepFailed, Waiting } from '@/components/auth/step-status' import { FaucetLinks } from '@/components/faucet-links' import { faucetUrls } from '@/lib/faucets' +import { coreEndpoints } from '@/lib/auth/asset-lock' import { ACTIVE_NETWORK, NotDeployedError } from '@/lib/constants' import { typicalIssueCredits } from '@/lib/sdk' import { creditsAsDash } from '@/lib/view/format' import { isAbort } from '@/lib/sdk/facade' import { PHASE_TEXT, STEP_MS, connectPlatform, loadSdkLibrary, type ConnectPhase } from '@/lib/auth/connect' import { withTimeout } from '@/lib/timeout' -import { coreEndpoints } from '@/lib/auth/asset-lock' import { clearCreationJournal, createIdentityFromMnemonic, @@ -215,6 +215,11 @@ export function CreateIdentityFlow({ onDone }: { onDone: () => void }): JSX.Elem setError(new NotDeployedError(ACTIVE_NETWORK).message + '.') return } + // Nothing could watch the deposit: never show an address for it. + if (coreEndpoints(network).dapi.length === 0) { + setError('This build lists no Dash network nodes, so it cannot watch a deposit. Use another Forge build or dg auth new.') + return + } const deposit = await checkWords(m) if (deposit === null) return runWords.current = m @@ -471,8 +476,8 @@ export function CreateIdentityFlow({ onDone }: { onDone: () => void }): JSX.Elem ) : null}

- Waiting for your deposit. It usually takes a few minutes. If Dash nodes don't answer, this page asks{' '} - {new URL(coreEndpoints(network).insight).host} instead (change it in Settings). Amounts are checked either way. + Waiting for your deposit. It usually takes a few minutes. This page watches for it through the Dash network's own nodes and + checks the amounts itself.

{error && !running && notCreated === 'final' ? ( // Nothing left to retry with: this deposit cannot create the identity. diff --git a/forge-web/components/keys-panel.tsx b/forge-web/components/keys-panel.tsx index ee7487718..bd9f7355f 100644 --- a/forge-web/components/keys-panel.tsx +++ b/forge-web/components/keys-panel.tsx @@ -19,10 +19,8 @@ import { UnlimitedKeyWarning } from '@/components/auth/wallet-connect-flow' import { useAuth } from '@/contexts/auth-context' import { useUiStore } from '@/hooks/use-ui-store' import { Button } from '@/components/ui/button' -import { Field, Input } from '@/components/ui/input' import { creditsAsDash, formatDate } from '@/lib/view/format' import { keyBudgetWords, renewableInWallet } from '@/lib/view/funds' -import { INSIGHT_OVERRIDE_KEY, coreEndpoints } from '@/lib/auth/asset-lock' import { FORGET_DELETES, topUpStays } from '@/components/top-up-stays' import { KeyTopUpDialog } from '@/components/key-top-up-dialog' import { KeyRevokeDialog } from '@/components/key-revoke-dialog' @@ -52,11 +50,6 @@ export function KeysPanel(): JSX.Element { const [confirm, confirmDialog] = useConfirmAction() const openLogin = useUiStore((s) => s.openLogin) const missingGrant = nextGrant(grants) - const [explorer, setExplorer] = useState(() => - typeof window === 'undefined' ? '' : window.localStorage.getItem(INSIGHT_OVERRIDE_KEY) ?? '', - ) - // What the field accepts: an https URL (http would let a network observer answer for it). - const explorerProblem = explorer.trim() !== '' && !/^https:\/\/[^\s/]+/.test(explorer.trim()) ? 'Use an https:// address. Not saved.' : null const low = funds?.reason === 'key-budget' || funds?.reason === 'key-expiry' const [topUpOpen, setTopUpOpen] = useState(false) // Only a stored Forge browser key (a budget on a group-bound key) can be topped up here. @@ -211,29 +204,6 @@ export function KeysPanel(): JSX.Element { {topUpOpen ? setTopUpOpen(false)} /> : null} {revokeOpen ? setRevokeOpen(false)} /> : null} {confirmDialog} - - { - setExplorer(e.target.value) - const v = e.target.value.trim() - if (v === '') window.localStorage.removeItem(INSIGHT_OVERRIDE_KEY) - else if (/^https:\/\//.test(v)) window.localStorage.setItem(INSIGHT_OVERRIDE_KEY, v.replace(/\/+$/, '')) - }} - /> - {explorerProblem ? ( -

- {explorerProblem} -

- ) : null} -
) } diff --git a/forge-web/lib/auth/asset-lock.ts b/forge-web/lib/auth/asset-lock.ts index 4217557bd..6acd5c719 100644 --- a/forge-web/lib/auth/asset-lock.ts +++ b/forge-web/lib/auth/asset-lock.ts @@ -1,24 +1,24 @@ /** * Core-chain side of creating an identity in the browser (`ux-dx-spec.md` §2.2 tile 2), ported - * from `tools/mint-identity` (tx.mjs, lock.mjs, dapi-core.mjs): + * from `tools/mint-identity` (tx.mjs, lock.mjs, dapi-core.mjs) and aligned with govhub and + * mainnet-bridge: * * 1. watch the deposit address through DAPI (the evonodes' Core service, `dapi-core.ts`): a * bloom-filtered `subscribeToTransactionsWithProofs` feed from the height the creation - * started at. A block explorer (Insight; configurable) is asked only when the feed is - * idle or unavailable; + * started at, on two nodes at once; * 2. build and sign a type-8 asset-lock transaction spending them to one credit output - * controlled by the asset-lock key, and broadcast it (DAPI first, the explorer as - * fallback); - * 3. prove the lock: an InstantSend lock where a public `getislocks` endpoint exists - * (testnet), else a chain-lock proof once DAPI reports the transaction mined and - * Platform's chain-locked Core height reaches its block (devnets). + * controlled by the asset-lock key, and broadcast it; + * 3. prove the lock: its InstantSend lock, taken off feeds opened on three nodes before the + * broadcast (an islock is sent once, live, and never replayed), else a chain-lock proof + * once DAPI reports the transaction mined and Platform's chain-locked Core height + * reaches its block. * - * Neither source is trusted with amounts. Every input is derived from a raw funding - * transaction whose txid is computed here — for the explorer, fetched and checked against the - * txid it named — and parsed for the output's value and script before anything is signed. - * (The legacy sighash does not commit to input values: trusting a claimed amount would let a - * lying source turn the deposit into miner fees.) A node or explorer can delay the user or - * hide funds, but not take them or learn a key. + * Only the evonodes are asked: no block explorer or JSON-RPC service. No node is trusted with + * amounts. Every input is derived from a raw funding transaction whose txid is computed here, + * and parsed for the output's value and script before anything is signed. (The legacy sighash + * does not commit to input values: trusting a claimed amount would let a lying source turn the + * deposit into miner fees.) A node can delay the user or hide funds, but not take them or + * learn a key, and Platform checks an islock's signature itself. */ import * as secp from '@noble/secp256k1' @@ -28,49 +28,31 @@ import { sha256 } from '@noble/hashes/sha2.js' import { bytesToHex, concatBytes, hexToBytes } from '@noble/hashes/utils.js' import { ACTIVE_NETWORK, NETWORKS, type Network } from '../constants' -import { isAbort, sleep } from '../sdk/facade' +import { anyOf } from '../sdk/budget' +import { sleep } from '../sdk/facade' import { base58CheckDecode } from './base58' -import { DapiCore, bloomFilter } from './dapi-core' +import { DapiCore, bloomFilter, shuffled, type BloomFilter } from './dapi-core' import { decodeWif } from './wif' // @noble/secp256k1 v3 needs sync hashes wired for sign/getPublicKey. secp.hashes.sha256 = sha256 secp.hashes.hmacSha256 = (k, m) => hmac(sha256, k, m) -/** DAPI, the fallback explorer, and the lock-proof endpoint per network. */ +/** The Core endpoints per network. */ export interface CoreEndpoints { - /** DAPI nodes (`https://host:port`) serving the Core gRPC-web service; empty = explorer only. */ + /** DAPI nodes (`https://host:port`) serving the Core gRPC-web service. */ readonly dapi: readonly string[] - /** Insight API base URL, asked only when DAPI cannot answer. */ - readonly insight: string - /** JSON-RPC with `getislocks` (InstantSend proof), or null to use chain-lock proofs. */ - readonly islockRpc: string | null } -/** Where Settings stores a user-chosen block explorer (Insight API base URL). */ -export const INSIGHT_OVERRIDE_KEY = 'forge:insight-url' - -/** The endpoints this build uses (Settings may override the explorer, spec §2.2). */ +/** The endpoints this build uses. */ export function coreEndpoints(network: Network = ACTIVE_NETWORK.network): CoreEndpoints { - const override = typeof window !== 'undefined' ? window.localStorage.getItem(INSIGHT_OVERRIDE_KEY) : null - const dapi = NETWORKS[network].dapiAddresses - if (network === 'devnet') { - return { dapi, insight: override ?? `https://insight.${ACTIVE_NETWORK.devnetName}.networks.dash.org/insight-api`, islockRpc: null } - } - if (network === 'testnet') { - return { dapi, insight: override ?? 'https://insight.testnet.networks.dash.org/insight-api', islockRpc: 'https://trpc.digitalcash.dev' } - } - return { dapi, insight: override ?? 'https://insight.dash.org/insight-api', islockRpc: null } + return { dapi: NETWORKS[network].dapiAddresses } } -function dapiOf(ep: CoreEndpoints): DapiCore | null { - return ep.dapi.length > 0 ? new DapiCore(ep.dapi) : null +function dapiOf(ep: CoreEndpoints): DapiCore { + return new DapiCore(ep.dapi) } -// --------------------------------------------------------------------------- -// Insight -// --------------------------------------------------------------------------- - export interface Utxo { readonly txid: string readonly vout: number @@ -79,17 +61,6 @@ export interface Utxo { readonly confirmations: number } -async function getJson(url: string, init?: RequestInit): Promise { - const res = await fetch(url, init) - if (!res.ok) throw new Error(`${new URL(url).host}: HTTP ${res.status}`) - return (await res.json()) as T -} - -export async function getUtxos(ep: CoreEndpoints, address: string): Promise { - const rows = await getJson(`${ep.insight}/addr/${address}/utxo`) - return rows.map((u) => ({ txid: u.txid, vout: u.vout, satoshis: u.satoshis, scriptPubKey: u.scriptPubKey, confirmations: u.confirmations ?? 0 })) -} - /** The value and script of each output of a raw transaction (Dash; special payload skipped). */ export function parseOutputs(raw: Uint8Array): { value: bigint; script: Uint8Array }[] { let at = 0 @@ -132,78 +103,21 @@ export function parseOutputs(raw: Uint8Array): { value: bigint; script: Uint8Arr return out } -/** - * The deposit's spendable outputs, each proven from its raw funding transaction: the raw - * bytes must hash to the txid the explorer named, and the value and script come from those - * bytes. Outputs that do not pay `address`'s P2PKH script are dropped. - */ -export async function verifiedUtxos(ep: CoreEndpoints, address: string, listed: readonly Utxo[]): Promise { - const script = bytesToHex(p2pkh(addressHash(address))) - const out: Utxo[] = [] - const rawCache = new Map() - for (const u of listed) { - let raw = rawCache.get(u.txid) - if (!raw) { - raw = hexToBytes((await getJson<{ rawtx: string }>(`${ep.insight}/rawtx/${u.txid}`)).rawtx) - if (txid(raw) !== u.txid) throw new Error(`the block explorer returned a transaction that is not ${u.txid}`) - rawCache.set(u.txid, raw) - } - const o = parseOutputs(raw)[u.vout] - if (!o || bytesToHex(o.script) !== script) continue - if (o.value > BigInt(Number.MAX_SAFE_INTEGER)) throw new Error('deposit output too large') - out.push({ ...u, satoshis: Number(o.value), scriptPubKey: script }) - } - return out -} - -/** The height `txid` was mined at (null while unconfirmed): DAPI first, the explorer if DAPI fails. */ +/** The height `txid` was mined at (null while unconfirmed or unknown). */ export async function getTxHeight(ep: CoreEndpoints, txid: string): Promise { - const dapi = dapiOf(ep) - if (dapi) { - try { - return (await dapi.transaction(txid))?.height ?? null - } catch { - // fall through to the explorer - } - } - const tx = await getJson<{ blockheight?: number }>(`${ep.insight}/tx/${txid}`) - return typeof tx.blockheight === 'number' && tx.blockheight >= 0 ? tx.blockheight : null -} - -/** Whether the network knows `txid` (mempool or chain), through DAPI or the explorer. */ -async function txKnown(ep: CoreEndpoints, txid: string): Promise { - const dapi = dapiOf(ep) - if (dapi && (await dapi.transaction(txid).catch(() => null))) return true - return getJson(`${ep.insight}/tx/${txid}`).then( - () => true, - () => false, - ) + return (await dapiOf(ep).transaction(txid))?.height ?? null } /** - * Broadcast through DAPI, else the explorer. Re-sending an accepted transaction is harmless; - * a transaction the network already has counts as sent, so a lost response never strands the - * deposit. + * Broadcast through DAPI. Re-sending an accepted transaction is harmless; a transaction the + * network already has counts as sent, so a lost response never strands the deposit. */ export async function broadcastTx(ep: CoreEndpoints, rawHex: string, txid: string): Promise { const dapi = dapiOf(ep) - let dapiError: unknown = null - if (dapi) { - try { - await dapi.broadcast(hexToBytes(rawHex)) - return - } catch (e) { - dapiError = e - } - } try { - await getJson(`${ep.insight}/tx/send`, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ rawtx: rawHex }), - }) + await dapi.broadcast(hexToBytes(rawHex)) } catch (e) { - if (!(await txKnown(ep, txid))) throw dapiError ?? e + if (!(await dapi.transaction(txid).catch(() => null))) throw e } } @@ -333,7 +247,7 @@ export function buildAssetLock(utxos: readonly Utxo[], priv: Uint8Array): { raw: const fee = ASSET_LOCK_FEE_PER_INPUT * utxos.length const locked = total - fee if (locked <= 0) throw new Error('deposit too small to cover the fee') - // Every input's script must be the lock key's own P2PKH (verifiedUtxos checked the values). + // Every input's script must be the lock key's own P2PKH (the values come from the raw transactions, `DepositTracker`). const own = bytesToHex(p2pkh(hash160(secp.getPublicKey(priv, true)))) if (utxos.some((u) => u.scriptPubKey !== own)) throw new Error('an input is not paid to the deposit key') if (locked + fee !== total) throw new Error('asset-lock fee mismatch') @@ -382,40 +296,157 @@ export type LockProof = | { readonly type: 'instant'; readonly txid: string; readonly raw: Uint8Array; readonly islock: Uint8Array } | { readonly type: 'chain'; readonly txid: string; readonly height: number } -async function fetchIslock(rpc: string, id: string): Promise { - const res = await fetch(rpc, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ method: 'getislocks', params: [[id]] }), +/** How long, after the broadcast, an InstantSend lock is waited for before a chain-lock proof. */ +const ISLOCK_WAIT_MS = 60_000 +/** Feeds watching for the islock, each on its own node: one can be down or drop the lock. */ +const ISLOCK_FEEDS = 3 +/** The longest the broadcast waits for an islock feed to open. */ +const ISLOCK_OPEN_MS = 10_000 + +/** The txid (display hex) an ISDLOCK locks: version 1 | inputs (36 bytes each) | txid | cycle hash | BLS signature. */ +export function islockTxid(islock: Uint8Array): string | null { + if (islock[0] !== 1 || islock.length < 2) return null + let at = 2 + let inputs = islock[1] as number + if (inputs === 0xfd) { + inputs = (islock[2] as number) | ((islock[3] as number) << 8) + at = 4 + } else if (inputs > 0xfd) return null + at += inputs * 36 + if (islock.length !== at + 32 + 32 + 96) return null + return bytesToHex(islock.slice(at, at + 32).reverse()) +} + +/** + * A filter matching the asset lock: its txid and the outpoints it spends, each in Core's byte + * order (what rs-dapi matches) and as displayed (what the older JS DAPI matched). + */ +export function lockFilter(lock: { txid: string; raw: Uint8Array }): BloomFilter { + const both = (display: string, tail: Uint8Array = new Uint8Array(0)): Uint8Array[] => { + const b = hexToBytes(display) + return [concatBytes(b.slice().reverse(), tail), concatBytes(b, tail)] + } + const elements = both(lock.txid) + for (const i of parseTx(lock.raw).inputs) elements.push(...both(i.txid, u32(i.vout))) + return bloomFilter(elements) +} + +/** `p`, or null once `ms` pass first. */ +function within(p: Promise, ms: number): Promise { + let timer: ReturnType | undefined + const late = new Promise((r) => { + timer = setTimeout(() => r(null), ms) }) - if (!res.ok) return null - const data = (await res.json()) as { result?: { txid?: string; hex?: string }[] } - const hit = data.result?.find((r) => r?.txid === id && r.hex) - return hit?.hex ? hexToBytes(hit.hex) : null + return Promise.race([p, late]).finally(() => clearTimeout(timer)) } /** - * Wait until the asset lock is provable: an InstantSend lock where the network offers one - * (up to 90 s), else a chain-lock proof once the tx is mined and Platform's chain-locked Core - * height reaches it (`platformChainLockedHeight` is read from Platform status). + * Run `feed` on up to `lanes` nodes at once. A lane whose feed fails or ends with null moves to + * the next untried node (with `forever`, round the list again), `retryMs` later. Resolves with + * the first non-null result, or null once every lane gave up or `signal` stopped them. */ -export async function obtainLockProof( +function onNodes( + nodes: readonly string[], + lanes: number, + signal: AbortSignal, + feed: (node: string) => Promise, + opts: { forever?: boolean; retryMs?: number } = {}, +): Promise { + const order = shuffled(nodes) + let next = 0 + let done = false + return new Promise((resolve) => { + const lane = async (): Promise => { + while (!done && !signal.aborted) { + if (next >= order.length) { + if (!opts.forever) return + next = 0 + } + const result = await feed(order[next++] as string).catch(() => null) + if (result !== null) { + done = true + resolve(result) + return + } + if (opts.retryMs) await sleep(opts.retryMs, signal).catch(() => undefined) + } + } + const n = Math.max(1, Math.min(lanes, order.length)) + let left = n + for (let i = 0; i < n; i++) { + void lane().finally(() => { + if (--left === 0) resolve(null) + }) + } + }) +} + +/** + * The first islock for `lock` on feeds from `from`, on {@link ISLOCK_FEEDS} nodes at once; + * null once they all fail or `signal` stops them. `onOpen` fires when a feed is listening. + */ +function watchInstantLock( + dapi: DapiCore, + lock: { txid: string; raw: Uint8Array }, + from: number, + signal: AbortSignal, + onOpen: () => void, +): Promise { + const filter = lockFilter(lock) + return onNodes(dapi.nodes, ISLOCK_FEEDS, signal, async (node) => { + for await (const ev of dapi.watch(filter, from, { node, signal, onOpen })) { + if (ev.kind !== 'islocks') continue + const hit = ev.locks.find((l) => islockTxid(l) === lock.txid) + if (hit) return hit + } + return null + }) +} + +/** + * Broadcast the asset lock and wait until it is provable. Feeds watching for its InstantSend + * lock open first (an islock is sent once, live); the first matching one within a minute of + * the broadcast makes an instant proof. Otherwise, and for a lock already mined or locked, a chain-lock + * proof once the lock is mined and Platform's chain-locked Core height reaches it + * (`platformChainLockedHeight` is read from Platform status). Re-sending an accepted lock is + * harmless, so a resumed creation calls this again. + */ +export async function sendAndProveLock( ep: CoreEndpoints, lock: { txid: string; raw: Uint8Array }, platformChainLockedHeight: () => Promise, - opts: { signal?: AbortSignal; onStatus?: (s: string) => void; timeoutMs?: number } = {}, + opts: { signal?: AbortSignal; onStatus?: (s: string) => void; timeoutMs?: number; islockWaitMs?: number } = {}, ): Promise { + opts.signal?.throwIfAborted() + const dapi = dapiOf(ep) const deadline = Date.now() + (opts.timeoutMs ?? 20 * 60 * 1000) - if (ep.islockRpc) { + const known = await dapi.transaction(lock.txid).catch(() => null) + let height = known?.height ?? null + // Mined, or already InstantSend-locked on a resumed run (the islock is not sent again): a + // chain-lock proof. + if (height === null && known?.instantLocked !== true) { opts.onStatus?.('Waiting for InstantSend…') - const until = Date.now() + 90_000 - while (Date.now() < until) { - const islock = await fetchIslock(ep.islockRpc, lock.txid).catch(() => null) - if (islock) return { type: 'instant', txid: lock.txid, raw: lock.raw, islock } - await sleep(3000, opts.signal) + const stop = new AbortController() + const signal = opts.signal ? anyOf(opts.signal, stop.signal) : stop.signal + try { + const tip = await dapi.bestHeight().catch(() => null) + let opened = (): void => undefined + const open = new Promise((r) => { + opened = r + }) + // A couple of blocks back, so every node's history is short and its live feed starts at once. + const islock = tip === null ? Promise.resolve(null) : watchInstantLock(dapi, lock, tip - 2, signal, opened) + // Listening before the broadcast; a feed that never opens only costs the chain-lock wait. + await within(Promise.race([open, islock]), ISLOCK_OPEN_MS) + opts.signal?.throwIfAborted() + await broadcastTx(ep, bytesToHex(lock.raw), lock.txid) + const found = await within(islock, opts.islockWaitMs ?? ISLOCK_WAIT_MS) + opts.signal?.throwIfAborted() + if (found) return { type: 'instant', txid: lock.txid, raw: lock.raw, islock: found } + } finally { + stop.abort() } } - let height: number | null = null while (Date.now() < deadline) { if (height === null) { opts.onStatus?.('Waiting for the deposit to be mined…') @@ -508,15 +539,14 @@ function parseTx(raw: Uint8Array): { inputs: { txid: string; vout: number }[]; o return { inputs, outputs: parseOutputs(raw) } } -/** The explorer's view of the deposit, every output proven from its raw transaction. */ -async function explorerDeposit(ep: CoreEndpoints, address: string): Promise { - return verifiedUtxos(ep, address, await getUtxos(ep, address)) -} - -/** The Core height to start a deposit watch from (the tip, before the address is shown). */ +/** + * The Core height to start a deposit watch from (the tip, before the address is shown): null + * when no node answers. A build with no DAPI nodes throws, so no address is shown that nothing + * could watch. + */ export async function currentHeight(ep: CoreEndpoints): Promise { const dapi = dapiOf(ep) - return dapi ? dapi.bestHeight().catch(() => null) : null + return dapi.bestHeight().catch(() => null) } /** Where a deposit watch starts: a recorded height, else a height or start time. */ @@ -539,112 +569,142 @@ export async function watchFrom(dapi: DapiCore, start: WatchStart): Promise { +function addressFilter(address: string): BloomFilter { return bloomFilter([addressHash(address)], 0.0001, undefined, 50) } +/** How long a deposit check's feed must be quiet, once its history is in, before a payment stands. */ +const SETTLE_MS = 3_000 +/** + * The same for "nothing there": longer, since a node sends its mempool's matches in one batch + * once it has read them all, and a false zero could leave a payment behind. + */ +const SETTLE_EMPTY_MS = 15_000 +/** How long the second node is waited for once the first has answered. */ +const SECOND_OPINION_MS = 5_000 + +/** + * What one node's feed from `height` says `address` holds: its history block by block, then + * its mempool. The total stands once (about) every block to the tip is in and the feed has been + * quiet for {@link SETTLE_MS} ({@link SETTLE_EMPTY_MS} for zero); null if the feed ends first. + */ +async function heldOn(dapi: DapiCore, node: string, address: string, height: number, blocks: number, signal: AbortSignal): Promise { + const tracker = new DepositTracker(address) + const feed = dapi.watch(addressFilter(address), height, { node, signal }) + let seen = 0 + let quietSince = Date.now() + let pending = feed.next() + for (;;) { + // Returning (or failing) aborts the fetch under a read still in flight: observe it. + pending.catch(() => undefined) + const next = await within(pending, SETTLE_MS) + if (next === null) { + if (seen >= blocks && (tracker.total > 0 || Date.now() - quietSince >= SETTLE_EMPTY_MS)) return tracker.total + continue + } + if (next.done) return null + quietSince = Date.now() + if (next.value.kind === 'block') seen++ + else if (next.value.kind === 'txs') for (const raw of next.value.txs) tracker.ingest(raw) + pending = feed.next() + } +} + /** - * What `address` holds (duffs): the larger of DAPI's confirmed history from `from` and the - * explorer's view. Throws when neither can say — and when only DAPI answered zero, since its - * history leaves out the mempool and an unconfirmed payment would read as empty. + * What `address` holds (duffs), confirmed or in the mempool, from DAPI feeds from `from`: the + * larger of what two nodes say (one can lag or hide a payment). Throws when no node answers + * within `timeoutMs`. */ export async function depositHeld(ep: CoreEndpoints, address: string, from: WatchStart, timeoutMs = 60_000): Promise { const dapi = dapiOf(ep) - let viaDapi: number | null = null - if (dapi) { - try { - const height = await watchFrom(dapi, from) - const tracker = new DepositTracker(address) - const count = (await dapi.bestHeight()) - height + 1 - const signal = AbortSignal.timeout(timeoutMs) - for await (const txs of dapi.watch(addressFilter(address), height, { count: Math.max(1, count), signal })) { - for (const raw of txs) tracker.ingest(raw) + const run = new AbortController() + const timer = setTimeout(() => run.abort(), timeoutMs) + try { + const height = await watchFrom(dapi, from) + // A node a block or two behind the tip sends that many fewer. + const blocks = (await dapi.bestHeight()) - height - 1 + const order = shuffled(dapi.nodes) + const answers: number[] = [] + let answered = (): void => undefined + const first = new Promise((r) => { + answered = r + }) + const lanes = [0, 1].map(async (start) => { + for (let i = start; i < order.length && !run.signal.aborted; i += 2) { + const held = await heldOn(dapi, order[i] as string, address, height, blocks, run.signal).catch(() => null) + if (held !== null) { + answers.push(held) + answered() + return + } } - viaDapi = tracker.total - } catch { - // unknown through DAPI; the explorer may still say - } + }) + const all = Promise.all(lanes) + await Promise.race([all, first.then(() => within(all, SECOND_OPINION_MS))]) + if (answers.length > 0) return Math.max(...answers) + } catch { + // no node answered; reported below + } finally { + clearTimeout(timer) + run.abort() } - const viaExplorer = await getUtxos(ep, address).then( - (u) => u.reduce((s, x) => s + x.satoshis, 0), - () => null, - ) - if (viaExplorer !== null) return Math.max(viaExplorer, viaDapi ?? 0) - if (viaDapi !== null && viaDapi > 0) return viaDapi - throw new Error('could not check the deposit address (the network nodes see no confirmed payment and the block explorer is unreachable)') + throw new Error('could not check the deposit address (no Dash network node answered)') } -/** How long the DAPI feed may be silent before the explorer is asked as well. */ -const FEED_IDLE_MS = 30_000 +/** Feeds watching a deposit, each on its own node. */ +const DEPOSIT_FEEDS = 2 +/** A feed silent this long is replaced (a merkle block arrives for every new block, ~2.5 minutes). */ +const FEED_IDLE_MS = 10 * 60_000 /** * Wait until `address` holds at least `minDuffs`, and return its outputs. The deposit is seen - * on a DAPI bloom-filtered transaction feed from `from` (history, then the mempool and - * new blocks); the explorer is asked whenever the feed is idle, fails, or DAPI is not - * configured. + * on DAPI bloom-filtered transaction feeds from `from` (history, then the mempool and new + * blocks) on two nodes at once; a feed that fails, ends or goes silent moves to another node. */ export async function waitForDeposit( ep: CoreEndpoints, address: string, minDuffs: number, - opts: { signal?: AbortSignal; onSeen?: (duffs: number) => void; intervalMs?: number; from?: WatchStart } = {}, + opts: { signal?: AbortSignal; onSeen?: (duffs: number) => void; from?: WatchStart } = {}, ): Promise { const dapi = dapiOf(ep) const tracker = new DepositTracker(address) - const askExplorer = async (): Promise => { - const utxos = await explorerDeposit(ep, address).catch(() => null) - if (utxos) { - const total = utxos.reduce((s, u) => s + u.satoshis, 0) - opts.onSeen?.(Math.max(total, tracker.total)) - if (total >= minDuffs) return utxos - } - return null - } - for (;;) { - if (opts.signal?.aborted) throw new DOMException('cancelled', 'AbortError') - if (dapi) { - const run = new AbortController() - const stop = (): void => run.abort() - opts.signal?.addEventListener('abort', stop, { once: true }) - try { - const height = await watchFrom(dapi, opts.from ?? { startedAt: Date.now() }) - const feed = dapi.watch(addressFilter(address), height, { signal: run.signal }) - // One read in flight at a time: an idle timeout asks the explorer, then keeps waiting - // on the same read (a second next() would queue behind it and drop its batch). - let pending = feed.next() - // Returning (or failing) aborts the fetch under a read still in flight: observe it. - pending.catch(() => undefined) - for (;;) { - let timer: ReturnType | undefined - const idle = new Promise<'idle'>((r) => { - timer = setTimeout(() => r('idle'), FEED_IDLE_MS) - }) - const next = await Promise.race([pending, idle]) - clearTimeout(timer) - if (next === 'idle') { - const found = await askExplorer() - if (found) return found - continue + const from = opts.from ?? { startedAt: Date.now() } + const stop = new AbortController() + const signal = opts.signal ? anyOf(opts.signal, stop.signal) : stop.signal + try { + const found = await onNodes( + dapi.nodes, + DEPOSIT_FEEDS, + signal, + async (node) => { + // Ended on return too: a read still pending under an idle feed would otherwise hold it open. + const run = new AbortController() + try { + const feed = dapi.watch(addressFilter(address), await watchFrom(dapi, from), { node, signal: anyOf(signal, run.signal) }) + let pending = feed.next() + for (;;) { + pending.catch(() => undefined) + const next = await within(pending, FEED_IDLE_MS) + if (next === null || next.done) return null + if (next.value.kind === 'txs') { + for (const raw of next.value.txs) tracker.ingest(raw) + opts.onSeen?.(tracker.total) + if (tracker.total >= minDuffs) return tracker.utxos + } + pending = feed.next() } - if (next.done) break - for (const raw of next.value) tracker.ingest(raw) - opts.onSeen?.(tracker.total) - if (tracker.total >= minDuffs) return tracker.utxos - pending = feed.next() - pending.catch(() => undefined) + } finally { + run.abort() } - } catch (e) { - if (isAbort(e) && opts.signal?.aborted) throw e - // The feed failed or ended: ask the explorer, then reconnect. - } finally { - run.abort() - opts.signal?.removeEventListener('abort', stop) - } - } - const found = await askExplorer() + }, + { forever: true, retryMs: 5000 }, + ) if (found) return found - await sleep(dapi ? 5000 : (opts.intervalMs ?? 4000), opts.signal) + throw new DOMException('cancelled', 'AbortError') + } finally { + stop.abort() } } diff --git a/forge-web/lib/auth/auth-v2.test.ts b/forge-web/lib/auth/auth-v2.test.ts index 9bc849134..68bea4153 100644 --- a/forge-web/lib/auth/auth-v2.test.ts +++ b/forge-web/lib/auth/auth-v2.test.ts @@ -21,7 +21,7 @@ import { unlockWithPassphrase, unlockedSecret, } from './vault' -import { buildAssetLock, buildPayment, hash160, txid, verifiedUtxos, type Utxo } from './asset-lock' +import { DepositTracker, buildAssetLock, buildPayment, hash160, txid, type Utxo } from './asset-lock' import { masterMaterialFromFile } from './identity-file' import { base58CheckEncode } from './base58' import { encodeWif } from './wif' @@ -157,7 +157,7 @@ describe('asset-lock transaction', () => { expect(lock.txid).toBe('452aa5a12caf5b23d314ac9ae8ef129a283f9a4c72492cff1fc5bf9cfc6d4fff') }) - describe('a lying block explorer', () => { + describe('deposit outputs', () => { const priv = new Uint8Array(32).fill(5) const pub = secp.getPublicKey(priv, true) const script = `76a914${bytesToHex(hash160(pub))}88ac` @@ -169,33 +169,17 @@ describe('asset-lock transaction', () => { { address, duffs: 500_000_000 }, address, ) - const ep = { dapi: [], insight: 'https://explorer.invalid', islockRpc: null } - const serve = (rawtx: Uint8Array): void => { - vi.stubGlobal('fetch', async (url: string) => ({ ok: true, json: async () => ({ rawtx: bytesToHex(rawtx) }), url })) - } - - it('cannot make the lock burn the deposit by under-reporting its value', async () => { - serve(funding.raw) - const listed: Utxo[] = [{ txid: funding.txid, vout: 0, satoshis: 2_000_000, scriptPubKey: script, confirmations: 1 }] - const utxos = await verifiedUtxos(ep, address, listed) - expect(utxos[0]?.satoshis).toBe(500_000_000) - const lock = buildAssetLock(utxos, priv) - expect(lock.lockedDuffs).toBe(500_000_000 - 1000) - }) - - it('rejects a raw transaction that does not hash to the listed txid', async () => { - serve(funding.raw) - const listed: Utxo[] = [{ txid: 'ee'.repeat(32), vout: 0, satoshis: 500_000_000, scriptPubKey: script, confirmations: 1 }] - await expect(verifiedUtxos(ep, address, listed)).rejects.toThrow(/not ee/) - }) - - it('drops outputs that do not pay the deposit address', async () => { - serve(funding.raw) - const listed: Utxo[] = [{ txid: funding.txid, vout: 1, satoshis: 500_000_000, scriptPubKey: script, confirmations: 1 }] + it('takes values from the raw transaction, so the lock cannot burn the deposit', () => { + const t = new DepositTracker(address) + t.ingest(funding.raw) // Output 1 is the change back to the same address in this fixture: still ours. - expect((await verifiedUtxos(ep, address, listed)).length).toBe(1) - const other: Utxo[] = [{ txid: funding.txid, vout: 7, satoshis: 1, scriptPubKey: script, confirmations: 1 }] - expect(await verifiedUtxos(ep, address, other)).toEqual([]) + expect(t.utxos.map((u) => [u.txid, u.vout])).toEqual([ + [funding.txid, 0], + [funding.txid, 1], + ]) + const pay = t.utxos.filter((u) => u.vout === 0) + expect(pay[0]?.satoshis).toBe(500_000_000) + expect(buildAssetLock(pay, priv).lockedDuffs).toBe(500_000_000 - 1000) }) it('refuses to sign inputs whose script is not the deposit key', () => { diff --git a/forge-web/lib/auth/create-identity.test.ts b/forge-web/lib/auth/create-identity.test.ts index 5ac7fb97f..bcf7b67c7 100644 --- a/forge-web/lib/auth/create-identity.test.ts +++ b/forge-web/lib/auth/create-identity.test.ts @@ -68,9 +68,8 @@ vi.mock('@dashevo/evo-sdk', () => { } }) vi.mock('./asset-lock', () => ({ - coreEndpoints: () => ({ insight: 'https://insight.invalid' }), - broadcastTx: async () => undefined, - obtainLockProof: async () => ({ type: 'chain', txid: 'ab'.repeat(32), height: 100 }), + coreEndpoints: () => ({ dapi: [] }), + sendAndProveLock: async () => ({ type: 'chain', txid: 'ab'.repeat(32), height: 100 }), currentHeight: async () => 1, waitForDeposit: async () => [], buildAssetLock: () => { diff --git a/forge-web/lib/auth/create-identity.ts b/forge-web/lib/auth/create-identity.ts index 205ef392a..600e9ec80 100644 --- a/forge-web/lib/auth/create-identity.ts +++ b/forge-web/lib/auth/create-identity.ts @@ -6,8 +6,8 @@ * 2. the deposit address (the mnemonic's BIP-44 asset-lock key) and QR; funds arrive from * any wallet (or the faucet on dev networks) and are seen on a DAPI transaction feed * that replays from the Core height recorded when the creation started; - * 3. the asset lock is built, saved to the journal, broadcast, and proven (InstantSend or - * chain lock); + * 3. the asset lock is built, saved to the journal, broadcast, and proven (its InstantSend + * lock, off DAPI feeds opened before the broadcast, or a chain lock); * 4. this browser's limited key is generated and **stored in the vault first**, then one * IdentityCreate registers the canonical key set (MASTER, HIGH, CRITICAL auth, CRITICAL * transfer, MEDIUM encryption; DIP-13 from the mnemonic, so `dg` and the bridge open the @@ -49,12 +49,11 @@ import { isStaleConnectionError } from '../sdk/unreachable' import { assertWritesAllowed, serialized } from '../sdk/write' import { errorMessage } from '../utils' import { - broadcastTx, buildAssetLock, coreEndpoints, currentHeight, depositHeld, - obtainLockProof, + sendAndProveLock, waitForDeposit, wifBytes, type CoreEndpoints, @@ -393,10 +392,9 @@ export async function createIdentityFromMnemonic( } const lockTxid = journal.lockTxid as string const lockRaw = hexToBytes(journal.lockRaw as string) - // Idempotent: re-sending an accepted transaction is harmless (DAPI, else the explorer). - await broadcastTx(ep, bytesToHex(lockRaw), lockTxid) + // Broadcast (again, on a resumed run: harmless) and prove. params.onStage?.('proving') - const proof: LockProof = await obtainLockProof(ep, { txid: lockTxid, raw: lockRaw }, () => platformClh(sdk), { + const proof: LockProof = await sendAndProveLock(ep, { txid: lockTxid, raw: lockRaw }, () => platformClh(sdk), { signal: params.signal, onStatus: (s) => params.onStage?.('proving', s), }) diff --git a/forge-web/lib/auth/dapi-core.test.ts b/forge-web/lib/auth/dapi-core.test.ts index 923d8e427..6a17a419b 100644 --- a/forge-web/lib/auth/dapi-core.test.ts +++ b/forge-web/lib/auth/dapi-core.test.ts @@ -1,18 +1,37 @@ /** - * Identity creation without a block explorer: DAPI Core over gRPC-web (framing, bloom filter, - * the transaction feed) and the asset-lock helpers falling back from a down Insight — the - * 2026-09-27 moutai outage, where every explorer request returned 503. + * Identity creation through the evonodes alone: DAPI Core over gRPC-web (framing, bloom filter, + * the transaction feed and its InstantSend locks) and the asset-lock helpers built on it. No + * block explorer or JSON-RPC service is asked (the 2026-09-27 moutai outage had every explorer + * request return 503; testnet's public `getislocks` RPC stopped following the chain). */ import * as secp from '@noble/secp256k1' -import { concatBytes } from '@noble/hashes/utils.js' +import { bytesToHex, concatBytes, hexToBytes } from '@noble/hashes/utils.js' import { afterEach, describe, expect, it, vi } from 'vitest' -import { DapiCore, bloomContains, bloomFilter, decode, field, murmur3, takeFrames } from './dapi-core' -import { DepositTracker, broadcastTx, buildPayment, getTxHeight, hash160, txid, waitForDeposit, type CoreEndpoints, type Utxo } from './asset-lock' +import { DapiCore, bloomContains, bloomFilter, decode, field, murmur3, takeFrames, type FeedEvent } from './dapi-core' +import { + DepositTracker, + broadcastTx, + buildAssetLock, + buildPayment, + currentHeight, + depositHeld, + getTxHeight, + hash160, + islockTxid, + lockFilter, + sendAndProveLock, + txid, + waitForDeposit, + watchFrom, + type CoreEndpoints, + type Utxo, +} from './asset-lock' import { base58CheckEncode } from './base58' const SERVICE = 'org.dash.platform.dapi.v0.Core' +const TIP = 88_900 function frame(flag: number, payload: Uint8Array): Uint8Array { const h = new Uint8Array(5) @@ -22,70 +41,119 @@ function frame(flag: number, payload: Uint8Array): Uint8Array { } const trailer = (status: number, message = ''): Uint8Array => frame(0x80, new TextEncoder().encode(`grpc-status:${status}\r\n${message ? `grpc-message:${message}\r\n` : ''}`)) +const islockFrame = (lock: Uint8Array): Uint8Array => frame(0, field(2, field(1, lock))) + +const PRIV = new Uint8Array(32).fill(9) /** A deposit key, its address, and a funding transaction paying it `duffs` (output 0). */ function deposit(duffs: number): { address: string; raw: Uint8Array; id: string; script: string } { - const priv = new Uint8Array(32).fill(9) - const pub = secp.getPublicKey(priv, true) - const script = `76a914${Array.from(hash160(pub), (b) => b.toString(16).padStart(2, '0')).join('')}88ac` + const pub = secp.getPublicKey(PRIV, true) + const script = `76a914${bytesToHex(hash160(pub))}88ac` const address = base58CheckEncode(new Uint8Array([140, ...hash160(pub)])) const utxo: Utxo = { txid: 'cd'.repeat(32), vout: 0, satoshis: duffs + 10_000, scriptPubKey: script, confirmations: 9 } - const pay = buildPayment([utxo], priv, { address, duffs }, address, 10_000) + const pay = buildPayment([utxo], PRIV, { address, duffs }, address, 10_000) return { address, raw: pay.raw, id: pay.txid, script } } +/** An ISDLOCK (version 1) for `txid` spending `inputs`. */ +function islock(id: string, inputs = 1): Uint8Array { + return concatBytes(Uint8Array.of(1, inputs), new Uint8Array(36 * inputs).fill(7), hexToBytes(id).reverse(), new Uint8Array(32).fill(8), new Uint8Array(96).fill(9)) +} + /** - * A fetch that plays a down Insight (503 for every explorer URL) and a DAPI node serving the - * Core service from `txs`, a transaction feed of `feed`, and a broadcast log. + * A fetch that plays DAPI nodes serving the Core service: transactions in `txs`, a transaction + * feed (one merkle block per historical block to {@link TIP}, then `feed`, then live), and a + * broadcast log. A broadcast sends `islocksOnBroadcast(raw)` down every open live feed and, + * with `mineOnBroadcast`, makes the transaction known at that height. */ -function network(opts: { feed?: Uint8Array[][]; txs?: Map }) { +function network(opts: { + feed?: Uint8Array[][] + txs?: Map + /** The feed's transactions come this long after its history (a slow mempool read). */ + feedDelayMs?: number + islocksOnBroadcast?: (raw: Uint8Array) => Uint8Array[] + mineOnBroadcast?: number + broadcastFails?: boolean +}) { const broadcasts: Uint8Array[] = [] - const explorerHits: string[] = [] + const calls: string[] = [] + const live = new Set>() + const txs = opts.txs ?? new Map() const fetchImpl = vi.fn(async (url: string, init?: RequestInit): Promise => { - if (url.startsWith('https://insight.invalid')) { - explorerHits.push(url) - return new Response('Back-end server is at capacity', { status: 503 }) - } const method = url.slice(url.indexOf(SERVICE) + SERVICE.length + 1) + calls.push(method) const body = (init?.body as Uint8Array).slice(5) const req = decode(body) - if (method === 'getBestBlockHeight') return new Response(concatBytes(frame(0, field(1, 88_900)), trailer(0))) + if (method === 'getBestBlockHeight') return new Response(concatBytes(frame(0, field(1, TIP)), trailer(0))) if (method === 'broadcastTransaction') { - broadcasts.push(req.get(1)?.[0] as Uint8Array) + const raw = req.get(1)?.[0] as Uint8Array + if (opts.broadcastFails) return new Response(null, { headers: { 'grpc-status': '3', 'grpc-message': 'txn-already-known' } }) + broadcasts.push(raw) + if (opts.mineOnBroadcast) txs.set(txid(raw), { raw, height: opts.mineOnBroadcast }) + for (const c of live) for (const l of opts.islocksOnBroadcast?.(raw) ?? []) c.enqueue(islockFrame(l)) return new Response(concatBytes(frame(0, field(1, 'ok')), trailer(0))) } if (method === 'getTransaction') { const id = new TextDecoder().decode(req.get(1)?.[0] as Uint8Array) - const tx = opts.txs?.get(id) + const tx = txs.get(id) if (!tx) return new Response(null, { headers: { 'grpc-status': '5', 'grpc-message': 'Transaction%20not%20found' } }) - const msg = concatBytes(field(1, tx.raw), field(2, new Uint8Array(32).fill(1)), field(3, tx.height), field(6, true)) + const mined = tx.height > 0 + const msg = concatBytes( + field(1, tx.raw), + ...(mined ? [field(2, new Uint8Array(32).fill(1)), field(3, tx.height), field(6, true)] : []), + ...(tx.instantLocked ? [field(5, true)] : []), + ) return new Response(concatBytes(frame(0, msg), trailer(0))) } if (method === 'subscribeToTransactionsWithProofs') { - const chunks = (opts.feed ?? []).map((txs) => frame(0, field(1, concatBytes(...txs.map((t) => field(1, t)))))) - // A merkle block (field 3) and an islock batch (field 2) must be skipped. - chunks.unshift(frame(0, field(3, new Uint8Array(80)))) - chunks.push(frame(0, field(2, field(1, new Uint8Array(4))))) - const history = (req.get(4)?.[0] ?? 0) as number + const from = (req.get(3)?.[0] ?? 1) as number + const count = (req.get(4)?.[0] ?? 0) as number + const blocks = Array.from({ length: Math.max(0, (count || TIP - from + 1)) }, () => frame(0, field(3, new Uint8Array(80)))) + const feed = (opts.feed ?? []).map((t) => frame(0, field(1, concatBytes(...t.map((x) => field(1, x)))))) + const chunks = opts.feedDelayMs ? blocks : [...blocks, ...feed] + const signal = init?.signal + let ctl: ReadableStreamDefaultController | undefined const stream = new ReadableStream({ start(c) { + ctl = c for (const ch of chunks) c.enqueue(ch) // History mode (count > 0) ends with OK trailers; a live subscription stays open. - if (history > 0) { + if (count > 0) { c.enqueue(trailer(0)) c.close() + } else live.add(c) + if (opts.feedDelayMs) { + setTimeout(() => { + try { + for (const ch of feed) c.enqueue(ch) + } catch { + // closed meanwhile + } + }, opts.feedDelayMs) } }, + cancel() { + if (ctl) live.delete(ctl) + }, + }) + signal?.addEventListener('abort', () => { + if (ctl) live.delete(ctl) + try { + ctl?.error(new DOMException('aborted', 'AbortError')) + } catch { + // already closed + } }) return new Response(stream) } return new Response(null, { status: 404 }) }) vi.stubGlobal('fetch', fetchImpl) - return { fetchImpl, broadcasts, explorerHits } + return { fetchImpl, broadcasts, calls, live } } -const EP: CoreEndpoints = { dapi: ['https://dapi.invalid:1443'], insight: 'https://insight.invalid/insight-api', islockRpc: null } +const NODES = ['https://dapi-a.invalid:1443', 'https://dapi-b.invalid:1443', 'https://dapi-c.invalid:1443', 'https://dapi-d.invalid:1443'] +const EP: CoreEndpoints = { dapi: NODES } describe('DAPI Core client', () => { afterEach(() => vi.unstubAllGlobals()) @@ -132,12 +200,27 @@ describe('DAPI Core client', () => { expect(bloomContains(f, new Uint8Array(20).fill(4))).toBe(false) expect(f.flags).toBe(1) expect(f.data.length).toBeLessThanOrEqual(36_000) - // Room for the outpoints BLOOM_UPDATE_ALL adds: 50 elements at 1e-4 is 119 bytes. - expect(bloomFilter([hash], 0.0001, 7, 50).data.length).toBe(119) + // Never under DAPI's 200-byte floor (50 elements at 1e-4 alone would be 119 bytes). + expect(f.data.length).toBe(200) + expect(bloomFilter([hash], 0.0001, 7, 50).data.length).toBe(200) + expect(bloomFilter([hash], 0.0001, 7, 500).data.length).toBe(1198) + }) + + it('yields matched transactions, islocks and merkle blocks from the feed', async () => { + const d = deposit(1_000_000) + network({ feed: [[d.raw]] }) + const core = new DapiCore(EP.dapi) + const events: FeedEvent[] = [] + let opened = false + for await (const ev of core.watch(bloomFilter([new Uint8Array(20)]), TIP - 1, { count: 2, node: NODES[1], onOpen: () => (opened = true) })) events.push(ev) + expect(opened).toBe(true) + expect(events.map((e) => e.kind)).toEqual(['block', 'block', 'txs']) + const sub = vi.mocked(fetch).mock.calls.find(([u]) => String(u).endsWith('subscribeToTransactionsWithProofs')) + expect(String(sub?.[0]).startsWith(NODES[1] as string)).toBe(true) }) }) -describe('identity creation with Insight down (HTTP 503)', () => { +describe('identity creation through DAPI alone', () => { afterEach(() => vi.unstubAllGlobals()) it('sees the deposit on the DAPI feed, broadcasts and reads the height through DAPI', async () => { @@ -154,12 +237,21 @@ describe('identity creation with Insight down (HTTP 503)', () => { await broadcastTx(EP, '00', 'ab'.repeat(32)) expect(net.broadcasts.length).toBe(1) expect(await getTxHeight(EP, 'ab'.repeat(32))).toBe(88_861) - expect(net.explorerHits).toEqual([]) }) - it('without DAPI the explorer outage is still an error (nothing silently succeeds)', async () => { - network({}) - await expect(broadcastTx({ ...EP, dapi: [] }, '00', 'ab'.repeat(32))).rejects.toThrow(/503/) + it('a refused broadcast of a transaction the network has counts as sent; an unknown one throws', async () => { + const d = deposit(1_000_000) + network({ broadcastFails: true, txs: new Map([[d.id, { raw: d.raw, height: 0 }]]) }) + await broadcastTx(EP, bytesToHex(d.raw), d.id) + await expect(broadcastTx(EP, '00', 'ee'.repeat(32))).rejects.toThrow(/already-known/) + }) + + it('without DAPI nodes nothing silently succeeds, and no watch start is offered', async () => { + await expect(broadcastTx({ dapi: [] }, '00', 'ab'.repeat(32))).rejects.toThrow(/no DAPI nodes/) + await expect(currentHeight({ dapi: [] })).rejects.toThrow(/no DAPI nodes/) + // A node that does not answer is only "height unknown". + vi.stubGlobal('fetch', async () => new Response(null, { status: 503 })) + expect(await currentHeight(EP)).toBeNull() }) it('the tracker drops outputs a later transaction spends', () => { @@ -182,7 +274,6 @@ describe('where a deposit watch starts', () => { it('uses the recorded height, else rewinds past the creation start', async () => { network({}) - const { watchFrom } = await import('./asset-lock') const core = new DapiCore(EP.dapi) expect(await watchFrom(core, 88_000)).toBe(88_000 - 6) // Started 59 minutes ago: 24 blocks at 2.5 minutes (rounded up), plus the 50-block margin, from @@ -192,12 +283,128 @@ describe('where a deposit watch starts', () => { expect(await watchFrom(core, { startedAt: 0 })).toBe(88_900 - 4032) }) - it('a DAPI-only zero is not "empty" (the mempool is not in its history); a payment is seen', async () => { - const { depositHeld } = await import('./asset-lock') + /** `depositHeld`, with fake timers run until it settles. */ + async function held(address: string): Promise { + vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'Date'] }) + try { + let out: number | Error | undefined + depositHeld(EP, address, TIP - 3).then( + (n) => (out = n), + (e: Error) => (out = e), + ) + for (let i = 0; i < 200 && out === undefined; i++) await vi.advanceTimersByTimeAsync(500) + if (out instanceof Error) throw out + if (out === undefined) throw new Error('depositHeld did not settle') + return out + } finally { + vi.useRealTimers() + } + } + + it('what the address holds: zero once the history is in, else what the feed paid it (mempool included)', async () => { const d = deposit(4_000_000) network({}) - await expect(depositHeld(EP, d.address, 88_000)).rejects.toThrow(/could not check/) + expect(await held(d.address)).toBe(0) network({ feed: [[d.raw]] }) - expect(await depositHeld(EP, d.address, 88_000)).toBe(4_000_000) + expect(await held(d.address)).toBe(4_000_000) + }) + + it('a mempool batch that comes seconds after the history still counts (no false zero)', async () => { + const d = deposit(4_000_000) + network({ feed: [[d.raw]], feedDelayMs: 8_000 }) + expect(await held(d.address)).toBe(4_000_000) + }) + + it('no node answering is an error, not "empty"', async () => { + vi.stubGlobal('fetch', async () => new Response(null, { status: 503 })) + await expect(depositHeld(EP, deposit(1).address, TIP - 3)).rejects.toThrow(/could not check/) + }) +}) + +describe('the asset lock\'s InstantSend proof', () => { + afterEach(() => vi.unstubAllGlobals()) + + /** A signed asset lock spending a deposit, and its funding output. */ + function assetLock(): { txid: string; raw: Uint8Array; funding: string } { + const d = deposit(3_000_000) + const lock = buildAssetLock([{ txid: d.id, vout: 0, satoshis: 3_000_000, scriptPubKey: d.script, confirmations: 0 }], PRIV) + return { txid: lock.txid, raw: lock.raw, funding: d.id } + } + + it('reads the txid an ISDLOCK locks, and refuses malformed ones', () => { + const id = 'ab'.repeat(31) + 'cd' + expect(islockTxid(islock(id))).toBe(id) + expect(islockTxid(islock(id, 3))).toBe(id) + expect(islockTxid(islock(id).slice(1))).toBeNull() + expect(islockTxid(islock(id).slice(0, -1))).toBeNull() + expect(islockTxid(new Uint8Array(0))).toBeNull() + }) + + it('the lock filter matches its txid and the outpoint it spends in both byte orders', () => { + const lock = assetLock() + const f = lockFilter(lock) + const vout = Uint8Array.of(0, 0, 0, 0) + expect(f.data.length).toBeGreaterThanOrEqual(200) + for (const id of [lock.txid, lock.funding]) { + const display = hexToBytes(id) + const internal = display.slice().reverse() + const tail = id === lock.txid ? new Uint8Array(0) : vout + expect(bloomContains(f, concatBytes(internal, tail))).toBe(true) + expect(bloomContains(f, concatBytes(display, tail))).toBe(true) + } + expect(bloomContains(f, new Uint8Array(32).fill(1))).toBe(false) + }) + + it('opens feeds on three nodes before the broadcast and proves with the islock they carry', async () => { + const lock = assetLock() + const net = network({ islocksOnBroadcast: (raw) => [islock('ee'.repeat(32)), islock(txid(raw))] }) + const proof = await sendAndProveLock(EP, lock, async () => null) + expect(proof).toEqual({ type: 'instant', txid: lock.txid, raw: lock.raw, islock: islock(lock.txid) }) + expect(net.broadcasts).toEqual([lock.raw]) + const subs = net.fetchImpl.mock.calls.filter(([u]) => String(u).endsWith('subscribeToTransactionsWithProofs')).map(([u]) => String(u).split('/org.')[0]) + expect(new Set(subs).size).toBe(3) + expect(net.calls.indexOf('subscribeToTransactionsWithProofs')).toBeLessThan(net.calls.indexOf('broadcastTransaction')) + // A couple of blocks back from the tip, live (count 0). + const req = decode(((net.fetchImpl.mock.calls.find(([u]) => String(u).endsWith('subscribeToTransactionsWithProofs'))?.[1] as RequestInit).body as Uint8Array).slice(5)) + expect(req.get(3)?.[0]).toBe(TIP - 2) + expect(req.get(4)).toBeUndefined() + // Every feed is closed once the proof is in. + await vi.waitFor(() => expect(net.live.size).toBe(0)) + }) + + it('without a matching islock, proves with a chain lock once Platform reaches the block', async () => { + const lock = assetLock() + const net = network({ islocksOnBroadcast: () => [islock('ee'.repeat(32))], mineOnBroadcast: 88_901 }) + const status: string[] = [] + const proof = await sendAndProveLock(EP, lock, async () => 88_901, { islockWaitMs: 50, onStatus: (s) => status.push(s) }) + expect(proof).toEqual({ type: 'chain', txid: lock.txid, height: 88_901 }) + expect(net.broadcasts.length).toBe(1) + expect(status[0]).toMatch(/InstantSend/) + await vi.waitFor(() => expect(net.live.size).toBe(0)) + }) + + it('a resumed lock already InstantSend-locked in the mempool goes straight to a chain lock', async () => { + const lock = assetLock() + const net = network({ txs: new Map([[lock.txid, { raw: lock.raw, height: 0, instantLocked: true }]]) }) + const proof = await sendAndProveLock(EP, lock, async () => null, { timeoutMs: 100 }).catch((e: Error) => e) + // Not mined yet: no islock wait, no broadcast, no feed; the chain-lock wait times out. + expect(proof).toBeInstanceOf(Error) + expect(net.broadcasts).toEqual([]) + expect(net.calls).not.toContain('subscribeToTransactionsWithProofs') + }, 10_000) // one 5-second chain-lock poll + + it('an aborted signal stops before anything is asked', async () => { + const net = network({}) + await expect(sendAndProveLock(EP, assetLock(), async () => null, { signal: AbortSignal.abort() })).rejects.toThrow() + expect(net.calls).toEqual([]) + }) + + it('a lock already mined is neither re-sent nor waited on for an islock', async () => { + const lock = assetLock() + const net = network({ txs: new Map([[lock.txid, { raw: lock.raw, height: 88_870 }]]) }) + const proof = await sendAndProveLock(EP, lock, async () => 88_880) + expect(proof).toEqual({ type: 'chain', txid: lock.txid, height: 88_870 }) + expect(net.broadcasts).toEqual([]) + expect(net.calls).not.toContain('subscribeToTransactionsWithProofs') }) }) diff --git a/forge-web/lib/auth/dapi-core.ts b/forge-web/lib/auth/dapi-core.ts index a6b0a030e..eea01101e 100644 --- a/forge-web/lib/auth/dapi-core.ts +++ b/forge-web/lib/auth/dapi-core.ts @@ -10,16 +10,24 @@ * getBestBlockHeight {} → { uint32 height = 1 } * subscribeToTransactionsWithProofs (server stream) * { BloomFilter bloom_filter = 1; uint32 from_block_height = 3; - * uint32 count = 4 } → { RawTransactions raw_transactions = 1 | … } + * uint32 count = 4 } → { RawTransactions raw_transactions = 1 | + * InstantSendLockMessages instant_send_lock_messages = 2 | + * bytes raw_merkle_block = 3 } * * The evonodes' gateway allows cross-origin gRPC-web (`x-grpc-web`, `content-type`), so no - * block explorer or proxy is involved. + * block explorer, RPC service or proxy is involved. */ import { concatBytes } from '@noble/hashes/utils.js' +import { anyOf } from '../sdk/budget' + const SERVICE = 'org.dash.platform.dapi.v0.Core' const CALL_TIMEOUT_MS = 20_000 +/** How long a node may take to accept a stream (a silent one would hold the caller's lane). */ +const OPEN_TIMEOUT_MS = 10_000 +/** The smallest filter sent: DAPI nodes treat tinier ones as saturated (govhub `bloom.ts`). */ +const MIN_BLOOM_BYTES = 200 const GRPC_NOT_FOUND = 5 /** gRPC codes worth another node: CANCELLED, UNKNOWN, DEADLINE_EXCEEDED, RESOURCE_EXHAUSTED, INTERNAL, UNAVAILABLE. */ const RETRYABLE = new Set([1, 2, 4, 8, 13, 14]) @@ -152,7 +160,7 @@ export interface BloomFilter { /** * A filter holding `elements`, sized like Dash Core's CBloomFilter for `capacity` elements - * (default: just these) at `fpRate`. + * (default: just these) at `fpRate`, and never under {@link MIN_BLOOM_BYTES}. */ export function bloomFilter( elements: readonly Uint8Array[], @@ -161,7 +169,7 @@ export function bloomFilter( capacity = elements.length, ): BloomFilter { const n = Math.max(1, capacity, elements.length) - const size = Math.max(1, Math.min(Math.floor((-1 / (Math.LN2 * Math.LN2)) * n * Math.log(fpRate) / 8), 36_000)) + const size = Math.max(MIN_BLOOM_BYTES, Math.min(Math.floor((-1 / (Math.LN2 * Math.LN2)) * n * Math.log(fpRate) / 8), 36_000)) const hashFuncs = Math.max(1, Math.min(Math.floor(((size * 8) / n) * Math.LN2), 50)) const data = new Uint8Array(size) for (const e of elements) { @@ -220,7 +228,7 @@ function statusError(method: string, status: number, message: string | null | un return new DapiError(`${method}: ${message ? decodeURIComponent(message) : `gRPC status ${status}`}`, status) } -function shuffled(list: readonly T[]): T[] { +export function shuffled(list: readonly T[]): T[] { const a = [...list] for (let i = a.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)) @@ -229,6 +237,14 @@ function shuffled(list: readonly T[]): T[] { return a } +/** One message of a transaction feed. */ +export type FeedEvent = + | { readonly kind: 'txs'; readonly txs: Uint8Array[] } + /** InstantSend locks (raw ISDLOCK bytes) of matched transactions; only sent live, never replayed. */ + | { readonly kind: 'islocks'; readonly locks: Uint8Array[] } + /** A block's filtered merkle block: the history replays one per block, then one per new block. */ + | { readonly kind: 'block' } + export interface CoreTx { readonly raw: Uint8Array /** Mined height; null while in the mempool. */ @@ -307,18 +323,37 @@ export class DapiCore { } /** - * Stream the raw transactions `filter` matches from block `fromHeight`: history, then (with - * `count` 0) the mempool and new blocks. Yields one batch per RawTransactions message and - * returns when the node ends the stream. + * Stream what `filter` matches from block `fromHeight` on one node (`opts.node`, else a random + * one): history, then (with `count` 0) the mempool and new blocks, with the InstantSend locks + * of matched transactions as they arrive. `onOpen` fires once the node accepts the stream. + * Returns when the node ends it. */ - async *watch(filter: BloomFilter, fromHeight: number, opts: { count?: number; signal?: AbortSignal } = {}): AsyncGenerator { + async *watch( + filter: BloomFilter, + fromHeight: number, + opts: { count?: number; signal?: AbortSignal; node?: string; onOpen?: () => void } = {}, + ): AsyncGenerator { const bloom = concatBytes(field(1, filter.data), field(2, filter.hashFuncs), field(3, filter.tweak), field(4, filter.flags)) const request = concatBytes(field(1, bloom), field(3, Math.max(1, fromHeight)), ...(opts.count ? [field(4, opts.count)] : [])) - const node = shuffled(this.nodes)[0] as string - const res = await this.post(node, 'subscribeToTransactionsWithProofs', request, opts.signal) + const node = opts.node ?? (shuffled(this.nodes)[0] as string) + // The caller's signal stops the stream, and so does a node that has not accepted it within + // OPEN_TIMEOUT_MS. + const tooSlow = new AbortController() + const opening = setTimeout(() => tooSlow.abort(), OPEN_TIMEOUT_MS) + let res: Response + try { + res = await this.post(node, 'subscribeToTransactionsWithProofs', request, opts.signal ? anyOf(opts.signal, tooSlow.signal) : tooSlow.signal) + } catch (e) { + if (tooSlow.signal.aborted && !opts.signal?.aborted) throw new DapiError(`subscribeToTransactionsWithProofs to ${node} timed out`, 4) + throw e + } finally { + clearTimeout(opening) + } if (!res.body) throw new DapiError('subscribeToTransactionsWithProofs: no response stream', 14) + opts.onOpen?.() const reader = res.body.getReader() let buf: Uint8Array = new Uint8Array(0) + const list = (b: Uint8Array): Uint8Array[] => (decode(b).get(1) ?? []).filter((t): t is Uint8Array => t instanceof Uint8Array) try { for (;;) { const { value, done } = await reader.read() @@ -326,11 +361,14 @@ export class DapiCore { const { messages, trailers, rest } = takeFrames(concatBytes(buf, value)) buf = rest for (const msg of messages) { - const raw = bytesOf(decode(msg), 1) - if (raw.length > 0) { - const txs = (decode(raw).get(1) ?? []).filter((t): t is Uint8Array => t instanceof Uint8Array) - if (txs.length > 0) yield txs - } + const m = decode(msg) + if (m.has(1)) { + const txs = list(bytesOf(m, 1)) + if (txs.length > 0) yield { kind: 'txs', txs } + } else if (m.has(2)) { + const locks = list(bytesOf(m, 2)) + if (locks.length > 0) yield { kind: 'islocks', locks } + } else if (m.has(3)) yield { kind: 'block' } } if (trailers) { const status = Number(trailers['grpc-status'] ?? 0) diff --git a/forge-web/lib/auth/identity-top-up.test.ts b/forge-web/lib/auth/identity-top-up.test.ts index 326a3ecf6..4bcbf7497 100644 --- a/forge-web/lib/auth/identity-top-up.test.ts +++ b/forge-web/lib/auth/identity-top-up.test.ts @@ -46,11 +46,11 @@ vi.mock('@dashevo/evo-sdk', () => ({ }, })) vi.mock('./asset-lock', () => ({ - coreEndpoints: () => ({ insight: 'https://insight.invalid' }), - broadcastTx: async () => { + coreEndpoints: () => ({ dapi: [] }), + sendAndProveLock: async () => { chain.broadcasts++ + return { type: 'chain', txid: 'ab'.repeat(32), height: 100 } }, - obtainLockProof: async () => ({ type: 'chain', txid: 'ab'.repeat(32), height: 100 }), currentHeight: async () => chain.height, depositHeld: async () => { if (chain.left === null) throw new Error('unreachable') diff --git a/forge-web/lib/auth/identity-top-up.ts b/forge-web/lib/auth/identity-top-up.ts index 3a2a7490d..db59134c7 100644 --- a/forge-web/lib/auth/identity-top-up.ts +++ b/forge-web/lib/auth/identity-top-up.ts @@ -32,7 +32,7 @@ import { authSdk, isAbort } from '../sdk/facade' import { assertWritesAllowed, balanceBeforeWrite } from '../sdk/write' import { evoSdkService } from '../sdk/service' import { errorMessage } from '../utils' -import { broadcastTx, buildAssetLock, coreEndpoints, currentHeight, depositHeld, obtainLockProof, waitForDeposit, wifBytes, type CoreEndpoints } from './asset-lock' +import { buildAssetLock, coreEndpoints, currentHeight, depositHeld, sendAndProveLock, waitForDeposit, wifBytes, type CoreEndpoints } from './asset-lock' import { assetLockUse, platformClh } from './create-identity' import { deriveAt, deriveMasterKey, normalizeMnemonic, topUpKeyPath } from './hd' import { controlsKey } from './wif' @@ -256,10 +256,9 @@ async function runTopUp( } const lockTxid = journal.lockTxid as string const lockRaw = hexToBytes(journal.lockRaw as string) - // Idempotent: re-sending an accepted transaction is harmless. - await broadcastTx(ep, bytesToHex(lockRaw), lockTxid) + // Broadcast (again, on a resumed run: harmless) and prove. params.onStage?.('proving') - const proof = await obtainLockProof(ep, { txid: lockTxid, raw: lockRaw }, () => platformClh(sdk), { + const proof = await sendAndProveLock(ep, { txid: lockTxid, raw: lockRaw }, () => platformClh(sdk), { signal: params.signal, onStatus: (s) => params.onStage?.('proving', s), }) diff --git a/forge-web/lib/auth/islock.live.test.ts b/forge-web/lib/auth/islock.live.test.ts new file mode 100644 index 000000000..f0f7fc16b --- /dev/null +++ b/forge-web/lib/auth/islock.live.test.ts @@ -0,0 +1,49 @@ +/** + * The asset lock's InstantSend proof, live on testnet: the deposit seen on DAPI feeds, the lock + * broadcast, and its islock taken off the feeds opened before the broadcast (no block explorer, + * no `getislocks` RPC). SKIPPED by default (real spend: the deposit is locked into credits no + * identity claims): + * + * FORGE_LIVE=1 FORGE_ISLOCK_WIF= FORGE_ISLOCK_FROM= \ + * pnpm exec vitest run lib/auth/islock.live.test.ts + * + * Fund the WIF's P2PKH address first (the testnet faucet sends 1 DASH). + */ + +import * as secp from '@noble/secp256k1' +import { bytesToHex } from '@noble/hashes/utils.js' +import { describe, expect, it } from 'vitest' + +import { NETWORKS } from '../constants' +import { buildAssetLock, hash160, islockTxid, sendAndProveLock, waitForDeposit, wifBytes } from './asset-lock' +import { base58CheckEncode } from './base58' + +const WIF = process.env['FORGE_ISLOCK_WIF'] ?? '' +const FROM = Number(process.env['FORGE_ISLOCK_FROM'] ?? 0) + +describe.skipIf(process.env['FORGE_LIVE'] !== '1' || WIF === '' || FROM <= 0)('InstantSend proof on testnet (live)', () => { + it('proves the asset lock with the islock DAPI streams', async () => { + const ep = { dapi: NETWORKS.testnet.dapiAddresses } + const priv = wifBytes(WIF) + const address = base58CheckEncode(new Uint8Array([140, ...hash160(secp.getPublicKey(priv, true))])) + // eslint-disable-next-line no-console + console.log('deposit address', address) + const utxos = await waitForDeposit(ep, address, 100_000, { from: FROM, signal: AbortSignal.timeout(300_000) }) + // eslint-disable-next-line no-console + console.log('deposit', utxos.map((u) => `${u.txid}:${u.vout} ${u.satoshis}`).join(', ')) + const lock = buildAssetLock(utxos, priv) + // eslint-disable-next-line no-console + console.log('asset lock', lock.txid) + const started = Date.now() + const status: string[] = [] + const proof = await sendAndProveLock(ep, lock, async () => null, { onStatus: (s) => status.push(s), timeoutMs: 120_000 }) + // eslint-disable-next-line no-console + console.log('proof', proof.type, `after ${Date.now() - started} ms`, status) + expect(proof.type).toBe('instant') + if (proof.type === 'instant') { + expect(islockTxid(proof.islock)).toBe(lock.txid) + // eslint-disable-next-line no-console + console.log('islock', bytesToHex(proof.islock)) + } + }, 600_000) +})