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 getSaplingRoot and reloadFromCheckpoint #83

Merged
merged 1 commit into from
Apr 8, 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
24 changes: 24 additions & 0 deletions js/pivx_shield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,30 @@ export class PIVXShield {
getLastSyncedBlock() {
return this.lastProcessedBlock;
}

/**
* @returns sapling root
*/
async getSaplingRoot(): Promise<string> {
return await this.callWorker<string>(
"get_sapling_root",
this.commitmentTree,
);
}

/**
* Reloads from checkpoint. Needs to be resynced to use
*/
async reloadFromCheckpoint(checkpointBlock: number): Promise<void> {
const [effectiveHeight, commitmentTree] = await this.callWorker<
[number, string]
>("get_closest_checkpoint", checkpointBlock, this.isTestnet);
this.commitmentTree = commitmentTree;
this.lastProcessedBlock = effectiveHeight;
this.unspentNotes = [];
this.pendingSpentNotes = new Map();
this.pendingUnspentNotes = new Map();
}
}

export interface UTXO {
Expand Down
21 changes: 21 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
use std::io::Cursor;

use pivx_primitives::{
merkle_tree::{CommitmentTree, HashSer},
sapling::Node,
};
pub use wasm_bindgen::prelude::*;

pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
Expand All @@ -8,3 +16,16 @@ pub fn set_panic_hook() {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

#[wasm_bindgen]
pub fn get_sapling_root(tree_hex: &str) -> Result<JsValue, JsValue> {
let buff = Cursor::new(
hex::decode(tree_hex).map_err(|_| "Cannot decode commitment tree from hexadecimal")?,
);
let tree = CommitmentTree::<Node>::read(buff).map_err(|_| "Cannot decode commitment tree!")?;
let mut root = Vec::new();
tree.root()
.write(&mut root)
.map_err(|_| "Cannot write sapling root")?;
Ok(JsValue::from_str(&hex::encode(root)))
}
Loading