Skip to content

Commit

Permalink
Merge pull request #54 from ergoplatform/add-digest-method
Browse files Browse the repository at this point in the history
Add digest method
  • Loading branch information
ross-weir authored Jan 21, 2024
2 parents 32a3157 + 51fa2e9 commit 8872fef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-ducks-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ergoplatform/authenticated-avl-tree": patch
---

Add `BatchAVLProver.digest()` method
7 changes: 7 additions & 0 deletions packages/authenticated-avl-tree/src/batch_avl_prover.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::batch_node::AVLTree;
use crate::operation::{Operation, TsOperationType};
use derive_more::{From, Into};
use ergo_avltree_rust::authenticated_tree_ops::AuthenticatedTreeOps;
use ergo_avltree_rust::batch_avl_prover::BatchAVLProver as NativeBatchAVLProver;
use ergo_lib_utils::MapJsValueErrorResult;
use js_sys::Uint8Array;
Expand Down Expand Up @@ -41,4 +42,10 @@ impl BatchAVLProver {
pub fn generate_proof(&mut self) -> Box<[u8]> {
self.0.generate_proof().to_vec().into_boxed_slice()
}

pub fn digest(&self) -> JsValue {
self.0.digest().map_or(JsValue::UNDEFINED, |digest| {
Uint8Array::from(digest.to_vec().as_slice()).into()
})
}
}
10 changes: 10 additions & 0 deletions packages/authenticated-avl-tree/tests/batch-avl-prover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ function removeOp(key: string) {
}

describe("BatchAVLProver", () => {
it("should return the expected empty digest", () => {
const tree = new AVLTree(32);
const bv = new BatchAVLProver(tree, true);
const actualDigest = bytesToHex(bv.digest());

expect(actualDigest).toBe(
"4ec61f485b98eb87153f7c57db4f5ecd75556fddbc403b41acf8441fde8e160900"
);
});

it("should produce the same proofs as the native rust library", () => {
const insertList = [
insertOp(
Expand Down

0 comments on commit 8872fef

Please sign in to comment.