-
Notifications
You must be signed in to change notification settings - Fork 1
Add passport verification #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,27 @@ | ||
import type { Request, Response } from 'express'; | ||
import { validateRequest } from '@/utils'; | ||
import type { ProjectApplicationForManager } from '@/ext/passport/types'; | ||
import { isVerified } from '@/ext/passport/credentialverification'; | ||
|
||
interface SocialCredentialBody { | ||
application: Partial<ProjectApplicationForManager>; | ||
} | ||
|
||
export const validateSocialCredential = async ( | ||
req: Request, | ||
res: Response | ||
): Promise<void> => { | ||
validateRequest(req, res); | ||
|
||
const { application } = req.body as SocialCredentialBody; | ||
|
||
try { | ||
const result = await isVerified(application); | ||
res.json({ | ||
message: 'Social credential validated', | ||
provider: result, | ||
}); | ||
} catch (error) { | ||
res.status(400).json({ message: 'Error validating social credential' }); | ||
} | ||
}; |
This file contains hidden or 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,99 @@ | ||
import { PassportVerifier } from '@gitcoinco/passport-sdk-verifier'; | ||
import type { | ||
VerifiableCredential, | ||
ProjectApplicationForManager, | ||
ProjectApplicationMetadata, | ||
} from './types'; | ||
|
||
export const IAM_SERVER = | ||
'did:key:z6MkghvGHLobLEdj1bgRLhS4LPGJAvbMA1tn2zcRyqmYU5LC'; | ||
|
||
const verifier = new PassportVerifier(); | ||
|
||
export async function isVerified( | ||
application: Partial<ProjectApplicationForManager> | undefined | ||
): Promise<{ | ||
twitter: { isVerified: boolean }; | ||
github: { isVerified: boolean }; | ||
}> { | ||
const applicationMetadata = application?.metadata; | ||
|
||
const verifyCredential = async ( | ||
provider: 'twitter' | 'github' | ||
): Promise<boolean> => { | ||
const verifiableCredential = | ||
applicationMetadata?.application.project.credentials[provider]; | ||
if (verifiableCredential === undefined) { | ||
return false; | ||
} | ||
|
||
console.log('verifiableCredential', verifiableCredential); | ||
console.log('issuer', verifiableCredential.issuer); | ||
|
||
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()) | ||
); | ||
|
||
// todo: why is vcHasValidProof always false? | ||
console.log( | ||
`isVerified: ${vcHasValidProof} : ${vcIssuedByValidIAMServer} : ${providerMatchesProject} : ${vcIssuedToAtLeastOneProjectOwner}` | ||
); | ||
|
||
return ( | ||
vcHasValidProof && | ||
vcIssuedByValidIAMServer && | ||
providerMatchesProject && | ||
vcIssuedToAtLeastOneProjectOwner | ||
); | ||
}; | ||
|
||
const [twitterVerified, githubVerified] = await Promise.all([ | ||
verifyCredential('twitter'), | ||
verifyCredential('github'), | ||
]); | ||
|
||
return { | ||
twitter: { isVerified: twitterVerified }, | ||
github: { isVerified: githubVerified }, | ||
}; | ||
} | ||
|
||
function vcIssuedToAddress(vc: VerifiableCredential, address: string): boolean { | ||
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 | ||
): boolean { | ||
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 hidden or 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,130 @@ | ||
export interface VerifiableCredential { | ||
'@context': string[]; | ||
type: string[]; | ||
credentialSubject: { | ||
id: string; | ||
'@context': Array<Record<string, string>>; | ||
hash?: string; | ||
provider?: string; | ||
address?: string; | ||
challenge?: string; | ||
}; | ||
issuer: string; | ||
issuanceDate: string; | ||
expirationDate: string; | ||
proof: { | ||
type: string; | ||
proofPurpose: string; | ||
verificationMethod: string; | ||
created: string; | ||
jws: string; | ||
}; | ||
} | ||
|
||
export type ProjectCredentials = Record<string, VerifiableCredential>; | ||
|
||
export interface ProjectOwner { | ||
address: string; | ||
} | ||
|
||
export interface ProjectMetadata { | ||
title: string; | ||
description: string; | ||
website: string; | ||
bannerImg?: string; | ||
logoImg?: string; | ||
projectTwitter?: string; | ||
userGithub?: string; | ||
projectGithub?: string; | ||
credentials: ProjectCredentials; | ||
owners: ProjectOwner[]; | ||
recipient?: string; | ||
createdAt: number; | ||
lastUpdated: number; | ||
} | ||
|
||
export interface ApplicationAnswer { | ||
type: string; | ||
hidden: boolean; | ||
question: string; | ||
questionId: number; | ||
encryptedAnswer?: { | ||
ciphertext: string; | ||
encryptedSymmetricKey: string; | ||
}; | ||
answer: string; | ||
} | ||
|
||
export interface ProjectApplicationMetadata { | ||
signature: string; | ||
application: { | ||
round: string; | ||
answers: ApplicationAnswer[]; | ||
project: ProjectMetadata; | ||
recipient: string; | ||
}; | ||
} | ||
|
||
export interface BaseDonorValues { | ||
totalAmountDonatedInUsd: number; | ||
totalDonationsCount: number; | ||
uniqueDonorsCount: number; | ||
} | ||
|
||
export type ApplicationStatus = | ||
| 'PENDING' | ||
| 'APPROVED' | ||
| 'IN_REVIEW' | ||
| 'REJECTED' | ||
| 'APPEAL' | ||
| 'FRAUD' | ||
| 'RECEIVED' | ||
| 'CANCELLED'; | ||
|
||
export interface ProjectApplication extends BaseDonorValues { | ||
id: string; | ||
projectId: string; | ||
chainId: number; | ||
roundId: string; | ||
status: ApplicationStatus; | ||
metadataCid: string; | ||
metadata: ProjectApplicationMetadata; | ||
distributionTransaction: string | null; | ||
} | ||
|
||
interface StatusSnapshot { | ||
status: ApplicationStatus; | ||
updatedAtBlock: string; | ||
updatedAt: string; | ||
} | ||
|
||
export interface ProjectApplicationForManager extends ProjectApplication { | ||
anchorAddress: `0x${string}`; | ||
statusSnapshots: StatusSnapshot[]; | ||
round: { | ||
strategyName: string; | ||
strategyAddress: string; | ||
roundMetadata: { | ||
name: string; | ||
}; | ||
applicationsStartTime: string; | ||
applicationsEndTime: string; | ||
donationsEndTime: string; | ||
donationsStartTime: string; | ||
}; | ||
canonicalProject: { | ||
roles: Array<{ address: `0x${string}` }>; | ||
}; | ||
} | ||
|
||
export interface PastApplication { | ||
id: string; | ||
roundId: string; | ||
statusSnapshots: StatusSnapshot[]; | ||
status: ApplicationStatus; | ||
round: { | ||
roundMetadata: { | ||
name: string; | ||
}; | ||
}; | ||
} |
This file contains hidden or 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,10 +1,12 @@ | ||
import { Router } from 'express'; | ||
import evaluationRoutes from '@/routes/evaluationRoutes'; | ||
import poolRoutes from '@/routes/poolRoutes'; | ||
import passportRoutes from '@/routes/passportValidationRoutes'; | ||
|
||
const router = Router(); | ||
|
||
router.use('/evaluate', evaluationRoutes); | ||
router.use('/pools', poolRoutes); | ||
router.use('/passport', passportRoutes); | ||
|
||
export default router; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.