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 digest method #54

Merged
merged 2 commits into from
Jan 21, 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
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
Loading