Skip to content

Commit

Permalink
Minor wallet framework updates (#367)
Browse files Browse the repository at this point in the history
* fix account derivation index relay in bip32

* fix serializations and sanity checks

* zeroize bip39 mnemonic after bip44 scan

* code formatting
  • Loading branch information
aspect authored Dec 27, 2023
1 parent a4ba784 commit 8185182
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
16 changes: 12 additions & 4 deletions wallet/core/src/account/variants/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,17 @@ impl Bip32 {
let (id, storage_key) = make_account_hashes(from_bip32(&prv_key_data_id, &storable));
let inner = Arc::new(Inner::new(wallet, id, storage_key, settings));

let derivation =
AddressDerivationManager::new(wallet, BIP32_ACCOUNT_KIND.into(), &xpub_keys, ecdsa, 0, None, 1, Default::default())
.await?;
let derivation = AddressDerivationManager::new(
wallet,
BIP32_ACCOUNT_KIND.into(),
&xpub_keys,
ecdsa,
account_index,
None,
1,
Default::default(),
)
.await?;

Ok(Self { inner, prv_key_data_id, account_index, xpub_keys, ecdsa, derivation })
}
Expand All @@ -126,7 +134,7 @@ impl Bip32 {
BIP32_ACCOUNT_KIND.into(),
&xpub_keys,
ecdsa,
0,
account_index,
None,
1,
address_derivation_indexes,
Expand Down
15 changes: 8 additions & 7 deletions wallet/core/src/storage/local/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,10 @@ impl Interface for LocalStore {
}

async fn open(&self, wallet_secret: &Secret, args: OpenArgs) -> Result<()> {
if self.inner()?.is_modified() {
panic!("LocalStore::open called while modified flag is true!");
if let Some(inner) = self.inner.lock().unwrap().as_ref() {
if inner.is_modified() {
panic!("LocalStore::open called while modified flag is true!");
}
}

let location = self.location.lock().unwrap().clone().unwrap();
Expand All @@ -412,11 +414,10 @@ impl Interface for LocalStore {
let mut descriptors = vec![];
for filename in wallets.into_iter() {
let path = folder.join(format!("{}.wallet", filename));
let title = fs::read_to_string(&path)
.await
.ok()
.and_then(|json| serde_json::Value::from_str(json.as_str()).ok())
.and_then(|data| data.get("name").and_then(|v| v.as_str()).map(|v| v.to_string()));
// TODO - refactor on native to read directly from file (skip temporary buffer creation)
let wallet_data = fs::read(&path).await;
let title =
wallet_data.ok().and_then(|data| WalletStorage::try_from_slice(data.as_slice()).ok()).and_then(|wallet| wallet.title);
descriptors.push(WalletDescriptor { title, filename });
}

Expand Down
2 changes: 1 addition & 1 deletion wallet/core/src/storage/local/transaction/fsio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,6 @@ async fn write(path: &Path, record: &TransactionRecord, secret: Option<&Secret>,
} else {
Encryptable::from(record.clone())
};
fs::write_json(path, &data).await?;
fs::write(path, &data.try_to_vec()?).await?;
Ok(())
}
11 changes: 10 additions & 1 deletion wallet/core/src/utxo/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,15 @@ impl UtxoContext {
}

pub async fn register_addresses(&self, addresses: &[Address]) -> Result<()> {
if addresses.is_empty() {
log_error!("utxo processor: register for an empty address set");
}

let local = self.addresses();

// addresses are filtered for a known address set where
// addresses can already be registered with the processor
// as a part of address space (Scan window) pre-caching.
let addresses = addresses
.iter()
.filter_map(|address| {
Expand All @@ -654,7 +661,9 @@ impl UtxoContext {
})
.collect::<Vec<_>>();

self.processor().register_addresses(addresses, self).await?;
if addresses.is_not_empty() {
self.processor().register_addresses(addresses, self).await?;
}

Ok(())
}
Expand Down
4 changes: 3 additions & 1 deletion wallet/core/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ impl Wallet {
/// accounts.
pub async fn scan_bip44_accounts(
self: &Arc<Self>,
bip39_mnemonic: String,
mut bip39_mnemonic: String,
bip39_passphrase: Option<Secret>,
address_scan_extent: u32,
account_scan_extent: u32,
Expand Down Expand Up @@ -1211,6 +1211,8 @@ impl Wallet {
account_index += 1;
}

bip39_mnemonic.zeroize();

Ok(last_account_index)
}

Expand Down

0 comments on commit 8185182

Please sign in to comment.