mirror of
https://github.com/selfxyz/self.git
synced 2026-04-05 03:00:53 -04:00
remove previous proof verifier (#176)
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
import dotenv from "dotenv";
|
||||
import { getContractInstance } from "./getContracts";
|
||||
import { getChain } from "./chains";
|
||||
import { ProofVerifier } from "./proofVerifier";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const network = process.env.NETWORK as string;
|
||||
const chain = getChain(network);
|
||||
|
||||
export class PassportVerifier extends ProofVerifier {
|
||||
constructor(
|
||||
ofacEnabled: boolean,
|
||||
olderThanEnabled: boolean,
|
||||
excludedCountriesEnabled: boolean,
|
||||
ofacRoot: string,
|
||||
olderThan: string,
|
||||
excludedCountries: string[],
|
||||
identityCommitmentRoot: string,
|
||||
)
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
import { CIRCUIT_CONSTANTS } from "../../../../../contracts/test/utils/constants";
|
||||
import {
|
||||
Formatter,
|
||||
CircuitAttributeHandler
|
||||
} from "../../../../../contracts/test/utils/formatter";
|
||||
import {
|
||||
PublicSignals,
|
||||
Groth16Proof,
|
||||
groth16
|
||||
} from "snarkjs";
|
||||
|
||||
export class ProofVerifier {
|
||||
protected ofacEnabled: boolean;
|
||||
protected olderThanEnabled: boolean;
|
||||
protected excludedCountriesEnabled: boolean;
|
||||
|
||||
protected ofacRoot: string;
|
||||
protected olderThan: string;
|
||||
protected excludedCountries: string[];
|
||||
protected identityCommitmentRoot: string;
|
||||
|
||||
protected vcAndDiscloseCircuitVKey: any;
|
||||
|
||||
constructor(
|
||||
ofacEnabled: boolean,
|
||||
olderThanEnabled: boolean,
|
||||
excludedCountriesEnabled: boolean,
|
||||
ofacRoot: string,
|
||||
olderThan: string,
|
||||
excludedCountries: string[],
|
||||
identityCommitmentRoot: string,
|
||||
vcAndDiscloseCircuitVKey: any
|
||||
) {
|
||||
this.ofacEnabled = ofacEnabled;
|
||||
this.olderThanEnabled = olderThanEnabled;
|
||||
this.excludedCountriesEnabled = excludedCountriesEnabled;
|
||||
this.ofacRoot = ofacRoot;
|
||||
this.olderThan = olderThan;
|
||||
this.excludedCountries = excludedCountries;
|
||||
this.identityCommitmentRoot = identityCommitmentRoot;
|
||||
this.vcAndDiscloseCircuitVKey = vcAndDiscloseCircuitVKey;
|
||||
}
|
||||
|
||||
public async verifyVcAndDiscloseProof(
|
||||
proof: Groth16Proof,
|
||||
publicSignals: PublicSignals,
|
||||
) {
|
||||
|
||||
if (publicSignals[CIRCUIT_CONSTANTS.VC_AND_DISCLOSE_MERKLE_ROOT_INDEX] !== this.identityCommitmentRoot) {
|
||||
throw new Error("Invalid identity commitment root");
|
||||
}
|
||||
|
||||
let dateNum = new Array(6).fill(0);
|
||||
for (let i = 0; i < 6; i++) {
|
||||
dateNum[i] = publicSignals[CIRCUIT_CONSTANTS.VC_AND_DISCLOSE_CURRENT_DATE_INDEX + i];
|
||||
}
|
||||
|
||||
let currentTimestamp = Formatter.proofDateToUnixTimestamp(dateNum);
|
||||
if(
|
||||
currentTimestamp < getStartOfDayTimestamp() - 86400 + 1 ||
|
||||
currentTimestamp > getStartOfDayTimestamp() + 86400 - 1
|
||||
) {
|
||||
throw new Error("Invalid current date");
|
||||
}
|
||||
|
||||
let revealedDataPacked: [bigint, bigint, bigint] = [0n, 0n, 0n];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
revealedDataPacked[i] = publicSignals[CIRCUIT_CONSTANTS.VC_AND_DISCLOSE_REVEALED_DATA_PACKED_INDEX + i];
|
||||
}
|
||||
|
||||
if (this.ofacEnabled) {
|
||||
if (revealedDataPacked[2] !== 1n) {
|
||||
throw new Error("Invalid OFAC");
|
||||
}
|
||||
if (this.ofacRoot != publicSignals[CIRCUIT_CONSTANTS.VC_AND_DISCLOSE_SMT_ROOT_INDEX]) {
|
||||
throw new Error("Invalid OFAC root");
|
||||
}
|
||||
}
|
||||
|
||||
// extract the oler than value
|
||||
if (this.olderThanEnabled) {
|
||||
const bytes = Formatter.fieldElementsToBytes(revealedDataPacked);
|
||||
if (!CircuitAttributeHandler.compareOlderThan(bytes, Number(this.olderThan))) {
|
||||
throw new Error("Invalid older than");
|
||||
}
|
||||
}
|
||||
|
||||
// extract the excluded countries value
|
||||
if (this.excludedCountriesEnabled) {
|
||||
const forbiddenCountries = Formatter.extractForbiddenCountriesFromPacked(revealedDataPacked[CIRCUIT_CONSTANTS.VC_AND_DISCLOSE_FORBIDDEN_COUNTRIES_LIST_PACKED_INDEX]);
|
||||
for (let i = 0; i < this.excludedCountries.length; i++) {
|
||||
for (let j = 0; j < forbiddenCountries.length; j++) {
|
||||
if (forbiddenCountries[j] === this.excludedCountries[i]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
throw new Error("Invalid excluded countries");
|
||||
}
|
||||
}
|
||||
|
||||
const isValid = await groth16.verify(this.vcAndDiscloseCircuitVKey, publicSignals, proof);
|
||||
if (!isValid) {
|
||||
throw new Error("Invalid VC and Disclose proof");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getStartOfDayTimestamp() {
|
||||
const now = new Date();
|
||||
now.setHours(0, 0, 0, 0);
|
||||
return now.getTime();
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import Elysia, { t } from 'elysia';
|
||||
import { ProofVerifier } from '../../contracts/application/proofVerifier';
|
||||
import { RegistryContract } from '../../contracts/application/registryContract';
|
||||
import { HubContract } from '../application/hubContract';
|
||||
import { getChain } from '../../contracts/application/chains';
|
||||
@@ -381,67 +380,6 @@ export const ContractsController = new Elysia()
|
||||
},
|
||||
}
|
||||
)
|
||||
.post(
|
||||
'verify-vc-and-disclose-proof',
|
||||
async (request) => {
|
||||
try {
|
||||
const registryContract = new RegistryContract(
|
||||
getChain(process.env.NETWORK as string),
|
||||
process.env.PRIVATE_KEY as `0x${string}`,
|
||||
process.env.RPC_URL as string
|
||||
);
|
||||
|
||||
const identityCommitmentRoot = await registryContract.getIdentityCommitmentMerkleRoot();
|
||||
const ofacRoot = await registryContract.getOfacRoot();
|
||||
|
||||
const { proof, publicSignals } = request.body;
|
||||
|
||||
const proofVerifier = new ProofVerifier(
|
||||
process.env.OFAC_ENABLED === "true",
|
||||
process.env.OLDER_THAN_ENABLED === "true",
|
||||
process.env.EXCLUDED_COUNTRIES_ENABLED === "true",
|
||||
ofacRoot,
|
||||
process.env.OLDER_THAN || "18",
|
||||
(process.env.EXCLUDED_COUNTRIES || "USA,IRN,CHN").split(','),
|
||||
identityCommitmentRoot,
|
||||
{}
|
||||
);
|
||||
|
||||
await proofVerifier.verifyVcAndDiscloseProof(proof, publicSignals);
|
||||
|
||||
return {
|
||||
status: "success",
|
||||
data: ["Valid VC and disclose proof"],
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
status: "error",
|
||||
message: error instanceof Error ? error.message : "Unknown error",
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
proof: t.Any(),
|
||||
publicSignals: t.Any(),
|
||||
}),
|
||||
response: {
|
||||
200: t.Object({
|
||||
status: t.String(),
|
||||
data: t.Array(t.String()),
|
||||
}),
|
||||
500: t.Object({
|
||||
status: t.String(),
|
||||
message: t.String(),
|
||||
}),
|
||||
},
|
||||
detail: {
|
||||
tags: ['Contracts'],
|
||||
summary: 'Verify a VC and disclose a proof',
|
||||
description: 'Verify a VC and disclose a proof',
|
||||
},
|
||||
},
|
||||
)
|
||||
.get(
|
||||
'dsc-commitment-tree',
|
||||
async () => {
|
||||
|
||||
Reference in New Issue
Block a user