Skip to content

Commit

Permalink
integrate graphql to getAttestations (#48)
Browse files Browse the repository at this point in the history
* integrate graphql to getAttestations

* comment unused libs

* remove unused files

* remove unused files
  • Loading branch information
mehdi-torabiv authored Aug 27, 2024
1 parent bdf1e93 commit 773c621
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 221 deletions.
40 changes: 0 additions & 40 deletions src/components/shared/CustomTable.test.tsx

This file was deleted.

23 changes: 14 additions & 9 deletions src/libs/oci/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Address, parseAbiItem, WalletClient } from 'viem';
import { SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
import { Address, parseAbiItem } from 'viem';
import {
SchemaDecodedItem,
SchemaEncoder,
} from '@ethereum-attestation-service/eas-sdk';

import * as LitJsSdk from '@lit-protocol/lit-node-client';
import { EncryptToJsonPayload, SessionSigsMap } from '@lit-protocol/types';
import { publicClient } from './client';
import sepoliaChain from '../../utils/contracts/eas/sepoliaChain.json';

import { LitNetwork } from '@lit-protocol/constants';
import * as LitJsSdk from '@lit-protocol/lit-node-client';
import { sepolia } from 'viem/chains';
import { EncryptToJsonPayload, SessionSigsMap } from '@lit-protocol/types';
import { SCHEMA_TYPES } from '../../utils/contracts/eas/constants';

export interface IAttestation {
uid: `0x${string}`;
Expand Down Expand Up @@ -76,9 +78,7 @@ export const hasActiveRevocationTime = (attestations: IAttestation[]) => {
};

export const getAttestationData = (attestation: IAttestation) => {
const schemaEncoder = new SchemaEncoder(
'bytes32 key, string provider, string secret'
);
const schemaEncoder = new SchemaEncoder(SCHEMA_TYPES);

const decodedData = schemaEncoder.decodeData(attestation.data);

Expand Down Expand Up @@ -113,3 +113,8 @@ export const decryptAttestation = async (

return JSON.parse(decryptedData);
};

export const decodeAttestationData = (data: string): SchemaDecodedItem[] => {
const schemaEncoder = new SchemaEncoder(SCHEMA_TYPES);
return schemaEncoder.decodeData(data);
};
269 changes: 101 additions & 168 deletions src/pages/Identifiers/Identifiers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,102 +10,41 @@ import {
Paper,
Box,
Avatar,
CircularProgress,
} from '@mui/material';
import VerifiedIcon from '@mui/icons-material/Verified';
import { FaDiscord, FaGoogle } from 'react-icons/fa';
import { useNavigate } from 'react-router-dom';
import { useEffect, useState } from 'react';
import { decryptAttestation, getAttestations } from '../../libs/oci';
import { useAccount } from 'wagmi';
import useLit from '../../hooks/LitProvider';
import useSessionSigs from '../../hooks/useSessionSigs';
import { useSigner } from '../../utils/eas-wagmi-utils';
import { GraphQLClient, gql } from 'graphql-request';
import { useQuery } from '@tanstack/react-query';
import {
SchemaDecodedItem,
SchemaEncoder,
} from '@ethereum-attestation-service/eas-sdk';
// import { useState } from 'react';
// import { decryptAttestation, getAttestations } from '../../libs/oci';
// import { useAccount } from 'wagmi';
// import useLit from '../../hooks/LitProvider';
// import useSessionSigs from '../../hooks/useSessionSigs';
// import { useSigner } from '../../utils/eas-wagmi-utils';
// import { GraphQLClient, gql } from 'graphql-request';
// import { useQuery } from '@tanstack/react-query';
// import {
// SchemaDecodedItem,
// SchemaEncoder,
// } from '@ethereum-attestation-service/eas-sdk';
import { useGetAttestations } from '../../services/eas/query';

const identifiers = [
{ name: 'Discord', icon: FaDiscord, verified: false, color: 'text-blue-500' },
{ name: 'Google', icon: FaGoogle, verified: false, color: 'text-red-500' },
];

export function Identifiers() {
const graphQLClient = new GraphQLClient(
'https://sepolia.easscan.org/graphql'
);

const SCHEMA_TYPES = 'bytes32 key, string provider, string secret';

const decodettestationData = (data: string): SchemaDecodedItem[] => {
const schemaEncoder = new SchemaEncoder(SCHEMA_TYPES);
return schemaEncoder.decodeData(data);
};

const useGetAttestations = () => {
return useQuery({
queryKey: ['getAttestations'],
queryFn: async () => {
const result = await graphQLClient.request(gql`
query Attestations {
attestations(
where: {
attester: {
equals: "0x2d7B3e18D45846DA09D78e3644F15BD4aafa634d"
}
recipient: {
equals: "0x026B727b60D336806B87d60e95B6d7FAd2443dD6"
}
revoked: { equals: false }
schemaId: {
equals: "0x85e90e3e16d319578888790af3284fea8bca549305071531e7478e3e0b5e7d6d"
}
}
) {
id
attester
recipient
refUID
revocable
revocationTime
expirationTime
data
}
}
`);

// Decode each attestation's data and log it
result.attestations.forEach((attestation: any) => {
const decodedData = decodettestationData(attestation.data);
console.log({ decodedData });
});

// If you want to return decoded data, you can do something like this:
const decodedAttestations =
result &&
result.attestations.map((attestation: any) => ({
...attestation,
decodedData: decodettestationData(attestation.data),
}));

return decodedAttestations; // or return result.attestations if you don't need the decoded data in the result
},
});
};

const { data, isLoading, error } = useGetAttestations();
const { data } = useGetAttestations();
console.log({ data });

const { litNodeClient } = useLit();
const { chainId } = useAccount();
const { sessionSigs, createSessionSigs } = useSessionSigs();
const [decryptedData, setDecryptedData] = useState<any | null>(null);
// const { litNodeClient } = useLit();
// const { chainId } = useAccount();
// const { sessionSigs, createSessionSigs } = useSessionSigs();
// const [decryptedData, setDecryptedData] = useState<any | null>(null);

// const signer = useSigner();
// const { isConnected, address } = useAccount();

const signer = useSigner();
const { isConnected, address } = useAccount();
const navigate = useNavigate();

const handleRevoke = (identifier: string) => {
Expand All @@ -117,107 +56,101 @@ export function Identifiers() {
navigate(`/identifiers/${identifier.toLowerCase()}/attestation`);
};

const fetchAttestations = async () => {
if (!address) throw new Error('No address found');
// const fetchAttestations = async () => {
// if (!address) throw new Error('No address found');

const attestations = await getAttestations(address as `0x${string}`);
console.log({ attestations });
// const attestations = await getAttestations(address as `0x${string}`);
// console.log({ attestations });

return attestations;
};
// return attestations;
// };

useEffect(() => {
if (isConnected && signer && chainId && litNodeClient) {
console.log(litNodeClient, 'litNodeClient');
// useEffect(() => {
// if (isConnected && signer && chainId && litNodeClient) {
// console.log(litNodeClient, 'litNodeClient');

createSessionSigs({ signer, chainId, litNodeClient });
}
}, [signer, isConnected, litNodeClient, chainId, createSessionSigs]);
// createSessionSigs({ signer, chainId, litNodeClient });
// }
// }, [signer, isConnected, litNodeClient, chainId, createSessionSigs]);

useEffect(() => {
if (!sessionSigs) return;
const decrypt = async () => {
if (!sessionSigs) throw new Error('No sessionSigs found');
// useEffect(() => {
// if (!sessionSigs) return;
// const decrypt = async () => {
// if (!sessionSigs) throw new Error('No sessionSigs found');

const attestations = await fetchAttestations();
// const attestations = await fetchAttestations();

const decryptedSecrets = await Promise.all(
attestations.map(async (attestation) => {
return decryptAttestation(litNodeClient, attestation, sessionSigs);
})
);
setDecryptedData(decryptedSecrets);
};
// const decryptedSecrets = await Promise.all(
// attestations.map(async (attestation) => {
// return decryptAttestation(litNodeClient, attestation, sessionSigs);
// })
// );
// setDecryptedData(decryptedSecrets);
// };

decrypt();
}, [sessionSigs]);
// decrypt();
// }, [sessionSigs]);

return (
<div>
<Typography variant="h6" gutterBottom>
Identifiers
</Typography>
{isLoading && <CircularProgress />}
{error && (
<Typography color="error">Failed to load attestations</Typography>
)}
{!isLoading && !error && (
<List>
<ListItem>
<ListItemText primary="Identifier" />
<ListItemSecondaryAction>
<Typography variant="body2" style={{ marginRight: 40 }}>
Actions
</Typography>
</ListItemSecondaryAction>
</ListItem>
<Divider />
{identifiers.map((identifier, index) => (
<Box key={index} mb={2}>
<Paper elevation={1} className="rounded-xl py-2">
<ListItem>
<Avatar>
<identifier.icon
size={28}
className={`${identifier.verified ? 'text-black' : 'text-white'}`}
/>
</Avatar>
<ListItemText
primary={
<div className="flex items-center">
{identifier.verified && (
<VerifiedIcon className="text-blue-400 mr-2" />
)}
{identifier.name}
</div>
}
style={{ marginLeft: 16 }}
<List>
<ListItem>
<ListItemText primary="Identifier" />
<ListItemSecondaryAction>
<Typography variant="body2" style={{ marginRight: 40 }}>
Actions
</Typography>
</ListItemSecondaryAction>
</ListItem>
<Divider />
{identifiers.map((identifier, index) => (
<Box key={index} mb={2}>
<Paper elevation={1} className="rounded-xl py-2">
<ListItem>
<Avatar>
<identifier.icon
size={28}
className={`${identifier.verified ? 'text-black' : 'text-white'}`}
/>
<ListItemSecondaryAction>
{identifier.verified ? (
<Button
variant="outlined"
color="error"
onClick={() => handleRevoke(identifier.name)}
>
Revoke
</Button>
) : (
<Button
variant="contained"
color="primary"
onClick={() => handleConnect(identifier.name)}
>
Connect
</Button>
)}
</ListItemSecondaryAction>
</ListItem>
</Paper>
</Box>
))}
</List>
)}
</Avatar>
<ListItemText
primary={
<div className="flex items-center">
{identifier.verified && (
<VerifiedIcon className="text-blue-400 mr-2" />
)}
{identifier.name}
</div>
}
style={{ marginLeft: 16 }}
/>
<ListItemSecondaryAction>
{identifier.verified ? (
<Button
variant="outlined"
color="error"
onClick={() => handleRevoke(identifier.name)}
>
Revoke
</Button>
) : (
<Button
variant="contained"
color="primary"
onClick={() => handleConnect(identifier.name)}
>
Connect
</Button>
)}
</ListItemSecondaryAction>
</ListItem>
</Paper>
</Box>
))}
</List>
</div>
);
}
Expand Down
Loading

0 comments on commit 773c621

Please sign in to comment.