Skip to content

Commit

Permalink
More sqlite improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Mar 9, 2024
1 parent b008add commit b24d6f7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
38 changes: 11 additions & 27 deletions zcash_client_sqlite/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,23 @@ fn get_sql_values_for_account_parameters<'a, P: consensus::Parameters>(
hd_seed_fingerprint: Some(hdaccount.hd_seed_fingerprint().as_bytes()),
hd_account_index: Some(hdaccount.account_index().into()),
ufvk: Some(hdaccount.ufvk().encode(params)),
uivk: ufvk_to_uivk(hdaccount.ufvk(), params)?,
uivk: hdaccount
.ufvk()
.to_unified_incoming_viewing_key()
.map_err(|e| SqliteClientError::CorruptedData(e.to_string()))?
.to_uivk()
.encode(&params.network_type()),
},
Account::Imported(ImportedAccount::Full(ufvk)) => AccountSqlValues {
account_type: AccountType::Imported.into(),
hd_seed_fingerprint: None,
hd_account_index: None,
ufvk: Some(ufvk.encode(params)),
uivk: ufvk_to_uivk(ufvk, params)?,
uivk: ufvk
.to_unified_incoming_viewing_key()
.map_err(|e| SqliteClientError::CorruptedData(e.to_string()))?
.to_uivk()
.encode(&params.network_type()),
},
Account::Imported(ImportedAccount::Incoming(uivk)) => AccountSqlValues {
account_type: AccountType::Imported.into(),
Expand All @@ -325,31 +334,6 @@ fn get_sql_values_for_account_parameters<'a, P: consensus::Parameters>(
})
}

pub(crate) fn ufvk_to_uivk<P: consensus::Parameters>(
ufvk: &UnifiedFullViewingKey,
params: &P,
) -> Result<String, SqliteClientError> {
let mut ivks: Vec<Ivk> = Vec::new();
if let Some(orchard) = ufvk.orchard() {
ivks.push(Ivk::Orchard(orchard.to_ivk(Scope::External).to_bytes()));
}
if let Some(sapling) = ufvk.sapling() {
let ivk = sapling.to_external_ivk();
ivks.push(Ivk::Sapling(ivk.to_bytes()));
}
#[cfg(feature = "transparent-inputs")]
if let Some(tfvk) = ufvk.transparent() {
let tivk = tfvk.derive_external_ivk()?;
ivks.push(Ivk::P2pkh(tivk.serialize().try_into().map_err(|_| {
SqliteClientError::BadAccountData("Unable to serialize transparent IVK.".to_string())
})?));
}

let uivk = zcash_address::unified::Uivk::try_from_items(ivks)
.map_err(|e| SqliteClientError::BadAccountData(format!("Unable to derive UIVK: {}", e)))?;
Ok(uivk.encode(&params.network_type()))
}

pub(crate) fn add_account<P: consensus::Parameters>(
conn: &rusqlite::Transaction,
params: &P,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::{collections::HashSet, rc::Rc};

use crate::wallet::{init::WalletMigrationError, ufvk_to_uivk, AccountType};
use crate::wallet::{init::WalletMigrationError, AccountType};
use rusqlite::{named_params, Transaction};
use schemer_rusqlite::RusqliteMigration;
use secrecy::{ExposeSecret, SecretVec};
use uuid::Uuid;
use zcash_address::unified::Encoding;
use zcash_client_backend::keys::UnifiedSpendingKey;
use zcash_keys::keys::{HdSeedFingerprint, UnifiedFullViewingKey};
use zcash_primitives::consensus;
Expand Down Expand Up @@ -113,8 +114,11 @@ impl<P: consensus::Parameters> RusqliteMigration for Migration<P> {
));
}

let uivk = ufvk_to_uivk(&ufvk_parsed, &self.params)
.map_err(|e| WalletMigrationError::CorruptedData(e.to_string()))?;
let uivk = ufvk_parsed
.to_unified_incoming_viewing_key()
.map_err(|e| WalletMigrationError::CorruptedData(e.to_string()))?
.to_uivk()
.encode(&self.params.network_type());

transaction.execute(r#"
INSERT INTO accounts_new (id, account_type, hd_seed_fingerprint, hd_account_index, ufvk, uivk, birthday_height, recover_until_height)
Expand Down

0 comments on commit b24d6f7

Please sign in to comment.