Skip to content

Commit f49a7e7

Browse files
feat: improve key wallet (#112)
* more work * more work * temp * temp * temp * temp * more work * more work * more work
1 parent 0ab1b62 commit f49a7e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+9141
-3115
lines changed

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ version = "0.39.6"
77

88
[patch.crates-io]
99
dashcore_hashes = { path = "hashes" }
10-
# Use fixed version of elliptic-curve-tools with DefaultIsZeroes trait bound
11-
elliptic-curve-tools = { git = "https://github.com/QuantumExplorer/elliptic-curve-tools", branch = "fix/DefaultIsZeroesToSumOfProducts" }
1210

1311
[profile.release]
1412
# Default to unwinding for most crates

dash-network/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//! Dash network types shared across Dash crates
22
3+
#[cfg(feature = "bincode")]
4+
use bincode_derive::{Decode, Encode};
35
use std::fmt;
46

57
/// The cryptocurrency network to act on.
68
#[derive(Copy, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Debug)]
79
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
810
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
911
#[non_exhaustive]
10-
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
12+
#[cfg_attr(feature = "bincode", derive(Encode, Decode))]
1113
pub enum Network {
1214
/// Classic Dash Core Payment Chain
1315
Dash,

dash/src/address.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,15 @@ pub enum Payload {
439439
WitnessProgram(WitnessProgram),
440440
}
441441

442+
impl Payload {
443+
pub fn as_pubkey_hash(&self) -> Option<&PubkeyHash> {
444+
match self {
445+
Payload::PubkeyHash(pubkey_hash) => Some(pubkey_hash),
446+
_ => None,
447+
}
448+
}
449+
}
450+
442451
/// Witness program as defined in BIP141.
443452
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
444453
pub struct WitnessProgram {

key-wallet-ffi/src/balance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub unsafe extern "C" fn wallet_get_account_balance(
8585
let wallet = &*wallet;
8686
let network_rust: key_wallet::Network = network.into();
8787

88-
use key_wallet::account::types::{AccountType, StandardAccountType};
88+
use key_wallet::account::account_type::{AccountType, StandardAccountType};
8989
let _account_type = AccountType::Standard {
9090
index: account_index,
9191
standard_account_type: StandardAccountType::BIP44Account,

key-wallet-ffi/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ impl From<key_wallet::Error> for FFIError {
148148
Error::KeyError(_) | Error::Bip32(_) | Error::Secp256k1(_) | Error::Base58 => {
149149
(FFIErrorCode::WalletError, err.to_string())
150150
}
151+
Error::NoKeySource => {
152+
(FFIErrorCode::InvalidState, "No key source available".to_string())
153+
}
154+
#[allow(unreachable_patterns)]
155+
_ => (FFIErrorCode::WalletError, err.to_string()),
151156
};
152157

153158
FFIError::error(code, msg)

key-wallet-ffi/src/managed_wallet.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::ptr;
1010

1111
use crate::error::{FFIError, FFIErrorCode};
1212
use crate::types::{FFINetwork, FFIWallet};
13-
use key_wallet::account::address_pool::KeySource;
13+
use key_wallet::managed_account::address_pool::{AddressPoolType, KeySource};
1414
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
1515

1616
/// FFI wrapper for ManagedWalletInfo
@@ -126,7 +126,7 @@ pub unsafe extern "C" fn managed_wallet_get_next_bip44_receive_address(
126126

127127
// Generate the next receive address
128128
let xpub = account.extended_public_key();
129-
match managed_account.get_next_receive_address(&xpub) {
129+
match managed_account.next_receive_address(Some(&xpub)) {
130130
Ok(address) => {
131131
let address_str = address.to_string();
132132
match CString::new(address_str) {
@@ -246,7 +246,7 @@ pub unsafe extern "C" fn managed_wallet_get_next_bip44_change_address(
246246

247247
// Generate the next change address
248248
let xpub = account.extended_public_key();
249-
match managed_account.get_next_change_address(&xpub) {
249+
match managed_account.next_change_address(Some(&xpub)) {
250250
Ok(address) => {
251251
let address_str = address.to_string();
252252
match CString::new(address_str) {
@@ -400,7 +400,7 @@ pub unsafe extern "C" fn managed_wallet_get_bip_44_external_address_range(
400400
..
401401
} = &mut managed_account.account_type
402402
{
403-
match external_addresses.get_address_range(start_index, end_index, &key_source) {
403+
match external_addresses.address_range(start_index, end_index, &key_source) {
404404
Ok(addrs) => addrs,
405405
Err(e) => {
406406
FFIError::set_error(
@@ -582,7 +582,7 @@ pub unsafe extern "C" fn managed_wallet_get_bip_44_internal_address_range(
582582
..
583583
} = &mut managed_account.account_type
584584
{
585-
match internal_addresses.get_address_range(start_index, end_index, &key_source) {
585+
match internal_addresses.address_range(start_index, end_index, &key_source) {
586586
Ok(addrs) => addrs,
587587
Err(e) => {
588588
FFIError::set_error(
@@ -732,6 +732,7 @@ mod tests {
732732
use crate::managed_wallet::*;
733733
use crate::types::FFINetwork;
734734
use crate::wallet;
735+
use key_wallet::managed_account::managed_account_type::ManagedAccountType;
735736
use std::ffi::{CStr, CString};
736737
use std::ptr;
737738

@@ -924,12 +925,10 @@ mod tests {
924925

925926
#[test]
926927
fn test_comprehensive_address_generation() {
927-
use key_wallet::account::address_pool::AddressPool;
928-
use key_wallet::account::{
929-
ManagedAccount, ManagedAccountCollection, ManagedAccountType, StandardAccountType,
930-
};
928+
use key_wallet::account::{ManagedAccount, ManagedAccountCollection, StandardAccountType};
931929
use key_wallet::bip32::DerivationPath;
932930
use key_wallet::gap_limit::GapLimitManager;
931+
use key_wallet::managed_account::address_pool::AddressPool;
933932

934933
let mut error = FFIError::success();
935934

@@ -963,13 +962,13 @@ mod tests {
963962
// Create a managed account with address pools
964963
let external_pool = AddressPool::new(
965964
DerivationPath::from(vec![key_wallet::bip32::ChildNumber::from_normal_idx(0).unwrap()]),
966-
false,
965+
AddressPoolType::External,
967966
20,
968967
network,
969968
);
970969
let internal_pool = AddressPool::new(
971970
DerivationPath::from(vec![key_wallet::bip32::ChildNumber::from_normal_idx(1).unwrap()]),
972-
true,
971+
AddressPoolType::Internal,
973972
20,
974973
network,
975974
);

key-wallet-ffi/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl FFIAccountType {
168168
index: u32,
169169
registration_index: Option<u32>,
170170
) -> Option<key_wallet::AccountType> {
171-
use key_wallet::account::types::StandardAccountType;
171+
use key_wallet::account::account_type::StandardAccountType;
172172
match self {
173173
FFIAccountType::StandardBIP44 => Some(key_wallet::AccountType::Standard {
174174
index,
@@ -207,7 +207,7 @@ impl FFIAccountType {
207207

208208
/// Convert from AccountType
209209
pub fn from_account_type(account_type: &key_wallet::AccountType) -> (Self, u32, Option<u32>) {
210-
use key_wallet::account::types::StandardAccountType;
210+
use key_wallet::account::account_type::StandardAccountType;
211211
match account_type {
212212
key_wallet::AccountType::Standard {
213213
index,

key-wallet-ffi/src/utxo_tests.rs

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod utxo_tests {
33
use super::super::*;
44
use crate::error::{FFIError, FFIErrorCode};
55
use crate::types::FFINetwork;
6+
use key_wallet::managed_account::managed_account_type::ManagedAccountType;
67
use std::ffi::CStr;
78
use std::ptr;
89

@@ -189,10 +190,10 @@ mod utxo_tests {
189190
use crate::managed_wallet::FFIManagedWalletInfo;
190191
use dashcore::blockdata::script::ScriptBuf;
191192
use dashcore::{Address, OutPoint, TxOut, Txid};
192-
use key_wallet::account::managed_account::ManagedAccount;
193-
use key_wallet::account::managed_account_collection::ManagedAccountCollection;
194-
use key_wallet::account::types::{ManagedAccountType, StandardAccountType};
193+
use key_wallet::account::account_type::StandardAccountType;
195194
use key_wallet::gap_limit::GapLimitManager;
195+
use key_wallet::managed_account::managed_account_collection::ManagedAccountCollection;
196+
use key_wallet::managed_account::ManagedAccount;
196197
use key_wallet::utxo::Utxo;
197198
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
198199
use key_wallet::Network;
@@ -211,16 +212,16 @@ mod utxo_tests {
211212
ManagedAccountType::Standard {
212213
index: 0,
213214
standard_account_type: StandardAccountType::BIP44Account,
214-
external_addresses: key_wallet::account::address_pool::AddressPoolBuilder::default(
215-
)
216-
.base_path(key_wallet::DerivationPath::from(vec![]))
217-
.build()
218-
.unwrap(),
219-
internal_addresses: key_wallet::account::address_pool::AddressPoolBuilder::default(
220-
)
221-
.base_path(key_wallet::DerivationPath::from(vec![]))
222-
.build()
223-
.unwrap(),
215+
external_addresses:
216+
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
217+
.base_path(key_wallet::DerivationPath::from(vec![]))
218+
.build()
219+
.unwrap(),
220+
internal_addresses:
221+
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
222+
.base_path(key_wallet::DerivationPath::from(vec![]))
223+
.build()
224+
.unwrap(),
224225
},
225226
Network::Testnet,
226227
GapLimitManager::default(),
@@ -304,10 +305,10 @@ mod utxo_tests {
304305
use crate::managed_wallet::FFIManagedWalletInfo;
305306
use dashcore::blockdata::script::ScriptBuf;
306307
use dashcore::{Address, OutPoint, TxOut, Txid};
307-
use key_wallet::account::managed_account::ManagedAccount;
308-
use key_wallet::account::managed_account_collection::ManagedAccountCollection;
309-
use key_wallet::account::types::{ManagedAccountType, StandardAccountType};
308+
use key_wallet::account::account_type::StandardAccountType;
310309
use key_wallet::gap_limit::GapLimitManager;
310+
use key_wallet::managed_account::managed_account_collection::ManagedAccountCollection;
311+
use key_wallet::managed_account::ManagedAccount;
311312
use key_wallet::utxo::Utxo;
312313
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
313314
use key_wallet::Network;
@@ -325,16 +326,16 @@ mod utxo_tests {
325326
ManagedAccountType::Standard {
326327
index: 0,
327328
standard_account_type: StandardAccountType::BIP44Account,
328-
external_addresses: key_wallet::account::address_pool::AddressPoolBuilder::default(
329-
)
330-
.base_path(key_wallet::DerivationPath::from(vec![]))
331-
.build()
332-
.unwrap(),
333-
internal_addresses: key_wallet::account::address_pool::AddressPoolBuilder::default(
334-
)
335-
.base_path(key_wallet::DerivationPath::from(vec![]))
336-
.build()
337-
.unwrap(),
329+
external_addresses:
330+
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
331+
.base_path(key_wallet::DerivationPath::from(vec![]))
332+
.build()
333+
.unwrap(),
334+
internal_addresses:
335+
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
336+
.base_path(key_wallet::DerivationPath::from(vec![]))
337+
.build()
338+
.unwrap(),
338339
},
339340
Network::Testnet,
340341
GapLimitManager::default(),
@@ -364,16 +365,16 @@ mod utxo_tests {
364365
ManagedAccountType::Standard {
365366
index: 0,
366367
standard_account_type: StandardAccountType::BIP32Account,
367-
external_addresses: key_wallet::account::address_pool::AddressPoolBuilder::default(
368-
)
369-
.base_path(key_wallet::DerivationPath::from(vec![]))
370-
.build()
371-
.unwrap(),
372-
internal_addresses: key_wallet::account::address_pool::AddressPoolBuilder::default(
373-
)
374-
.base_path(key_wallet::DerivationPath::from(vec![]))
375-
.build()
376-
.unwrap(),
368+
external_addresses:
369+
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
370+
.base_path(key_wallet::DerivationPath::from(vec![]))
371+
.build()
372+
.unwrap(),
373+
internal_addresses:
374+
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
375+
.base_path(key_wallet::DerivationPath::from(vec![]))
376+
.build()
377+
.unwrap(),
377378
},
378379
Network::Testnet,
379380
GapLimitManager::default(),
@@ -400,7 +401,7 @@ mod utxo_tests {
400401
let mut coinjoin_account = ManagedAccount::new(
401402
ManagedAccountType::CoinJoin {
402403
index: 0,
403-
addresses: key_wallet::account::address_pool::AddressPoolBuilder::default()
404+
addresses: key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
404405
.base_path(key_wallet::DerivationPath::from(vec![]))
405406
.build()
406407
.unwrap(),
@@ -456,10 +457,10 @@ mod utxo_tests {
456457
use crate::managed_wallet::FFIManagedWalletInfo;
457458
use dashcore::blockdata::script::ScriptBuf;
458459
use dashcore::{Address, OutPoint, TxOut, Txid};
459-
use key_wallet::account::managed_account::ManagedAccount;
460-
use key_wallet::account::managed_account_collection::ManagedAccountCollection;
461-
use key_wallet::account::types::{ManagedAccountType, StandardAccountType};
460+
use key_wallet::account::account_type::StandardAccountType;
462461
use key_wallet::gap_limit::GapLimitManager;
462+
use key_wallet::managed_account::managed_account_collection::ManagedAccountCollection;
463+
use key_wallet::managed_account::ManagedAccount;
463464
use key_wallet::utxo::Utxo;
464465
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
465466
use key_wallet::Network;
@@ -477,16 +478,16 @@ mod utxo_tests {
477478
ManagedAccountType::Standard {
478479
index: 0,
479480
standard_account_type: StandardAccountType::BIP44Account,
480-
external_addresses: key_wallet::account::address_pool::AddressPoolBuilder::default(
481-
)
482-
.base_path(key_wallet::DerivationPath::from(vec![]))
483-
.build()
484-
.unwrap(),
485-
internal_addresses: key_wallet::account::address_pool::AddressPoolBuilder::default(
486-
)
487-
.base_path(key_wallet::DerivationPath::from(vec![]))
488-
.build()
489-
.unwrap(),
481+
external_addresses:
482+
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
483+
.base_path(key_wallet::DerivationPath::from(vec![]))
484+
.build()
485+
.unwrap(),
486+
internal_addresses:
487+
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
488+
.base_path(key_wallet::DerivationPath::from(vec![]))
489+
.build()
490+
.unwrap(),
490491
},
491492
Network::Testnet,
492493
GapLimitManager::default(),
@@ -516,16 +517,16 @@ mod utxo_tests {
516517
ManagedAccountType::Standard {
517518
index: 0,
518519
standard_account_type: StandardAccountType::BIP44Account,
519-
external_addresses: key_wallet::account::address_pool::AddressPoolBuilder::default(
520-
)
521-
.base_path(key_wallet::DerivationPath::from(vec![]))
522-
.build()
523-
.unwrap(),
524-
internal_addresses: key_wallet::account::address_pool::AddressPoolBuilder::default(
525-
)
526-
.base_path(key_wallet::DerivationPath::from(vec![]))
527-
.build()
528-
.unwrap(),
520+
external_addresses:
521+
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
522+
.base_path(key_wallet::DerivationPath::from(vec![]))
523+
.build()
524+
.unwrap(),
525+
internal_addresses:
526+
key_wallet::managed_account::address_pool::AddressPoolBuilder::default()
527+
.base_path(key_wallet::DerivationPath::from(vec![]))
528+
.build()
529+
.unwrap(),
529530
},
530531
Network::Dash,
531532
GapLimitManager::default(),

0 commit comments

Comments
 (0)