generated from olivmath/template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
.hypothesis | ||
.vscode | ||
*cache* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) })) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |