Skip to content

Commit

Permalink
error check for invalid whitelist URI
Browse files Browse the repository at this point in the history
  • Loading branch information
joselvelez committed Mar 5, 2022
1 parent d6aa2c3 commit 67b6f95
Showing 1 changed file with 93 additions and 82 deletions.
175 changes: 93 additions & 82 deletions middleware/generateMerkleProof.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,96 +7,107 @@ const axios = require('axios');
const express = require('express')
const router = express.Router()

/**
* TO DO: Add a check for a generated tree that is unbalanced. At a minimum
* give a notice of unbalaned tree and perhaps add functionality to auto-balance.
*/

let tree;
let root;
let leaf
let leafIndex;

// Takes any full IPFS URI and returns the CID only
const parseCID = (ipfsURI) => {
const uriSplit = ipfsURI.split('/');
const ipfsCID = uriSplit[uriSplit.length-1];
return ipfsCID;
}

const generateMerkleTree = async (ipfsURIWhitelist, leafToVerify) => {
try {

const whitelistCID = parseCID(ipfsURIWhitelist);

const url = 'https://gateway.pinata.cloud/ipfs/' + whitelistCID;
const whitelistData = await axios.get(url, {
headers: {
pinata_api_key: process.env['PINATA_API_KEY'],
pinata_secret_api_key: process.env['PINATA_API_SECRET']
}
})

console.log(whitelistData.data, 'WHITE LIST DATA RETRIEVAL!?!?!?!');

tree = new MerkleTree(whitelistData.data, keccak256, {sortPairs: true, sortLeaves: true, sort: true, hashLeaves: true});
root = tree.getHexRoot();
leaf = leafToVerify;
} catch (e) {
console.log(e);
}
}
/**
* Using the selected leaf value, lookup the corresponding hash
* and index from the Merkle Tree Summary json file
*/
const getLeafHashFromTreeSummary = (leafToVerify) => {
try {
const leafHash = `0x${keccak256(leafToVerify).toString('hex')}`;
console.log(tree.getLeafIndex(leafHash), 'leafIndex');
leafIndex = tree.getLeafIndex(leafHash);
} catch (e) {
console.log(e);
const runProof = async (ipfsURIWhitelist, leafToVerify) => {
let tree;
let leaf;
let leafIndex;
let whitelistDataResponse;

const fetchWhitelistFromIPFS = async (ipfsURIWhitelist) => {
try {
const whitelistCID = parseCID(ipfsURIWhitelist);

const url = 'https://gateway.pinata.cloud/ipfs/' + whitelistCID;
const whitelistData = await axios.get(url, {
timeout: 3000,
headers: {
pinata_api_key: process.env['PINATA_API_KEY'],
pinata_secret_api_key: process.env['PINATA_API_SECRET']
}
});
whitelistDataResponse = whitelistData.data;
console.log(whitelistData.data, 'WHITE LIST DATA RETRIEVAL!?!?!?!');
return true;
} catch (e) {
console.log(e);
return false;
}
}
}

const getProof = () => {
try {
if (leafIndex == -1) {
console.log('Error: value does not exist within the list');
return {
leaf: leaf,
errorMsg: 'Error: value does not exist within the list'
}
}

const leaves = tree.getHexLeaves();
const proof = tree.getHexProof(leaves[leafIndex]);
const leafValueHex = toHex(leaf);
const generateMerkleTree = async (whitelistDataResponse, leafToVerify) => {
try {
tree = new MerkleTree(whitelistDataResponse, keccak256, {
sortPairs: true,
sortLeaves: true,
sort: true,
hashLeaves: true
});
leaf = leafToVerify;
} catch (e) {
console.log(e);
}
}

const proofObj = {
leafValue: leaf,
leafHex: leafValueHex,
leafHash: leaves[leafIndex],
proof: proof
};
/**
* Using the selected leaf value, lookup the corresponding hash
* and index from the Merkle Tree Summary json file
*/
const getLeafHashFromTreeSummary = (leafToVerify) => {
try {
const leafHash = `0x${keccak256(leafToVerify).toString('hex')}`;
console.log(tree.getLeafIndex(leafHash), 'leafIndex');
leafIndex = tree.getLeafIndex(leafHash);
} catch (e) {
console.log(e);
}
}

console.log('proof obj before check', proofObj);
// Takes any full IPFS URI and returns the CID only
const parseCID = (ipfsURI) => {
const uriSplit = ipfsURI.split('/');
const ipfsCID = uriSplit[uriSplit.length-1];
return ipfsCID;
}

const getProof = () => {
try {
if (leafIndex == -1) {
console.log('Error: value does not exist within the list');
return {
leaf: leaf,
errorMsg: 'Error: value does not exist within the list'
}
}

const leaves = tree.getHexLeaves();
const proof = tree.getHexProof(leaves[leafIndex]);
const leafValueHex = toHex(leaf);

console.log(`Proof generated for ${leaf}`);

return proofObj;
} catch (e) {
console.log(e);
const proofObj = {
leafValue: leaf,
leafHex: leafValueHex,
leafHash: leaves[leafIndex],
proof: proof
};

console.log('proof obj before check', proofObj);
console.log(`Proof generated for ${leaf}`);

return proofObj;
} catch (e) {
console.log(e);
}
}
}

const runProof = async (whitelist, leafToVerify) => {
await generateMerkleTree(whitelist, leafToVerify);
// pass in a leaf value to generate a proof
getLeafHashFromTreeSummary(leaf);
return getProof();
if (await fetchWhitelistFromIPFS(ipfsURIWhitelist)) {
await generateMerkleTree(whitelistDataResponse, leafToVerify);
// pass in a leaf value to generate a proof
getLeafHashFromTreeSummary(leaf);
return getProof();
} else {
console.log(`Error: IPFS URI Invalid`);
}
return { errorMsg: 'Error: IPFS whitelist link is invalid' }
}

router.post('/proof', async (req, res) => {
Expand Down

0 comments on commit 67b6f95

Please sign in to comment.