This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix wallet import #7873
Merged
Merged
Fix wallet import #7873
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e170c8e
rpc: generate new account id for imported wallets
andresilva 9346d29
ethstore: handle duplicate wallet filenames
andresilva 4bf912d
ethstore: simplify deduplication of wallet file names
andresilva 7ba0bc4
ethstore: do not dedup wallet filenames on update
andresilva d7cc6a5
ethstore: fix minor grumbles
andresilva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -153,8 +153,42 @@ impl<T> DiskDirectory<T> where T: KeyFileManager { | |
) | ||
} | ||
|
||
/// insert account with given file name | ||
pub fn insert_with_filename(&self, account: SafeAccount, filename: String) -> Result<SafeAccount, Error> { | ||
/// insert account with given file name. if the filename is a duplicate of any stored account, a number is appended | ||
/// to the filename. | ||
pub fn insert_with_filename(&self, account: SafeAccount, mut filename: String) -> Result<SafeAccount, Error> { | ||
// check for duplicate filenames and append/increment counter | ||
let dups: Vec<_> = self.files()?.into_iter().filter_map(|f| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also - maybe just check if file exists ( |
||
match f.file_name().map(|n| n.to_string_lossy()) { | ||
Some(ref name) if name.starts_with(&filename) => Some(name.to_string()), | ||
_ => None | ||
} | ||
}).collect(); | ||
|
||
if !dups.is_empty() { | ||
let mut max = 0; | ||
let mut found = false; | ||
|
||
for dup in dups { | ||
if dup.len() == filename.len() { | ||
// we found a file with the same name | ||
found = true; | ||
} | ||
else if dup[filename.len()..].starts_with("-") { | ||
// this is a duplicate file with a counter appended | ||
match dup[filename.len() + 1..].parse() { | ||
Ok(n) if n > max => { | ||
max = n; | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
} | ||
|
||
if found { | ||
filename.push_str(&format!("-{}", max + 1)) | ||
} | ||
} | ||
|
||
// update account filename | ||
let original_account = account.clone(); | ||
let mut account = account; | ||
|
@@ -317,6 +351,34 @@ mod test { | |
let _ = fs::remove_dir_all(dir); | ||
} | ||
|
||
#[test] | ||
fn should_handle_duplicate_filenames() { | ||
// given | ||
let mut dir = env::temp_dir(); | ||
dir.push("ethstore_should_handle_duplicate_filenames"); | ||
let keypair = Random.generate().unwrap(); | ||
let password = "hello world"; | ||
let directory = RootDiskDirectory::create(dir.clone()).unwrap(); | ||
|
||
// when | ||
let account = SafeAccount::create(&keypair, [0u8; 16], password, 1024, "Test".to_owned(), "{}".to_owned()); | ||
let filename = "test".to_string(); | ||
|
||
directory.insert_with_filename(account.clone(), "foo".to_string()).unwrap(); | ||
directory.insert_with_filename(account.clone(), "testa".to_string()).unwrap(); | ||
let res1 = directory.insert_with_filename(account.clone(), filename.clone()); | ||
let res2 = directory.insert_with_filename(account.clone(), filename.clone()); | ||
let res3 = directory.insert_with_filename(account.clone(), filename.clone()); | ||
|
||
// then | ||
assert_eq!(res1.unwrap().filename.unwrap(), filename); | ||
assert_eq!(res2.unwrap().filename.unwrap(), format!("{}-1", filename)); | ||
assert_eq!(res3.unwrap().filename.unwrap(), format!("{}-2", filename)); | ||
|
||
// cleanup | ||
let _ = fs::remove_dir_all(dir); | ||
} | ||
|
||
#[test] | ||
fn should_manage_vaults() { | ||
// given | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please check that this comment in
update
(https://github.com/paritytech/parity/blob/006b2f35640d2b729b77b4c5f93b2e9b6a10c5d4/ethstore/src/accounts_dir/disk.rs#L202) is still valid? I mean - if existing account is updated (the file with the same name already exists), file won't be duplicated (with suffix append).