Skip to content

Commit d9c48b9

Browse files
committed
scan using the postboard ufvk
1 parent 1e90326 commit d9c48b9

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

src/wallet.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use zcash_client_backend::wallet::OvkPolicy;
3232
use zcash_client_backend::zip321::{Payment, TransactionRequest};
3333
use zcash_client_backend::ShieldedProtocol;
3434
use zcash_client_memory::MemoryWalletDb;
35-
use zcash_keys::keys::UnifiedSpendingKey;
35+
use zcash_keys::keys::{UnifiedFullViewingKey, UnifiedSpendingKey};
3636
use zcash_primitives::consensus::{self, BlockHeight, Network};
3737
use zcash_primitives::transaction::components::amount::NonNegativeAmount;
3838
use zcash_primitives::transaction::fees::zip317::FeeRule;
@@ -159,6 +159,44 @@ where
159159
Ok("0".to_string())
160160
}
161161

162+
pub async fn import_ufvk(
163+
&mut self,
164+
ufvk: UnifiedFullViewingKey,
165+
birthday_height: Option<u32>,
166+
) -> Result<String, Error> {
167+
let birthday = match birthday_height {
168+
Some(height) => height,
169+
None => {
170+
let chain_tip: u32 = self
171+
.client
172+
.get_latest_block(service::ChainSpec::default())
173+
.await?
174+
.into_inner()
175+
.height
176+
.try_into()
177+
.expect("block heights must fit into u32");
178+
chain_tip - 100
179+
}
180+
};
181+
// Construct an `AccountBirthday` for the account's birthday.
182+
let birthday = {
183+
// Fetch the tree state corresponding to the last block prior to the wallet's
184+
// birthday height. NOTE: THIS APPROACH LEAKS THE BIRTHDAY TO THE SERVER!
185+
let request = service::BlockId {
186+
height: (birthday - 1).into(),
187+
..Default::default()
188+
};
189+
let treestate = self.client.get_tree_state(request).await?.into_inner();
190+
AccountBirthday::from_treestate(treestate, None).map_err(|_| Error::BirthdayError)?
191+
};
192+
193+
let _account = self
194+
.db
195+
.import_account_ufvk(&ufvk, &birthday, AccountPurpose::Spending)?;
196+
197+
Ok("0".to_string())
198+
}
199+
162200
pub fn suggest_scan_ranges(&self) -> Result<Vec<BlockRange>, Error> {
163201
Ok(self.db.suggest_scan_ranges().map(|ranges| {
164202
ranges

tests/tests.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,80 @@ async fn test_get_and_scan_range_native() {
114114
let summary = w.get_wallet_summary().unwrap();
115115
tracing::info!("Wallet summary: {:?}", summary);
116116
}
117+
118+
// let s = zcash_keys::encoding::decode_extended_full_viewing_key(
119+
// constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
120+
// &self.ufvk.trim(),
121+
// )
122+
// .unwrap()
123+
// .to_diversifiable_full_viewing_key();
124+
// let ufvk = UnifiedFullViewingKey::new(None, Some(s), None).unwrap();
125+
126+
#[cfg(feature = "native")]
127+
#[tokio::test]
128+
async fn test_post_board() {
129+
let key_str = "zxviews1q0duytgcqqqqpqre26wkl45gvwwwd706xw608hucmvfalr759ejwf7qshjf5r9aa7323zulvz6plhttp5mltqcgs9t039cx2d09mgq05ts63n8u35hyv6h9nc9ctqqtue2u7cer2mqegunuulq2luhq3ywjcz35yyljewa4mgkgjzyfwh6fr6jd0dzd44ghk0nxdv2hnv4j5nxfwv24rwdmgllhe0p8568sgqt9ckt02v2kxf5ahtql6s0ltjpkckw8gtymxtxuu9gcr0swvz";
130+
131+
use zcash_keys::keys::UnifiedFullViewingKey;
132+
use zcash_primitives::{consensus, constants};
133+
let db_cache = tempfile::tempdir().unwrap();
134+
let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap();
135+
136+
initialize();
137+
let url = "https://zec.rocks:443";
138+
let c = tonic::transport::Channel::from_shared(url).unwrap();
139+
140+
let tls = tonic::transport::ClientTlsConfig::new()
141+
.domain_name("zec.rocks")
142+
.with_webpki_roots();
143+
let channel = c.tls_config(tls).unwrap();
144+
145+
#[cfg(feature = "sqlite-db")]
146+
let wallet_db = {
147+
use zcash_client_sqlite::{
148+
chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb,
149+
};
150+
151+
let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap();
152+
let mut wallet_db = WalletDb::for_path(&_db_data, consensus::Network::MainNetwork).unwrap();
153+
init_blockmeta_db(&mut db_cache).unwrap();
154+
init_wallet_db(&mut wallet_db, None).unwrap();
155+
wallet_db
156+
};
157+
158+
#[cfg(not(feature = "sqlite-db"))]
159+
let wallet_db = zcash_client_memory::MemoryWalletDb::new(
160+
consensus::Network::MainNetwork,
161+
webz_core::PRUNING_DEPTH,
162+
);
163+
164+
let mut w = Wallet::new(
165+
wallet_db,
166+
channel.connect().await.unwrap(),
167+
Network::MainNetwork,
168+
NonZeroU32::try_from(1).unwrap(),
169+
)
170+
.unwrap();
171+
172+
let s = zcash_keys::encoding::decode_extended_full_viewing_key(
173+
constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
174+
&key_str.trim(),
175+
)
176+
.unwrap();
177+
178+
let ufvk = UnifiedFullViewingKey::from_sapling_extended_full_viewing_key(s).unwrap();
179+
let id = w.import_ufvk(ufvk, Some(2477329)).await.unwrap();
180+
tracing::info!("Created account with id: {}", id);
181+
182+
tracing::info!("Syncing wallet");
183+
w.sync(|scanned_to, tip| {
184+
println!("Scanned: {}/{}", scanned_to, tip);
185+
})
186+
.await
187+
.unwrap();
188+
189+
tracing::info!("Syncing complete :)");
190+
191+
let summary = w.get_wallet_summary().unwrap();
192+
tracing::info!("Wallet summary: {:?}", summary);
193+
}

0 commit comments

Comments
 (0)