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

Allow external sapling prover download #97

Merged
merged 1 commit into from
Feb 12, 2025
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
15 changes: 15 additions & 0 deletions js/pivx_shield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,21 @@ export class PIVXShield {
}
}

async proverIsLoaded() {
return await this.callWorker<boolean>("prover_is_loaded");
}

async loadSaplingProverWithBytes(
sapling_output_bytes: Uint8Array,
sapling_spend_bytes: Uint8Array,
) {
return await this.callWorker<boolean>(
"load_prover_with_bytes",
sapling_output_bytes,
sapling_spend_bytes,
);
}

/**
* @returns The last block that has been decoded
*/
Expand Down
72 changes: 57 additions & 15 deletions src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,53 @@ pub async fn get_with_url(url: &str) -> Result<&'static impl TxProver, Box<dyn E
let spend_url = format!("{}/sapling-spend.params", url);
let sapling_output_bytes = c.get(&out_url).send().await?.bytes().await?;
let sapling_spend_bytes = c.get(&spend_url).send().await?.bytes().await?;
check_and_create_prover(&sapling_output_bytes, &sapling_spend_bytes)
})
.await
}

#[cfg(not(test))]
fn check_and_create_prover(
sapling_output_bytes: &[u8],
sapling_spend_bytes: &[u8],
) -> Result<LocalTxProver, Box<dyn Error>> {
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<dyn Error>> {
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<dyn Error>> {
Ok(PROVER.get_or_init(|| async { MockTxProver }).await)
}

#[cfg(test)]
pub async fn get_with_url(_url: &str) -> Result<&'static impl TxProver, Box<dyn Error>> {
Ok(PROVER.get_or_init(|| async { MockTxProver }).await)
Expand All @@ -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()
}
Loading