Skip to content

Commit

Permalink
Fix for mnemonic import in existing wallet (kaspanet#289)
Browse files Browse the repository at this point in the history
* fix for the wallet mnemonic import
* additional wallet import unit tests and initial ktrv,ktub key import support
  • Loading branch information
surinder83singh authored Oct 7, 2023
1 parent 3bee833 commit ebaf58a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
4 changes: 2 additions & 2 deletions wallet/bip32/src/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ impl Prefix {

/// Is this a public key?
pub fn is_public(self) -> bool {
&self.chars[1..] == b"pub"
&self.chars[1..] == b"pub" || &self.chars[1..] == b"tub"
}

/// Is this a private key?
pub fn is_private(self) -> bool {
&self.chars[1..] == b"prv"
&self.chars[1..] == b"prv" || &self.chars[1..] == b"trv"
}

/// Get the [`Version`] number.
Expand Down
52 changes: 50 additions & 2 deletions wallet/core/src/derivation/gen1/hd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ impl WalletDerivationManager {
}

/// Serialize this key as a self-[`Zeroizing`] `String`.
pub fn to_string(&self) -> Zeroizing<String> {
let key = self.extended_public_key.to_string(Some(Prefix::KPUB));
pub fn to_string(&self, prefix: Option<Prefix>) -> Zeroizing<String> {
let key = self.extended_public_key.to_string(Some(prefix.unwrap_or(Prefix::KPUB)));
Zeroizing::new(key)
}
}
Expand Down Expand Up @@ -528,6 +528,54 @@ mod tests {
}
}

#[tokio::test]
async fn wallet_from_mnemonic() {
let mnemonic = "fringe ceiling crater inject pilot travel gas nurse bulb bullet horn segment snack harbor dice laugh vital cigar push couple plastic into slender worry";
let mnemonic = kaspa_bip32::Mnemonic::new(mnemonic, kaspa_bip32::Language::English).unwrap();
let xprv = kaspa_bip32::ExtendedPrivateKey::<kaspa_bip32::SecretKey>::new(mnemonic.to_seed("")).unwrap();
let xprv_str = xprv.to_string(kaspa_bip32::Prefix::KPRV).to_string();
assert_eq!(
xprv_str,
"kprv5y2qurMHCsXYrpeDB395BY2DPKYHUGaCMpFAYRi1cmhwin1bWRyUXVbtTyy54FCGxPnnEvbK9WaiaQgkGS9ngGxmHy1bubZYY6MTokeYP2Q",
"xprv not matched"
);

let wallet = WalletDerivationManager::from_master_xprv(&xprv_str, false, 0, None).unwrap();
let xpub_str = wallet.to_string(Some(kaspa_bip32::Prefix::KPUB)).to_string();
assert_eq!(
xpub_str,
"kpub2HtoTgsG6e1c7ixJ6JY49otNSzhEKkwnH6bsPHLAXUdYnfEuYw9LnhT7uRzaS4LSeit2rzutV6z8Fs9usdEGKnNe6p1JxfP71mK8rbUfYWo",
"drived kpub not matched"
);

println!("Extended kpub: {}\n", xpub_str);
}

#[tokio::test]
async fn address_test_by_ktrv() {
let mnemonic = "hunt bitter praise lift buyer topic crane leopard uniform network inquiry over grain pass match crush marine strike doll relax fortune trumpet sunny silk";
let mnemonic = kaspa_bip32::Mnemonic::new(mnemonic, kaspa_bip32::Language::English).unwrap();
let xprv = kaspa_bip32::ExtendedPrivateKey::<kaspa_bip32::SecretKey>::new(mnemonic.to_seed("")).unwrap();
let ktrv_str = xprv.to_string(kaspa_bip32::Prefix::KTRV).to_string();
assert_eq!(
ktrv_str,
"ktrv5himbbCxArFU2CHiEQyVHP1ABS1tA1SY88CwePzGeM8gHfWmkNBXehhKsESH7UwcxpjpDdMNbwtBfyPoZ7W59kYfVnUXKRgv8UguDns2FQb",
"master ktrv not matched"
);

let wallet = WalletDerivationManager::from_master_xprv(&ktrv_str, false, 0, None).unwrap();
let ktub_str = wallet.to_string(Some(kaspa_bip32::Prefix::KTUB)).to_string();
assert_eq!(
ktub_str,
"ktub23beJLczbxoS4emYHxm5H2rPnXJPGTwjNLAc8JyjHnSFLPMJBj5h3U8oWbn1x1jayZRov6uhvGd4zUGrWH6PkYZMWsykUsQWYqjbLnHrzUE",
"drived ktub not matched"
);

let key = wallet.derive_receive_pubkey(1).unwrap();
let address = PubkeyDerivationManager::create_address(&key, Prefix::Testnet, false).unwrap().to_string();
assert_eq!(address, "kaspatest:qrc2959g0pqda53glnfd238cdnmk24zxzkj8n5x83rkktx4h73dkc4ave6wyg")
}

#[tokio::test]
async fn generate_addresses_by_range() {
let master_xprv =
Expand Down
3 changes: 2 additions & 1 deletion wallet/core/src/runtime/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,9 @@ impl Wallet {

let account: Arc<dyn Account> = match account_kind {
AccountKind::Bip32 => {
let xpub_keys = Arc::new(vec![]);
let account_index = 0;
let xpub_key = prv_key_data.create_xpub(payment_secret, account_kind, account_index).await?;
let xpub_keys = Arc::new(vec![xpub_key.to_string(Some(kaspa_bip32::Prefix::KPUB))]);
let ecdsa = false;
// ---

Expand Down

0 comments on commit ebaf58a

Please sign in to comment.