diff --git a/Cargo.toml b/Cargo.toml index 7bdd97c..f9065ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,14 @@ edition = "2021" [lib] crate-type = ["cdylib", "rlib"] +[[example]] +name = "simple-sync" +path = "examples/simple-sync.rs" + +[[example]] +name = "message-board-sync" +path = "examples/message-board-sync.rs" + [profile.release] # Tell `rustc` to optimize for small code size. opt-level = 3 @@ -54,7 +62,7 @@ tonic = { version = "0.12", default-features = false, features = [ ] } # Used in Native tests -tokio = { version = "1.0", features = ["rt", "macros"], optional = true } +tokio = { version = "1.0", features = ["rt", "macros", "rt-multi-thread"], optional = true } zcash_client_sqlite = { git = "https://github.com/ChainSafe/librustzcash", rev = "a77e8a0204dab421fdbf5822e585716339567b96", default-features = false, features = ["unstable", "orchard"], optional = true } getrandom = { version = "0.2", features = ["js"] } diff --git a/examples/message-board-sync.rs b/examples/message-board-sync.rs new file mode 100644 index 0000000..864ddee --- /dev/null +++ b/examples/message-board-sync.rs @@ -0,0 +1,77 @@ +use std::sync::Once; + +use std::num::NonZeroU32; +use webz_core::Wallet; +use zcash_primitives::consensus::Network; +static INIT: Once = Once::new(); +const SAPLING_EFVK: &'static str = "zxviews1q0duytgcqqqqpqre26wkl45gvwwwd706xw608hucmvfalr759ejwf7qshjf5r9aa7323zulvz6plhttp5mltqcgs9t039cx2d09mgq05ts63n8u35hyv6h9nc9ctqqtue2u7cer2mqegunuulq2luhq3ywjcz35yyljewa4mgkgjzyfwh6fr6jd0dzd44ghk0nxdv2hnv4j5nxfwv24rwdmgllhe0p8568sgqt9ckt02v2kxf5ahtql6s0ltjpkckw8gtymxtxuu9gcr0swvz"; +pub fn initialize() { + INIT.call_once(|| { + webz_core::init::start(); + }); +} +#[tokio::main] +async fn main() { + use zcash_keys::keys::UnifiedFullViewingKey; + use zcash_primitives::{consensus, constants}; + let db_cache = tempfile::tempdir().unwrap(); + let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap(); + + initialize(); + let url = "https://zec.rocks:443"; + let c = tonic::transport::Channel::from_shared(url).unwrap(); + + let tls = tonic::transport::ClientTlsConfig::new() + .domain_name("zec.rocks") + .with_webpki_roots(); + let channel = c.tls_config(tls).unwrap(); + + #[cfg(feature = "sqlite-db")] + let wallet_db = { + use zcash_client_sqlite::{ + chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb, + }; + + let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap(); + let mut wallet_db = WalletDb::for_path(&_db_data, consensus::Network::MainNetwork).unwrap(); + init_blockmeta_db(&mut db_cache).unwrap(); + init_wallet_db(&mut wallet_db, None).unwrap(); + wallet_db + }; + + #[cfg(not(feature = "sqlite-db"))] + let wallet_db = zcash_client_memory::MemoryWalletDb::new( + consensus::Network::MainNetwork, + webz_core::PRUNING_DEPTH, + ); + + let mut w = Wallet::new( + wallet_db, + channel.connect().await.unwrap(), + Network::MainNetwork, + NonZeroU32::try_from(1).unwrap(), + ) + .unwrap(); + + let s = zcash_keys::encoding::decode_extended_full_viewing_key( + constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, + &SAPLING_EFVK.trim(), + ) + .unwrap(); + + let ufvk = UnifiedFullViewingKey::from_sapling_extended_full_viewing_key(s).unwrap(); + let id = w.import_ufvk(ufvk, Some(2477329)).await.unwrap(); + tracing::info!("Created account with id: {}", id); + + tracing::info!("Syncing wallet"); + w.sync(|scanned_to, tip| { + println!("Scanned: {}/{}", scanned_to, tip); + }) + .await + .unwrap(); + + tracing::info!("Syncing complete :)"); + + let summary = w.get_wallet_summary().unwrap(); + tracing::info!("Wallet summary: {:?}", summary); +} \ No newline at end of file diff --git a/examples/simple-sync.rs b/examples/simple-sync.rs new file mode 100644 index 0000000..fb7dcf1 --- /dev/null +++ b/examples/simple-sync.rs @@ -0,0 +1,82 @@ +use std::sync::Once; + +use std::num::NonZeroU32; +use webz_core::Wallet; +use zcash_address::ZcashAddress; +use zcash_primitives::consensus::Network; + +const SEED: &str = "visit armed kite pen cradle toward reward clay marble oil write dove blind oyster silk oyster original message skate bench tone enable stadium element"; +const HD_INDEX: u32 = 0; +const BIRTHDAY: Option = Some(2577329); + +static INIT: Once = Once::new(); +pub fn initialize() { + INIT.call_once(|| { + webz_core::init::start(); + }); +} + +#[cfg(feature = "native")] +#[tokio::main] +async fn main() { + let db_cache = tempfile::tempdir().unwrap(); + let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap(); + + initialize(); + let url = "https://testnet.zec.rocks:443"; + let c = tonic::transport::Channel::from_shared(url).unwrap(); + + let tls = tonic::transport::ClientTlsConfig::new() + .domain_name("testnet.zec.rocks") + .with_webpki_roots(); + let channel = c.tls_config(tls).unwrap(); + + #[cfg(feature = "sqlite-db")] + let wallet_db = { + use zcash_client_sqlite::{ + chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb, + }; + + let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap(); + let mut wallet_db = WalletDb::for_path(&_db_data, Network::TestNetwork).unwrap(); + init_blockmeta_db(&mut db_cache).unwrap(); + init_wallet_db(&mut wallet_db, None).unwrap(); + wallet_db + }; + + #[cfg(not(feature = "sqlite-db"))] + let wallet_db = + zcash_client_memory::MemoryWalletDb::new(Network::TestNetwork, webz_core::PRUNING_DEPTH); + + let mut w = Wallet::new( + wallet_db, + channel.connect().await.unwrap(), + Network::TestNetwork, + NonZeroU32::try_from(1).unwrap(), + ) + .unwrap(); + + let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap(); + tracing::info!("Created account with id: {}", id); + + tracing::info!("Syncing wallet"); + w.sync(|scanned_to, tip| { + println!("Scanned: {}/{}", scanned_to, tip); + }) + .await + .unwrap(); + + tracing::info!("Syncing complete :)"); + + let summary = w.get_wallet_summary().unwrap(); + tracing::info!("Wallet summary: {:?}", summary); + + tracing::info!("Proposing a transaction"); + let addr = ZcashAddress::try_from_encoded("utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu"); + + w.transfer(SEED, 0, addr.unwrap(), 1000).await.unwrap(); + tracing::info!("Transaction proposed"); + + let summary = w.get_wallet_summary().unwrap(); + tracing::info!("Wallet summary: {:?}", summary); +} diff --git a/justfile b/justfile index 7faa255..d9863e4 100644 --- a/justfile +++ b/justfile @@ -7,11 +7,17 @@ build: test-web: WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std" -test-native: - cargo test -r -- --nocapture +example-native: + cargo run -r --example simple-sync -test-sqlite: - cargo test -r --features="sqlite-db" -- --nocapture +example-sqlite: + cargo run -r --example simple-sync --features="sqlite-db" + +example-message-board: + cargo run -r --example message-board-sync + +example-message-board-sqlite: + cargo run -r --example message-board-sync --features="sqlite-db" check: cargo check diff --git a/src/wallet.rs b/src/wallet.rs index 488c4b2..4748d3f 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -43,9 +43,6 @@ use zcash_client_backend::proposal::Proposal; const BATCH_SIZE: u32 = 10000; -// type Proposal = -// zcash_client_backend::proposal::Proposal; - /// # A Zcash wallet /// /// A wallet is a set of accounts that can be synchronized together with the blockchain. diff --git a/tests/tests.rs b/tests/tests.rs index 0fd21a5..610cd67 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,27 +1,20 @@ use wasm_bindgen_test::*; wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); -use webz_core::{bindgen::wallet::WebWallet, Wallet}; -use zcash_address::ZcashAddress; -use zcash_keys::keys::UnifiedFullViewingKey; -use zcash_primitives::{ - consensus::{MainNetwork, Network}, - constants, -}; +use std::sync::Once; +use webz_core::bindgen::wallet::WebWallet; const SEED: &str = "visit armed kite pen cradle toward reward clay marble oil write dove blind oyster silk oyster original message skate bench tone enable stadium element"; const HD_INDEX: u32 = 0; const BIRTHDAY: Option = Some(2577329); // Required to initialize the logger and panic hooks only once -use std::{num::NonZeroU32, sync::Once}; static INIT: Once = Once::new(); pub fn initialize() { INIT.call_once(|| { webz_core::init::start(); }); } -const key_str: &'static str = "zxviews1q0duytgcqqqqpqre26wkl45gvwwwd706xw608hucmvfalr759ejwf7qshjf5r9aa7323zulvz6plhttp5mltqcgs9t039cx2d09mgq05ts63n8u35hyv6h9nc9ctqqtue2u7cer2mqegunuulq2luhq3ywjcz35yyljewa4mgkgjzyfwh6fr6jd0dzd44ghk0nxdv2hnv4j5nxfwv24rwdmgllhe0p8568sgqt9ckt02v2kxf5ahtql6s0ltjpkckw8gtymxtxuu9gcr0swvz"; #[wasm_bindgen_test] async fn test_get_and_scan_range() { @@ -29,15 +22,7 @@ async fn test_get_and_scan_range() { let mut w = WebWallet::new("test", "https://zcash-testnet.chainsafe.dev", 1).unwrap(); - // let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap(); - let s = zcash_keys::encoding::decode_extended_full_viewing_key( - constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, - &key_str.trim(), - ) - .unwrap(); - - let id = w.import_ufvk(&key_str, Some(2477329)).await.unwrap(); - + let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap(); tracing::info!("Created account with id: {}", id); tracing::info!("Syncing wallet"); @@ -59,146 +44,3 @@ async fn test_get_and_scan_range() { let summary = w.get_wallet_summary().unwrap(); tracing::info!("Wallet summary: {:?}", summary); } - -#[cfg(feature = "native")] -#[tokio::test] -async fn test_get_and_scan_range_native() { - use zcash_primitives::consensus; - let db_cache = tempfile::tempdir().unwrap(); - let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap(); - - initialize(); - let url = "https://testnet.zec.rocks:443"; - let c = tonic::transport::Channel::from_shared(url).unwrap(); - - let tls = tonic::transport::ClientTlsConfig::new() - .domain_name("testnet.zec.rocks") - .with_webpki_roots(); - let channel = c.tls_config(tls).unwrap(); - - #[cfg(feature = "sqlite-db")] - let wallet_db = { - use zcash_client_sqlite::{ - chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb, - }; - - let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap(); - let mut wallet_db = WalletDb::for_path(&_db_data, consensus::Network::TestNetwork).unwrap(); - init_blockmeta_db(&mut db_cache).unwrap(); - init_wallet_db(&mut wallet_db, None).unwrap(); - wallet_db - }; - - #[cfg(not(feature = "sqlite-db"))] - let wallet_db = zcash_client_memory::MemoryWalletDb::new( - consensus::Network::TestNetwork, - webz_core::PRUNING_DEPTH, - ); - - let mut w = Wallet::new( - wallet_db, - channel.connect().await.unwrap(), - Network::TestNetwork, - NonZeroU32::try_from(1).unwrap(), - ) - .unwrap(); - - let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap(); - tracing::info!("Created account with id: {}", id); - - tracing::info!("Syncing wallet"); - w.sync(|scanned_to, tip| { - println!("Scanned: {}/{}", scanned_to, tip); - }) - .await - .unwrap(); - - tracing::info!("Syncing complete :)"); - - let summary = w.get_wallet_summary().unwrap(); - tracing::info!("Wallet summary: {:?}", summary); - - tracing::info!("Proposing a transaction"); - let addr = ZcashAddress::try_from_encoded("utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu"); - - w.transfer(SEED, 0, addr.unwrap(), 1000).await.unwrap(); - tracing::info!("Transaction proposed"); - - let summary = w.get_wallet_summary().unwrap(); - tracing::info!("Wallet summary: {:?}", summary); -} - -// let s = zcash_keys::encoding::decode_extended_full_viewing_key( -// constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, -// &self.ufvk.trim(), -// ) -// .unwrap() -// .to_diversifiable_full_viewing_key(); -// let ufvk = UnifiedFullViewingKey::new(None, Some(s), None).unwrap(); - -#[cfg(feature = "native")] -#[tokio::test] -async fn test_post_board() { - use zcash_keys::keys::UnifiedFullViewingKey; - use zcash_primitives::{consensus, constants}; - let db_cache = tempfile::tempdir().unwrap(); - let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap(); - - initialize(); - let url = "https://zec.rocks:443"; - let c = tonic::transport::Channel::from_shared(url).unwrap(); - - let tls = tonic::transport::ClientTlsConfig::new() - .domain_name("zec.rocks") - .with_webpki_roots(); - let channel = c.tls_config(tls).unwrap(); - - #[cfg(feature = "sqlite-db")] - let wallet_db = { - use zcash_client_sqlite::{ - chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb, - }; - - let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap(); - let mut wallet_db = WalletDb::for_path(&_db_data, consensus::Network::MainNetwork).unwrap(); - init_blockmeta_db(&mut db_cache).unwrap(); - init_wallet_db(&mut wallet_db, None).unwrap(); - wallet_db - }; - - #[cfg(not(feature = "sqlite-db"))] - let wallet_db = zcash_client_memory::MemoryWalletDb::new( - consensus::Network::MainNetwork, - webz_core::PRUNING_DEPTH, - ); - - let mut w = Wallet::new( - wallet_db, - channel.connect().await.unwrap(), - Network::MainNetwork, - NonZeroU32::try_from(1).unwrap(), - ) - .unwrap(); - - let s = zcash_keys::encoding::decode_extended_full_viewing_key( - constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, - &key_str.trim(), - ) - .unwrap(); - - let ufvk = UnifiedFullViewingKey::from_sapling_extended_full_viewing_key(s).unwrap(); - let id = w.import_ufvk(ufvk, Some(2477329)).await.unwrap(); - tracing::info!("Created account with id: {}", id); - - tracing::info!("Syncing wallet"); - w.sync(|scanned_to, tip| { - println!("Scanned: {}/{}", scanned_to, tip); - }) - .await - .unwrap(); - - tracing::info!("Syncing complete :)"); - - let summary = w.get_wallet_summary().unwrap(); - tracing::info!("Wallet summary: {:?}", summary); -}