zcash_client_sqlite::query::get_address()

This commit is contained in:
Jack Grigg 2019-03-09 02:23:31 +00:00
parent c0cf55c127
commit c8b70e569c
3 changed files with 50 additions and 0 deletions

View File

@ -243,12 +243,16 @@ pub fn init_blocks_table<P: AsRef<Path>>(
#[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);
}
}

View File

@ -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;

View File

@ -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<P: AsRef<Path>>(db_data: P, account: u32) -> Result<String, Error> {
let data = Connection::open(db_data)?;
let addr = data.query_row(
"SELECT address FROM accounts
WHERE account = ?",
&[account],
|row| row.get(0),
)?;
Ok(addr)
}