Skip to content

Commit

Permalink
feat(app): check and verify possible stamps
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalmnt committed May 16, 2024
1 parent d75824d commit 97040d3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/components/DashboardScorePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ScorerContext } from "../context/scorerContext";
import { Spinner } from "@chakra-ui/react";
import { useCustomization } from "../hooks/useCustomization";
import { isDynamicCustomization } from "../utils/customizationUtils";
import { use1ClickVerification } from "../hooks/use1ClickVerification";

// Hexagon SVGs generated using https://codepen.io/wvr/pen/WrNgJp
// with the values listed below for each ring
Expand Down Expand Up @@ -103,9 +104,15 @@ export const DashboardScorePanel = ({ className }: { className: string }) => {
const customTitle = isDynamicCustomization(customization) ? customization.scorerPanel?.title : undefined;
const customText = isDynamicCustomization(customization) ? customization.scorerPanel?.text : undefined;

const { fetchCredentials } = use1ClickVerification();

useEffect(() => {
fetchCredentials();
}, []);

return (
<div
className={`${className} flex flex-col border border-foreground-6 text-foreground rounded bg-gradient-to-b from-background to-background-2`}
className={`${className} flex flex-col border border-foreground-6 rounded bg-gradient-to-b from-background to-background-2`}
>
<div className="flex p-4 border-b border-foreground-6">
<img alt="Person Icon" className="mr-2" src="/assets/personIcon.svg" />
Expand All @@ -116,7 +123,7 @@ export const DashboardScorePanel = ({ className }: { className: string }) => {
<ScoreRing className="shrink-0" />
</div>

<p className="shrink p-4">
<p className="shrink p-4 text-foreground">
{customText ||
"Your Unique Humanity Score is based out of 100 and measures your uniqueness. The current passing threshold is 20. " +
"Scores may vary across different apps, especially due to abuse or attacks on the service."}
Expand Down
90 changes: 90 additions & 0 deletions app/hooks/use1ClickVerification.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { PLATFORM_ID, PROVIDER_ID, Passport, StampPatch, ValidResponseBody } from "@gitcoin/passport-types";
import { useState, useEffect, useCallback, useContext } from "react";
import { PlatformProps } from "../components/GenericPlatform";
import { ValidatedPlatform, fetchPossibleEVMStamps } from "../signer/utils";
import { useDatastoreConnectionContext } from "../context/datastoreConnectionContext";
import { IAM_SIGNATURE_TYPE, iamUrl } from "../config/stamp_config";
import { fetchVerifiableCredential } from "@gitcoin/passport-identity";
import { createSignedPayload } from "../utils/helpers";
import { CeramicContext } from "../context/ceramicContext";
import { useWalletStore } from "../context/walletStore";

export const use1ClickVerification = () => {
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const [validatedPlatforms, setValidatedPlatforms] = useState<ValidatedPlatform[]>([]);
const { did } = useDatastoreConnectionContext();
const { passport, allPlatforms } = useContext(CeramicContext);
const address = useWalletStore((state) => state.address);

const fetchCredentials = useCallback(async () => {
setIsLoading(true);
setError(null);

if (!did) {
setIsLoading(false);
setError(new Error("No DID found"));
return;
}

if (!address) {
setIsLoading(false);
setError(new Error("No address"));
return;
}

try {
const validatedPlatforms = await fetchPossibleEVMStamps(address, allPlatforms, passport);
if (validatedPlatforms.length === 0) {
setIsLoading(false);
// Nothing to do
return;
}
setValidatedPlatforms(validatedPlatforms);
const platformTypes = validatedPlatforms.map((platform) => platform.platformProps.platform.platformId);
const validatedProviderIds = validatedPlatforms
.map((platform) =>
platform.platformProps.platFormGroupSpec.map((group) => group.providers.map((provider) => provider.name))
)
.flat(2);

const credentialResponse = await fetchVerifiableCredential(
iamUrl,
{
type: "EVMBulkVerify",
types: platformTypes,
version: "0.0.0",
address: address || "",
proofs: {},
signatureType: IAM_SIGNATURE_TYPE,
},
(data: any) => createSignedPayload(did, data)
);

// Should be able to assume that all stamps should be patched
const validCredentials =
credentialResponse.credentials?.filter((cred: any): cred is ValidResponseBody => !cred.error) || [];

// console.log({ patches });

// const stampPatches: StampPatch[] = validatedProviderIds
// .map((provider: PROVIDER_ID) => {
// const { credential } =
// validCredentials.find((cred) => cred.credential.credentialSubject.provider === provider) || {};
// if (credential) return { provider, credential };
// // shouldn't need this
// // else if (!selectedProviders.includes(provider)) return { provider };
// else return null;
// })
// .filter((patch): patch is StampPatch => patch !== null);

// await handlePatchStamps(stampPatches);
} catch (error) {
setError(error as Error);
}

setIsLoading(false);
}, [address, allPlatforms, passport, did]);

return { isLoading, error, validatedPlatforms, fetchCredentials };
};

0 comments on commit 97040d3

Please sign in to comment.