Skip to content
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

feat(platforms): add Proof of Passport to stamps #2488

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions platforms/src/ProofofPassport/App-Bindings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//App-bindings.ts - EVM
import { AppContext, ProviderPayload } from "../types";
import { Platform } from "../utils/platform";

export class ProofOfPassportPlatform extends Platform {
platformId = "ProofofPassport";
path = "ProofOfPassport";
clientId: string = null;
redirectUri: string = null;
isEVM = true;
banner = {
heading:
"Proof of Passport",
cta: {
label: "Learn more",
url: "https://proofofpassport.com",
},
};

async getProviderPayload(appContext: AppContext): Promise<ProviderPayload> {
const result = await Promise.resolve({});
return result;
}
}
27 changes: 27 additions & 0 deletions platforms/src/ProofofPassport/Providers-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { PlatformSpec, PlatformGroupSpec, Provider } from "../types";
import { ProofOfPassportProvider } from "./Providers";

export const PlatformDetails: PlatformSpec = {
icon: "./assets/ProofofPassportStampIcon.svg",
platform: "ProofofPassport",
name: "Proof of Passport",
description: "Scan the NFC chip inside your passport to prove your humanity, powered by ZK for complete anonymity. Open source project.",
connectMessage: "Connect Account",
website: "https://proofofpassport.com/",
};

export const ProviderConfig: PlatformGroupSpec[] = [
{
platformGroup: "Name of the Stamp platform group",
providers: [
{
title: "Prove your humanity with Proof of Passport",
description: "Powered by ZK cryptography to ensure complete anonymity. Open source project.",
name: "ProofofPassport",
},
]
},
];

export const providers: Provider[] = [new ProofOfPassportProvider()]

1 change: 1 addition & 0 deletions platforms/src/ProofofPassport/Providers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ProofOfPassportProvider } from "./proofOfPassport";
54 changes: 54 additions & 0 deletions platforms/src/ProofofPassport/Providers/proofOfPassport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { Provider, ProviderOptions } from "../../types";
import type { RequestPayload, VerifiedPayload } from "@gitcoin/passport-types";

import { getTokenBalance } from "./utils";

export const RPC_URL = process.env.RPC_URL;

export type ethErc721PossessionProviderOptions = {
threshold: number;
contractAddress: string;
decimalNumber: number;
error: string;
};

export class ProofOfPassportProvider implements Provider {
type = "";

_options: ethErc721PossessionProviderOptions = {
threshold: 1,
contractAddress: "0x98aA4401ef9d3dFed09D8c98B5a62FA325CF23b3",
decimalNumber: 0,
error: "Coin Possession Provider Error",
};

constructor(options: ProviderOptions = {}) {
this._options = { ...this._options, ...options };
}

async verify(payload: RequestPayload): Promise<VerifiedPayload> {
const { address } = payload;
let valid = false;
let amount = 0;
try {
amount = await getTokenBalance(address, this._options.contractAddress, this._options.decimalNumber, payload);

} catch (e) {
return {
valid: false,
errors: [this._options.error],
};
} finally {
console.log("amount:", amount);
valid = amount >= this._options.threshold;
}
return {
valid,
record: valid
? {
address: address.toLocaleLowerCase()
}
: {},
};
}
}
29 changes: 29 additions & 0 deletions platforms/src/ProofofPassport/Providers/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

import { RequestPayload } from "@gitcoin/passport-types";
import { Contract } from "@ethersproject/contracts";
import { getRPCProvider } from "../../utils/signer";

const ERC721_ABI = [
{
"inputs": [{ "internalType": "address", "name": "owner", "type": "address" }],
"name": "balanceOf",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false, "stateMutability": "view", "type": "function", "constant": true
}
]
export async function getTokenBalance(
address: string,
tokenContractAddress: string,
decimalNumber: number,
payload: RequestPayload
): Promise<number> {
const staticProvider = getRPCProvider(payload);
console.log("tokenContractAddress:", tokenContractAddress);
console.log("address", address);
console.log("decimalNumber:", decimalNumber);
const readContract = new Contract(tokenContractAddress, ERC721_ABI, staticProvider);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
const tokenBalance: string = await readContract?.balanceOf(address);
console.log("tokenBalance call:", tokenBalance);
return parseFloat(tokenBalance);
}
57 changes: 57 additions & 0 deletions platforms/src/ProofofPassport/__tests__/proofOfPassport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable */
import { JsonRpcSigner, JsonRpcProvider } from "@ethersproject/providers";
import { ProofOfPassportProvider } from "../Providers/proofOfPassport";
import { getTokenBalance } from "../Providers/utils";
import { RequestPayload } from "@gitcoin/passport-types";

jest.mock("../Providers/utils", () => ({
getTokenBalance: jest.fn()
}));

const MOCK_ADDRESS = "0x3336A5a627672A39967efa3Cd281e8e08E235ce2";
const MOCK_ADDRESS_LOWER = MOCK_ADDRESS.toLocaleLowerCase();

beforeEach(() => {
jest.clearAllMocks();
});

const mockTokenAddress = "0x5550ab114E3cf857b5bDd195eA9f753FAFd1cA91";

describe('ProofOfPassportProvider Tests', () => {
let proofOfPassportProvider: ProofOfPassportProvider;

beforeEach(() => {
proofOfPassportProvider = new ProofOfPassportProvider({
threshold: 1,
recordAttribute: 'tokenCount',
contractAddress: mockTokenAddress,
decimalNumber: 0,
error: 'Token balance fetch error'
});
});

it('should verify token balance is above threshold', async () => {
(getTokenBalance as jest.Mock).mockResolvedValueOnce(1);

const result = await proofOfPassportProvider.verify({
address: MOCK_ADDRESS
} as unknown as RequestPayload);

expect(result.valid).toBe(true);
expect(result.record).toEqual({
address: MOCK_ADDRESS_LOWER
});
});

it('should reverse with a balance of 0', async () => {
(getTokenBalance as jest.Mock).mockResolvedValueOnce(0);


const result = await proofOfPassportProvider.verify({
address: MOCK_ADDRESS
} as unknown as RequestPayload);

expect(result.valid).toBe(false);
});

});
Loading
Loading