From c8b70e569ced8ec99392e0e89379043d12d33e3b Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sat, 9 Mar 2019 02:23:31 +0000 Subject: [PATCH] zcash_client_sqlite::query::get_address() --- zcash_client_sqlite/src/init.rs | 21 +++++++++++++++++++++ zcash_client_sqlite/src/lib.rs | 1 + zcash_client_sqlite/src/query.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 zcash_client_sqlite/src/query.rs diff --git a/zcash_client_sqlite/src/init.rs b/zcash_client_sqlite/src/init.rs index c5ea7936c..e457ad930 100644 --- a/zcash_client_sqlite/src/init.rs +++ b/zcash_client_sqlite/src/init.rs @@ -243,12 +243,16 @@ pub fn init_blocks_table>( #[cfg(test)] mod tests { use tempfile::NamedTempFile; + use zcash_client_backend::{ + constants::testnet::HRP_SAPLING_PAYMENT_ADDRESS, encoding::decode_payment_address, + }; use zcash_primitives::{ block::BlockHash, zip32::{ExtendedFullViewingKey, ExtendedSpendingKey}, }; use super::{init_accounts_table, init_blocks_table, init_data_database}; + use crate::query::get_address; #[test] fn init_accounts_table_only_works_once() { @@ -283,4 +287,21 @@ mod tests { // Subsequent calls should return an error init_blocks_table(&db_data, 2, BlockHash([2; 32]), 2, &[]).unwrap_err(); } + + #[test] + fn init_accounts_table_stores_correct_address() { + let data_file = NamedTempFile::new().unwrap(); + let db_data = data_file.path(); + init_data_database(&db_data).unwrap(); + + // Add an account to the wallet + let extsk = ExtendedSpendingKey::master(&[]); + let extfvks = [ExtendedFullViewingKey::from(&extsk)]; + init_accounts_table(&db_data, &extfvks).unwrap(); + + // The account's address should be in the data DB + let addr = get_address(&db_data, 0).unwrap(); + let pa = decode_payment_address(HRP_SAPLING_PAYMENT_ADDRESS, &addr).unwrap(); + assert_eq!(pa.unwrap(), extsk.default_address().unwrap().1); + } } diff --git a/zcash_client_sqlite/src/lib.rs b/zcash_client_sqlite/src/lib.rs index 767b58280..32ebc1f9d 100644 --- a/zcash_client_sqlite/src/lib.rs +++ b/zcash_client_sqlite/src/lib.rs @@ -26,6 +26,7 @@ use zcash_primitives::zip32::ExtendedFullViewingKey; pub mod error; pub mod init; +pub mod query; fn address_from_extfvk(extfvk: &ExtendedFullViewingKey) -> String { let addr = extfvk.default_address().unwrap().1; diff --git a/zcash_client_sqlite/src/query.rs b/zcash_client_sqlite/src/query.rs new file mode 100644 index 000000000..be585d92f --- /dev/null +++ b/zcash_client_sqlite/src/query.rs @@ -0,0 +1,28 @@ +//! Functions for querying information in the data database. + +use rusqlite::Connection; +use std::path::Path; + +use crate::error::Error; + +/// Returns the address for the account. +/// +/// # Examples +/// +/// ``` +/// use zcash_client_sqlite::query::get_address; +/// +/// let addr = get_address("/path/to/data.db", 0); +/// ``` +pub fn get_address>(db_data: P, account: u32) -> Result { + let data = Connection::open(db_data)?; + + let addr = data.query_row( + "SELECT address FROM accounts + WHERE account = ?", + &[account], + |row| row.get(0), + )?; + + Ok(addr) +}