Skip to content

Commit

Permalink
Merge branch 'main' into feat/discovery-protocol-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Dec 18, 2024
2 parents 04cc202 + 812ce46 commit 83304ca
Show file tree
Hide file tree
Showing 10 changed files with 439 additions and 337 deletions.
2 changes: 1 addition & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
],
"spec": "tests/**/*.test.ts",
"require": "ts-node/register",
"timeout": "300000",
"timeout": "400000",
"maxDiffSize": "10000"
}
348 changes: 159 additions & 189 deletions package-lock.json

Large diffs are not rendered by default.

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 }]
};
}
}
13 changes: 10 additions & 3 deletions src/iden3comm/handlers/contract-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
BasicHandlerOptions,
IProtocolMessageHandler
} from './message-handler';

/**
* Interface that allows the processing of the contract request
*
Expand Down Expand Up @@ -69,6 +68,7 @@ export class ContractRequestHandler
implements IContractRequestHandler, IProtocolMessageHandler
{
private readonly _supportedCircuits = [
CircuitId.AuthV2,
CircuitId.AtomicQueryMTPV2OnChain,
CircuitId.AtomicQuerySigV2OnChain,
CircuitId.AtomicQueryV3OnChain
Expand Down Expand Up @@ -125,12 +125,19 @@ export class ContractRequestHandler
throw new Error(`Invalid chain id ${chain_id}`);
}
const verifierDid = message.from ? DID.parse(message.from) : undefined;

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

const zkpResponses = await processZeroKnowledgeProofRequests(
did,
message?.body?.scope,
scope,
verifierDid,
this._proofService,
{ ethSigner, challenge, supportedCircuits: this._supportedCircuits }
{
ethSigner,
challenge,
supportedCircuits: this._supportedCircuits
}
);

const methodId = message.body.transaction_data.method_id.replace('0x', '');
Expand Down
66 changes: 66 additions & 0 deletions src/iden3comm/utils/contract-request.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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';

/**
* @beta
* Retrieves the request ID from circuit string.
* CircuitId.AuthV2 - 940499666
* @returns The request ID.
*/
export function calculateRequestIdForCircuit(circuitId: CircuitId): number {
const circuitHash = keccak256(byteEncoder.encode(circuitId));
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.
* @beta
* @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;

// this is now hardcoded calculated value for 'authV2' that can be changed in the future.
const id = 940499666;

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 { proof, pub_signals } = await proofService.generateAuthV2Proof(hash, senderDid);

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,4 +1,5 @@
export * from './envelope';
export * from './message';
export * from './did';
export * from './contract-request.utils';
export * from './accept-profile';
17 changes: 17 additions & 0 deletions src/proof/proof-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ export interface IProofService {
*/
generateAuthV2Inputs(hash: Uint8Array, did: DID, circuitId: CircuitId): Promise<Uint8Array>;

/**
* generates auth inputs
*
* @param {Uint8Array} hash - challenge that will be signed
* @param {DID} did - identity that will generate a proof
* @returns `Promise<ZKProof>`
*/
generateAuthV2Proof(hash: Uint8Array, did: DID): Promise<ZKProof>;

/**
* state verification function
*
Expand Down Expand Up @@ -488,6 +497,14 @@ export class ProofService implements IProofService {
return authInputs.inputsMarshal();
}

/** {@inheritdoc IProofService.generateAuthV2Proof} */
async generateAuthV2Proof(challenge: Uint8Array, did: DID): Promise<ZKProof> {
const authInputs = await this.generateAuthV2Inputs(challenge, did, CircuitId.AuthV2);

const zkProof = await this._prover.generate(authInputs, CircuitId.AuthV2);
return zkProof;
}

async verifyState(
circuitId: string,
pubSignals: string[],
Expand Down
Loading

0 comments on commit 83304ca

Please sign in to comment.