Skip to content

Commit 8da3639

Browse files
committed
message board sync on web
1 parent bb0e9df commit 8da3639

File tree

6 files changed

+83
-19
lines changed

6 files changed

+83
-19
lines changed

justfile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@ default:
44
build:
55
wasm-pack build -t web --release --out-dir ./packages/webz-core -Z --no-default-features --features="wasm" build-std="panic_abort,std"
66

7+
## Wasm Tests
78
test-web:
89
WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std"
10+
11+
test-message-board-web:
12+
WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std" --test message-board-sync
13+
14+
test-simple-web:
15+
WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std" --test simple-sync-and-send
916

10-
example-native:
17+
18+
## Native Examples
19+
example-simple:
1120
cargo run -r --example simple-sync
1221

13-
example-sqlite:
22+
example-simple-sqlite:
1423
cargo run -r --example simple-sync --features="sqlite-db"
1524

1625
example-message-board:

src/bindgen/wallet.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ pub struct WebWallet {
3737
inner: MemoryWallet<tonic_web_wasm_client::Client>,
3838
}
3939

40+
impl WebWallet {
41+
fn network_from_str (
42+
network: &str
43+
) -> Result<consensus::Network, Error> {
44+
match network {
45+
"main" => Ok(consensus::Network::MainNetwork),
46+
"test" => Ok(consensus::Network::TestNetwork),
47+
_ => {
48+
Err(Error::InvalidNetwork(network.to_string()))
49+
}
50+
}
51+
}
52+
}
53+
4054
#[wasm_bindgen]
4155
impl WebWallet {
4256
/// Create a new instance of a Zcash wallet for a given network
@@ -46,13 +60,7 @@ impl WebWallet {
4660
lightwalletd_url: &str,
4761
min_confirmations: u32,
4862
) -> Result<WebWallet, Error> {
49-
let network = match network {
50-
"main" => consensus::Network::MainNetwork,
51-
"test" => consensus::Network::TestNetwork,
52-
_ => {
53-
return Err(Error::InvalidNetwork(network.to_string()));
54-
}
55-
};
63+
let network = Self::network_from_str(network)?;
5664
let min_confirmations = NonZeroU32::try_from(min_confirmations)
5765
.map_err(|_| Error::InvalidMinConformations(min_confirmations))?;
5866
let client = Client::new(lightwalletd_url.to_string());
@@ -67,6 +75,7 @@ impl WebWallet {
6775
})
6876
}
6977

78+
7079
/// Add a new account to the wallet
7180
///
7281
/// # Arguments
@@ -90,14 +99,10 @@ impl WebWallet {
9099
key: &str,
91100
birthday_height: Option<u32>,
92101
) -> Result<String, Error> {
93-
let s = zcash_keys::encoding::decode_extended_full_viewing_key(
94-
MAIN_NETWORK.hrp_sapling_extended_full_viewing_key(),
95-
&key.trim(),
96-
)
97-
.unwrap();
98-
let ufvk = UnifiedFullViewingKey::from_sapling_extended_full_viewing_key(s).unwrap();
99-
100-
self.inner.import_ufvk(ufvk, birthday_height).await
102+
103+
let ufvk = UnifiedFullViewingKey::decode(&self.inner.network, key).map_err(Error::KeyParseError)?;
104+
105+
self.inner.import_ufvk(&ufvk, birthday_height).await
101106
}
102107

103108
pub fn suggest_scan_ranges(&self) -> Result<Vec<BlockRange>, Error> {

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub enum Error {
4444
InvalidAmount(#[from] zcash_primitives::transaction::components::amount::BalanceError),
4545
#[error("Failed to send transaction")]
4646
SendFailed { code: i32, reason: String },
47+
#[error("Failed to parse key: {0}")]
48+
KeyParseError(String),
4749

4850
#[cfg(feature = "sqlite-db")]
4951
#[error("Sqlite error: {0}")]

src/wallet.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ where
158158

159159
pub async fn import_ufvk(
160160
&mut self,
161-
ufvk: UnifiedFullViewingKey,
161+
ufvk: &UnifiedFullViewingKey,
162162
birthday_height: Option<u32>,
163163
) -> Result<String, Error> {
164164
let birthday = match birthday_height {
@@ -189,7 +189,7 @@ where
189189

190190
let _account = self
191191
.db
192-
.import_account_ufvk(&ufvk, &birthday, AccountPurpose::ViewOnly)?;
192+
.import_account_ufvk(ufvk, &birthday, AccountPurpose::ViewOnly)?;
193193

194194
Ok("0".to_string())
195195
}

tests/message-board-sync.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use wasm_bindgen_test::*;
2+
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
3+
4+
use std::sync::Once;
5+
use zcash_keys::keys::UnifiedFullViewingKey;
6+
use zcash_primitives::consensus::Network;
7+
use zcash_primitives::constants;
8+
use webz_core::bindgen::wallet::WebWallet;
9+
10+
// Required to initialize the logger and panic hooks only once
11+
static INIT: Once = Once::new();
12+
pub fn initialize() {
13+
INIT.call_once(|| {
14+
webz_core::init::start();
15+
});
16+
}
17+
const SAPLING_EFVK: &'static str = "zxviews1q0duytgcqqqqpqre26wkl45gvwwwd706xw608hucmvfalr759ejwf7qshjf5r9aa7323zulvz6plhttp5mltqcgs9t039cx2d09mgq05ts63n8u35hyv6h9nc9ctqqtue2u7cer2mqegunuulq2luhq3ywjcz35yyljewa4mgkgjzyfwh6fr6jd0dzd44ghk0nxdv2hnv4j5nxfwv24rwdmgllhe0p8568sgqt9ckt02v2kxf5ahtql6s0ltjpkckw8gtymxtxuu9gcr0swvz";
18+
19+
#[wasm_bindgen_test]
20+
async fn test_message_board() {
21+
initialize();
22+
23+
let mut w = WebWallet::new("main", "https://zcash-mainnet.chainsafe.dev", 1).unwrap();
24+
25+
let s = zcash_keys::encoding::decode_extended_full_viewing_key(
26+
constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
27+
&SAPLING_EFVK.trim(),
28+
)
29+
.unwrap();
30+
31+
let ufvk = UnifiedFullViewingKey::from_sapling_extended_full_viewing_key(s).unwrap();
32+
let ufvk_str = ufvk.encode(&Network::MainNetwork);
33+
let id = w.import_ufvk(&ufvk_str, Some(2477329)).await.unwrap();
34+
tracing::info!("Created account with id: {}", id);
35+
36+
tracing::info!("Syncing wallet");
37+
w.sync(&js_sys::Function::new_with_args(
38+
"scanned_to, tip",
39+
"console.log('Scanned: ', scanned_to, '/', tip)",
40+
))
41+
.await
42+
.unwrap();
43+
tracing::info!("Syncing complete :)");
44+
45+
let summary = w.get_wallet_summary().unwrap();
46+
tracing::info!("Wallet summary: {:?}", summary);
47+
}

tests/tests.rs renamed to tests/simple-sync-and-send.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ async fn test_get_and_scan_range() {
4444
let summary = w.get_wallet_summary().unwrap();
4545
tracing::info!("Wallet summary: {:?}", summary);
4646
}
47+

0 commit comments

Comments
 (0)