-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eba43e3
commit ba1a847
Showing
4 changed files
with
89 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,30 @@ | ||
import type { Address } from "viem"; | ||
import { gql, request } from "graphql-request"; | ||
import { createPublicClient, http } from 'viem' | ||
import { base } from 'viem/chains' | ||
import { Attestation, AttestationSchema } from "./types"; | ||
import { AttestationSchema } from "./types"; | ||
import type { Chain } from 'viem' | ||
|
||
// TODO: should resolve based on chain id? | ||
export const indexApi = "https://base.easscan.org/graphql"; | ||
export const easScanGraphQLAPI = "https://base.easscan.org/graphql"; | ||
|
||
const schemaUids: Record<number, Record<AttestationSchema, string>> = { | ||
const supportedChains: Record<number, { schemaUids: Record<string, string>, attesterAddresses: string[] }> = { | ||
[base.id]: { | ||
"VERIFIED COUNTRY":"0x1801901fabd0e6189356b4fb52bb0ab855276d84f7ec140839fbd1f6801ca065", | ||
"VERIFIED ACCOUNT": "0xf8b05c79f090979bf4a80270aba232dff11a10d9ca55c4f88de95317970f0de9" | ||
schemaUids: { | ||
"VERIFIED COUNTRY":"0x1801901fabd0e6189356b4fb52bb0ab855276d84f7ec140839fbd1f6801ca065", | ||
"VERIFIED ACCOUNT": "0xf8b05c79f090979bf4a80270aba232dff11a10d9ca55c4f88de95317970f0de9" | ||
}, | ||
attesterAddresses: ["0x357458739F90461b99789350868CD7CF330Dd7EE"] | ||
}, | ||
}; | ||
} | ||
|
||
export const attesterAddresses: Record<number, string[]> = { | ||
[base.id]: ["0x357458739F90461b99789350868CD7CF330Dd7EE"], | ||
}; | ||
export function isChainSupported(chain: Chain): boolean { | ||
return !!supportedChains[chain.id]; | ||
} | ||
|
||
// TODO: use client from network but allow passing a custom client | ||
export const client = createPublicClient({ | ||
chain: base, | ||
transport: http(), | ||
}); | ||
|
||
export function schemasToUids(schemas: AttestationSchema[], clientChainId?: number): string[] { | ||
// TODO: better error handling | ||
export function getChainSchemasUids(schemas: AttestationSchema[], clientChainId?: number): string[] { | ||
if (!clientChainId) { | ||
return []; | ||
} | ||
return schemas.map((schema) => schemaUids[clientChainId][schema]); | ||
} | ||
return schemas.map((schema) => supportedChains[clientChainId]['schemaUids'][schema]); | ||
} | ||
|
||
export function getAttesterAddresses(chain: Chain): string[] { | ||
return supportedChains[chain.id].attesterAddresses; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,34 @@ | ||
import type { Address } from "viem"; | ||
import type { Chain } from 'viem'; | ||
import { AttestationSchema } from "./types"; | ||
import { getAttestation } from './getAttestation'; | ||
import { schemasToUids, client } from './attestation'; | ||
|
||
import { isChainSupported, getChainSchemasUids } from './attestation'; | ||
|
||
/** | ||
* Checks if the specified address has verified attestations for the given chain and expected schemas. | ||
* | ||
* @param chain - The blockchain to check for attestations. | ||
* @param address - The address to check for attestations. | ||
* @param expectedSchemas - An array of attestation schemas that are expected. | ||
* @returns A promise that resolves to a boolean indicating whether the address has the expected attestations. | ||
* @throws Will throw an error if the chain is not supported. | ||
*/ | ||
export async function hasVerifiedAttestations( | ||
chain: Chain, | ||
address: Address, | ||
expectedSchemas: AttestationSchema[] = [] | ||
): Promise<boolean> { | ||
if (!address || expectedSchemas?.length === 0) { | ||
if (!chain || !address || expectedSchemas.length === 0) { | ||
return false; | ||
} | ||
const schemaUids = schemasToUids(expectedSchemas, client.chain.id); | ||
const attestations = await getAttestation(address, { schemas: expectedSchemas }); | ||
const schemasFound = attestations.map( | ||
(attestation) => attestation.schemaId, | ||
); | ||
|
||
return schemaUids.every((desiredSchemaUid) => schemasFound.includes(desiredSchemaUid)); | ||
if (!isChainSupported(chain)) { | ||
throw new Error(`Chain ${chain.id} is not supported`); | ||
} | ||
|
||
const schemaUids = getChainSchemasUids(expectedSchemas, chain.id); | ||
const attestations = await getAttestation(chain, address, { schemas: expectedSchemas }); | ||
const schemasFound = attestations.map(attestation => attestation.schemaId); | ||
|
||
return schemaUids.every(schemaUid => schemasFound.includes(schemaUid)); | ||
} |