Skip to content
Draft
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
4 changes: 2 additions & 2 deletions key-wallet-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ name = "key_wallet_ffi"
crate-type = ["cdylib", "staticlib", "lib"]

[features]
default = ["bincode", "eddsa", "bls", "bip38"]
default = ["serde", "eddsa", "bls", "bip38"]
bip38 = ["key-wallet/bip38"]
bincode = ["key-wallet-manager/bincode", "key-wallet/bincode"]
serde = ["key-wallet/serde", "key-wallet-manager/serde"]
eddsa = ["dashcore/eddsa", "key-wallet/eddsa"]
bls = ["dashcore/bls", "key-wallet/bls"]

Expand Down
6 changes: 3 additions & 3 deletions key-wallet-ffi/src/wallet_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ pub unsafe extern "C" fn wallet_manager_add_wallet_from_mnemonic(
/// - `error` must be a valid pointer to an FFIError structure or null
/// - The caller must ensure all pointers remain valid for the duration of this call
/// - The caller must free the returned wallet_bytes using wallet_manager_free_wallet_bytes()
#[cfg(feature = "bincode")]
#[cfg(feature = "serde")]
#[no_mangle]
pub unsafe extern "C" fn wallet_manager_add_wallet_from_mnemonic_return_serialized_bytes(
manager: *mut FFIWalletManager,
Expand Down Expand Up @@ -397,7 +397,7 @@ pub unsafe extern "C" fn wallet_manager_add_wallet_from_mnemonic_return_serializ
/// - `bytes_len` must match the original allocation size
/// - The pointer must not be used after calling this function
/// - This function must only be called once per buffer
#[cfg(feature = "bincode")]
#[cfg(feature = "serde")]
#[no_mangle]
pub unsafe extern "C" fn wallet_manager_free_wallet_bytes(wallet_bytes: *mut u8, bytes_len: usize) {
if !wallet_bytes.is_null() && bytes_len > 0 {
Expand All @@ -422,7 +422,7 @@ pub unsafe extern "C" fn wallet_manager_free_wallet_bytes(wallet_bytes: *mut u8,
/// - `wallet_id_out` must be a valid pointer to a 32-byte array that will receive the wallet ID
/// - `error` must be a valid pointer to an FFIError structure or null
/// - The caller must ensure all pointers remain valid for the duration of this call
#[cfg(feature = "bincode")]
#[cfg(feature = "serde")]
#[no_mangle]
pub unsafe extern "C" fn wallet_manager_import_wallet_from_bytes(
manager: *mut FFIWalletManager,
Expand Down
2 changes: 1 addition & 1 deletion key-wallet-ffi/src/wallet_manager_serialization_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Tests for wallet serialization FFI functions

#[cfg(all(test, feature = "bincode"))]
#[cfg(all(test, feature = "serde"))]
mod tests {
use crate::error::{FFIError, FFIErrorCode};
use crate::types::FFIWalletAccountCreationOptions;
Expand Down
4 changes: 2 additions & 2 deletions key-wallet-ffi/src/wallet_manager_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ mod tests {
}
}

#[cfg(feature = "bincode")]
#[cfg(feature = "serde")]
#[test]
fn test_create_wallet_from_mnemonic_return_serialized_bytes() {
let mut error = FFIError::success();
Expand Down Expand Up @@ -1062,7 +1062,7 @@ mod tests {
}
}

#[cfg(feature = "bincode")]
#[cfg(feature = "serde")]
#[test]
fn test_serialized_wallet_across_managers() {
let mut error = FFIError::success();
Expand Down
2 changes: 1 addition & 1 deletion key-wallet-ffi/tests/test_import_wallet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Test for wallet import from bytes via FFI

#[cfg(feature = "bincode")]
#[cfg(feature = "serde")]
#[cfg(test)]
mod tests {
use key_wallet_ffi::error::{FFIError, FFIErrorCode};
Expand Down
5 changes: 2 additions & 3 deletions key-wallet-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ readme = "README.md"
license = "CC0-1.0"

[features]
default = ["std", "bincode"]
default = ["std", "serde", "bincode"]
std = ["key-wallet/std", "dashcore/std", "dashcore_hashes/std", "secp256k1/std"]
serde = ["dep:serde", "key-wallet/serde", "dashcore/serde"]
getrandom = ["key-wallet/getrandom"]
bincode = ["dep:bincode", "key-wallet/bincode"]
test-utils = []

[dependencies]
Expand All @@ -22,8 +21,8 @@ dashcore = { path = "../dash", default-features = false }
dashcore_hashes = { path = "../hashes", default-features = false }
secp256k1 = { version = "0.30.0", default-features = false, features = ["recovery"] }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
bincode = { version = "2.0.1", optional = true, features = ["serde"] }
async-trait = "0.1"
bincode = { version = "2.0.1", optional = true }
zeroize = { version = "1.8", features = ["derive"] }
rayon = "1.11"
tokio = { version = "1.32", features = ["full"] }
Expand Down
23 changes: 12 additions & 11 deletions key-wallet-manager/src/wallet_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl<T: WalletInfoInterface> WalletManager<T> {
/// # Security Note
/// When `downgrade_to_pubkey_wallet` is true, the returned wallet contains NO private key material,
/// making it safe to use on potentially compromised systems or for creating watch-only wallets.
#[cfg(feature = "bincode")]
#[cfg(feature = "serde")]
#[allow(clippy::too_many_arguments)]
pub fn create_wallet_from_mnemonic_return_serialized_bytes(
&mut self,
Expand Down Expand Up @@ -260,10 +260,10 @@ impl<T: WalletInfoInterface> WalletManager<T> {
}

// Serialize the wallet to bytes
let serialized_bytes = bincode::encode_to_vec(&final_wallet, bincode::config::standard())
.map_err(|e| {
WalletError::InvalidParameter(format!("Failed to serialize wallet: {}", e))
})?;
let serialized_bytes =
bincode::serde::encode_to_vec(&final_wallet, bincode::config::standard()).map_err(
|e| WalletError::InvalidParameter(format!("Failed to serialize wallet: {}", e)),
)?;

// Add the wallet to the manager
let mut managed_info = T::from_wallet(&final_wallet);
Expand Down Expand Up @@ -460,17 +460,18 @@ impl<T: WalletInfoInterface> WalletManager<T> {
/// # Returns
/// * `Ok(WalletId)` - The computed wallet ID of the imported wallet
/// * `Err(WalletError)` - If deserialization fails or the wallet already exists
#[cfg(feature = "bincode")]
#[cfg(feature = "serde")]
pub fn import_wallet_from_bytes(
&mut self,
wallet_bytes: &[u8],
) -> Result<WalletId, WalletError> {
// Deserialize the wallet from bincode
let wallet: Wallet = bincode::decode_from_slice(wallet_bytes, bincode::config::standard())
.map_err(|e| {
WalletError::InvalidParameter(format!("Failed to deserialize wallet: {}", e))
})?
.0;
let wallet: Wallet =
bincode::serde::decode_from_slice(wallet_bytes, bincode::config::standard())
.map_err(|e| {
WalletError::InvalidParameter(format!("Failed to deserialize wallet: {}", e))
})?
.0;

// Compute wallet ID from the wallet's root public key
let wallet_id = wallet.compute_wallet_id();
Expand Down
8 changes: 2 additions & 6 deletions key-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ license = "CC0-1.0"
[features]
default = ["std"]
std = ["dashcore_hashes/std", "secp256k1/std", "bip39/std", "getrandom", "dash-network/std", "rand"]
serde = ["dep:serde", "dep:serde_json", "dashcore_hashes/serde", "secp256k1/serde", "dash-network/serde", "dashcore/serde"]
bincode = ["serde", "dep:bincode", "dep:bincode_derive", "dash-network/bincode", "dashcore_hashes/bincode", "dashcore/bincode"]
serde = ["dep:serde", "dashcore_hashes/serde", "secp256k1/serde", "dash-network/serde", "dashcore/serde"]
bip38 = ["scrypt", "aes", "bs58", "rand"]
eddsa = ["dashcore/eddsa"]
bls = ["dashcore/bls"]
Expand All @@ -36,10 +35,7 @@ sha2 = { version = "0.10", default-features = false }
bs58 = { version = "0.5", default-features = false, features = ["check", "alloc"], optional = true }
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"], optional = true }
# Serialization
bincode = { version = "2.0.1", optional = true }
bincode_derive = { version = "2.0.1", optional = true }
base64 = { version = "0.22", optional = true }
serde_json = { version = "1.0", optional = true }
hex = { version = "0.4"}
hkdf = { version = "0.12", default-features = false }
zeroize = { version = "1.8", features = ["derive"] }
Expand All @@ -49,6 +45,6 @@ async-trait = "0.1"
[dev-dependencies]
dashcore = { path="../dash", features = ["test-utils"] }
hex = "0.4"
key-wallet = { path = ".", features = ["test-utils", "bip38", "serde", "bincode", "eddsa", "bls"] }
key-wallet = { path = ".", features = ["test-utils", "bip38", "serde", "eddsa", "bls"] }
tokio = { version = "1", features = ["macros", "rt"] }
test-case = "3.3"
5 changes: 0 additions & 5 deletions key-wallet/src/account/account_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use alloc::collections::BTreeMap;
use alloc::vec::Vec;
#[cfg(feature = "bincode")]
use bincode_derive::{Decode, Encode};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand All @@ -21,7 +19,6 @@ pub type DashpayContactIdentityId = [u8; 32];

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
pub struct DashpayAccountKey {
pub index: u32,
pub user_identity_id: DashpayOurUserIdentityId,
Expand All @@ -31,7 +28,6 @@ pub struct DashpayAccountKey {
/// Key for Platform Payment accounts (DIP-17)
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
pub struct PlatformPaymentAccountKey {
/// Account index (hardened)
pub account: u32,
Expand All @@ -42,7 +38,6 @@ pub struct PlatformPaymentAccountKey {
/// Collection of accounts organized by type
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
pub struct AccountCollection {
/// Standard BIP44 accounts by index
pub standard_bip44_accounts: BTreeMap<u32, Account>,
Expand Down
4 changes: 0 additions & 4 deletions key-wallet/src/account/account_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ use crate::transaction_checking::transaction_router::{
AccountTypeToCheck, PlatformAccountConversionError,
};
use crate::Network;
#[cfg(feature = "bincode")]
use bincode_derive::{Decode, Encode};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Account types supported by the wallet
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
pub enum StandardAccountType {
/// Standard BIP44 account for regular transactions m/44'/coin_type'/account'/x/x
#[default]
Expand All @@ -28,7 +25,6 @@ pub enum StandardAccountType {
/// Account types supported by the wallet
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
pub enum AccountType {
/// Standard BIP44 account for regular transactions
Standard {
Expand Down
18 changes: 0 additions & 18 deletions key-wallet/src/account/bls_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use dashcore::Address;
use serde::{Deserialize, Serialize};

use crate::bip32::{ChainCode, Fingerprint};
#[cfg(feature = "bincode")]
use bincode_derive::{Decode, Encode};
use dashcore::blsful::{Bls12381G2Impl, SerializationFormat};

use crate::account::derivation::AccountDerivation;
Expand All @@ -28,7 +26,6 @@ pub use dashcore::blsful::SecretKey;
/// BLS account structure for Platform and masternode operations
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
pub struct BLSAccount {
/// Wallet id (stored as Vec for serialization)
pub parent_wallet_id: Option<Vec<u8>>,
Expand Down Expand Up @@ -164,21 +161,6 @@ impl BLSAccount {
watch_only.is_watch_only = true;
watch_only
}

/// Serialize account to bytes
#[cfg(feature = "bincode")]
pub fn serialize(&self) -> Result<Vec<u8>> {
bincode::encode_to_vec(self, bincode::config::standard())
.map_err(|e| Error::Serialization(e.to_string()))
}

/// Deserialize account from bytes
#[cfg(feature = "bincode")]
pub fn deserialize(data: &[u8]) -> Result<Self> {
bincode::decode_from_slice(data, bincode::config::standard())
.map(|(account, _)| account)
.map_err(|e| Error::Serialization(e.to_string()))
}
}

impl AccountTrait for BLSAccount {
Expand Down
3 changes: 0 additions & 3 deletions key-wallet/src/account/coinjoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
//! This module contains structures for managing CoinJoin address pools.

use crate::managed_account::address_pool::AddressPool;
#[cfg(feature = "bincode")]
use bincode_derive::{Decode, Encode};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// CoinJoin-specific address pools
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
pub struct CoinJoinPools {
/// CoinJoin receive addresses
pub external: AddressPool,
Expand Down
18 changes: 0 additions & 18 deletions key-wallet/src/account/eddsa_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ use serde::{Deserialize, Serialize};
use crate::account::derivation::AccountDerivation;
use crate::bip32::{ChainCode, Fingerprint};
use crate::managed_account::address_pool::AddressPoolType;
#[cfg(feature = "bincode")]
use bincode_derive::{Decode, Encode};

/// EdDSA (Ed25519) account structure for Platform identity operations
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
pub struct EdDSAAccount {
/// Wallet id (stored as Vec for serialization)
pub parent_wallet_id: Option<Vec<u8>>,
Expand Down Expand Up @@ -156,21 +153,6 @@ impl EdDSAAccount {
watch_only
}

/// Serialize account to bytes
#[cfg(feature = "bincode")]
pub fn serialize(&self) -> Result<Vec<u8>> {
bincode::encode_to_vec(self, bincode::config::standard())
.map_err(|e| Error::Serialization(e.to_string()))
}

/// Deserialize account from bytes
#[cfg(feature = "bincode")]
pub fn deserialize(data: &[u8]) -> Result<Self> {
bincode::decode_from_slice(data, bincode::config::standard())
.map(|(account, _)| account)
.map_err(|e| Error::Serialization(e.to_string()))
}

/// Derive a Platform identity key at index
pub fn derive_identity_key(&self, index: u32) -> Result<ExtendedEd25519PubKey> {
self.derive_ed25519_key_at_index(index)
Expand Down
4 changes: 0 additions & 4 deletions key-wallet/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ pub mod eddsa_account;
// pub mod scan;
pub mod account_type;
pub mod derivation;
mod serialization;

use core::fmt;

#[cfg(feature = "bincode")]
use bincode_derive::{Decode, Encode};
use secp256k1::Secp256k1;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -53,7 +50,6 @@ pub use eddsa_account::EdDSAAccount;
/// identity information that doesn't change during normal operation.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
pub struct Account {
/// Wallet id
pub parent_wallet_id: Option<[u8; 32]>,
Expand Down
Loading
Loading