diff --git a/js/pivx_shield.ts b/js/pivx_shield.ts index 5096ec3..4e5deb0 100644 --- a/js/pivx_shield.ts +++ b/js/pivx_shield.ts @@ -569,6 +569,21 @@ export class PIVXShield { } } + async proverIsLoaded() { + return await this.callWorker("prover_is_loaded"); + } + + async loadSaplingProverWithBytes( + sapling_output_bytes: Uint8Array, + sapling_spend_bytes: Uint8Array, + ) { + return await this.callWorker( + "load_prover_with_bytes", + sapling_output_bytes, + sapling_spend_bytes, + ); + } + /** * @returns The last block that has been decoded */ diff --git a/src/prover.rs b/src/prover.rs index f8711d2..f54c066 100644 --- a/src/prover.rs +++ b/src/prover.rs @@ -41,26 +41,53 @@ pub async fn get_with_url(url: &str) -> Result<&'static impl TxProver, Box Result> { + if sha256::digest(&*sapling_output_bytes) + != "2f0ebbcbb9bb0bcffe95a397e7eba89c29eb4dde6191c339db88570e3f3fb0e4" + { + Err("Sha256 does not match for sapling output")?; + } - if sha256::digest(&*sapling_output_bytes) - != "2f0ebbcbb9bb0bcffe95a397e7eba89c29eb4dde6191c339db88570e3f3fb0e4" - { - Err("Sha256 does not match for sapling output")?; - } - - if sha256::digest(&*sapling_spend_bytes) - != "8e48ffd23abb3a5fd9c5589204f32d9c31285a04b78096ba40a79b75677efc13" - { - Err("Sha256 does not match for sapling spend")?; - } - Ok(LocalTxProver::from_bytes( - &sapling_spend_bytes, - &sapling_output_bytes, - )) + if sha256::digest(&*sapling_spend_bytes) + != "8e48ffd23abb3a5fd9c5589204f32d9c31285a04b78096ba40a79b75677efc13" + { + Err("Sha256 does not match for sapling spend")?; + } + Ok(LocalTxProver::from_bytes( + &sapling_spend_bytes, + &sapling_output_bytes, + )) +} + +#[cfg(not(test))] +pub async fn init_with_bytes( + sapling_output_bytes: &[u8], + sapling_spend_bytes: &[u8], +) -> Result<&'static impl TxProver, Box> { + PROVER + .get_or_try_init(|| async { + check_and_create_prover(sapling_output_bytes, sapling_spend_bytes) }) .await } +#[cfg(test)] +pub async fn init_with_bytes( + _sapling_output_bytes: &[u8], + _sapling_spend_bytes: &[u8], +) -> Result<&'static impl TxProver, Box> { + Ok(PROVER.get_or_init(|| async { MockTxProver }).await) +} + #[cfg(test)] pub async fn get_with_url(_url: &str) -> Result<&'static impl TxProver, Box> { Ok(PROVER.get_or_init(|| async { MockTxProver }).await) @@ -76,3 +103,18 @@ pub async fn load_prover() -> bool { pub async fn load_prover_with_url(url: &str) -> bool { get_with_url(url).await.is_ok() } + +#[wasm_bindgen] +pub async fn load_prover_with_bytes( + sapling_output_bytes: &[u8], + sapling_spend_bytes: &[u8], +) -> bool { + init_with_bytes(sapling_output_bytes, sapling_spend_bytes) + .await + .is_ok() +} + +#[wasm_bindgen] +pub fn prover_is_loaded() -> bool { + PROVER.initialized() +}