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

README.md typo fix #9

Open
wants to merge 2 commits 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ There are three folders in this repository:

You can run the client from the top-level directory with `node client/index`. This file is a script which will send an HTTP request to the server.

Think of the client as the _prover_ here. It needs to prove to the server that some `name` is in the `MERKLE_ROOT` on the server.
Think of the client as the _prover_ here. It needs to prove to the server that some `name` is in the `MERKLE_ROOT` on the server.

## Server

You can run the server from the top-level directory with `node server/index`. This file is an express server which will be hosted on port 1225 and respond to the client's request.

Think of the server as the _verifier_ here. It needs to verify that the `name` passed by the client is in the `MERKLE_ROOT`. If it is, then we can send the gift!
Think of the server as the _verifier_ here. It needs to verify that the `name` passed by the client is in the `MERKLE_ROOT`. If it is, then we can send the gift!

## Utils

There are a few files in utils:

- The `niceList.json` which contains all the names of the people who deserve a gift this year (this is randomly generated, feel free to add yourself and others to this list!)
- The `example.js` script shows how we can generate a root, generate a proof and verify that some value is in the root using the proof. Try it out from the top-level folder with `node/example.js`
- The `example.js` script shows how we can generate a root, generate a proof and verify that some value is in the root using the proof. Try it out from the top-level folder with `node utils/example.js`
- The `MerkleTree.js` should look familiar from the Merkle Tree module! This one has been modified so you should not have to deal with any crypto type conversion. You can import this in your client/server
- The `verifyProof.js` should also look familiar. This was the last stage in the module. You can use this function to prove a name is in the merkle root, as show in the example.
19 changes: 13 additions & 6 deletions client/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
const axios = require('axios');
const niceList = require('../utils/niceList.json');
const MerkleTree = require('../utils/MerkleTree');
const axios = require("axios");
const niceList = require("../utils/niceList.json");
const MerkleTree = require("../utils/MerkleTree");

const serverUrl = 'http://localhost:1225';
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 name = "Mr. Otis Koelpin III";
const merkleTree = new MerkleTree(niceList);
console.log(typeof merkleTree.getRoot());
const index = niceList.indexOf(name);
const proof = merkleTree.getProof(index);

const { data: gift } = await axios.post(`${serverUrl}/gift`, {
// TODO: add request body parameters here!
leaf: name,
proof,
});

console.log({ gift });
}

main();
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.

21 changes: 10 additions & 11 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require('express');
const verifyProof = require('../utils/verifyProof');
const express = require("express");
const verifyProof = require("../utils/verifyProof");

const port = 1225;

Expand All @@ -8,19 +8,18 @@ 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 = '';
const MERKLE_ROOT = "ddd59a2ffccddd60ff47993312821cd57cf30f7f14fb82937ebe2c4dc78375aa";

app.post('/gift', (req, res) => {
app.post("/gift", (req, res) => {
// grab the parameters from the front-end here
const body = req.body;

// TODO: prove that a name is in the list
const isInTheList = false;
if(isInTheList) {
res.send("You got a toy robot!");
}
else {
res.send("You are not on the list :(");
// TODO: prove that a name is in the list
const isInTheList = verifyProof(body.proof, body.leaf, MERKLE_ROOT);
if (isInTheList) {
res.send(`${body.leaf} You got a toy robot!`);
} else {
res.send(`${body.leaf} You are not on the list :(`);
}
});

Expand Down
16 changes: 8 additions & 8 deletions utils/example.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const MerkleTree = require('./MerkleTree');
const niceList = require('./niceList');
const verifyProof = require('./verifyProof');
const MerkleTree = require("./MerkleTree");
const niceList = require("./niceList");
const verifyProof = require("./verifyProof");

// create the merkle tree for the whole nice list
const merkleTree = new MerkleTree(niceList);

// get the root
const root = merkleTree.getRoot();

// find the proof that norman block is in the list
const name = 'Norman Block';
const index = niceList.findIndex(n => n === name);
// find the proof that norman block is in the list
const name = "Norman Block";
const index = niceList.findIndex((n) => n === name);
const proof = merkleTree.getProof(index);

// verify proof against the Merkle Root
console.log( verifyProof(proof, name, root) ); // true, Norman Block is in the list!
console.log(verifyProof(proof, name, root)); // true, Norman Block is in the list!

// TRY IT OUT: what happens if you try a name not in the list, or a fake proof?
// TRY IT OUT: what happens if you try a name not in the list, or a fake proof?