From 14e730fd69becb6d826e6549f74cc507662f4a35 Mon Sep 17 00:00:00 2001 From: Fazal E Rabbi Date: Mon, 12 Dec 2022 13:10:16 +0500 Subject: [PATCH] Create the merkle tree, proof and verify --- .gitignore | 1 + client/index.js | 14 +++++++++++++- docker-compose.yml | 13 +++++++++++++ docker/nodejs/Dockerfile | 5 +++++ package-lock.json | 16 ---------------- server/index.js | 12 ++++++++++-- 6 files changed, 42 insertions(+), 19 deletions(-) create mode 100755 docker-compose.yml create mode 100755 docker/nodejs/Dockerfile diff --git a/.gitignore b/.gitignore index 6704566..57dd8b0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ npm-debug.log* yarn-debug.log* yarn-error.log* lerna-debug.log* +.DS_Store # Diagnostic reports (https://nodejs.org/api/report.html) report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json diff --git a/client/index.js b/client/index.js index 28980e4..d020a3e 100644 --- a/client/index.js +++ b/client/index.js @@ -4,11 +4,23 @@ const MerkleTree = require('../utils/MerkleTree'); const serverUrl = 'http://localhost:1225'; +const index = niceList.findIndex(n => n === process.argv[2]); +if (!index || index < 0 || index > niceList.length || !process.argv[2]) { + console.log('Name not found in nice list!'); + process.exit(1); +} + +const leaf = process.argv[3]; +const merkleTree = new MerkleTree(niceList); +const proof = merkleTree.getProof(index); + + async function main() { // TODO: how do we prove to the server we're on the nice list? const { data: gift } = await axios.post(`${serverUrl}/gift`, { - // TODO: add request body parameters here! + leaf, + proof, }); console.log({ gift }); diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100755 index 0000000..82113da --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +version: "3" + +services: + node-server: + tty: true + build: + context: . + dockerfile: docker/nodejs/Dockerfile + ports: + - 1225:1225 + restart: unless-stopped + volumes: + - ./:/app \ No newline at end of file diff --git a/docker/nodejs/Dockerfile b/docker/nodejs/Dockerfile new file mode 100755 index 0000000..eb70933 --- /dev/null +++ b/docker/nodejs/Dockerfile @@ -0,0 +1,5 @@ +FROM node:18.12 +LABEL name="Fazal-E-Rabbi" email="fazal.newtech@gmail.com" + +WORKDIR /app +EXPOSE 1225 diff --git a/package-lock.json b/package-lock.json index 70a452c..29fc2e4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,22 +6,11 @@ "": { "name": "holiday-gift-list", "dependencies": { - "@faker-js/faker": "^7.6.0", "axios": "^1.2.0", - "body-parser": "^1.20.1", "ethereum-cryptography": "^1.1.2", "express": "^4.18.2" } }, - "node_modules/@faker-js/faker": { - "version": "7.6.0", - "resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-7.6.0.tgz", - "integrity": "sha512-XK6BTq1NDMo9Xqw/YkYyGjSsg44fbNwYRx7QK2CuoQgyy+f1rrTDHoExVM5PsyXCtfl2vs2vVJ0MN0yN6LppRw==", - "engines": { - "node": ">=14.0.0", - "npm": ">=6.0.0" - } - }, "node_modules/@noble/hashes": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.1.2.tgz", @@ -785,11 +774,6 @@ } }, "dependencies": { - "@faker-js/faker": { - "version": "7.6.0", - "resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-7.6.0.tgz", - "integrity": "sha512-XK6BTq1NDMo9Xqw/YkYyGjSsg44fbNwYRx7QK2CuoQgyy+f1rrTDHoExVM5PsyXCtfl2vs2vVJ0MN0yN6LppRw==" - }, "@noble/hashes": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.1.2.tgz", diff --git a/server/index.js b/server/index.js index cc302ca..56ba43e 100644 --- a/server/index.js +++ b/server/index.js @@ -8,14 +8,22 @@ 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 niceList = require('../utils/niceList.json'); +// const MerkleTree = require('../utils/MerkleTree'); +// const merkleTree = new MerkleTree(niceList); + +// const root = merkleTree.getRoot(); +// console.log(root); + +const MERKLE_ROOT = 'ddd59a2ffccddd60ff47993312821cd57cf30f7f14fb82937ebe2c4dc78375aa'; 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; + const isInTheList = verifyProof(body.proof, body.leaf, MERKLE_ROOT); if(isInTheList) { res.send("You got a toy robot!"); }