Skip to content

Commit 812ce46

Browse files
Kolezhniukvmidyllicvolodymyr-basiuk
authored
Add support of Auth V2 validator (#281)
* Add support of Auth V2 validator * Fix linters * Fix comments * Remove process auth validator * upd package json * format * remove IProver. Add method for generation Auth v2 proof * fix * fixes * fix unit tests --------- Co-authored-by: vmidyllic <[email protected]> Co-authored-by: vbasiuk <[email protected]>
1 parent b7eb85e commit 812ce46

12 files changed

+446
-344
lines changed

.mocharc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
],
55
"spec": "tests/**/*.test.ts",
66
"require": "ts-node/register",
7-
"timeout": "300000",
7+
"timeout": "400000",
88
"maxDiffSize": "10000"
99
}

package-lock.json

+161-191
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@0xpolygonid/js-sdk",
3-
"version": "1.25.0",
3+
"version": "1.25.1",
44
"description": "SDK to work with Polygon ID",
55
"main": "dist/node/cjs/index.js",
66
"module": "dist/node/esm/index.js",

src/circuits/auth-v2.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { Hash, Proof } from '@iden3/js-merkletree';
22
import { Claim, Id } from '@iden3/js-iden3-core';
33
import { CircuitError, GISTProof, TreeState } from './models';
4-
import { BaseConfig, getNodeAuxValue, prepareSiblingsStr } from './common';
4+
import {
5+
BaseConfig,
6+
getNodeAuxValue,
7+
IStateInfoPubSignals,
8+
prepareSiblingsStr,
9+
StatesInfo
10+
} from './common';
511
import { Signature } from '@iden3/js-crypto';
612
import { byteDecoder, byteEncoder } from '../utils';
713

@@ -117,7 +123,7 @@ interface AuthV2CircuitInputs {
117123
* @public
118124
* @class AuthV2PubSignals
119125
*/
120-
export class AuthV2PubSignals {
126+
export class AuthV2PubSignals implements IStateInfoPubSignals {
121127
userID!: Id;
122128
challenge!: bigint;
123129
GISTRoot!: Hash;
@@ -143,4 +149,11 @@ export class AuthV2PubSignals {
143149
this.GISTRoot = Hash.fromString(sVals[2]);
144150
return this;
145151
}
152+
153+
getStatesInfo(): StatesInfo {
154+
return {
155+
states: [],
156+
gists: [{ id: this.userID, root: this.GISTRoot }]
157+
};
158+
}
146159
}

src/iden3comm/handlers/contract-request.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
BasicHandlerOptions,
1313
IProtocolMessageHandler
1414
} from './message-handler';
15-
1615
/**
1716
* Interface that allows the processing of the contract request
1817
*
@@ -69,6 +68,7 @@ export class ContractRequestHandler
6968
implements IContractRequestHandler, IProtocolMessageHandler
7069
{
7170
private readonly _supportedCircuits = [
71+
CircuitId.AuthV2,
7272
CircuitId.AtomicQueryMTPV2OnChain,
7373
CircuitId.AtomicQuerySigV2OnChain,
7474
CircuitId.AtomicQueryV3OnChain
@@ -125,12 +125,19 @@ export class ContractRequestHandler
125125
throw new Error(`Invalid chain id ${chain_id}`);
126126
}
127127
const verifierDid = message.from ? DID.parse(message.from) : undefined;
128+
129+
const { scope = [] } = message.body;
130+
128131
const zkpResponses = await processZeroKnowledgeProofRequests(
129132
did,
130-
message?.body?.scope,
133+
scope,
131134
verifierDid,
132135
this._proofService,
133-
{ ethSigner, challenge, supportedCircuits: this._supportedCircuits }
136+
{
137+
ethSigner,
138+
challenge,
139+
supportedCircuits: this._supportedCircuits
140+
}
134141
);
135142

136143
const methodId = message.body.transaction_data.method_id.replace('0x', '');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { keccak256 } from 'ethers';
2+
import { byteEncoder, hexToBytes, isEthereumIdentity } from '../../utils';
3+
import { CircuitId } from '../../circuits';
4+
import { Hex } from '@iden3/js-crypto';
5+
import { DID } from '@iden3/js-iden3-core';
6+
import { IProofService } from '../../proof';
7+
import { ZeroKnowledgeProofResponse } from '../types';
8+
9+
/**
10+
* @beta
11+
* Retrieves the request ID from circuit string.
12+
* CircuitId.AuthV2 - 940499666
13+
* @returns The request ID.
14+
*/
15+
export function calculateRequestIdForCircuit(circuitId: CircuitId): number {
16+
const circuitHash = keccak256(byteEncoder.encode(circuitId));
17+
const dataView = new DataView(Hex.decodeString(circuitHash.replace('0x', '')).buffer);
18+
const id = dataView.getUint32(0);
19+
return id;
20+
}
21+
22+
/**
23+
* Prepares the zero-knowledge proof response for the AuthV2 circuit.
24+
* @beta
25+
* @param address - The address associated with the request.
26+
* @param senderDid - The sender's decentralized identifier (DID).
27+
* @param proofService - The proof service used to generate the proof.
28+
* @returns A promise that resolves to an array of ZeroKnowledgeProofResponse objects.
29+
*/
30+
export async function prepareAuthV2ZeroKnowledgeResponse(
31+
address: string,
32+
senderDid: DID,
33+
proofService: IProofService
34+
): Promise<ZeroKnowledgeProofResponse[]> {
35+
const circuitId = CircuitId.AuthV2;
36+
37+
// this is now hardcoded calculated value for 'authV2' that can be changed in the future.
38+
const id = 940499666;
39+
40+
if (isEthereumIdentity(senderDid)) {
41+
return [
42+
{
43+
circuitId,
44+
id,
45+
pub_signals: [],
46+
proof: {
47+
pi_a: [],
48+
pi_b: [],
49+
pi_c: [],
50+
protocol: 'groth16'
51+
}
52+
}
53+
];
54+
}
55+
const hash = Uint8Array.from([...hexToBytes(address), ...new Uint8Array(12)]).reverse();
56+
const { proof, pub_signals } = await proofService.generateAuthV2Proof(hash, senderDid);
57+
58+
return [
59+
{
60+
circuitId,
61+
id,
62+
pub_signals,
63+
proof
64+
}
65+
];
66+
}

src/iden3comm/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './envelope';
22
export * from './message';
33
export * from './did';
4+
export * from './contract-request.utils';
45
export * from './accept-profile';

src/proof/proof-service.ts

+17
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ export interface IProofService {
131131
*/
132132
generateAuthV2Inputs(hash: Uint8Array, did: DID, circuitId: CircuitId): Promise<Uint8Array>;
133133

134+
/**
135+
* generates auth inputs
136+
*
137+
* @param {Uint8Array} hash - challenge that will be signed
138+
* @param {DID} did - identity that will generate a proof
139+
* @returns `Promise<ZKProof>`
140+
*/
141+
generateAuthV2Proof(hash: Uint8Array, did: DID): Promise<ZKProof>;
142+
134143
/**
135144
* state verification function
136145
*
@@ -488,6 +497,14 @@ export class ProofService implements IProofService {
488497
return authInputs.inputsMarshal();
489498
}
490499

500+
/** {@inheritdoc IProofService.generateAuthV2Proof} */
501+
async generateAuthV2Proof(challenge: Uint8Array, did: DID): Promise<ZKProof> {
502+
const authInputs = await this.generateAuthV2Inputs(challenge, did, CircuitId.AuthV2);
503+
504+
const zkProof = await this._prover.generate(authInputs, CircuitId.AuthV2);
505+
return zkProof;
506+
}
507+
491508
async verifyState(
492509
circuitId: string,
493510
pubSignals: string[],

0 commit comments

Comments
 (0)