Skip to content

fix standalone private keys encryption and usage - #1959

Merged
OBorce merged 2 commits into
masterfrom
fix/wallet-standalone-keys-encryption
Aug 20, 2025
Merged

OBorce merged 2 commits into
masterfrom
fix/wallet-standalone-keys-encryption

Conversation

@OBorce

@OBorce OBorce commented Aug 19, 2025

Copy link
Copy Markdown
Contributor
  • fix encryption of the standalone private keys when a new password is created or changed
  • fix watching/scanning for relevant public key destinations belonging to standalone keys (only public key has destinations used to work before)
  • fix wallet balance to show spendable UTXOs belonging to standalone private keys.

Comment thread wallet/src/wallet/tests.rs Outdated

#[track_caller]
fn create_block<B, P>(
fn create_block_with_address_reward<B, P>(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The name looks strange. create_block_with_reward_address or create_block_with_reward_destination would be better.

  2. It doesn't make sense to return an Address from this function. Or even constructing it, because the function only needs Destination.

Comment thread wallet/src/wallet/tests.rs Outdated

// success after unlock
wallet.unlock_wallet(&password.unwrap()).unwrap();
if rng.gen::<bool>() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In such cases it's better to add a parameter to the test instead of randomly deciding what to do (because the code paths are quite different)

Comment thread wallet/src/wallet/tests.rs Outdated
Comment on lines +1293 to +1295
wallet
.add_standalone_private_key(DEFAULT_ACCOUNT_INDEX, standalone_sk, None)
.unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, it's better to check both cases - i.e. both when the key is added before the wallet is encrypted and after that.
(via a test parameter or at least a random bool)

)
.unwrap();
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another scenario worth checking is:

  1. encrypt the wallet.
  2. add a standalone private key.
  3. while the wallet is encrypted, encrypt it again with a different password.
  4. check that that the key works (i.e. it was re-encrypted with the new password).

Probably it's better to make it a separate test though.

Comment thread wallet/src/wallet/tests.rs Outdated
// Check amount is still zero
let coin_balance = get_coin_balance(&wallet);
assert_eq!(coin_balance, Amount::ZERO);
assert_eq!(coin_balance, block1_amount);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "Check amount is still zero" is now wrong.

P.S. the later comment "but the transaction has been added to the wallet" will also have to be changed.

Comment thread wallet/src/wallet/tests.rs Outdated
vec![],
block1_amount,
0,
Destination::PublicKey(standalone_pk),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should try both PublicKey and PublicKeyHash here (via a test parameter or a random bool).

Comment thread wallet/src/account/mod.rs
self.key_chain.is_public_key_hash_mine_or_watched(*pkh)
}
Destination::PublicKey(pk) => self.key_chain.is_public_key_mine(pk),
Destination::PublicKey(pk) => self.key_chain.is_public_key_mine_or_watched(pk.clone()),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, I guess there is no test that covers this fix. Can you add one?

Comment thread wallet/src/wallet/tests.rs Outdated
#[rstest]
#[trace]
#[case(Seed::from_entropy())]
fn locked_wallet_standalone_keys(#[case] seed: Seed) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I'd say the emphasis here is on "standalone_keys" and not "locked_wallet".
  2. Do we still need test_add_standalone_private_key? It looks like it does only a subset of what this test does. (I guess the only thing this test doesn't do is check the result of wallet.get_transaction, but we could add the check).
  3. If it's still needed, let's put the two tests near each other.

Comment thread wallet/src/wallet/tests.rs Outdated
P: SignerProvider,
{
let address = wallet.get_new_address(DEFAULT_ACCOUNT_INDEX).unwrap().1;
let address = Address::new(chain_config, address).unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're constructing this only to call into_object later. Pass the Destination to make_address_output directly.

@OBorce
OBorce force-pushed the fix/wallet-standalone-keys-encryption branch from 3d30f5f to 6822c1c Compare August 20, 2025 09:12
@OBorce
OBorce force-pushed the fix/wallet-standalone-keys-encryption branch from 6822c1c to 7331ff4 Compare August 20, 2025 11:19
@OBorce
OBorce merged commit 3074e1f into master Aug 20, 2025
28 checks passed
@OBorce
OBorce deleted the fix/wallet-standalone-keys-encryption branch August 20, 2025 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants