Skip to content

Commit

Permalink
add: merkletreejs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olivmath committed Dec 24, 2023
1 parent 320d431 commit 109bb8a
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.hypothesis
.vscode
*cache*
Expand Down
9 changes: 9 additions & 0 deletions test/merkletreejs/merkle_proof/merkle_proof_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { MerkleTree } = require('merkletreejs');
const SHA256 = require('crypto-js/sha256');

const leaves = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'].map(x => SHA256(x));
const tree = new MerkleTree(leaves, SHA256);
const leaf = SHA256('a');
const proof = tree.getProof(leaf).map(node => ({ data: node.data.toString('hex'), position: node.position }));

console.log(JSON.stringify({ proof, isValid: tree.verify(proof, leaf, tree.getRoot()) }))
19 changes: 19 additions & 0 deletions test/merkletreejs/merkle_proof/merkle_proof_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from merkly.mtree import MerkleTree
import hashlib
import json


def sha256(x, y):
data = x + y
return hashlib.sha256(data).digest()


leaves = ["a", "b", "c", "d", "e", "f", "g", "h"]
tree = MerkleTree(leaves, sha256)
leaf = "a"
proof = tree.proof(leaf)
formatted_proof = [
{"data": node.data.hex(), "position": node.side.name.lower()} for node in proof
]

print(json.dumps({"proof": formatted_proof, "isValid": tree.verify(proof, leaf)}))
8 changes: 8 additions & 0 deletions test/merkletreejs/merkle_root/merkle_root_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { MerkleTree } = require('merkletreejs');
const SHA256 = require('crypto-js/sha256');

const leaves = ['a', 'b', 'c', 'd'].map(SHA256);
const tree = new MerkleTree(leaves, SHA256, {});
const root = tree.getRoot().toString('hex');

console.log(JSON.stringify({ root }));
15 changes: 15 additions & 0 deletions test/merkletreejs/merkle_root/merkle_root_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from merkly.mtree import MerkleTree
import hashlib
import json


def sha256(x, y):
data = x + y
return hashlib.sha256(data).digest()


leaves = ["a", "b", "c", "d"]
tree = MerkleTree(leaves, sha256)
root = tree.root.hex()

print(json.dumps({"root": root}))
10 changes: 10 additions & 0 deletions test/merkletreejs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "merkletreejs",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"merkletreejs": "^0.3.11",
"web3": "^4.3.0"
}
}
30 changes: 30 additions & 0 deletions test/merkletreejs/test_merkle_proof_compatibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pytest import mark
import subprocess
import json


@mark.merkletreejs
def test_merkle_proof_compatibility_between_merkletreejs_and_merkly():
result = subprocess.run(["yarn"], check=False)

assert result.returncode == 0, result.stderr

result_js = subprocess.run(
["node", "./test/merkletreejs/merkle_proof/merkle_proof_test.js"],
capture_output=True,
text=True,
check=True,
)
assert result_js.returncode == 0, result_js.stderr
data_js = json.loads(result_js.stdout)

result_py = subprocess.run(
["python", "./test/merkletreejs/merkle_proof/merkle_proof_test.py"],
capture_output=True,
text=True,
check=True,
)
assert result_py.returncode == 0, result_py.stderr
data_py = json.loads(result_py.stdout)

assert data_js == data_py
30 changes: 30 additions & 0 deletions test/merkletreejs/test_merkle_root_compatibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pytest import mark
import subprocess
import json


@mark.merkletreejs
def test_merkle_root_compatibility_between_merkletreejs_and_merkly():
result = subprocess.run(["yarn"], check=False)

assert result.returncode == 0, result.stderr

result_js = subprocess.run(
["node", "./test/merkletreejs/merkle_root/merkle_root_test.js"],
capture_output=True,
text=True,
check=False,
)
assert result_js.returncode == 0, result_js.stderr
merkle_root_js = json.loads(result_js.stdout)

result_py = subprocess.run(
["python", "./test/merkletreejs/merkle_root/merkle_root_test.py"],
capture_output=True,
text=True,
check=False,
)
assert result_py.returncode == 0, result_py.stderr
merkle_root_py = json.loads(result_py.stdout)

assert merkle_root_js == merkle_root_py

0 comments on commit 109bb8a

Please sign in to comment.