Skip to content

Commit

Permalink
Merge pull request #83 from Duddino/getSaplingRoot
Browse files Browse the repository at this point in the history
Add getSaplingRoot and reloadFromCheckpoint
  • Loading branch information
Duddino authored Apr 8, 2024
2 parents 06d3897 + d85c8ad commit 2037df9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
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)))
}

0 comments on commit 2037df9

Please sign in to comment.