Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ members = [
]

[workspace.dependencies]
dashcore = { git = "https://github.com/dashpay/rust-dashcore", rev = "5db46b4d2bdc50b0fbc8d9acbebe72775bb4132a" }
dash-spv = { git = "https://github.com/dashpay/rust-dashcore", rev = "5db46b4d2bdc50b0fbc8d9acbebe72775bb4132a" }
dash-spv-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "5db46b4d2bdc50b0fbc8d9acbebe72775bb4132a" }
key-wallet = { git = "https://github.com/dashpay/rust-dashcore", rev = "5db46b4d2bdc50b0fbc8d9acbebe72775bb4132a" }
key-wallet-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "5db46b4d2bdc50b0fbc8d9acbebe72775bb4132a" }
key-wallet-manager = { git = "https://github.com/dashpay/rust-dashcore", rev = "5db46b4d2bdc50b0fbc8d9acbebe72775bb4132a" }
dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore", rev = "5db46b4d2bdc50b0fbc8d9acbebe72775bb4132a" }
dashcore = { git = "https://github.com/dashpay/rust-dashcore", rev = "3f6500200042a1388d34832214c4e9dc3ee72df8" }
dash-spv = { git = "https://github.com/dashpay/rust-dashcore", rev = "3f6500200042a1388d34832214c4e9dc3ee72df8" }
dash-spv-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "3f6500200042a1388d34832214c4e9dc3ee72df8" }
key-wallet = { git = "https://github.com/dashpay/rust-dashcore", rev = "3f6500200042a1388d34832214c4e9dc3ee72df8" }
key-wallet-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "3f6500200042a1388d34832214c4e9dc3ee72df8" }
key-wallet-manager = { git = "https://github.com/dashpay/rust-dashcore", rev = "3f6500200042a1388d34832214c4e9dc3ee72df8" }
dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore", rev = "3f6500200042a1388d34832214c4e9dc3ee72df8" }

# Optimize heavy crypto crates even in dev/test builds so that
# Halo 2 proof generation and verification run at near-release speed.
Expand Down
6 changes: 6 additions & 0 deletions packages/rs-platform-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ key-wallet-manager = { workspace = true, optional = true }
# Core dependencies
dashcore = { workspace = true }

# SPV context provider dependencies (optional)
dash-spv = { workspace = true, optional = true }
dash-context-provider = { path = "../rs-context-provider", optional = true }
tokio = { version = "1.41", features = ["sync"], optional = true }

# Standard dependencies
thiserror = "1.0"
async-trait = "0.1"
Expand All @@ -35,3 +40,4 @@ default = ["bls", "eddsa", "manager"]
bls = ["key-wallet/bls"]
eddsa = ["key-wallet/eddsa"]
manager = ["key-wallet-manager"]
spv-context = ["dep:dash-spv", "dep:dash-context-provider", "dep:tokio"]
3 changes: 3 additions & 0 deletions packages/rs-platform-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub mod identity_manager;
pub mod managed_identity;
pub mod platform_wallet_info;

#[cfg(feature = "spv-context")]
pub mod spv_context_provider;

// Re-export main types at crate root
pub use block_time::BlockTime;
pub use contact_request::ContactRequest;
Expand Down
157 changes: 157 additions & 0 deletions packages/rs-platform-wallet/src/spv_context_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//! SPV-based Context Provider
//!
//! Pure Rust implementation that reads quorum data directly from a
//! [`MasternodeListEngine`], with no FFI calls.
//!
//! # Architecture
//!
//! The [`SpvContextProvider`] holds an `Arc<RwLock<MasternodeListEngine>>`
//! (shared with the SPV client) and reads quorum public keys by looking up
//! the masternode list closest to the requested core chain-locked height.
//!
//! This design eliminates the need for FFI round-trips: the same in-memory
//! masternode list engine that the SPV client populates during sync is read
//! directly by the Platform SDK's proof verifier.
//!
//! # Usage
//!
//! ```ignore
//! use std::sync::Arc;
//! use tokio::sync::RwLock;
//! use dash_spv::MasternodeListEngine;
//! use dashcore::Network;
//! use platform_wallet::spv_context_provider::SpvContextProvider;
//!
//! let engine: Arc<RwLock<MasternodeListEngine>> = /* from DashSpvClient */;
//! let provider = SpvContextProvider::new(engine, Network::Testnet);
//! ```

use std::sync::Arc;

use dash_context_provider::ContextProvider;
use dash_context_provider::ContextProviderError;
use dash_spv::LLMQType;
use dash_spv::MasternodeListEngine;
use dashcore::hashes::Hash;
use dashcore::Network;
use dashcore::QuorumHash;
use dpp::data_contract::TokenConfiguration;
use dpp::prelude::{CoreBlockHeight, DataContract, Identifier};
use dpp::version::PlatformVersion;
use tokio::sync::RwLock;

/// Context provider backed by an SPV client's synced masternode data.
///
/// Reads quorum public keys directly from the [`MasternodeListEngine`]
/// without any FFI calls. The engine is shared with the SPV client via
/// `Arc<RwLock<...>>`, so all data stays in-process.
pub struct SpvContextProvider {
masternode_engine: Arc<RwLock<MasternodeListEngine>>,
network: Network,
}

impl SpvContextProvider {
/// Create a new SPV context provider.
///
/// # Arguments
///
/// * `masternode_engine` - Shared reference to the masternode list engine,
/// typically obtained from [`DashSpvClient::masternode_list_engine()`].
/// * `network` - The Dash network (mainnet, testnet, devnet, etc.).
pub fn new(masternode_engine: Arc<RwLock<MasternodeListEngine>>, network: Network) -> Self {
Self {
masternode_engine,
network,
}
}
}

impl ContextProvider for SpvContextProvider {
fn get_quorum_public_key(
&self,
quorum_type: u32,
quorum_hash: [u8; 32],
core_chain_locked_height: u32,
) -> Result<[u8; 48], ContextProviderError> {
let quorum_type_u8 = u8::try_from(quorum_type).map_err(|_| {
ContextProviderError::InvalidQuorum(format!(
"Quorum type {} exceeds u8 range",
quorum_type
))
})?;
let llmq_type: LLMQType = quorum_type_u8.into();
let quorum_hash = QuorumHash::from_byte_array(quorum_hash);

// Use try_read() instead of blocking_read() because this sync method
// may be called from within a Tokio async context (proof verification
// happens inside async tasks). blocking_read() would panic in that case.
let engine = self.masternode_engine.try_read().map_err(|_| {
ContextProviderError::Generic(
"Masternode engine lock is busy; retry quorum lookup".to_string(),
)
})?;
let (before, _after) = engine.masternode_lists_around_height(core_chain_locked_height);

let ml = before.ok_or_else(|| {
ContextProviderError::InvalidQuorum(format!(
"No masternode list found at or before height {}",
core_chain_locked_height
))
})?;

let list_height = ml.known_height;

let quorums = ml.quorums.get(&llmq_type).ok_or_else(|| {
ContextProviderError::InvalidQuorum(format!(
"No quorums of type {} found at list height {} (requested {})",
quorum_type, list_height, core_chain_locked_height
))
})?;

let quorum = quorums.get(&quorum_hash).ok_or_else(|| {
ContextProviderError::InvalidQuorum(format!(
"Quorum not found: type {} at list height {} (requested {}) \
with hash {:x} (masternode list has {} quorums of this type)",
quorum_type,
list_height,
core_chain_locked_height,
quorum_hash,
quorums.len()
))
})?;

let pubkey_bytes: &[u8; 48] = quorum.quorum_entry.quorum_public_key.as_ref();
Ok(*pubkey_bytes)
}

fn get_platform_activation_height(&self) -> Result<CoreBlockHeight, ContextProviderError> {
match self.network {
Network::Mainnet => Ok(1_888_888),
Network::Testnet => Ok(1_289_520),
Network::Devnet => Ok(1),
_ => Err(ContextProviderError::Generic(format!(
"Platform activation height unknown for network {:?}",
self.network
))),
}
}

fn get_data_contract(
&self,
_data_contract_id: &Identifier,
_platform_version: &PlatformVersion,
) -> Result<Option<Arc<DataContract>>, ContextProviderError> {
// Data contract lookup is handled by the SDK's contract cache,
// not the SPV layer.
Ok(None)
}

fn get_token_configuration(
&self,
_token_id: &Identifier,
) -> Result<Option<TokenConfiguration>, ContextProviderError> {
// Token configuration lookup is handled by the SDK's contract cache,
// not the SPV layer.
Ok(None)
}
}
6 changes: 6 additions & 0 deletions packages/rs-sdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ rs-sdk-trusted-context-provider = { path = "../rs-sdk-trusted-context-provider",
] }
simple-signer = { path = "../simple-signer" }

# SPV client integration for quorum-based proof verification
dash-spv-ffi = { workspace = true }

# Platform Wallet integration for DashPay support
platform-wallet-ffi = { path = "../rs-platform-wallet-ffi" }

# Platform Wallet (pure-Rust SPV context provider)
platform-wallet = { path = "../rs-platform-wallet", features = ["spv-context"] }

# FFI and serialization
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
Loading
Loading