Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for any existing wallet file in case wallet_file_name is given None. #305

Merged
merged 2 commits into from
Dec 22, 2024

Conversation

KnowWhoami
Copy link
Collaborator

@KnowWhoami KnowWhoami commented Nov 17, 2024

Changing the wallet_name argument to be of type Option<String> in PR 291 introduces an internal bug.

Bug:

Now , taker/ makerd cli creates a new wallet each time we call there command if we do not pass the wallet_name argument.

Why:

coinswap/src/taker/api.rs

Lines 186 to 205 in 972585c

let mut wallet = if let Some(file_name) = wallet_file_name {
let wallet_path = wallets_dir.join(&file_name);
rpc_config.wallet_name = file_name;
if wallet_path.exists() {
// Try loading wallet
let wallet = Wallet::load(&rpc_config, &wallet_path)?;
log::info!("Wallet file at {:?} successfully loaded.", wallet_path);
wallet
} else {
// Create wallet with the given name.
let mnemonic = Mnemonic::generate(12).unwrap();
let seedphrase = mnemonic.to_string();
let wallet = Wallet::init(&wallet_path, &rpc_config, seedphrase, "".to_string())?;
log::info!("New Wallet created at : {:?}", wallet_path);
wallet
}
} else {
// Create default wallet
let mnemonic = Mnemonic::generate(12).unwrap();

As per our current implemetation, we first check the value of wallet_file_path -> if it is None -> then it creates a new wallet.

Why we don't get this error before this change?

Before that , args.wallet_name is set to the default value i.e taker in case of taker-cli before passing it to Taker::init api -> that's why it often loads up the same wallet even though we do not pass the wallet argument.

This Pr aims to fix this bug by searching for any existing wallet file in the wallets_dir in case wallet_file_path is passed as None.
It also simplifies the wallet related logic in both Taker::init and Maker::init.

Solution (After discussion):

  • take default wallet name as maker/taker-wallet if wallet_file_name=None instead of maker-unique_identifier where this identifier is created from mnemonics.

Note to Reviewers:

  • In 2e4f8e4
    I try to make file_path input more flexible by considering impl<AsRef<Path>> instead of &PathBuf or &Path etc
    as mentioned in Rust Design pattern

  • This pattern is often used in low level crates as it provides flexibility at the caller site that it can take any value of type which must implements AsRef<Path>.

  • IMO, It would be useful when we will modularise our crate but for now, It seems a bit over-engineered to me.
    what's your say?

Copy link

codecov bot commented Nov 17, 2024

Codecov Report

Attention: Patch coverage is 74.35897% with 10 lines in your changes missing coverage. Please review.

Project coverage is 73.56%. Comparing base (41c43de) to head (3096859).
Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
src/wallet/api.rs 63.63% 4 Missing ⚠️
src/maker/api.rs 72.72% 3 Missing ⚠️
src/taker/api.rs 66.66% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #305      +/-   ##
==========================================
+ Coverage   73.51%   73.56%   +0.05%     
==========================================
  Files          33       33              
  Lines        4104     4082      -22     
==========================================
- Hits         3017     3003      -14     
+ Misses       1087     1079       -8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@mojoX911 mojoX911 left a comment

Choose a reason for hiding this comment

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

Concept Ack.

But it needs some more thinking and redesign. Our random naming behavior is not at all UX friendly, and better if we drop it or keep it test only.

Deatils below.

src/maker/api.rs Outdated Show resolved Hide resolved
Copy link

@Shourya742 Shourya742 left a comment

Choose a reason for hiding this comment

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

ACK. Just minor nits. We'll remove the static dispatch stuff once we've finalized our APIs. Until then, there's no harm in keeping them, though they might slightly increase the binary size, but not by a significant margin.

tests/test_framework/mod.rs Show resolved Hide resolved
src/wallet/storage.rs Outdated Show resolved Hide resolved
src/wallet/storage.rs Outdated Show resolved Hide resolved
src/wallet/storage.rs Outdated Show resolved Hide resolved
src/wallet/api.rs Outdated Show resolved Hide resolved
src/taker/api.rs Outdated Show resolved Hide resolved
src/wallet/api.rs Outdated Show resolved Hide resolved
Copy link

@mojoX911 mojoX911 left a comment

Choose a reason for hiding this comment

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

Ack. Great cleanup. Just one nit. No other blocking comments from my side.

This now nicely goes into following fixes for UX that can be done in followup PRs.

  • Show the mnemonic, only once at init, so the user can back it up. This will require bringing back bip39.

  • Allow for wallet discovery using a mnemonic. User puts in the mnemonic and the wallet find all the seed and fidelity utxos. Swapcoins would be lost tho, for that we need to find some ways to backup the swap data.

  • Add password protection on the wallet. Its embarrassing to have wallet data in clear. We have postponed it so far but now is the time to patch it. If we can use some encrypt decrypt API from secp or rust-bitcoin lib that would be best.

src/wallet/api.rs Show resolved Hide resolved
src/wallet/storage.rs Outdated Show resolved Hide resolved
@KnowWhoami
Copy link
Collaborator Author

KnowWhoami commented Dec 21, 2024

Ack. Great cleanup. Just one nit. No other blocking comments from my side.

This now nicely goes into following fixes for UX that can be done in followup PRs.
Show the mnemonic, only once at init, so the user can back it up. This will require bringing back bip39.

Including the first task here itself , as it's small change.
created an issue for rest tasks #333

@KnowWhoami KnowWhoami force-pushed the create_wallet branch 3 times, most recently from c8af5c2 to 3096859 Compare December 21, 2024 13:53
@KnowWhoami
Copy link
Collaborator Author

It's ready.

 There is no benefit in considering `taker_behaviour` as Optional This often ends up in passing `None` in `TestFramework::init` at many places , which makes it a bit confusing. Thus making it a mandatory field helps to be more explicit here.
 This also includes:
  - replace the concept of using fingerprint  of `master_key` as wallet name to  `maker/taker-wallet`.
  - Showinng `mnemoics` while deriving `Master key` of new wallet.
  - Use `&Path` instead of `&PathBuf` to make these the api's flexible.
Copy link

@mojoX911 mojoX911 left a comment

Choose a reason for hiding this comment

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

Ack

@Shourya742 Shourya742 merged commit 9c1f324 into citadel-tech:master Dec 22, 2024
8 checks passed
@KnowWhoami KnowWhoami deleted the create_wallet branch December 23, 2024 13:43
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.

3 participants