This repository has been archived by the owner on Feb 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds Passport Credential Verification (#48)
* add verification utils * add/implement hook * fix verification * fix types * formatting * put back prettier * remove logs
- Loading branch information
Showing
12 changed files
with
1,678 additions
and
1,565 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useState, useEffect } from "react"; | ||
|
||
import { isVerified } from "@/lib/passport/credentialVerification"; | ||
|
||
import { ProjectApplicationForManager } from "../services/allo"; | ||
|
||
export function useCredentialverification( | ||
applicationMetadata: ProjectApplicationForManager | undefined, | ||
) { | ||
const [isTwitterVerified, setIsTwitterVerified] = useState<boolean>(false); | ||
const [isGithubVerified, setIsGithubVerified] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
async function checkVerification() { | ||
if (applicationMetadata) { | ||
const twitterVerified = await isVerified("twitter", applicationMetadata); | ||
const githubVerified = await isVerified("github", applicationMetadata); | ||
|
||
setIsTwitterVerified(twitterVerified); | ||
setIsGithubVerified(githubVerified); | ||
} | ||
} | ||
|
||
checkVerification(); | ||
}, [applicationMetadata]); | ||
|
||
return { isTwitterVerified, isGithubVerified }; | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { DIDKitLib } from "@gitcoinco/passport-sdk-types"; | ||
import * as DIDKit from "@spruceid/didkit-wasm"; | ||
|
||
import { VerifiableCredential } from "@/features/checker/services/allo"; | ||
|
||
export class PassportVerifier { | ||
_DIDKit: DIDKitLib | undefined; | ||
|
||
constructor() { | ||
this.init(); | ||
} | ||
|
||
async init(): Promise<void> { | ||
// webpacks `experiments.asyncWebAssembly=true` option imports the wasm asynchronously but typescript | ||
// doesn't recognise the import as a Promise, so we wrap it in a Promise and resolve before using... | ||
await new Promise((resolve) => resolve(DIDKit)).then( | ||
async (didkit: { default: Promise<DIDKitLib> } | DIDKitLib | any) => { | ||
if (didkit.default) { | ||
await Promise.resolve(didkit.default).then((didkit) => { | ||
this._DIDKit = didkit as typeof DIDKit; | ||
}); | ||
} else { | ||
this._DIDKit = didkit as typeof DIDKit; | ||
} | ||
}, | ||
); | ||
} | ||
|
||
async verifyCredential(credential: VerifiableCredential): Promise<boolean> { | ||
// ensure DIDKit is established | ||
if (!this._DIDKit) { | ||
await this.init(); | ||
} | ||
|
||
// extract expirationDate | ||
// const { expirationDate, proof } = credential; | ||
const { proof } = credential; | ||
|
||
// check that the credential is still valid (not expired) | ||
// if (new Date(expirationDate) > new Date()) { | ||
try { | ||
if (this._DIDKit === undefined) { | ||
throw new Error("DIDKit is not initialized"); | ||
} | ||
|
||
const verify = JSON.parse( | ||
await this._DIDKit.verifyCredential( | ||
JSON.stringify(credential), | ||
`{"proofPurpose":"${proof.proofPurpose}"}`, | ||
), | ||
) as { checks: string[]; warnings: string[]; errors: string[] }; | ||
|
||
// did we get any errors when we attempted to verify? | ||
return verify.errors.length === 0; | ||
} catch (e) { | ||
// if didkit throws, etc. | ||
return false; | ||
} | ||
// } else { | ||
// // past expiry :( | ||
// return false; | ||
// } | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
ProjectApplicationMetadata, | ||
type ProjectApplicationForManager, | ||
type VerifiableCredential, | ||
} from "@/features/checker/services/allo"; | ||
|
||
import { PassportVerifier } from "./PassportVerifier"; | ||
|
||
export const IAM_SERVER = "did:key:z6MkghvGHLobLEdj1bgRLhS4LPGJAvbMA1tn2zcRyqmYU5LC"; | ||
|
||
const verifier = new PassportVerifier(); | ||
|
||
export async function isVerified( | ||
provider: "twitter" | "github", | ||
application: ProjectApplicationForManager | undefined, | ||
): Promise<boolean> { | ||
const applicationMetadata = application?.metadata; | ||
const verifiableCredential = applicationMetadata?.application.project.credentials[provider]; | ||
if (verifiableCredential === undefined) { | ||
return false; | ||
} | ||
|
||
const vcHasValidProof = await verifier.verifyCredential(verifiableCredential); | ||
const vcIssuedByValidIAMServer = verifiableCredential.issuer === IAM_SERVER; | ||
const providerMatchesProject = vcProviderMatchesProject( | ||
provider, | ||
verifiableCredential, | ||
applicationMetadata, | ||
); | ||
const roleAddresses = application?.canonicalProject?.roles.map((role) => role.address); | ||
const vcIssuedToAtLeastOneProjectOwner = (roleAddresses ?? []).some((role) => | ||
vcIssuedToAddress(verifiableCredential, role.toLowerCase()), | ||
); | ||
return ( | ||
vcHasValidProof && | ||
vcIssuedByValidIAMServer && | ||
providerMatchesProject && | ||
vcIssuedToAtLeastOneProjectOwner | ||
); | ||
} | ||
|
||
function vcIssuedToAddress(vc: VerifiableCredential, address: string) { | ||
const vcIdSplit = vc.credentialSubject.id.split(":"); | ||
const addressFromId = vcIdSplit[vcIdSplit.length - 1]; | ||
return addressFromId.toLowerCase() === address.toLowerCase(); | ||
} | ||
|
||
function vcProviderMatchesProject( | ||
provider: string, | ||
verifiableCredential: VerifiableCredential, | ||
applicationMetadata: ProjectApplicationMetadata | undefined, | ||
) { | ||
let vcProviderMatchesProject = false; | ||
if (provider === "twitter") { | ||
vcProviderMatchesProject = | ||
verifiableCredential.credentialSubject.provider?.split("#")[1].toLowerCase() === | ||
applicationMetadata?.application.project?.projectTwitter?.toLowerCase(); | ||
} else if (provider === "github") { | ||
vcProviderMatchesProject = | ||
verifiableCredential.credentialSubject.provider?.split("#")[1].toLowerCase() === | ||
applicationMetadata?.application.project?.projectGithub?.toLowerCase(); | ||
} | ||
return vcProviderMatchesProject; | ||
} |
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