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

Merkle Tree Project #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 19 additions & 5 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@ const MerkleTree = require('../utils/MerkleTree');
const serverUrl = 'http://localhost:1225';

async function main() {
// TODO: how do we prove to the server we're on the nice list?
// TODO: how do we prove to the server we're on the nice list?
const tree = new MerkleTree(niceList);

const { data: gift } = await axios.post(`${serverUrl}/gift`, {
// TODO: add request body parameters here!
});
const name = "Ephyrian"; // Example name to prove

console.log({ gift });
const index = niceList.findIndex(n => n === name);

const proof = tree.getProof(index);

const root = tree.getRoot(); /* Only for demonstration, not to be sent */


const { data: gift } = await axios.post(`${serverUrl}/gift`, {
// TODO: add request body parameters here!
name: name,
proof: proof,
root: root /* This could be for additional verification */
});

console.log("Sending to server:", { name, proof });
console.log({ gift });
}

main();
16 changes: 0 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
const express = require('express');
const verifyProof = require('../utils/verifyProof');
const { verifyProof, concat } = require('../utils/verifyProof');
const { keccak256 } = require('ethereum-cryptography/keccak');

const port = 1225;

const app = express();
app.use(express.json());

// TODO: hardcode a merkle root here representing the whole nice list
// paste the hex string in here, without the 0x prefix
const MERKLE_ROOT = '';
// (paste the hex string in here, without the 0x prefix)
const MERKLE_ROOT = '4cdf89e56895bbcabd3551d431b8ba74239d026cfac7af5fcb1c15934a4eaf34';

app.post('/gift', (req, res) => {
// grab the parameters from the front-end here
const body = req.body;
const { name, proof } = req.body;
const leaf = keccak256(Buffer.from(name)).toString('hex'); // Assuming names are stored as simple hex strings in the tree

console.log("Received proof for:", name, " with proof:", proof);

// TODO: prove that a name is in the list
const isInTheList = false;
const isInTheList = verifyProof(proof, leaf, MERKLE_ROOT, concat);
console.log("Verification result:", isInTheList);

if(isInTheList) {
res.send("You got a toy robot!");
}
Expand All @@ -24,6 +30,7 @@ app.post('/gift', (req, res) => {
}
});


app.listen(port, () => {
console.log(`Listening on port ${port}!`);
});
});
2 changes: 1 addition & 1 deletion utils/MerkleTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ class MerkleTree {
}
}

module.exports = MerkleTree;
module.exports = MerkleTree;
19 changes: 0 additions & 19 deletions utils/example.js

This file was deleted.

9 changes: 7 additions & 2 deletions utils/niceList.json
Original file line number Diff line number Diff line change
Expand Up @@ -998,5 +998,10 @@
"Everett Bergnaum DVM",
"Mr. Marty Fahey",
"Joel Raynor",
"Edmond Carroll PhD"
]
"Edmond Carroll PhD",
"Homer J. Simpson",
"Peter Griffin",
"Stan Smith",
"Cleveland Brown",
"Ephyrian"
]
38 changes: 28 additions & 10 deletions utils/verifyProof.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
const { keccak256 } = require('ethereum-cryptography/keccak');
const { hexToBytes, bytesToHex } = require('ethereum-cryptography/utils');
const { bytesToHex } = require('ethereum-cryptography/utils');

const concat = (left, right) => keccak256(Buffer.concat([left, right]));

function verifyProof(proof, leaf, root) {
proof = proof.map(({data, left}) => ({
left, data: hexToBytes(data)

proof = proof.map(({data, left}) => ({
left, data: Buffer.from(data, 'hex')
}));

let data = keccak256(Buffer.from(leaf));
console.log("Initial hash from leaf:", bytesToHex(data));

for (let i = 0; i < proof.length; i++) {
if (proof[i].left) {
data = concat(proof[i].data, data);
} else {
data = concat(data, proof[i].data);
}

console.log(`Step ${i}, applying data: ${bytesToHex(proof[i].data)}, left: ${proof[i].left}`);

data = proof[i].left ? concat(proof[i].data, data) : concat(data, proof[i].data);
console.log(`Hash after step ${i}:`, bytesToHex(data));
}

return bytesToHex(data) === root;
const finalComputedRoot = bytesToHex(data);

console.log("Computed root:", bytesToHex(data));
console.log("Expected root:", root);

return finalComputedRoot === root;
}

module.exports = verifyProof;
module.exports = { verifyProof, concat };