Skip to content

Commit

Permalink
Move-only: group account retrieval methods together.
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Dec 30, 2024
1 parent da98f9f commit 736bfd5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 68 deletions.
2 changes: 1 addition & 1 deletion components/zcash_address/src/kind/unified/ivk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloc::vec::Vec;
use zcash_protocol::constants;
use core::convert::{TryFrom, TryInto};
use zcash_protocol::constants;

use super::{
private::{SealedContainer, SealedItem},
Expand Down
134 changes: 67 additions & 67 deletions zcash_client_sqlite/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,73 @@ pub(crate) fn get_unified_full_viewing_keys<P: consensus::Parameters>(
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`],
/// if any.
pub(crate) fn get_account_for_ufvk<P: consensus::Parameters>(
Expand Down Expand Up @@ -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`]
/// and [`zip32::AccountId`], if any.
pub(crate) fn get_derived_account<P: consensus::Parameters>(
Expand Down Expand Up @@ -1929,29 +1952,6 @@ pub(crate) fn get_account_uuid(
.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.
pub(crate) fn chain_tip_height(
conn: &rusqlite::Connection,
Expand Down

0 comments on commit 736bfd5

Please sign in to comment.