Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes our Wallet generic over the backing wallet db #19

Merged
merged 5 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]

[[example]]
name = "simple-sync"
path = "examples/simple-sync.rs"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = 3
Expand All @@ -24,7 +28,7 @@ default = ["native"]

wasm = ["console_error_panic_hook", "dep:tracing-web"]
native = ["dep:tokio", "tonic/channel", "tonic/gzip", "tonic/tls-webpki-roots"]

sqlite-db = ["dep:zcash_client_sqlite"]
console_error_panic_hook = ["dep:console_error_panic_hook"]

[dependencies]
Expand Down Expand Up @@ -54,7 +58,8 @@ 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"] }
thiserror = "1.0.63"
Expand All @@ -68,10 +73,11 @@ nonempty = "0.7"
hex = "0.4.3"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing = "0.1.40"

subtle = "2.6.1"

[dev-dependencies]
wasm-bindgen-test = "0.3.42"
tempfile = "3.12"

[patch.crates-io]
zip32 = { git = "https://github.com/zcash/zip32.git", branch = "diversifier_index_ord"}
82 changes: 82 additions & 0 deletions examples/simple-sync.rs
Original file line number Diff line number Diff line change
@@ -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<u32> = 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);
}
Loading
Loading