From e10b27025b6c60dc969b4a0a5c89cd1a7ac91713 Mon Sep 17 00:00:00 2001 From: mrisqid Date: Sat, 10 Aug 2024 14:16:19 +0700 Subject: [PATCH] feat: get attestation by id and by schema id --- .../src/app/api/attestation/[id]/route.ts | 52 ++-- apps/haus/src/app/api/attestation/route.ts | 54 +++- apps/haus/src/app/hooks/useEAS.ts | 5 +- apps/haus/src/config/index.ts | 3 +- package-lock.json | 247 +++++++++--------- package.json | 1 + 6 files changed, 207 insertions(+), 155 deletions(-) diff --git a/apps/haus/src/app/api/attestation/[id]/route.ts b/apps/haus/src/app/api/attestation/[id]/route.ts index d64ba77..a422208 100644 --- a/apps/haus/src/app/api/attestation/[id]/route.ts +++ b/apps/haus/src/app/api/attestation/[id]/route.ts @@ -1,15 +1,21 @@ import { NextResponse } from 'next/server' -import { EAS } from '@ethereum-attestation-service/eas-sdk' -import { ethers } from 'ethers' -import { EAS_ADDRESS, PROVIDER } from '@/config' +import axios from 'axios' -const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY - -const customStringify = (obj) => { - return JSON.stringify(obj, (key, value) => (typeof value === 'bigint' ? value.toString() : value)) -} +const endpoint = 'https://optimism-sepolia.easscan.org/graphql' export async function GET(request: Request, context: { params: { id: string } }) { + const query = ` + query getAttestationById($where: AttestationWhereUniqueInput!) { + getAttestation(where: $where) { + decodedDataJson + attester + recipient + id + schemaId + } + } + ` + try { const attestationUID = context.params.id @@ -17,22 +23,20 @@ export async function GET(request: Request, context: { params: { id: string } }) return NextResponse.json({ message: 'Attestation UID is required' }, { status: 400 }) } - // Initialize the sdk with the address of the EAS Schema contract address - const easInstance = new EAS(EAS_ADDRESS as string) - - // Gets a default provider (in production use something else like infura/alchemy) - const provider = ethers.getDefaultProvider(PROVIDER) - const signer = new ethers.Wallet(PRIVATE_KEY as string, provider) - - // Connects an ethers style provider/signingProvider to perform read/write functions. - easInstance.connect(signer) - - const attestation = await easInstance.getAttestation(attestationUID) - console.log('Attestation detail: ', attestation) - - const data = customStringify(attestation) - - return NextResponse.json(data) + const response = await axios.post( + endpoint, + { + query, + variables: { where: { id: attestationUID } }, + }, + { + headers: { + 'Content-Type': 'application/json', + }, + }, + ) + + return NextResponse.json(response.data) } catch (error) { console.error('Error handling GET request', error) return NextResponse.json({ message: 'Something went wrong' }, { status: 500 }) diff --git a/apps/haus/src/app/api/attestation/route.ts b/apps/haus/src/app/api/attestation/route.ts index 181a650..43ba24e 100644 --- a/apps/haus/src/app/api/attestation/route.ts +++ b/apps/haus/src/app/api/attestation/route.ts @@ -1,9 +1,11 @@ -import { NextResponse } from 'next/server' +import { NextResponse, NextRequest } from 'next/server' import { EAS, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk' import { ethers } from 'ethers' -import { EAS_ADDRESS, SCHEMA_UID, PROVIDER } from '@/config' +import axios from 'axios' +import { SCHEMA_UID, PROVIDER } from '@/config' const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY +const EAS_ADDRESS = process.env.EAS_CONTRACT_ADDRESS export async function POST(request: Request) { try { @@ -63,3 +65,51 @@ export async function POST(request: Request) { return NextResponse.json({ message: 'Something went wrong' }, { status: 500 }) } } + +const endpoint = 'https://optimism-sepolia.easscan.org/graphql' + +export async function GET(request: NextRequest) { + const searchParams = request.nextUrl.searchParams + const query = ` + query Attestations($where: AttestationWhereInput, $orderBy: [AttestationOrderByWithRelationInput!]) { + attestations(where: $where, orderBy: $orderBy) { + attester + id + recipient + schemaId + timeCreated + decodedDataJson + } + } + ` + + try { + // schema uid: ?uid=xxxxxxxxxxxxxxxxxxxxxx + const uid = searchParams.get('uid') + + if (!uid) { + return NextResponse.json({ message: 'Schema UID is required' }, { status: 400 }) + } + + const response = await axios.post( + endpoint, + { + query, + variables: { + where: { schemaId: { equals: uid } }, + orderBy: [{ timeCreated: 'desc' }], + }, + }, + { + headers: { + 'Content-Type': 'application/json', + }, + }, + ) + + return NextResponse.json(response.data) + } catch (error) { + console.error('Error handling GET request', error) + return NextResponse.json({ message: 'Something went wrong' }, { status: 500 }) + } +} diff --git a/apps/haus/src/app/hooks/useEAS.ts b/apps/haus/src/app/hooks/useEAS.ts index 70cde79..8c75a4c 100644 --- a/apps/haus/src/app/hooks/useEAS.ts +++ b/apps/haus/src/app/hooks/useEAS.ts @@ -2,9 +2,10 @@ import { EAS } from '@ethereum-attestation-service/eas-sdk' import { ethers } from 'ethers' import { useEffect, useState } from 'react' -import { EAS_ADDRESS, PROVIDER } from '@/config' +import { PROVIDER } from '@/config' -export const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY +const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY +const EAS_ADDRESS = process.env.EAS_CONTRACT_ADDRESS export const useEAS = () => { const [eas, setEAS] = useState() diff --git a/apps/haus/src/config/index.ts b/apps/haus/src/config/index.ts index 17bf027..e442d97 100644 --- a/apps/haus/src/config/index.ts +++ b/apps/haus/src/config/index.ts @@ -141,6 +141,7 @@ export const EVENTS = [ }, ] -export const EAS_ADDRESS = process.env.EAS_CONTRACT_ADDRESS export const SCHEMA_UID = process.env.SCHEMA_UID export const PROVIDER = process.env.EAS_PROVIDER_URL || 'https://sepolia.optimism.io' +export const QUERY_ENDPOINT = + process.env.QUERY_ENDPOINT_URL || 'https://optimism-sepolia.easscan.org/graphql' diff --git a/package-lock.json b/package-lock.json index 6053e53..6946f81 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "@mui/material": "^5.16.6", "@mui/material-nextjs": "^5.16.6", "@worldcoin/idkit": "^1.2.2", + "axios": "^1.7.3", "big.js": "^6.2.1", "ethers": "^6.13.2", "next": "^14.2.5", @@ -5971,6 +5972,126 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/@next/swc-darwin-arm64": { + "version": "14.2.5", + "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.2.5.tgz", + "integrity": "sha512-/9zVxJ+K9lrzSGli1///ujyRfon/ZneeZ+v4ptpiPoOU+GKZnm8Wj8ELWU1Pm7GHltYRBklmXMTUqM/DqQ99FQ==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@next/swc-darwin-x64": { + "version": "14.2.5", + "resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-14.2.5.tgz", + "integrity": "sha512-vXHOPCwfDe9qLDuq7U1OYM2wUY+KQ4Ex6ozwsKxp26BlJ6XXbHleOUldenM67JRyBfVjv371oneEvYd3H2gNSA==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@next/swc-linux-arm64-gnu": { + "version": "14.2.5", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-14.2.5.tgz", + "integrity": "sha512-vlhB8wI+lj8q1ExFW8lbWutA4M2ZazQNvMWuEDqZcuJJc78iUnLdPPunBPX8rC4IgT6lIx/adB+Cwrl99MzNaA==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@next/swc-linux-arm64-musl": { + "version": "14.2.5", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-14.2.5.tgz", + "integrity": "sha512-NpDB9NUR2t0hXzJJwQSGu1IAOYybsfeB+LxpGsXrRIb7QOrYmidJz3shzY8cM6+rO4Aojuef0N/PEaX18pi9OA==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@next/swc-linux-x64-gnu": { + "version": "14.2.5", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-14.2.5.tgz", + "integrity": "sha512-8XFikMSxWleYNryWIjiCX+gU201YS+erTUidKdyOVYi5qUQo/gRxv/3N1oZFCgqpesN6FPeqGM72Zve+nReVXQ==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@next/swc-linux-x64-musl": { + "version": "14.2.5", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-14.2.5.tgz", + "integrity": "sha512-6QLwi7RaYiQDcRDSU/os40r5o06b5ue7Jsk5JgdRBGGp8l37RZEh9JsLSM8QF0YDsgcosSeHjglgqi25+m04IQ==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@next/swc-win32-arm64-msvc": { + "version": "14.2.5", + "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-14.2.5.tgz", + "integrity": "sha512-1GpG2VhbspO+aYoMOQPQiqc/tG3LzmsdBH0LhnDS3JrtDx2QmzXe0B6mSZZiN3Bq7IOMXxv1nlsjzoS1+9mzZw==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10" + } + }, + "node_modules/@next/swc-win32-ia32-msvc": { + "version": "14.2.5", + "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.2.5.tgz", + "integrity": "sha512-Igh9ZlxwvCDsu6438FXlQTHlRno4gFpJzqPjSIBZooD22tKeI4fE/YMRoHVJHmrQ2P5YL1DoZ0qaOKkbeFWeMg==", + "cpu": [ + "ia32" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10" + } + }, "node_modules/@next/swc-win32-x64-msvc": { "version": "14.2.5", "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-14.2.5.tgz", @@ -13382,7 +13503,6 @@ "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", - "dev": true, "license": "MIT" }, "node_modules/at-least-node": { @@ -13471,7 +13591,6 @@ "version": "1.7.3", "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.3.tgz", "integrity": "sha512-Ar7ND9pU99eJ9GpoGQKhKf58GpUOgnzuaB7ueNQ5BMi0p+LZ5oaEnfF999fAArcTIBwXTCHAmGcHOZJaWPq9Nw==", - "dev": true, "license": "MIT", "dependencies": { "follow-redirects": "^1.15.6", @@ -14980,7 +15099,6 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dev": true, "license": "MIT", "dependencies": { "delayed-stream": "~1.0.0" @@ -16214,7 +16332,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "dev": true, "license": "MIT", "engines": { "node": ">=0.4.0" @@ -19326,7 +19443,6 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", - "dev": true, "license": "MIT", "dependencies": { "asynckit": "^0.4.0", @@ -28712,7 +28828,6 @@ "version": "1.1.0", "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", - "dev": true, "license": "MIT" }, "node_modules/prr": { @@ -34135,126 +34250,6 @@ "optional": true } } - }, - "node_modules/@next/swc-darwin-arm64": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.2.5.tgz", - "integrity": "sha512-/9zVxJ+K9lrzSGli1///ujyRfon/ZneeZ+v4ptpiPoOU+GKZnm8Wj8ELWU1Pm7GHltYRBklmXMTUqM/DqQ99FQ==", - "cpu": [ - "arm64" - ], - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/@next/swc-darwin-x64": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-14.2.5.tgz", - "integrity": "sha512-vXHOPCwfDe9qLDuq7U1OYM2wUY+KQ4Ex6ozwsKxp26BlJ6XXbHleOUldenM67JRyBfVjv371oneEvYd3H2gNSA==", - "cpu": [ - "x64" - ], - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/@next/swc-linux-arm64-gnu": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-14.2.5.tgz", - "integrity": "sha512-vlhB8wI+lj8q1ExFW8lbWutA4M2ZazQNvMWuEDqZcuJJc78iUnLdPPunBPX8rC4IgT6lIx/adB+Cwrl99MzNaA==", - "cpu": [ - "arm64" - ], - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/@next/swc-linux-arm64-musl": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-14.2.5.tgz", - "integrity": "sha512-NpDB9NUR2t0hXzJJwQSGu1IAOYybsfeB+LxpGsXrRIb7QOrYmidJz3shzY8cM6+rO4Aojuef0N/PEaX18pi9OA==", - "cpu": [ - "arm64" - ], - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/@next/swc-linux-x64-gnu": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-14.2.5.tgz", - "integrity": "sha512-8XFikMSxWleYNryWIjiCX+gU201YS+erTUidKdyOVYi5qUQo/gRxv/3N1oZFCgqpesN6FPeqGM72Zve+nReVXQ==", - "cpu": [ - "x64" - ], - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/@next/swc-linux-x64-musl": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-14.2.5.tgz", - "integrity": "sha512-6QLwi7RaYiQDcRDSU/os40r5o06b5ue7Jsk5JgdRBGGp8l37RZEh9JsLSM8QF0YDsgcosSeHjglgqi25+m04IQ==", - "cpu": [ - "x64" - ], - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/@next/swc-win32-arm64-msvc": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-14.2.5.tgz", - "integrity": "sha512-1GpG2VhbspO+aYoMOQPQiqc/tG3LzmsdBH0LhnDS3JrtDx2QmzXe0B6mSZZiN3Bq7IOMXxv1nlsjzoS1+9mzZw==", - "cpu": [ - "arm64" - ], - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/@next/swc-win32-ia32-msvc": { - "version": "14.2.5", - "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.2.5.tgz", - "integrity": "sha512-Igh9ZlxwvCDsu6438FXlQTHlRno4gFpJzqPjSIBZooD22tKeI4fE/YMRoHVJHmrQ2P5YL1DoZ0qaOKkbeFWeMg==", - "cpu": [ - "ia32" - ], - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">= 10" - } } } } diff --git a/package.json b/package.json index 74b2fcf..8deb339 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@mui/material": "^5.16.6", "@mui/material-nextjs": "^5.16.6", "@worldcoin/idkit": "^1.2.2", + "axios": "^1.7.3", "big.js": "^6.2.1", "ethers": "^6.13.2", "next": "^14.2.5",