diff --git a/.gitignore b/.gitignore index 8079cec..4581abf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +node_modules .hypothesis .vscode *cache* diff --git a/test/merkletreejs/merkle_proof/merkle_proof_test.js b/test/merkletreejs/merkle_proof/merkle_proof_test.js new file mode 100644 index 0000000..4d2e44c --- /dev/null +++ b/test/merkletreejs/merkle_proof/merkle_proof_test.js @@ -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()) })) diff --git a/test/merkletreejs/merkle_proof/merkle_proof_test.py b/test/merkletreejs/merkle_proof/merkle_proof_test.py new file mode 100644 index 0000000..eaa3957 --- /dev/null +++ b/test/merkletreejs/merkle_proof/merkle_proof_test.py @@ -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)})) diff --git a/test/merkletreejs/merkle_root/merkle_root_test.js b/test/merkletreejs/merkle_root/merkle_root_test.js new file mode 100644 index 0000000..5f3e2ad --- /dev/null +++ b/test/merkletreejs/merkle_root/merkle_root_test.js @@ -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 })); diff --git a/test/merkletreejs/merkle_root/merkle_root_test.py b/test/merkletreejs/merkle_root/merkle_root_test.py new file mode 100644 index 0000000..cb69112 --- /dev/null +++ b/test/merkletreejs/merkle_root/merkle_root_test.py @@ -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})) diff --git a/test/merkletreejs/package.json b/test/merkletreejs/package.json new file mode 100644 index 0000000..e9072fc --- /dev/null +++ b/test/merkletreejs/package.json @@ -0,0 +1,10 @@ +{ + "name": "merkletreejs", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "dependencies": { + "merkletreejs": "^0.3.11", + "web3": "^4.3.0" + } +} diff --git a/test/merkletreejs/test_merkle_proof_compatibility.py b/test/merkletreejs/test_merkle_proof_compatibility.py new file mode 100644 index 0000000..ad756dc --- /dev/null +++ b/test/merkletreejs/test_merkle_proof_compatibility.py @@ -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 diff --git a/test/merkletreejs/test_merkle_root_compatibility.py b/test/merkletreejs/test_merkle_root_compatibility.py new file mode 100644 index 0000000..44a69a6 --- /dev/null +++ b/test/merkletreejs/test_merkle_root_compatibility.py @@ -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