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

Add ability to use different URL than duddino.com for sapling params #94

Merged
merged 1 commit into from
Oct 28, 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
8 changes: 0 additions & 8 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ multicore = ["pivx_proofs/multicore", "wasm-bindgen-rayon"]
[dependencies]
tokio = { version = "1.40.0", default-features = false, features = ["sync", "macros"] }
rayon = "1.10.0"
lazy_static = "1.5.0"
async_once = "0.2.6"
sha256 = { version = "1.5.0", default-features = false }
getrandom = { version = "0.2.15", features = ["js"] }
reqwest = { version = "0.12.0", features = ["blocking"] }
Expand Down
8 changes: 6 additions & 2 deletions js/pivx_shield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,12 @@ export class PIVXShield {
* But will be done lazily if note called explicitally.
* @returns resolves when the sapling prover is loaded
*/
async loadSaplingProver() {
return await this.callWorker<void>("load_prover");
async loadSaplingProver(url?: string) {
if (url) {
return await this.callWorker<boolean>("load_prover_with_url", url);
} else {
return await this.callWorker<boolean>("load_prover");
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod checkpoint;
mod keys;
mod mainnet_checkpoints;
mod prover;
mod testnet_checkpoints;
mod transaction;
mod utils;
Expand Down
60 changes: 60 additions & 0 deletions src/prover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use pivx_proofs::prover::LocalTxProver;
use reqwest::Client;
use std::error::Error;
use tokio::sync::OnceCell;
use wasm_bindgen::prelude::*;

static PROVER: OnceCell<LocalTxProver> = OnceCell::const_new();

pub async fn get_prover() -> &'static LocalTxProver {
let default_urls = &["https://https://pivxla.bz", "https://duddino.com"];
for url in default_urls {
if let Ok(prover) = get_with_url(url).await {
return prover;
}
}
panic!("Failed to download prover");
}

/**
* gets prover using the specified url. If the prover has already been downloaded
* no request will be made
*/
pub async fn get_with_url(url: &str) -> Result<&'static LocalTxProver, Box<dyn Error>> {
PROVER
.get_or_try_init(|| async {
let c = Client::new();
let out_url = format!("{}/sapling-output.params", url);
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?;

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,
))
})
.await
}

#[wasm_bindgen]
pub async fn load_prover() -> bool {
get_prover().await;
true
}

#[wasm_bindgen]
pub async fn load_prover_with_url(url: &str) -> bool {
get_with_url(url).await.is_ok()
}
59 changes: 6 additions & 53 deletions src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub use crate::keys::decode_extended_full_viewing_key;
pub use crate::keys::decode_extsk;
use crate::prover::get_prover;
pub use pivx_client_backend::decrypt_transaction;
pub use pivx_client_backend::keys::UnifiedFullViewingKey;
use pivx_primitives::consensus::Network;
Expand All @@ -13,11 +14,9 @@ pub use pivx_primitives::transaction::builder::Progress;

use crate::keys::decode_generic_address;
use crate::keys::GenericAddress;
use async_once::AsyncOnce;
#[cfg(feature = "multicore")]
use atomic_float::AtomicF32;
pub use either::Either;
use lazy_static::lazy_static;
pub use pivx_primitives::sapling::{note::Note, Node, Nullifier};
pub use pivx_primitives::transaction::builder::Builder;
pub use pivx_primitives::transaction::components::Amount;
Expand All @@ -30,7 +29,7 @@ pub use pivx_primitives::zip32::ExtendedSpendingKey;
pub use pivx_primitives::zip32::Scope;
pub use pivx_proofs::prover::LocalTxProver;
use rand_core::OsRng;
pub use reqwest::Client;

use secp256k1::SecretKey;
pub use serde::{Deserialize, Serialize};
use std::convert::TryInto;
Expand All @@ -43,13 +42,6 @@ use tokio::{join, sync::mpsc::Receiver, sync::mpsc::Sender};
pub use wasm_bindgen::prelude::*;
mod test;

lazy_static! {
static ref PROVER: AsyncOnce<LocalTxProver> = AsyncOnce::new(async {
let (sapling_spend_bytes, sapling_output_bytes): (Vec<u8>, Vec<u8>) =
fetch_params().await.expect("Cannot fetch params");
LocalTxProver::from_bytes(&sapling_spend_bytes, &sapling_output_bytes)
});
}
#[cfg(feature = "multicore")]
static TX_PROGRESS_LOCK: AtomicF32 = AtomicF32::new(0.0);

Expand All @@ -72,35 +64,7 @@ fn fee_calculator(
+ transparent_output_count * transparent_output_size
+ tx_offset_size)
}
async fn fetch_params() -> Result<(Vec<u8>, Vec<u8>), Box<dyn Error>> {
let c = Client::new();
let sapling_output_bytes = c
.get("https://duddino.com/sapling-output.params")
.send()
.await?
.bytes()
.await?;
let sapling_spend_bytes = c
.get("https://duddino.com/sapling-spend.params")
.send()
.await?
.bytes()
.await?;

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((sapling_spend_bytes.to_vec(), sapling_output_bytes.to_vec()))
}
#[wasm_bindgen]
#[cfg(feature = "multicore")]
pub fn read_tx_progress() -> f32 {
Expand All @@ -117,11 +81,6 @@ pub fn read_tx_progress() -> f32 {
pub fn set_tx_status(val: f32) {
TX_PROGRESS_LOCK.store(val, Ordering::Relaxed);
}
#[wasm_bindgen]
pub async fn load_prover() -> bool {
PROVER.get().await;
true
}

#[derive(Serialize, Deserialize)]
pub struct JSTxSaplingData {
Expand Down Expand Up @@ -158,15 +117,9 @@ pub fn handle_transaction(
})
.collect::<Vec<_>>();
let mut new_comp_note: Vec<(Note, IncrementalWitness<Node>)> = vec![];
let nullifiers = handle_transaction_internal(
&mut tree,
tx,
key,
true,
&mut comp_note,
&mut new_comp_note,
)
.map_err(|_| "Cannot decode tx")?;
let nullifiers =
handle_transaction_internal(&mut tree, tx, key, true, &mut comp_note, &mut new_comp_note)
.map_err(|_| "Cannot decode tx")?;
let ser_comp_note: Vec<(Note, String)> =
serialize_comp_note(comp_note).map_err(|_| "Cannot serialize notes")?;
let ser_new_comp_note: Vec<(Note, String)> =
Expand Down Expand Up @@ -449,7 +402,7 @@ pub async fn create_transaction_internal(
}
}

let prover = PROVER.get().await;
let prover = get_prover().await;
#[cfg(feature = "multicore")]
{
let (transmitter, mut receiver): (Sender<Progress>, Receiver<Progress>) =
Expand Down
Loading