Move-only: group account retrieval methods together.

This commit is contained in:
Kris Nuttycombe 2024-12-23 13:17:07 -07:00
parent da98f9f2b5
commit 736bfd555b
2 changed files with 68 additions and 68 deletions

View File

@ -1,6 +1,6 @@
use alloc::vec::Vec; use alloc::vec::Vec;
use zcash_protocol::constants;
use core::convert::{TryFrom, TryInto}; use core::convert::{TryFrom, TryInto};
use zcash_protocol::constants;
use super::{ use super::{
private::{SealedContainer, SealedItem}, private::{SealedContainer, SealedItem},

View File

@ -733,6 +733,73 @@ pub(crate) fn get_unified_full_viewing_keys<P: consensus::Parameters>(
Ok(res) Ok(res)
} }
fn parse_account_row<P: consensus::Parameters>(
row: &rusqlite::Row<'_>,
params: &P,
) -> Result<Account, SqliteClientError> {
let account_name = row.get("name")?;
let account_uuid = AccountUuid(row.get("uuid")?);
let kind = parse_account_source(
row.get("account_kind")?,
row.get("hd_seed_fingerprint")?,
row.get("hd_account_index")?,
row.get("has_spend_key")?,
row.get("key_source")?,
)?;
let ufvk_str: Option<String> = row.get("ufvk")?;
let viewing_key = if let Some(ufvk_str) = ufvk_str {
ViewingKey::Full(Box::new(
UnifiedFullViewingKey::decode(params, &ufvk_str).map_err(|e| {
SqliteClientError::CorruptedData(format!(
"Could not decode unified full viewing key for account {}: {}",
account_uuid.0, e
))
})?,
))
} else {
let uivk_str: String = row.get("uivk")?;
ViewingKey::Incoming(Box::new(
UnifiedIncomingViewingKey::decode(params, &uivk_str).map_err(|e| {
SqliteClientError::CorruptedData(format!(
"Could not decode unified incoming viewing key for account {}: {}",
account_uuid.0, e
))
})?,
))
};
Ok(Account {
name: account_name,
uuid: account_uuid,
kind,
viewing_key,
})
}
pub(crate) fn get_account<P: Parameters>(
conn: &rusqlite::Connection,
params: &P,
account_uuid: AccountUuid,
) -> Result<Option<Account>, SqliteClientError> {
let mut stmt = conn.prepare_cached(
r#"
SELECT name, uuid, account_kind,
hd_seed_fingerprint, hd_account_index, key_source,
ufvk, uivk, has_spend_key
FROM accounts
WHERE uuid = :account_uuid
"#,
)?;
let mut rows = stmt.query_and_then::<_, SqliteClientError, _, _>(
named_params![":account_uuid": account_uuid.0],
|row| parse_account_row(row, params),
)?;
rows.next().transpose()
}
/// Returns the account id corresponding to a given [`UnifiedFullViewingKey`], /// Returns the account id corresponding to a given [`UnifiedFullViewingKey`],
/// if any. /// if any.
pub(crate) fn get_account_for_ufvk<P: consensus::Parameters>( pub(crate) fn get_account_for_ufvk<P: consensus::Parameters>(
@ -782,50 +849,6 @@ pub(crate) fn get_account_for_ufvk<P: consensus::Parameters>(
} }
} }
fn parse_account_row<P: consensus::Parameters>(
row: &rusqlite::Row<'_>,
params: &P,
) -> Result<Account, SqliteClientError> {
let account_name = row.get("name")?;
let account_uuid = AccountUuid(row.get("uuid")?);
let kind = parse_account_source(
row.get("account_kind")?,
row.get("hd_seed_fingerprint")?,
row.get("hd_account_index")?,
row.get("has_spend_key")?,
row.get("key_source")?,
)?;
let ufvk_str: Option<String> = row.get("ufvk")?;
let viewing_key = if let Some(ufvk_str) = ufvk_str {
ViewingKey::Full(Box::new(
UnifiedFullViewingKey::decode(params, &ufvk_str).map_err(|e| {
SqliteClientError::CorruptedData(format!(
"Could not decode unified full viewing key for account {}: {}",
account_uuid.0, e
))
})?,
))
} else {
let uivk_str: String = row.get("uivk")?;
ViewingKey::Incoming(Box::new(
UnifiedIncomingViewingKey::decode(params, &uivk_str).map_err(|e| {
SqliteClientError::CorruptedData(format!(
"Could not decode unified incoming viewing key for account {}: {}",
account_uuid.0, e
))
})?,
))
};
Ok(Account {
name: account_name,
uuid: account_uuid,
kind,
viewing_key,
})
}
/// Returns the account id corresponding to a given [`SeedFingerprint`] /// Returns the account id corresponding to a given [`SeedFingerprint`]
/// and [`zip32::AccountId`], if any. /// and [`zip32::AccountId`], if any.
pub(crate) fn get_derived_account<P: consensus::Parameters>( pub(crate) fn get_derived_account<P: consensus::Parameters>(
@ -1929,29 +1952,6 @@ pub(crate) fn get_account_uuid(
.ok_or(SqliteClientError::AccountUnknown) .ok_or(SqliteClientError::AccountUnknown)
} }
pub(crate) fn get_account<P: Parameters>(
conn: &rusqlite::Connection,
params: &P,
account_uuid: AccountUuid,
) -> Result<Option<Account>, SqliteClientError> {
let mut stmt = conn.prepare_cached(
r#"
SELECT name, uuid, account_kind,
hd_seed_fingerprint, hd_account_index, key_source,
ufvk, uivk, has_spend_key
FROM accounts
WHERE uuid = :account_uuid
"#,
)?;
let mut rows = stmt.query_and_then::<_, SqliteClientError, _, _>(
named_params![":account_uuid": account_uuid.0],
|row| parse_account_row(row, params),
)?;
rows.next().transpose()
}
/// Returns the minimum and maximum heights of blocks in the chain which may be scanned. /// Returns the minimum and maximum heights of blocks in the chain which may be scanned.
pub(crate) fn chain_tip_height( pub(crate) fn chain_tip_height(
conn: &rusqlite::Connection, conn: &rusqlite::Connection,