Skip to content

Commit fe9e0bc

Browse files
dcgdcg
authored andcommitted
revert: restore original key-wallet-ffi/src/lib.rs implementation
Reverted all recent changes that replaced kw_address with dashcore types: - Restored 'address as kw_address' import - Reverted AddressType conversions back to kw_address types - Removed extra Error enum match cases (CoinJoinNotEnabled, Serialization, InvalidParameter) - Restored Address struct to use kw_address::Address - Uncommented AddressGenerator struct and all its methods - Restored generate() and generate_range() method implementations This brings the file back to its original working state with proper key-wallet address handling.
1 parent d8ec6a0 commit fe9e0bc

File tree

1 file changed

+77
-91
lines changed

1 file changed

+77
-91
lines changed

key-wallet-ffi/src/lib.rs

Lines changed: 77 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::str::FromStr;
44
use std::sync::Arc;
55

66
use key_wallet::{
7-
self as kw, derivation::HDWallet as KwHDWallet, mnemonic as kw_mnemonic,
7+
self as kw, address as kw_address, derivation::HDWallet as KwHDWallet, mnemonic as kw_mnemonic,
88
DerivationPath as KwDerivationPath, ExtendedPrivKey, ExtendedPubKey, Network as KwNetwork,
99
};
1010
use secp256k1::{PublicKey, Secp256k1};
@@ -75,20 +75,20 @@ pub enum AddressType {
7575
P2SH,
7676
}
7777

78-
impl From<dashcore::AddressType> for AddressType {
79-
fn from(t: dashcore::AddressType) -> Self {
78+
impl From<kw_address::AddressType> for AddressType {
79+
fn from(t: kw_address::AddressType) -> Self {
8080
match t {
81-
dashcore::AddressType::P2pkh => AddressType::P2PKH,
82-
dashcore::AddressType::P2sh => AddressType::P2SH,
81+
kw_address::AddressType::P2PKH => AddressType::P2PKH,
82+
kw_address::AddressType::P2SH => AddressType::P2SH,
8383
}
8484
}
8585
}
8686

87-
impl From<AddressType> for dashcore::AddressType {
87+
impl From<AddressType> for kw_address::AddressType {
8888
fn from(t: AddressType) -> Self {
8989
match t {
90-
AddressType::P2PKH => dashcore::AddressType::P2pkh,
91-
AddressType::P2SH => dashcore::AddressType::P2sh,
90+
AddressType::P2PKH => kw_address::AddressType::P2PKH,
91+
AddressType::P2SH => kw_address::AddressType::P2SH,
9292
}
9393
}
9494
}
@@ -189,15 +189,6 @@ impl From<kw::Error> for KeyWalletError {
189189
kw::Error::KeyError(msg) => KeyWalletError::KeyError {
190190
message: msg,
191191
},
192-
kw::Error::CoinJoinNotEnabled => KeyWalletError::InvalidMnemonic {
193-
message: "CoinJoin not enabled".into(),
194-
},
195-
kw::Error::Serialization(msg) => KeyWalletError::KeyError {
196-
message: msg,
197-
},
198-
kw::Error::InvalidParameter(msg) => KeyWalletError::InvalidDerivationPath {
199-
message: msg,
200-
},
201192
}
202193
}
203194
}
@@ -448,14 +439,12 @@ impl ExtPubKey {
448439

449440
// Address wrapper
450441
pub struct Address {
451-
inner: dashcore::Address,
442+
inner: kw_address::Address,
452443
}
453444

454445
impl Address {
455446
pub fn from_string(address: String, network: Network) -> Result<Self, KeyWalletError> {
456-
let inner = dashcore::Address::from_str(&address).map_err(|e| KeyWalletError::AddressError {
457-
message: e.to_string(),
458-
})?;
447+
let inner = kw_address::Address::from_str(&address).map_err(|e| KeyWalletError::from(e))?;
459448

460449
// Validate that the parsed network matches the expected network
461450
// Note: Testnet, Devnet, and Regtest all share the same address prefixes (140/19)
@@ -496,7 +485,7 @@ impl Address {
496485
PublicKey::from_slice(&public_key).map_err(|e| KeyWalletError::Secp256k1Error {
497486
message: e.to_string(),
498487
})?;
499-
let inner = dashcore::Address::p2pkh(&pubkey, network.into());
488+
let inner = kw_address::Address::p2pkh(&pubkey, network.into());
500489
Ok(Self {
501490
inner,
502491
})
@@ -526,75 +515,72 @@ impl Address {
526515
}
527516

528517
// Address generator wrapper
529-
// NOTE: AddressGenerator doesn't exist in dashcore, commented out for now
530-
// pub struct AddressGenerator {
531-
// inner: dashcore::address::AddressGenerator,
532-
// }
533-
534-
// impl AddressGenerator {
535-
// pub fn new(network: Network) -> Self {
536-
// Self {
537-
// inner: dashcore::address::AddressGenerator::new(network.into()),
538-
// }
539-
// }
540-
541-
// AddressGenerator methods commented out since AddressGenerator doesn't exist in dashcore
542-
// TODO: Implement address generation functionality without AddressGenerator
543-
// pub fn generate(
544-
// &self,
545-
// account_xpub: AccountXPub,
546-
// external: bool,
547-
// index: u32,
548-
// ) -> Result<Arc<Address>, KeyWalletError> {
549-
// // Parse the extended public key from string
550-
// let xpub =
551-
// ExtendedPubKey::from_str(&account_xpub.xpub).map_err(|e| KeyWalletError::KeyError {
552-
// message: e.to_string(),
553-
// })?;
554-
//
555-
// // Generate addresses for a single index
556-
// let addrs = self
557-
// .inner
558-
// .generate_range(&xpub, external, index, 1)
559-
// .map_err(|e| KeyWalletError::from(e))?;
560-
//
561-
// let addr = addrs.into_iter().next().ok_or_else(|| KeyWalletError::KeyError {
562-
// message: "Failed to generate address".into(),
563-
// })?;
564-
//
565-
// Ok(Arc::new(Address {
566-
// inner: addr,
567-
// }))
568-
// }
569-
//
570-
// pub fn generate_range(
571-
// &self,
572-
// account_xpub: AccountXPub,
573-
// external: bool,
574-
// start: u32,
575-
// count: u32,
576-
// ) -> Result<Vec<Arc<Address>>, KeyWalletError> {
577-
// // Parse the extended public key from string
578-
// let xpub =
579-
// ExtendedPubKey::from_str(&account_xpub.xpub).map_err(|e| KeyWalletError::KeyError {
580-
// message: e.to_string(),
581-
// })?;
582-
//
583-
// let addrs = self
584-
// .inner
585-
// .generate_range(&xpub, external, start, count)
586-
// .map_err(|e| KeyWalletError::from(e))?;
587-
//
588-
// Ok(addrs
589-
// .into_iter()
590-
// .map(|addr| {
591-
// Arc::new(Address {
592-
// inner: addr,
593-
// })
594-
// })
595-
// .collect())
596-
// }
597-
// }
518+
pub struct AddressGenerator {
519+
inner: kw_address::AddressGenerator,
520+
}
521+
522+
impl AddressGenerator {
523+
pub fn new(network: Network) -> Self {
524+
Self {
525+
inner: kw_address::AddressGenerator::new(network.into()),
526+
}
527+
}
528+
529+
pub fn generate(
530+
&self,
531+
account_xpub: AccountXPub,
532+
external: bool,
533+
index: u32,
534+
) -> Result<Arc<Address>, KeyWalletError> {
535+
// Parse the extended public key from string
536+
let xpub =
537+
ExtendedPubKey::from_str(&account_xpub.xpub).map_err(|e| KeyWalletError::KeyError {
538+
message: e.to_string(),
539+
})?;
540+
541+
// Generate addresses for a single index
542+
let addrs = self
543+
.inner
544+
.generate_range(&xpub, external, index, 1)
545+
.map_err(|e| KeyWalletError::from(e))?;
546+
547+
let addr = addrs.into_iter().next().ok_or_else(|| KeyWalletError::KeyError {
548+
message: "Failed to generate address".into(),
549+
})?;
550+
551+
Ok(Arc::new(Address {
552+
inner: addr,
553+
}))
554+
}
555+
556+
pub fn generate_range(
557+
&self,
558+
account_xpub: AccountXPub,
559+
external: bool,
560+
start: u32,
561+
count: u32,
562+
) -> Result<Vec<Arc<Address>>, KeyWalletError> {
563+
// Parse the extended public key from string
564+
let xpub =
565+
ExtendedPubKey::from_str(&account_xpub.xpub).map_err(|e| KeyWalletError::KeyError {
566+
message: e.to_string(),
567+
})?;
568+
569+
let addrs = self
570+
.inner
571+
.generate_range(&xpub, external, start, count)
572+
.map_err(|e| KeyWalletError::from(e))?;
573+
574+
Ok(addrs
575+
.into_iter()
576+
.map(|addr| {
577+
Arc::new(Address {
578+
inner: addr,
579+
})
580+
})
581+
.collect())
582+
}
583+
}
598584

599585
#[cfg(test)]
600586
mod network_compatibility_tests {

0 commit comments

Comments
 (0)