Skip to content

Commit

Permalink
[passport-gated] Passport migration (snapshot-labs#1158)
Browse files Browse the repository at this point in the history
* Passport API Integration

Migrated the first version of using the Passport API instead of the SDK.

* Passport API Validation Strategy

Updated passport-gated and passport-weighted validation strategies to call the Passport API rather than the SDK.

* updated passport API validation

Updated passport-gated and passport-weighted validation strategies to call the Passport API rather than the SDK.

* Updated .gitignore

* Removed .history file

* Update src/validations/basic/index.ts

Co-authored-by: Chaitanya <[email protected]>

* Update src/validations/passport-weighted/schema.json

Co-authored-by: Chaitanya <[email protected]>

* Update src/validations/passport-gated/examples.json

Co-authored-by: Chaitanya <[email protected]>

* Removed submitting and signing passport

We updated the passport API to no longer require a signed message by the user to submit a passport. I updated that functionality in the validation functions

* Update package.json

* Updated passport-gated

Passport-gated now checks for specific stamps and a minimum threshold passport score

* removed score check in passport-gated

Passport-gated no longer checks for Passport score. Now passport-gated just checks if the stamps match.

* Update src/validations/passport-gated/README.md

Co-authored-by: Sam <[email protected]>

* Update src/validations/passport-gated/README.md

* Update src/validations/passport-gated/README.md

* Update src/validations/passport-gated/index.ts

* Add back expiry and creation timestamp check and check operator

* Update yarn.lock

---------

Co-authored-by: Chaitanya <[email protected]>
Co-authored-by: Sam <[email protected]>
  • Loading branch information
3 people authored Jun 1, 2023
1 parent 519e775 commit e08497a
Show file tree
Hide file tree
Showing 12 changed files with 629 additions and 620 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ dist
# Remove some common IDE working directories
.idea
.vscode
.env
.history
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,23 @@
"blakejs": "^1.2.1",
"copyfiles": "^2.4.1",
"cross-fetch": "^3.1.6",
"dotenv": "^16.0.3",
"eth-ens-namehash": "^2.0.8",
"json-to-graphql-query": "^2.2.4",
"tulons": "^0.0.7"
},
"devDependencies": {
"@types/jest": "^28.1.4",
"@types/jest": "^29.5.1",
"@types/node": "^18.0.3",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"eslint": "^8.19.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^28.1.2",
"jest": "^29.5.0",
"prettier": "^2.7.1",
"ts-jest": "^28.0.5",
"ts-jest": "^29.1.0",
"typescript": "^4.7.4"
},
"engines": {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dotenv/config';
import strategies from './strategies';
import validations from './validations';
import utils from './utils';
Expand Down
30 changes: 29 additions & 1 deletion src/validations/passport-gated/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
# Gitcoin passport gated validation
# Gitcoin Passport Gated Validation

This repository provides a passport-gated validation strategy for Snapshot. The implementation integrates with the Gitcoin API to validate whether a user is authorized to vote on a proposal.

## Prerequisites

Before using this code, ensure that you have the following information stored in a `.env` file at the project root:

- `PASSPORT_API_KEY=<your-api-key>`

## Overview

This implementation uses the Gitcoin Passport API to check whether a user has a valid passport by looking for their stamps.

## Code Explanation

The main function in this codebase checks stamps for a user and returns a boolean value indicating whether the user has a valid passport.

## Modifications

The original code utilized the Passport SDK to check if the user has a valid passport and stamps.

[Coming Soon] However, with the introduction of the Passport API, we can now simplify the process by checking for a score.

## Last Modified

This code was last modified on June 1, 2023.

Feel free to customize and extend this implementation to suit your specific needs.
2 changes: 1 addition & 1 deletion src/validations/passport-gated/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"network": "1",
"snapshot": "latest",
"params": {
"stamps": ["Ens", "Twitter", "Github", "POAP", "SnapshotVotesProvider"],
"stamps": ["Ens", "Github", "POAP", "SnapshotVotesProvider"],
"operator": "OR"
}
}
Expand Down
80 changes: 49 additions & 31 deletions src/validations/passport-gated/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import snapshot from '@snapshot-labs/snapshot.js';
import fetch from 'cross-fetch';
import Validation from '../validation';
import {
getPassport,
getVerifiedStamps,
hasValidIssuanceAndExpiration
} from '../passport-weighted/helper';
import snapshot from '@snapshot-labs/snapshot.js';

const API_KEY =
process.env.PASSPORT_API_KEY || '0cErnp4F.nRDEUU4Z8y5YyxcU32swrggDFNfWtXtI';

const headers = API_KEY
? {
'Content-Type': 'application/json',
'X-API-Key': API_KEY
}
: undefined;

const GET_PASSPORT_STAMPS_URI = `https://api.scorer.gitcoin.co/registry/stamps/`;

function hasValidIssuanceAndExpiration(credential, proposalTs) {
const issuanceDate = Number(
new Date(credential.issuanceDate).getTime() / 1000
).toFixed(0);
const expirationDate = Number(
new Date(credential.expirationDate).getTime() / 1000
).toFixed(0);
if (issuanceDate <= proposalTs && expirationDate >= proposalTs) {
return true;
}
return false;
}

export default class extends Validation {
public id = 'passport-gated';
Expand All @@ -13,43 +34,40 @@ export default class extends Validation {
public title = 'Gitcoin Passport Gated';
public description =
'Protect your proposals from spam and vote manipulation by requiring users to have a Gitcoin Passport.';
async validate(): Promise<boolean> {
const requiredStamps = this.params.stamps;
const passport: any = await getPassport(this.author);
if (!passport) return false;
if (!passport.stamps?.length || !requiredStamps?.length) return false;

const verifiedStamps: any[] = await getVerifiedStamps(
passport,
this.author,
requiredStamps.map((stamp) => ({
id: stamp
}))

async validate(currentAddress = this.author): Promise<boolean> {
const requiredStamps = this.params.stamps || [];
const operator = this.params.operator;
if (!operator) throw new Error('Operator is required');

const stampsResponse = await fetch(
GET_PASSPORT_STAMPS_URI + currentAddress,
{ headers }
);
if (!verifiedStamps.length) return false;

const provider = snapshot.utils.getProvider(this.network);
const proposalTs = (await provider.getBlock(this.snapshot)).timestamp;
const stampsData = await stampsResponse.json();

const operator = this.params.operator;
if (!stampsData?.items) {
// throw new Error(
// 'You do not have a valid Gitcoin Passport. Create one by visiting https://passport.gitcoin.co/ '
// );
return false;
}

// check issuance and expiration
const validStamps = verifiedStamps
const provider = snapshot.utils.getProvider(this.network);
const proposalTs = (await provider.getBlock(this.snapshot)).timestamp;
// check expiration for all stamps
const validStamps = stampsData.items
.filter((stamp) =>
hasValidIssuanceAndExpiration(stamp.credential, proposalTs)
)
.map((stamp) => stamp.provider);

// console.log('validStamps', validStamps);
// console.log('requiredStamps', requiredStamps);
// console.log('operator', operator);
.map((stamp) => stamp.credential.credentialSubject.provider);

if (operator === 'AND') {
return requiredStamps.every((stamp) => validStamps.includes(stamp));
} else if (operator === 'OR') {
return requiredStamps.some((stamp) => validStamps.includes(stamp));
} else {
return false;
}
return false;
}
}
30 changes: 30 additions & 0 deletions src/validations/passport-weighted/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
# Gitcoin passport weighted validation

This repository provides a passport-weighted validation strategy for Snapshot. The implementation integrates with the Gitcoin API to validate whether a user is authorized to vote on a proposal.

## Prerequisites

Before using this code, ensure that you have the following information stored in a `.env` file at the project root:

- `NEXT_PUBLIC_GC_API_KEY=<your-api-key>`
- `NEXT_PUBLIC_GC_SCORER_ID=<your-scorer-id>`

## Overview

This implementation uses the Gitcoin Passport API to check whether a user has a passport score thats above the minScore threshold value.

## Code Explanation

The main function in this codebase returns a threshold score based on the user's passport.

## Modifications

The original code utilized the Passport SDK to check if the user meets the passport score threshold. However, with the introduction of the Passport API, we can now simplify the process by checking directly for the score.

## Last Modified

This code was last modified on May 11, 2023.

Feel free to customize and extend this implementation to suit your specific needs.



1 change: 1 addition & 0 deletions src/validations/passport-weighted/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"network": "1",
"snapshot": "latest",
"params": {
"minScore": 10,
"stamps": [
{ "id": "Ens", "weight": 2 },
{ "id": "Twitter", "weight": 0.5 },
Expand Down
117 changes: 0 additions & 117 deletions src/validations/passport-weighted/helper.ts

This file was deleted.

Loading

0 comments on commit e08497a

Please sign in to comment.