Skip to content

Commit 37c1b87

Browse files
Merge pull request #9 from rsksmart/feat/add-offchain-store-endpoint
Added Offchain Endpoint
2 parents 20f4f94 + 16019ec commit 37c1b87

File tree

5 files changed

+7491
-5273
lines changed

5 files changed

+7491
-5273
lines changed

graph.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import {
1010
buildSchema,
1111
UseMiddleware,
1212
} from "type-graphql";
13-
import { ApolloServer } from "apollo-server";
13+
import { ApolloServer } from "@apollo/server";
14+
import express from "express";
1415
import { prisma } from "./db.server";
16+
import { expressMiddleware } from '@apollo/server/express4';
17+
import { storeOffchainAttestation } from "./utils";
1518

1619
const PORT = process.env.GRAPH_PORT || 4000;
1720

@@ -138,13 +141,53 @@ export async function startGraph() {
138141
authChecker: customAuthChecker,
139142
});
140143

144+
const app = express();
145+
app.use(express.json());
146+
147+
app.post("/offchain/store", async (req, res) => {
148+
try {
149+
150+
if (!req.body.textJson) {
151+
return res.status(400).json({ error: "textJson is required" });
152+
}
153+
154+
let parsedJson;
155+
try {
156+
parsedJson = typeof req.body.textJson === "string"
157+
? JSON.parse(req.body.textJson)
158+
: req.body.textJson;
159+
} catch (parseError) {
160+
return res.status(400).json({ error: "Invalid JSON in textJson" });
161+
}
162+
const attestation = await storeOffchainAttestation(parsedJson);
163+
164+
res.json({ status: "ok", attestation });
165+
}
166+
catch (error) {
167+
const sanitizedError = typeof error === "string" ? error.replace(/\n|\r/g, "") : JSON.stringify(error).replace(/\n|\r/g, "");
168+
console.error("Error storing offchain attestation:", sanitizedError);
169+
res.status(500).json({ error: "Internal server error" });
170+
}
171+
});
172+
141173
const server = new ApolloServer({
142174
schema: schema,
143175
cache: "bounded",
144176
introspection: true,
145-
context: () => ({ prisma }),
146177
});
147178

148-
const { url } = await server.listen(PORT);
149-
console.log(`Server is running, GraphQL Playground available at ${url}`);
179+
await server.start();
180+
app.use(
181+
'/graphql',
182+
express.json({limit: '100kb'}),
183+
expressMiddleware(server, {
184+
context: async () => ({ prisma }),
185+
})
186+
);
187+
188+
app.listen(PORT, () => {
189+
console.log(`GraphQL en http://localhost:${PORT}/graphql`);
190+
console.log(`REST en http://localhost:${PORT}/offchain/store`);
191+
});
150192
}
193+

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,32 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"postinstall": "typechain --target ethers-v5 ./abis/*.json",
8+
"postinstall": "typechain --target ethers-v6 ./abis/*.json",
99
"start": "ts-node start.ts"
1010
},
1111
"keywords": [],
1212
"author": "",
1313
"license": "ISC",
1414
"dependencies": {
15-
"@ethereum-attestation-service/eas-sdk": "0.28.2",
15+
"@apollo/server": "4.12.1",
16+
"@ethereum-attestation-service/eas-sdk": "2.7.0",
1617
"@prisma/client": "4.13.0",
17-
"apollo-server": "^3.12.0",
18+
"@typechain/ethers-v6": "^0.5.1",
1819
"class-validator": "^0.14.0",
1920
"dayjs": "^1.11.5",
2021
"dotenv": "^16.0.3",
21-
"ethers": "^5.7.2",
22+
"ethers": "6.14.3",
23+
"express": "^4.18.2",
2224
"graphql": "^15.8.0",
2325
"graphql-fields": "^2.0.3",
2426
"p-limit": "^3.0.2",
2527
"prisma": "^4.13.0",
2628
"reflect-metadata": "^0.1.13",
2729
"ts-node": "^10.9.1",
2830
"type-graphql": "^1.1.1",
29-
"typegraphql-prisma": "^0.24.7",
30-
"typescript": "^4.8.4",
3131
"typechain": "^8.1.0",
32-
"@typechain/ethers-v5": "^10.1.0"
32+
"typegraphql-prisma": "^0.24.7",
33+
"typescript": "^4.8.4"
3334
},
3435
"devDependencies": {
3536
"@types/node": "^18.8.4"

start.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,18 @@ async function go() {
5454
const filter = {
5555
topics: [
5656
[
57-
ethers.utils.id(registeredEventSignatureV1),
58-
ethers.utils.id(registeredEventSignatureV2),
59-
ethers.utils.id(attestedEventSignature),
60-
ethers.utils.id(revokedEventSignature),
61-
ethers.utils.id(timestampEventSignature),
62-
ethers.utils.id(revokedOffchainEventSignature),
57+
ethers.id(registeredEventSignatureV1),
58+
ethers.id(registeredEventSignatureV2),
59+
ethers.id(attestedEventSignature),
60+
ethers.id(revokedEventSignature),
61+
ethers.id(timestampEventSignature),
62+
ethers.id(revokedOffchainEventSignature),
6363
],
6464
],
6565
};
6666

6767
if (!DISABLE_LISTENER) {
68-
provider.on(filter, async (log: ethers.providers.Log) => {
68+
provider.on(filter, async (log: ethers.Log) => {
6969
go();
7070
});
7171
}

0 commit comments

Comments
 (0)