Skip to content

Commit

Permalink
Add support of Auth V2 validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolezhniuk committed Oct 29, 2024
1 parent 826ad35 commit bfa246b
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 163 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@0xpolygonid/js-sdk",
"version": "1.21.0",
"version": "1.22.0",
"description": "SDK to work with Polygon ID",
"main": "dist/node/cjs/index.js",
"module": "dist/node/esm/index.js",
Expand Down
17 changes: 15 additions & 2 deletions src/circuits/auth-v2.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Hash, Proof } from '@iden3/js-merkletree';
import { Claim, Id } from '@iden3/js-iden3-core';
import { CircuitError, GISTProof, TreeState } from './models';
import { BaseConfig, getNodeAuxValue, prepareSiblingsStr } from './common';
import {
BaseConfig,
getNodeAuxValue,
IStateInfoPubSignals,
prepareSiblingsStr,
StatesInfo
} from './common';
import { Signature } from '@iden3/js-crypto';
import { byteDecoder, byteEncoder } from '../utils';

Expand Down Expand Up @@ -117,7 +123,7 @@ interface AuthV2CircuitInputs {
* @public
* @class AuthV2PubSignals
*/
export class AuthV2PubSignals {
export class AuthV2PubSignals implements IStateInfoPubSignals {
userID!: Id;
challenge!: bigint;
GISTRoot!: Hash;
Expand All @@ -143,4 +149,11 @@ export class AuthV2PubSignals {
this.GISTRoot = Hash.fromString(sVals[2]);
return this;
}

getStatesInfo(): StatesInfo {
return {
states: [],
gists: [{ id: this.userID, root: this.GISTRoot }]
};
}
}
34 changes: 26 additions & 8 deletions src/iden3comm/handlers/contract-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { FunctionSignatures, IOnChainZKPVerifier } from '../../storage';
import { Signer } from 'ethers';
import { processZeroKnowledgeProofRequests } from './common';
import { AbstractMessageHandler, IProtocolMessageHandler } from './message-handler';

import { prepareAuthV2ZeroKnowledgeResponse } from '../utils';
/**
* Interface that allows the processing of the contract request
*
Expand Down Expand Up @@ -65,6 +65,7 @@ export class ContractRequestHandler
implements IContractRequestHandler, IProtocolMessageHandler
{
private readonly _supportedCircuits = [
CircuitId.AuthV2,
CircuitId.AtomicQueryMTPV2OnChain,
CircuitId.AtomicQuerySigV2OnChain,
CircuitId.AtomicQueryV3OnChain
Expand Down Expand Up @@ -121,13 +122,30 @@ export class ContractRequestHandler
throw new Error(`Invalid chain id ${chain_id}`);
}
const verifierDid = message.from ? DID.parse(message.from) : undefined;
const zkpResponses = await processZeroKnowledgeProofRequests(
did,
message?.body?.scope,
verifierDid,
this._proofService,
{ ethSigner, challenge, supportedCircuits: this._supportedCircuits }
);

const { scope = [] } = message.body;

let zkpResponses: ZeroKnowledgeProofResponse[] = [];

if (scope.length) {
zkpResponses = await processZeroKnowledgeProofRequests(
did,
scope,
verifierDid,
this._proofService,
{
ethSigner,
challenge,
supportedCircuits: this._supportedCircuits
}
);
} else {
zkpResponses = await prepareAuthV2ZeroKnowledgeResponse(
await ctx.ethSigner.getAddress(),
did,
this._proofService
);
}

const methodId = message.body.transaction_data.method_id.replace('0x', '');
switch (methodId) {
Expand Down
67 changes: 67 additions & 0 deletions src/iden3comm/utils/contract-request.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { keccak256 } from 'ethers';
import { byteEncoder, hexToBytes, isEthereumIdentity } from '../../utils';
import { CircuitId } from '../../circuits';
import { Hex } from '@iden3/js-crypto';
import { DID } from '@iden3/js-iden3-core';
import { IProofService } from '../../proof';
import { ZeroKnowledgeProofResponse } from '../types';

/**
* Retrieves the AuthV2 request ID.
*
* @returns The AuthV2 request ID.
*/
export function getAuthV2RequestId(): number {
const circuitHash = keccak256(byteEncoder.encode(CircuitId.AuthV2));
const dataView = new DataView(Hex.decodeString(circuitHash.replace('0x', '')).buffer);
const id = dataView.getUint32(0);
return id;
}


/**
* Prepares the zero-knowledge proof response for the AuthV2 circuit.
* @param address - The address associated with the request.
* @param senderDid - The sender's decentralized identifier (DID).
* @param proofService - The proof service used to generate the proof.
* @returns A promise that resolves to an array of ZeroKnowledgeProofResponse objects.
*/
export async function prepareAuthV2ZeroKnowledgeResponse(
address: string,
senderDid: DID,
proofService: IProofService
): Promise<ZeroKnowledgeProofResponse[]> {
const circuitId = CircuitId.AuthV2;
const id = getAuthV2RequestId();

if (isEthereumIdentity(senderDid)) {
return [
{
circuitId,
id,
pub_signals: [],
proof: {
pi_a: [],
pi_b: [],
pi_c: [],
protocol: 'groth16'
}
}
];
}
const hash = Uint8Array.from([...hexToBytes(address), ...new Uint8Array(12)]).reverse();
const authInputs = await proofService.generateAuthV2Inputs(hash, senderDid, CircuitId.AuthV2);

const prover = proofService.getProver();

const { proof, pub_signals } = await prover.generate(authInputs, CircuitId.AuthV2);

return [
{
circuitId,
id,
pub_signals,
proof
}
];
}
1 change: 1 addition & 0 deletions src/iden3comm/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './envelope';
export * from './message';
export * from './did';
export * from './contract-request.utils';
11 changes: 11 additions & 0 deletions src/proof/proof-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ export interface IProofService {
query: ProofQuery,
opts?: { skipClaimRevocationCheck: boolean }
): Promise<{ cred: W3CCredential; revStatus: RevocationStatus | undefined }>;

/**
* Returns prover instance
* @returns {IZKProver}
*/
getProver(): IZKProver;
}
/**
* Proof service is an implementation of IProofService
Expand Down Expand Up @@ -200,6 +206,11 @@ export class ProofService implements IProofService {
);
}

/** {@inheritdoc IProofService.getProver} */
getProver(): IZKProver {
return this._prover;
}

/** {@inheritdoc IProofService.verifyProof} */
async verifyProof(zkp: ZKProof, circuitId: CircuitId): Promise<boolean> {
return this._prover.verify(zkp, circuitId);
Expand Down
Loading

0 comments on commit bfa246b

Please sign in to comment.