Skip to content
Merged
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
13 changes: 3 additions & 10 deletions app/src/stores/protocolStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
IDENTITY_TREE_URL_STAGING,
IDENTITY_TREE_URL_STAGING_ID_CARD,
} from '@selfxyz/common/constants';
import type { OfacTree } from '@selfxyz/common/utils/types';

import { fetchOfacTrees } from '@/utils/ofac';

Expand All @@ -29,11 +30,7 @@ interface ProtocolState {
deployed_circuits: any;
circuits_dns_mapping: any;
alternative_csca: Record<string, string>;
ofac_trees: {
passportNoAndNationality: any;
nameAndDob: any;
nameAndYob: any;
} | null;
ofac_trees: OfacTree | null;
fetch_deployed_circuits: (environment: 'prod' | 'stg') => Promise<void>;
fetch_circuits_dns_mapping: (environment: 'prod' | 'stg') => Promise<void>;
fetch_csca_tree: (environment: 'prod' | 'stg') => Promise<void>;
Expand All @@ -53,11 +50,7 @@ interface ProtocolState {
deployed_circuits: any;
circuits_dns_mapping: any;
alternative_csca: Record<string, string>;
ofac_trees: {
passportNoAndNationality: any;
nameAndDob: any;
nameAndYob: any;
} | null;
ofac_trees: OfacTree | null;
fetch_deployed_circuits: (environment: 'prod' | 'stg') => Promise<void>;
fetch_circuits_dns_mapping: (environment: 'prod' | 'stg') => Promise<void>;
fetch_csca_tree: (environment: 'prod' | 'stg') => Promise<void>;
Expand Down
9 changes: 2 additions & 7 deletions app/src/utils/ofac.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// SPDX-License-Identifier: BUSL-1.1; Copyright (c) 2025 Social Connect Labs, Inc.; Licensed under BUSL-1.1 (see LICENSE); Apache-2.0 from 2029-06-11

import { TREE_URL, TREE_URL_STAGING } from '@selfxyz/common/constants';

export interface OfacTrees {
passportNoAndNationality: any;
nameAndDob: any;
nameAndYob: any;
}
import type { OfacTree } from '@selfxyz/common/utils/types';

export type OfacVariant = 'passport' | 'id_card';

Expand All @@ -31,7 +26,7 @@ const fetchTree = async (url: string): Promise<any> => {
export const fetchOfacTrees = async (
environment: 'prod' | 'stg',
variant: OfacVariant = 'passport',
): Promise<OfacTrees> => {
): Promise<OfacTree> => {
const baseUrl = environment === 'prod' ? TREE_URL : TREE_URL_STAGING;

const ppNoNatUrl = `${baseUrl}/ofac/passport-no-nationality`;
Expand Down
169 changes: 23 additions & 146 deletions app/src/utils/proving/provingInputs.ts
Original file line number Diff line number Diff line change
@@ -1,163 +1,40 @@
// SPDX-License-Identifier: BUSL-1.1; Copyright (c) 2025 Social Connect Labs, Inc.; Licensed under BUSL-1.1 (see LICENSE); Apache-2.0 from 2029-06-11

import { poseidon2 } from 'poseidon-lite';
import { LeanIMT } from '@openpassport/zk-kit-lean-imt';
import { SMT } from '@openpassport/zk-kit-smt';

import {
attributeToPosition,
attributeToPosition_ID,
DEFAULT_MAJORITY,
ID_CARD_ATTESTATION_ID,
PASSPORT_ATTESTATION_ID,
} from '@selfxyz/common/constants';
import type { DocumentCategory, PassportData } from '@selfxyz/common/types';
import type { SelfApp, SelfAppDisclosureConfig } from '@selfxyz/common/utils';
import type { SelfApp } from '@selfxyz/common/utils';
import {
calculateUserIdentifierHash,
generateCircuitInputsDSC,
generateCircuitInputsRegister,
generateCircuitInputsVCandDisclose,
getCircuitNameFromPassportData,
hashEndpointWithScope,
} from '@selfxyz/common/utils';
generateTEEInputsDiscloseStateless,
generateTEEInputsDSC,
generateTEEInputsRegister,
} from '@selfxyz/common/utils/circuits/registerInputs';

import { useProtocolStore } from '@/stores/protocolStore';

export function generateTEEInputsDSC(
passportData: PassportData,
cscaTree: string[][],
env: 'prod' | 'stg',
) {
const inputs = generateCircuitInputsDSC(passportData, cscaTree);
const circuitName = getCircuitNameFromPassportData(passportData, 'dsc');
const endpointType = env === 'stg' ? 'staging_celo' : 'celo';
const endpoint = 'https://self.xyz';
return { inputs, circuitName, endpointType, endpoint };
}

export {
generateCircuitInputsRegister,
generateTEEInputsDSC,
generateTEEInputsRegister,
};
export function generateTEEInputsDisclose(
secret: string,
passportData: PassportData,
selfApp: SelfApp,
) {
const { scope, disclosures, endpoint, userId, userDefinedData, chainID } =
selfApp;
const userIdentifierHash = calculateUserIdentifierHash(
chainID,
userId,
userDefinedData,
);
const scope_hash = hashEndpointWithScope(endpoint, scope);
const document: DocumentCategory = passportData.documentCategory;

const selector_dg1 = getSelectorDg1(document, disclosures);

const majority = disclosures.minimumAge
? disclosures.minimumAge.toString()
: DEFAULT_MAJORITY;
const selector_older_than = disclosures.minimumAge ? '1' : '0';

const selector_ofac = disclosures.ofac ? 1 : 0;

const ofac_trees = useProtocolStore.getState()[document].ofac_trees;
if (!ofac_trees) {
throw new Error('OFAC trees not loaded');
}
let passportNoAndNationalitySMT: SMT | null = null;
const nameAndDobSMT = new SMT(poseidon2, true);
const nameAndYobSMT = new SMT(poseidon2, true);
if (document === 'passport') {
passportNoAndNationalitySMT = new SMT(poseidon2, true);
passportNoAndNationalitySMT.import(ofac_trees.passportNoAndNationality);
}
nameAndDobSMT.import(ofac_trees.nameAndDob);
nameAndYobSMT.import(ofac_trees.nameAndYob);

const serialized_tree = useProtocolStore.getState()[document].commitment_tree;
const tree = LeanIMT.import((a, b) => poseidon2([a, b]), serialized_tree);
const inputs = generateCircuitInputsVCandDisclose(
return generateTEEInputsDiscloseStateless(
secret,
document === 'passport' ? PASSPORT_ATTESTATION_ID : ID_CARD_ATTESTATION_ID,
passportData,
scope_hash,
selector_dg1,
selector_older_than,
tree,
majority,
passportNoAndNationalitySMT,
nameAndDobSMT,
nameAndYobSMT,
selector_ofac,
disclosures.excludedCountries ?? [],
userIdentifierHash.toString(),
selfApp,
(document: DocumentCategory, tree) => {
const protocolStore = useProtocolStore.getState();
switch (tree) {
case 'ofac':
return protocolStore[document].ofac_trees;
case 'commitment':
return protocolStore[document].commitment_tree;
default:
throw new Error('Unknown tree type');
}
},
);
return {
inputs,
circuitName:
passportData.documentCategory === 'passport'
? 'vc_and_disclose'
: 'vc_and_disclose_id',
endpointType: selfApp.endpointType,
endpoint: selfApp.endpoint,
};
}

export function generateTEEInputsRegister(
secret: string,
passportData: PassportData,
dscTree: string,
env: 'prod' | 'stg',
) {
const inputs = generateCircuitInputsRegister(secret, passportData, dscTree);
const circuitName = getCircuitNameFromPassportData(passportData, 'register');
const endpointType = env === 'stg' ? 'staging_celo' : 'celo';
const endpoint = 'https://self.xyz';
return { inputs, circuitName, endpointType, endpoint };
}

/*** DISCLOSURE ***/

function getSelectorDg1(
document: DocumentCategory,
disclosures: SelfAppDisclosureConfig,
) {
switch (document) {
case 'passport':
return getSelectorDg1Passport(disclosures);
case 'id_card':
return getSelectorDg1IdCard(disclosures);
}
}

function getSelectorDg1Passport(disclosures: SelfAppDisclosureConfig) {
const selector_dg1 = Array(88).fill('0');
Object.entries(disclosures).forEach(([attribute, reveal]) => {
if (['ofac', 'excludedCountries', 'minimumAge'].includes(attribute)) {
return;
}
if (reveal) {
const [start, end] =
attributeToPosition[attribute as keyof typeof attributeToPosition];
selector_dg1.fill('1', start, end + 1);
}
});
return selector_dg1;
}

function getSelectorDg1IdCard(disclosures: SelfAppDisclosureConfig) {
const selector_dg1 = Array(90).fill('0');
Object.entries(disclosures).forEach(([attribute, reveal]) => {
if (['ofac', 'excludedCountries', 'minimumAge'].includes(attribute)) {
return;
}
if (reveal) {
const [start, end] =
attributeToPosition_ID[
attribute as keyof typeof attributeToPosition_ID
];
selector_dg1.fill('1', start, end + 1);
}
});
return selector_dg1;
}
Loading
Loading