Skip to content
Clone

feat(pow-solver): add wasm-bindgen entry for browser solving

ZhenYi committed
parent 46529b4 commit b8bc776 Browse files

5 files changed

+33 −2

File tree 5
Modified lib/pow-solver/src/lib.rs View lib/pow-solver/src/lib.rs
@@ -1,3 +1,6 @@
11pub mod challenge;
22
3pub use challenge::{check, solve, Challenge};
No newline at end of file
3pub use challenge::{check, solve, Challenge};
4
5#[cfg(target_arch = "wasm32")]
6pub mod wasm;
No newline at end of file
Added lib/pow-solver/src/wasm.rs View lib/pow-solver/src/wasm.rs
@@ -0,0 +1,21 @@
1#![allow(unused_variables)]
2
3use wasm_bindgen::prelude::*;
4
5use crate::challenge::{check as check_native, solve as solve_native};
6
7#[wasm_bindgen(js_name = solve, catch)]
8pub fn solve(nonce: &str, difficulty: u32, max_attempts: u32) -> Result<String, JsError> {
9 solve_native(nonce, difficulty, max_attempts as u64)
10 .map(|n| n.to_string())
11 .ok_or_else(|| JsError::new("no solution within max_attempts"))
12}
13
14#[wasm_bindgen(js_name = check, catch)]
15pub fn check(nonce: &str, difficulty: u32, answer: &str) -> Result<bool, JsError> {
16 let parsed: u64 = answer
17 .trim()
18 .parse()
19 .map_err(|e| JsError::new(&format!("invalid answer: {e}")))?;
20 Ok(check_native(nonce, difficulty, parsed))
21}
No newline at end of file
Modified lib/pow-solver/Cargo.toml View lib/pow-solver/Cargo.toml
@@ -3,6 +3,10 @@
33version.workspace = true
44edition.workspace = true
55
6[lib]
7crate-type = ["rlib", "cdylib"]
8
69[dependencies]
710sha2 = { workspace = true }
8serde = { workspace = true, features = ["derive"] }
No newline at end of file
11serde = { workspace = true, features = ["derive"] }
12wasm-bindgen = { workspace = true }
No newline at end of file
Modified Cargo.lock View Cargo.lock
@@ -5881,6 +5881,7 @@
58815881dependencies = [
58825882 "serde",
58835883 "sha2 0.11.0",
5884 "wasm-bindgen",
58845885]
58855886
58865887[[package]]
Modified Cargo.toml View Cargo.toml
@@ -85,6 +85,8 @@
8585gitbaby = { git="https://gitea.gitdata.me/GitDataAi/GitBaby.git", rev = "41154f520b" }
8686mailbaby = { git = "https://github.com/mailbabys/mailbaby-rs", rev = "e408acf48a19e4fd554b020a865d144706f7c2de", features = ["rest", "grpc"] }
8787
88wasm-bindgen = "0.2"
89
8890
8991chrono = "0.4.45"
9092maud = { version = "0.27", features = ["axum"] }