Skip to content

Commit

Permalink
Update backend structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Phanco committed Jan 11, 2024
1 parent 52f75a8 commit 7d07556
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
22 changes: 22 additions & 0 deletions packages/claim-backend/src/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Request, Response} from "express";
import {address as addressUtil} from "@liskhq/lisk-cryptography";
import leafMap from "./leafMap";

export function check (req: Request, res: Response) {
const { address } = req.body;
try {
addressUtil.validateLisk32Address(address);
} catch (_) {
res.status(400).json({
error: true,
message: `'${address}' is not a valid address.`,
});
return;
}

if (!leafMap[address]) {
res.status(400).json({ error: true, message: `${address} has no eligible claim.` });
return;
}
res.json(leafMap[address]);
}
36 changes: 2 additions & 34 deletions packages/claim-backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,12 @@
import express, { Express } from 'express';
import { address as addressUtil } from '@liskhq/lisk-cryptography';
import * as fs from 'fs';
import { Leaf } from './interface';
import {check} from "./check";

const app: Express = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const { leaves } = JSON.parse(
fs.readFileSync('../../data/example/merkle-tree-result-detailed.json', 'utf-8'),
);

const leafMap: {
[lskAddress: string]: Leaf;
} = {};

for (const leaf of leaves) {
leafMap[leaf.lskAddress] = leaf;
}

console.log(`${Object.keys(leafMap).length} Leaves loaded`);
app.post('/check', (req: express.Request, res: express.Response) => {
const { address } = req.body;
try {
addressUtil.validateLisk32Address(address);
} catch (_) {
res.status(400).json({
error: true,
message: `'${address}' is not a valid address.`,
});
return;
}

if (!leafMap[address]) {
res.status(400).json({ error: true, message: `${address} has no eligible claim.` });
return;
}
res.json(leafMap[address]);
});
app.post('/check', check);

const PORT = process.env.PORT || 3000;

Expand Down
18 changes: 18 additions & 0 deletions packages/claim-backend/src/leafMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fs from "fs";
import {Leaf} from "./interface";

const { leaves } = JSON.parse(
fs.readFileSync('../../data/example/merkle-tree-result-detailed.json', 'utf-8'),
);

const leafMap: {
[lskAddress: string]: Leaf;
} = {};

for (const leaf of leaves) {
leafMap[leaf.lskAddress] = leaf;
}

console.log(`LeafMap: ${Object.keys(leafMap).length} Leaves loaded`);

export default leafMap;

0 comments on commit 7d07556

Please sign in to comment.