Skip to content

Commit bb0e9df

Browse files
committed
message board sync to own file
2 parents e062465 + dae657e commit bb0e9df

File tree

6 files changed

+181
-169
lines changed

6 files changed

+181
-169
lines changed

Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ edition = "2021"
1010
[lib]
1111
crate-type = ["cdylib", "rlib"]
1212

13+
[[example]]
14+
name = "simple-sync"
15+
path = "examples/simple-sync.rs"
16+
17+
[[example]]
18+
name = "message-board-sync"
19+
path = "examples/message-board-sync.rs"
20+
1321
[profile.release]
1422
# Tell `rustc` to optimize for small code size.
1523
opt-level = 3
@@ -54,7 +62,7 @@ tonic = { version = "0.12", default-features = false, features = [
5462
] }
5563

5664
# Used in Native tests
57-
tokio = { version = "1.0", features = ["rt", "macros"], optional = true }
65+
tokio = { version = "1.0", features = ["rt", "macros", "rt-multi-thread"], optional = true }
5866
zcash_client_sqlite = { git = "https://github.com/ChainSafe/librustzcash", rev = "a77e8a0204dab421fdbf5822e585716339567b96", default-features = false, features = ["unstable", "orchard"], optional = true }
5967

6068
getrandom = { version = "0.2", features = ["js"] }

examples/message-board-sync.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use std::sync::Once;
2+
3+
use std::num::NonZeroU32;
4+
use webz_core::Wallet;
5+
use zcash_primitives::consensus::Network;
6+
static INIT: Once = Once::new();
7+
const SAPLING_EFVK: &'static str = "zxviews1q0duytgcqqqqpqre26wkl45gvwwwd706xw608hucmvfalr759ejwf7qshjf5r9aa7323zulvz6plhttp5mltqcgs9t039cx2d09mgq05ts63n8u35hyv6h9nc9ctqqtue2u7cer2mqegunuulq2luhq3ywjcz35yyljewa4mgkgjzyfwh6fr6jd0dzd44ghk0nxdv2hnv4j5nxfwv24rwdmgllhe0p8568sgqt9ckt02v2kxf5ahtql6s0ltjpkckw8gtymxtxuu9gcr0swvz";
8+
pub fn initialize() {
9+
INIT.call_once(|| {
10+
webz_core::init::start();
11+
});
12+
}
13+
#[tokio::main]
14+
async fn main() {
15+
use zcash_keys::keys::UnifiedFullViewingKey;
16+
use zcash_primitives::{consensus, constants};
17+
let db_cache = tempfile::tempdir().unwrap();
18+
let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap();
19+
20+
initialize();
21+
let url = "https://zec.rocks:443";
22+
let c = tonic::transport::Channel::from_shared(url).unwrap();
23+
24+
let tls = tonic::transport::ClientTlsConfig::new()
25+
.domain_name("zec.rocks")
26+
.with_webpki_roots();
27+
let channel = c.tls_config(tls).unwrap();
28+
29+
#[cfg(feature = "sqlite-db")]
30+
let wallet_db = {
31+
use zcash_client_sqlite::{
32+
chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb,
33+
};
34+
35+
let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap();
36+
let mut wallet_db = WalletDb::for_path(&_db_data, consensus::Network::MainNetwork).unwrap();
37+
init_blockmeta_db(&mut db_cache).unwrap();
38+
init_wallet_db(&mut wallet_db, None).unwrap();
39+
wallet_db
40+
};
41+
42+
#[cfg(not(feature = "sqlite-db"))]
43+
let wallet_db = zcash_client_memory::MemoryWalletDb::new(
44+
consensus::Network::MainNetwork,
45+
webz_core::PRUNING_DEPTH,
46+
);
47+
48+
let mut w = Wallet::new(
49+
wallet_db,
50+
channel.connect().await.unwrap(),
51+
Network::MainNetwork,
52+
NonZeroU32::try_from(1).unwrap(),
53+
)
54+
.unwrap();
55+
56+
let s = zcash_keys::encoding::decode_extended_full_viewing_key(
57+
constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
58+
&SAPLING_EFVK.trim(),
59+
)
60+
.unwrap();
61+
62+
let ufvk = UnifiedFullViewingKey::from_sapling_extended_full_viewing_key(s).unwrap();
63+
let id = w.import_ufvk(ufvk, Some(2477329)).await.unwrap();
64+
tracing::info!("Created account with id: {}", id);
65+
66+
tracing::info!("Syncing wallet");
67+
w.sync(|scanned_to, tip| {
68+
println!("Scanned: {}/{}", scanned_to, tip);
69+
})
70+
.await
71+
.unwrap();
72+
73+
tracing::info!("Syncing complete :)");
74+
75+
let summary = w.get_wallet_summary().unwrap();
76+
tracing::info!("Wallet summary: {:?}", summary);
77+
}

examples/simple-sync.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use std::sync::Once;
2+
3+
use std::num::NonZeroU32;
4+
use webz_core::Wallet;
5+
use zcash_address::ZcashAddress;
6+
use zcash_primitives::consensus::Network;
7+
8+
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";
9+
const HD_INDEX: u32 = 0;
10+
const BIRTHDAY: Option<u32> = Some(2577329);
11+
12+
static INIT: Once = Once::new();
13+
pub fn initialize() {
14+
INIT.call_once(|| {
15+
webz_core::init::start();
16+
});
17+
}
18+
19+
#[cfg(feature = "native")]
20+
#[tokio::main]
21+
async fn main() {
22+
let db_cache = tempfile::tempdir().unwrap();
23+
let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap();
24+
25+
initialize();
26+
let url = "https://testnet.zec.rocks:443";
27+
let c = tonic::transport::Channel::from_shared(url).unwrap();
28+
29+
let tls = tonic::transport::ClientTlsConfig::new()
30+
.domain_name("testnet.zec.rocks")
31+
.with_webpki_roots();
32+
let channel = c.tls_config(tls).unwrap();
33+
34+
#[cfg(feature = "sqlite-db")]
35+
let wallet_db = {
36+
use zcash_client_sqlite::{
37+
chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb,
38+
};
39+
40+
let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap();
41+
let mut wallet_db = WalletDb::for_path(&_db_data, Network::TestNetwork).unwrap();
42+
init_blockmeta_db(&mut db_cache).unwrap();
43+
init_wallet_db(&mut wallet_db, None).unwrap();
44+
wallet_db
45+
};
46+
47+
#[cfg(not(feature = "sqlite-db"))]
48+
let wallet_db =
49+
zcash_client_memory::MemoryWalletDb::new(Network::TestNetwork, webz_core::PRUNING_DEPTH);
50+
51+
let mut w = Wallet::new(
52+
wallet_db,
53+
channel.connect().await.unwrap(),
54+
Network::TestNetwork,
55+
NonZeroU32::try_from(1).unwrap(),
56+
)
57+
.unwrap();
58+
59+
let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap();
60+
tracing::info!("Created account with id: {}", id);
61+
62+
tracing::info!("Syncing wallet");
63+
w.sync(|scanned_to, tip| {
64+
println!("Scanned: {}/{}", scanned_to, tip);
65+
})
66+
.await
67+
.unwrap();
68+
69+
tracing::info!("Syncing complete :)");
70+
71+
let summary = w.get_wallet_summary().unwrap();
72+
tracing::info!("Wallet summary: {:?}", summary);
73+
74+
tracing::info!("Proposing a transaction");
75+
let addr = ZcashAddress::try_from_encoded("utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu");
76+
77+
w.transfer(SEED, 0, addr.unwrap(), 1000).await.unwrap();
78+
tracing::info!("Transaction proposed");
79+
80+
let summary = w.get_wallet_summary().unwrap();
81+
tracing::info!("Wallet summary: {:?}", summary);
82+
}

justfile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ build:
77
test-web:
88
WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std"
99

10-
test-native:
11-
cargo test -r -- --nocapture
10+
example-native:
11+
cargo run -r --example simple-sync
1212

13-
test-sqlite:
14-
cargo test -r --features="sqlite-db" -- --nocapture
13+
example-sqlite:
14+
cargo run -r --example simple-sync --features="sqlite-db"
15+
16+
example-message-board:
17+
cargo run -r --example message-board-sync
18+
19+
example-message-board-sqlite:
20+
cargo run -r --example message-board-sync --features="sqlite-db"
1521

1622
check:
1723
cargo check

src/wallet.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ use zcash_client_backend::proposal::Proposal;
4343

4444
const BATCH_SIZE: u32 = 10000;
4545

46-
// type Proposal =
47-
// zcash_client_backend::proposal::Proposal<FeeRule, zcash_client_backend::wallet::NoteId>;
48-
4946
/// # A Zcash wallet
5047
///
5148
/// A wallet is a set of accounts that can be synchronized together with the blockchain.

0 commit comments

Comments
 (0)