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

merkletree API #154

Closed
wants to merge 14 commits into from
Closed
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: 6 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"image": "mcr.microsoft.com/devcontainers/universal:2",
"features": {
"ghcr.io/devcontainers/features/node:1": {}
}
}
1 change: 0 additions & 1 deletion api/README-no-api.md

This file was deleted.

102 changes: 102 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// server.js
import express from 'express';
import cors from 'cors';
import { merkleTreeObj } from './keys.js';
const app = express();
const port = 3000;

// Middleware
app.use(cors());
app.use(express.json());

// In-memory rate limiting variables
let globalRequestCount = 0;
const requestLimit = 5;
const timeWindow = 15000; // 15 seconds

// In-memory cache for storing inclusion proofs
const proofCache = {}; // In the format { publicKey: proofData }

// Reset the request count at the start of each time window
setInterval(() => {
globalRequestCount = 0;
}, timeWindow);

// Rate limiter middleware function
const rateLimiter = (req, res, next) => {
if (globalRequestCount >= requestLimit) {
console.log('Rate limit reached, delaying response');
setTimeout(next, 3000); // 3-second delay
} else {
globalRequestCount++;
next();
}
};
const compose =
(...fns) =>
initialValue =>
fns.reduceRight((acc, val) => val(acc), initialValue);

const snd = ([_x, y, ..._z]) => y;
const getHash = ({ hash }) => hash;
const isUndefined = x => x === undefined;
const trace = label => value => {
console.log(label, '::::', value);
return value;
};
const isValidCheck = compose(isUndefined, trace('after getHash'), getHash);

// Example endpoint
app.post('/api/transaction', rateLimiter, (req, res) => {
const {
publicKey: { key },
} = req.body;

// Check cache first
if (proofCache[key]) {
console.log('Returning cached proof for:', key);
console.group(
'------------- NESTED LOGGER OPEN:: proofCache[publicKey] -------------',
);
console.log('=====================================================');
console.log('::proofCache', proofCache);
console.log('----------------------------------------------');
console.log(':: proofCache[publicKey]', proofCache[key]);
console.log('=====================================================');
console.log(
'---------- NESTED LOGGER CLOSED:: proofCache[publicKey]----------',
);
console.groupEnd();
return res.json({
message: 'Transaction is valid',
proof: proofCache[key],
});
}

// Mock check for public key inclusion - replace with actual logic
const proof = checkInclusion(key);
console.log('------------------------');
console.log('proof::', proof);

if (proof) {
// Example proof, replace with actual computation logic

// Store in cache
proofCache[key] = proof;

res.json({ message: 'Transaction is valid', proof });
} else {
res.status(400).json({ message: 'Public key not eligible' });
}
});

// Mock function to check if a publicKey exists in the Merkle Tree
function checkInclusion(publicKey) {
// Replace this with your actual logic to check the Merkle Tree
return merkleTreeObj.constructProof(publicKey); // Assume all keys are valid for demonstration
}

// Starting the server
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
Loading
Loading