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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dash-spv-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "8fe9ea
key-wallet = { git = "https://github.com/dashpay/rust-dashcore", rev = "8fe9ea394306e5f9282505b24d09092ab9a33738" }
key-wallet-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "8fe9ea394306e5f9282505b24d09092ab9a33738" }
key-wallet-manager = { git = "https://github.com/dashpay/rust-dashcore", rev = "8fe9ea394306e5f9282505b24d09092ab9a33738" }
dash-network = { git = "https://github.com/dashpay/rust-dashcore", rev = "8fe9ea394306e5f9282505b24d09092ab9a33738" }
dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore", rev = "8fe9ea394306e5f9282505b24d09092ab9a33738" }

# Optimize heavy crypto crates even in dev/test builds so that
Expand Down
1 change: 1 addition & 0 deletions packages/rs-platform-wallet-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tokio = { version = "1", features = ["rt-multi-thread"] }
# Core dependencies (for Network type)
dashcore = { workspace = true }
key-wallet = { workspace = true, features = ["bincode"] }
dash-network = { workspace = true, features = ["ffi"] }

# Bincode used to serialize RootExtendedPubKey / ExtendedPubKey across
# FFI for watch-only restore. Same version pinned elsewhere in workspace.
Expand Down
12 changes: 3 additions & 9 deletions packages/rs-platform-wallet-ffi/src/core_wallet/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::error::*;
use crate::handle::*;
use crate::types::FFINetwork;

/// Destroy a CoreWallet handle.
#[no_mangle]
Expand Down Expand Up @@ -46,12 +47,10 @@ pub unsafe extern "C" fn core_wallet_get_balance(
}

/// Get the network this wallet operates on.
///
/// Returns: 0 = Mainnet, 1 = Testnet, 2 = Devnet, 3 = Regtest.
#[no_mangle]
pub unsafe extern "C" fn core_wallet_get_network(
handle: Handle,
out_network: *mut u32,
out_network: *mut FFINetwork,
_out_error: *mut PlatformWalletFFIError,
) -> PlatformWalletFFIResult {
if out_network.is_null() {
Expand All @@ -60,12 +59,7 @@ pub unsafe extern "C" fn core_wallet_get_network(

CORE_WALLET_STORAGE
.with_item(handle, |wallet| {
*out_network = match wallet.network() {
key_wallet::Network::Mainnet => 0,
key_wallet::Network::Testnet => 1,
key_wallet::Network::Devnet => 2,
key_wallet::Network::Regtest => 3,
};
*out_network = wallet.network().into();
PlatformWalletFFIResult::Success
})
.unwrap_or(PlatformWalletFFIResult::ErrorInvalidHandle)
Expand Down
23 changes: 4 additions & 19 deletions packages/rs-platform-wallet-ffi/src/derivation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use std::str::FromStr;
use dashcore::secp256k1::Secp256k1;
use key_wallet::bip32::{DerivationPath, ExtendedPrivKey};
use key_wallet::mnemonic::{Language, Mnemonic};
use key_wallet::Network;
use zeroize::Zeroizing;

use crate::error::*;
use crate::types::{FFINetwork, Network};

/// Parse a BIP-39 mnemonic against every supported wordlist in turn,
/// returning the first language that yields a valid mnemonic.
Expand Down Expand Up @@ -67,8 +67,7 @@ fn parse_mnemonic_any_language(phrase: &str) -> Result<Mnemonic, &'static str> {
/// * `mnemonic` — UTF-8 null-terminated BIP-39 mnemonic phrase.
/// * `passphrase` — optional UTF-8 null-terminated BIP-39 passphrase.
/// Pass `null` for no passphrase.
/// * `network` — `0` = Mainnet, `1` = Testnet, `2` = Devnet, `3` =
/// Regtest. Drives the xpriv version bytes; the derived secret
/// * `network` — Drives the xpriv version bytes; the derived secret
/// key itself is network-independent but the master key's chain
/// code derivation uses the HD seed directly.
/// * `path_utf8` — UTF-8 null-terminated BIP-32 path string, e.g.
Expand All @@ -95,7 +94,7 @@ fn parse_mnemonic_any_language(phrase: &str) -> Result<Mnemonic, &'static str> {
pub unsafe extern "C" fn platform_wallet_derive_ext_priv_key_from_mnemonic(
mnemonic: *const c_char,
passphrase: *const c_char,
network: u32,
network: FFINetwork,
path_utf8: *const c_char,
out_secret_key: *mut u8,
out_chain_code: *mut u8,
Expand Down Expand Up @@ -159,21 +158,7 @@ pub unsafe extern "C" fn platform_wallet_derive_ext_priv_key_from_mnemonic(
}
};

let network = match network {
0 => Network::Mainnet,
1 => Network::Testnet,
2 => Network::Devnet,
3 => Network::Regtest,
_ => {
if !out_error.is_null() {
*out_error = PlatformWalletFFIError::new(
PlatformWalletFFIResult::ErrorInvalidParameter,
"invalid network (expected 0..=3)",
);
}
return PlatformWalletFFIResult::ErrorInvalidParameter;
}
};
let network: Network = network.into();

let path = match DerivationPath::from_str(path_str) {
Ok(p) => p,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
use std::ffi::{c_void, CString};
use std::os::raw::c_char;

use crate::types::{FFINetwork, Network};
use dashcore::PrivateKey as DashPrivateKey;
use key_wallet::bip32::ExtendedPrivKey;
use platform_wallet::wallet::identity::network::derive_ecdsa_identity_auth_keypair_from_master;
use rs_sdk_ffi::DashSDKNetwork;
use zeroize::Zeroizing;

use crate::derive_and_persist_callbacks::{
mnemonic_resolver_result, MnemonicResolverHandle, MNEMONIC_RESOLVER_BUFFER_CAPACITY,
};
use crate::error::*;
use crate::identity_key_preview::IdentityKeyPreviewFFI;
use crate::identity_keys_from_mnemonic::{map_network, parse_mnemonic_any_language};
use crate::identity_keys_from_mnemonic::parse_mnemonic_any_language;

/// Derive a single ECDSA identity-authentication keypair at
/// `(identity_index, key_index)` from a BIP-39 mnemonic. Returns
Expand Down Expand Up @@ -72,7 +72,7 @@ use crate::identity_keys_from_mnemonic::{map_network, parse_mnemonic_any_languag
pub unsafe extern "C" fn dash_sdk_derive_identity_key_at_slot(
mnemonic_cstr: *const std::os::raw::c_char,
passphrase_cstr: *const std::os::raw::c_char,
network: DashSDKNetwork,
network: FFINetwork,
identity_index: u32,
key_index: u32,
out_row: *mut IdentityKeyPreviewFFI,
Expand Down Expand Up @@ -151,7 +151,7 @@ pub unsafe extern "C" fn dash_sdk_derive_identity_key_at_slot(
unsafe fn derive_at_slot_inner(
mnemonic_str: &str,
passphrase_str: &str,
network: DashSDKNetwork,
network: FFINetwork,
identity_index: u32,
key_index: u32,
out_row: *mut IdentityKeyPreviewFFI,
Expand All @@ -175,7 +175,7 @@ unsafe fn derive_at_slot_inner(
let seed: Zeroizing<[u8; 64]> = Zeroizing::new(mnemonic.to_seed(passphrase_str));

// ---- Master xpriv --------------------------------------------------------
let kw_network = map_network(network);
let kw_network: Network = network.into();
let master = match ExtendedPrivKey::new_master(kw_network, seed.as_ref()) {
Ok(m) => m,
Err(e) => {
Expand Down Expand Up @@ -338,7 +338,7 @@ unsafe fn derive_at_slot_inner(
/// - `out_error` may be NULL.
#[no_mangle]
pub unsafe extern "C" fn dash_sdk_derive_identity_key_at_slot_with_resolver(
network: DashSDKNetwork,
network: FFINetwork,
wallet_id_bytes: *const u8,
mnemonic_resolver_handle: *mut MnemonicResolverHandle,
identity_index: u32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ use std::ffi::{c_void, CString};
use std::os::raw::c_char;
use std::ptr;

use crate::types::{FFINetwork, Network};
use dashcore::hashes::Hash;
use dashcore::secp256k1::Secp256k1;
use key_wallet::bip32::{ExtendedPrivKey, ExtendedPubKey};
use rs_sdk_ffi::DashSDKNetwork;
use zeroize::{Zeroize, Zeroizing};

use crate::derive_and_persist_callbacks::{
Expand All @@ -89,7 +89,7 @@ use crate::derive_and_persist_callbacks::{
use crate::error::*;
use crate::identity_key_preview::IdentityKeyPreviewFFI;
use crate::identity_keys_from_mnemonic::{
identity_auth_derivation_path, map_network, parse_mnemonic_any_language,
identity_auth_derivation_path, parse_mnemonic_any_language,
};
use crate::identity_registration_with_signer::IdentityRegistrationKeyDerivationsFFI;

Expand Down Expand Up @@ -158,7 +158,7 @@ const SECURITY_LEVEL_HIGH: u8 = 2;
#[no_mangle]
#[allow(clippy::too_many_arguments)]
pub unsafe extern "C" fn dash_sdk_derive_and_persist_identity_keys(
network: DashSDKNetwork,
network: FFINetwork,
wallet_id_bytes: *const u8,
identity_index: u32,
key_count: u32,
Expand Down Expand Up @@ -281,7 +281,7 @@ pub unsafe extern "C" fn dash_sdk_derive_and_persist_identity_keys(
// (non-zeroized) `String` storage early.
drop(mnemonic);

let kw_network = map_network(network);
let kw_network: Network = network.into();
let master = match ExtendedPrivKey::new_master(kw_network, seed.as_ref()) {
Ok(m) => m,
Err(e) => {
Expand Down Expand Up @@ -629,7 +629,7 @@ mod tests {
let mut err = PlatformWalletFFIError::success();
let rc = unsafe {
dash_sdk_derive_and_persist_identity_keys(
DashSDKNetwork::SDKTestnet,
FFINetwork::Testnet,
wallet_id.as_ptr(),
7, // identity_index
3, // key_count
Expand Down Expand Up @@ -695,7 +695,7 @@ mod tests {
let mut err = PlatformWalletFFIError::success();
let rc = unsafe {
dash_sdk_derive_and_persist_identity_keys(
DashSDKNetwork::SDKTestnet,
FFINetwork::Testnet,
wallet_id.as_ptr(),
0,
2,
Expand All @@ -722,7 +722,7 @@ mod tests {
// Null out_pubkeys.
let rc = unsafe {
dash_sdk_derive_and_persist_identity_keys(
DashSDKNetwork::SDKTestnet,
FFINetwork::Testnet,
wallet_id.as_ptr(),
0,
1,
Expand All @@ -741,7 +741,7 @@ mod tests {
};
let rc = unsafe {
dash_sdk_derive_and_persist_identity_keys(
DashSDKNetwork::SDKTestnet,
FFINetwork::Testnet,
std::ptr::null(),
0,
1,
Expand Down Expand Up @@ -770,7 +770,7 @@ mod tests {
let mut err = PlatformWalletFFIError::success();
let rc = unsafe {
dash_sdk_derive_and_persist_identity_keys(
DashSDKNetwork::SDKTestnet,
FFINetwork::Testnet,
wallet_id.as_ptr(),
0,
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ use key_wallet::dip9::{
IDENTITY_AUTHENTICATION_PATH_MAINNET, IDENTITY_AUTHENTICATION_PATH_TESTNET,
};
use key_wallet::mnemonic::{Language, Mnemonic};
use rs_sdk_ffi::DashSDKNetwork;
use zeroize::Zeroizing;

use crate::error::*;
use crate::identity_key_preview::IdentityKeyPreviewFFI;
use crate::identity_registration_with_signer::IdentityRegistrationKeyDerivationsFFI;
use crate::types::{FFINetwork, Network};

/// Parse a BIP-39 mnemonic against every supported wordlist.
///
Expand Down Expand Up @@ -81,20 +81,6 @@ pub(crate) fn parse_mnemonic_any_language(phrase: &str) -> Result<Mnemonic, &'st
Err("phrase does not match any supported BIP-39 wordlist")
}

/// Map the C-ABI `DashSDKNetwork` enum to `key_wallet::Network`.
///
/// `Local` collapses to `Regtest` to match the rest of the FFI surface
/// (`dash_sdk_signer_create_from_private_key`, `dash_sdk_sign_with_mnemonic_and_path`).
pub(crate) fn map_network(network: DashSDKNetwork) -> key_wallet::Network {
match network {
DashSDKNetwork::SDKMainnet => key_wallet::Network::Mainnet,
DashSDKNetwork::SDKTestnet => key_wallet::Network::Testnet,
DashSDKNetwork::SDKRegtest => key_wallet::Network::Regtest,
DashSDKNetwork::SDKDevnet => key_wallet::Network::Devnet,
DashSDKNetwork::SDKLocal => key_wallet::Network::Regtest,
}
}

/// Build the DIP-9 identity-authentication derivation path
/// `m/9'/coin'/5'/0'/0'/identity_index'/key_index'`.
///
Expand All @@ -109,12 +95,12 @@ pub(crate) fn map_network(network: DashSDKNetwork) -> key_wallet::Network {
/// `platform_wallet::identity_auth_derivation_path` is `pub(crate)` and
/// this entry point sits outside that crate.
pub(crate) fn identity_auth_derivation_path(
network: key_wallet::Network,
network: Network,
identity_index: u32,
key_index: u32,
) -> Result<DerivationPath, String> {
let base_path: DerivationPath = match network {
key_wallet::Network::Mainnet => IDENTITY_AUTHENTICATION_PATH_MAINNET,
Network::Mainnet => IDENTITY_AUTHENTICATION_PATH_MAINNET,
_ => IDENTITY_AUTHENTICATION_PATH_TESTNET,
}
Comment thread
ZocoLini marked this conversation as resolved.
.into();
Expand Down Expand Up @@ -191,7 +177,7 @@ pub(crate) fn identity_auth_derivation_path(
pub unsafe extern "C" fn dash_sdk_derive_identity_keys_from_mnemonic(
mnemonic_cstr: *const std::os::raw::c_char,
passphrase_cstr: *const std::os::raw::c_char,
network: DashSDKNetwork,
network: FFINetwork,
identity_index: u32,
key_count: u32,
out_rows: *mut IdentityRegistrationKeyDerivationsFFI,
Expand Down Expand Up @@ -278,7 +264,7 @@ pub unsafe extern "C" fn dash_sdk_derive_identity_keys_from_mnemonic(
let seed: Zeroizing<[u8; 64]> = Zeroizing::new(mnemonic.to_seed(passphrase_str));

// ---- Master xpriv --------------------------------------------------------
let kw_network = map_network(network);
let kw_network: Network = network.into();
let master = match ExtendedPrivKey::new_master(kw_network, seed.as_ref()) {
Ok(m) => m,
Err(e) => {
Expand Down Expand Up @@ -513,7 +499,7 @@ mod tests {
dash_sdk_derive_identity_keys_from_mnemonic(
mnemonic.as_ptr(),
std::ptr::null(), // empty passphrase
DashSDKNetwork::SDKTestnet,
FFINetwork::Testnet,
7,
3,
&mut out,
Expand Down Expand Up @@ -574,7 +560,7 @@ mod tests {
dash_sdk_derive_identity_keys_from_mnemonic(
mnemonic.as_ptr(),
std::ptr::null(),
DashSDKNetwork::SDKMainnet,
FFINetwork::Mainnet,
0,
1,
&mut out,
Expand Down Expand Up @@ -606,7 +592,7 @@ mod tests {
dash_sdk_derive_identity_keys_from_mnemonic(
mnemonic.as_ptr(),
std::ptr::null(),
DashSDKNetwork::SDKTestnet,
FFINetwork::Testnet,
0,
0,
&mut out,
Expand Down Expand Up @@ -634,7 +620,7 @@ mod tests {
dash_sdk_derive_identity_keys_from_mnemonic(
mnemonic.as_ptr(),
std::ptr::null(),
DashSDKNetwork::SDKTestnet,
FFINetwork::Testnet,
0,
3,
&mut out,
Expand Down
Loading
Loading