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
2 changes: 1 addition & 1 deletion key-wallet-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ crate-type = ["cdylib", "staticlib", "lib"]
[features]
default = ["bincode", "eddsa", "bls", "bip38"]
bip38 = ["key-wallet/bip38"]
bincode = ["key-wallet/bincode", "key-wallet-manager/bincode"]
bincode = ["key-wallet/serde", "key-wallet-manager/bincode"]
eddsa = ["dashcore/eddsa", "key-wallet/eddsa"]
bls = ["dashcore/bls", "key-wallet/bls"]

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
23 changes: 12 additions & 11 deletions key-wallet-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,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 @@ -290,10 +290,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 @@ -497,17 +497,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
2 changes: 1 addition & 1 deletion key-wallet-manager/tests/test_serialized_wallets.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(feature = "bincode")]
#[cfg(feature = "serde")]
#[cfg(test)]
mod tests {
use key_wallet::wallet::initialization::WalletAccountCreationOptions;
Expand Down
11 changes: 4 additions & 7 deletions key-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ license = "CC0-1.0"

[features]
default = ["getrandom"]
serde = ["dep:serde", "dep:serde_json", "dashcore_hashes/serde", "secp256k1/serde", "dashcore/serde"]
bincode = ["serde", "dep:bincode", "dep:bincode_derive", "dashcore_hashes/bincode", "dashcore/bincode"]
serde = ["dep:serde", "dashcore_hashes/serde", "secp256k1/serde", "dashcore/serde"]
bip38 = ["scrypt", "aes", "bs58", "rand"]
eddsa = ["dashcore/eddsa"]
bls = ["dashcore/bls"]
Expand All @@ -23,7 +22,6 @@ dashcore_hashes = { path = "../hashes" }
dashcore = { path="../dash" }
secp256k1 = { version = "0.30.0", default-features = false, features = ["hashes", "recovery", "std"] }
bip39 = { version = "2.2.0", default-features = false, features = ["std", "chinese-simplified", "chinese-traditional", "czech", "french", "italian", "japanese", "korean", "portuguese", "spanish", "zeroize"] }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
base58ck = { version = "0.1.0", default-features = false }
bitflags = { version = "2.6", default-features = false }
getrandom = { version = "0.2", optional = true }
Expand All @@ -34,10 +32,9 @@ 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 }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
bincode = { version = "2.0.1", default-features = false, features = ["serde"], optional = true }
base64 = { version = "0.22", optional = true }
serde_json = { version = "1.0", optional = true }
hex = { version = "0.4"}
zeroize = { version = "1.8", features = ["derive"] }
tracing = "0.1"
Expand All @@ -46,7 +43,7 @@ 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"] }
rand = { version = "0.8", features = ["std", "std_rng"] }
test-case = "3.3"
tokio = { version = "1", features = ["macros", "rt"] }
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 std::collections::BTreeMap;

#[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 @@ -16,8 +16,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 @@ -27,7 +25,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 @@ -163,21 +160,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 @@ -17,13 +17,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 @@ -155,21 +152,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