Skip to content

Commit 823e455

Browse files
authored
Fix/auth response in submit (#314)
* refactor usage of auth proofs * fixes * 1.29.2 * prettier * simplification of redundant logic * fixes in tests * f
1 parent 6356bd5 commit 823e455

15 files changed

+198
-197
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

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

src/iden3comm/handlers/common.ts

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getRandomBytes, poseidon } from '@iden3/js-crypto';
22
import {
33
AcceptProfile,
44
AuthMethod,
5-
AuthProofResponse,
5+
AuthProof,
66
BasicMessage,
77
JsonDocumentObject,
88
JWSPackerParams,
@@ -11,14 +11,14 @@ import {
1111
ZeroKnowledgeProofRequest,
1212
ZeroKnowledgeProofResponse
1313
} from '../types';
14-
import { byteEncoder, bytesToHex, mergeObjects } from '../../utils';
14+
import { byteEncoder, mergeObjects } from '../../utils';
1515
import { RevocationStatus, W3CCredential } from '../../verifiable';
16-
import { BytesHelper, DID, getUnixTimestamp } from '@iden3/js-iden3-core';
16+
import { DID, getUnixTimestamp } from '@iden3/js-iden3-core';
1717
import { IProofService } from '../../proof';
1818
import { CircuitId } from '../../circuits';
1919
import { AcceptJwsAlgorithms, defaultAcceptProfile, MediaType } from '../constants';
2020
import { ethers, Signer } from 'ethers';
21-
import { packZkpProof, prepareZkpProof } from '../utils';
21+
import { packZkpProof, prepareZkpProof } from '../../storage/blockchain/common';
2222

2323
/**
2424
* Groups the ZeroKnowledgeProofRequest objects based on their groupId.
@@ -155,17 +155,13 @@ export const processProofAuth = async (
155155
opts: {
156156
supportedCircuits: CircuitId[];
157157
acceptProfile?: AcceptProfile;
158-
skipRevocation?: boolean;
159-
sender: string;
158+
senderAddress: string;
160159
zkpResponses: ZeroKnowledgeProofResponse[];
161160
}
162-
): Promise<{ authResponse: AuthProofResponse; authProof?: ZeroKnowledgeProofAuthResponse }> => {
161+
): Promise<{ authProof: AuthProof }> => {
163162
if (!opts.acceptProfile) {
164163
opts.acceptProfile = defaultAcceptProfile;
165164
}
166-
if (!opts.skipRevocation) {
167-
opts.skipRevocation = true;
168-
}
169165

170166
switch (opts.acceptProfile.env) {
171167
case MediaType.ZKPMessage:
@@ -177,52 +173,36 @@ export const processProofAuth = async (
177173
if (!opts.supportedCircuits.includes(circuitId as unknown as CircuitId)) {
178174
throw new Error(`Circuit ${circuitId} is not supported`);
179175
}
180-
if (!opts.sender) {
176+
if (!opts.senderAddress) {
181177
throw new Error('Sender address is not provided');
182178
}
183179
if (!opts.zkpResponses || opts.zkpResponses.length === 0) {
184180
throw new Error('ZKP responses are not provided');
185181
}
186-
const challengeAuth = calcChallengeAuthV2(opts.sender, opts.zkpResponses);
182+
const challengeAuth = calcChallengeAuthV2(opts.senderAddress, opts.zkpResponses);
187183

188184
const zkpRes: ZeroKnowledgeProofAuthResponse = await proofService.generateAuthProof(
189185
circuitId as unknown as CircuitId,
190186
to,
191-
{ challenge: challengeAuth, skipRevocation: opts.skipRevocation }
187+
{ challenge: challengeAuth }
192188
);
193-
194-
switch (circuitId as unknown as CircuitId) {
195-
case CircuitId.AuthV2: {
196-
const preparedZkpProof = prepareZkpProof(zkpRes.proof);
197-
const zkProofEncoded = packZkpProof(
198-
zkpRes.pub_signals,
199-
preparedZkpProof.a,
200-
preparedZkpProof.b,
201-
preparedZkpProof.c
202-
);
203-
204-
return {
205-
authResponse: {
206-
authMethod: AuthMethod.AUTHV2,
207-
proof: zkProofEncoded
208-
},
209-
authProof: zkpRes
210-
};
189+
return {
190+
authProof: {
191+
authMethod: AuthMethod.AUTHV2,
192+
zkp: zkpRes
211193
}
212-
}
194+
};
213195
}
214196
throw new Error(`Auth method is not supported`);
215197
case MediaType.SignedMessage:
216198
if (!opts.acceptProfile.alg || opts.acceptProfile.alg.length === 0) {
217199
throw new Error('Algorithm not specified');
218200
}
219201
if (opts.acceptProfile.alg[0] === AcceptJwsAlgorithms.ES256KR) {
220-
const ethIdProof = packEthIdentityProof(to);
221-
222202
return {
223-
authResponse: {
203+
authProof: {
224204
authMethod: AuthMethod.ETH_IDENTITY,
225-
proof: ethIdProof
205+
userDid: to
226206
}
227207
};
228208
}
@@ -278,12 +258,12 @@ export const processProofResponse = (zkProof: ZeroKnowledgeProofResponse) => {
278258

279259
/**
280260
* Calculates the challenge authentication V2 value.
281-
* @param sender - The address of the sender.
261+
* @param senderAddress - The address of the sender.
282262
* @param zkpResponses - An array of ZeroKnowledgeProofResponse objects.
283263
* @returns A bigint representing the challenge authentication value.
284264
*/
285265
export const calcChallengeAuthV2 = (
286-
sender: string,
266+
senderAddress: string,
287267
zkpResponses: ZeroKnowledgeProofResponse[]
288268
): bigint => {
289269
const responses = zkpResponses.map((zkpResponse) => {
@@ -300,7 +280,7 @@ export const calcChallengeAuthV2 = (
300280
ethers.keccak256(
301281
new ethers.AbiCoder().encode(
302282
['address', '(uint256 requestId,bytes proof,bytes metadata)[]'],
303-
[sender, responses]
283+
[senderAddress, responses]
304284
)
305285
)
306286
) & BigInt('0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')
@@ -331,12 +311,3 @@ export const verifyExpiresTime = (message: BasicMessage) => {
331311
throw new Error('Message expired');
332312
}
333313
};
334-
335-
/**
336-
* Packs an Ethereum identity proof from a Decentralized Identifier (DID).
337-
* @param did - Decentralized Identifier (DID) to pack.
338-
* @returns A hexadecimal string representing the packed DID identity proof.
339-
*/
340-
export const packEthIdentityProof = (did: DID): string => {
341-
return `0x${bytesToHex(BytesHelper.intToBytes(DID.idFromDID(did).bigInt()))}`;
342-
};

src/iden3comm/handlers/contract-request.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
ZeroKnowledgeProofResponse
1010
} from '../types';
1111
import { ContractInvokeRequest, ContractInvokeResponse } from '../types/protocol/contract-request';
12-
import { DID, ChainIds, getUnixTimestamp } from '@iden3/js-iden3-core';
12+
import { DID, ChainIds, getUnixTimestamp, BytesHelper } from '@iden3/js-iden3-core';
1313
import { FunctionSignatures, IOnChainZKPVerifier } from '../../storage';
1414
import { Signer } from 'ethers';
1515
import { processProofAuth, processZeroKnowledgeProofRequests, verifyExpiresTime } from './common';
@@ -19,6 +19,7 @@ import {
1919
IProtocolMessageHandler
2020
} from './message-handler';
2121
import { parseAcceptProfile } from '../utils';
22+
import { hexToBytes } from '../../utils';
2223

2324
/**
2425
* Interface that allows the processing of the contract request
@@ -149,7 +150,7 @@ export class ContractRequestHandler
149150
this._proofService,
150151
{
151152
ethSigner,
152-
challenge,
153+
challenge: challenge ?? BytesHelper.bytesToInt(hexToBytes(await ethSigner.getAddress())),
153154
supportedCircuits: this._supportedCircuits
154155
}
155156
);
@@ -199,18 +200,16 @@ export class ContractRequestHandler
199200

200201
const identifier = DID.parse(message.to);
201202

202-
const { authResponse, authProof } = await processProofAuth(identifier, this._proofService, {
203+
const { authProof } = await processProofAuth(identifier, this._proofService, {
203204
supportedCircuits: this._supportedCircuits,
204205
acceptProfile,
205-
skipRevocation: true,
206-
sender: await ethSigner.getAddress(),
206+
senderAddress: await ethSigner.getAddress(),
207207
zkpResponses: zkpResponses
208208
});
209209

210210
return this._zkpVerifier.submitResponse(
211211
ethSigner,
212212
message.body.transaction_data,
213-
authResponse,
214213
zkpResponses,
215214
authProof
216215
);
@@ -298,8 +297,8 @@ export class ContractRequestHandler
298297
}
299298
contractInvokeResponse.body = {
300299
...contractInvokeResponse.body,
301-
crossChainProofs: zkpResponses.crossChainProofs ?? [],
302-
authProofs: zkpResponses.authProofs ?? []
300+
crossChainProof: zkpResponses.crossChainProof,
301+
authProof: zkpResponses.authProof
303302
};
304303
}
305304
return contractInvokeResponse;

src/iden3comm/types/protocol/auth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
VerificationMethod as DidResolverVerificationMethod
99
} from 'did-resolver';
1010
import { RootInfo, StateInfo } from '../../../storage';
11-
import { AuthProofResponse } from './contract-request';
11+
import { AuthProof, CrossChainProof } from './contract-request';
1212

1313
/** AuthorizationResponseMessage is struct the represents iden3message authorization response */
1414
export type AuthorizationResponseMessage = BasicMessage & {
@@ -66,8 +66,8 @@ export type ZeroKnowledgeProofQuery = {
6666

6767
export type ZeroKnowledgeInvokeResponse = {
6868
responses: ZeroKnowledgeProofResponse[];
69-
crossChainProofs?: string[];
70-
authProofs?: AuthProofResponse[];
69+
crossChainProof?: CrossChainProof;
70+
authProof?: AuthProof;
7171
};
7272

7373
/** ZeroKnowledgeProofResponse represents structure of zkp response */

src/iden3comm/types/protocol/contract-request.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import { GlobalStateUpdate, IdentityStateUpdate } from '../../../storage/entities/state';
12
import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
23
import { BasicMessage } from '../packer';
3-
import { DIDDocument, ZeroKnowledgeProofRequest, ZeroKnowledgeProofResponse } from './auth';
4+
import {
5+
DIDDocument,
6+
ZeroKnowledgeProofAuthResponse,
7+
ZeroKnowledgeProofRequest,
8+
ZeroKnowledgeProofResponse
9+
} from './auth';
10+
import { DID } from '@iden3/js-iden3-core';
411

512
/** ContractInvokeRequest represents structure of contract invoke request object */
613
export type ContractInvokeRequest = BasicMessage & {
@@ -28,8 +35,8 @@ export type ContractInvokeResponseBody = {
2835
scope: Array<OnChainZeroKnowledgeProofResponse>;
2936
transaction_data: ContractInvokeTransactionData;
3037
did_doc?: DIDDocument;
31-
crossChainProofs?: string[];
32-
authProofs?: AuthProofResponse[];
38+
crossChainProof?: CrossChainProof;
39+
authProof?: AuthProof;
3340
};
3441

3542
/** OnChainZeroKnowledgeProofResponse represents structure of onchain zero knowledge proof response */
@@ -45,10 +52,21 @@ export type ContractInvokeTransactionData = {
4552
network?: string;
4653
};
4754

55+
export type AuthProofEthIdentity = {
56+
userDid: DID;
57+
};
58+
export type AuthProofZKP = {
59+
zkp: ZeroKnowledgeProofAuthResponse;
60+
};
61+
4862
/** AuthProofResponse represents structure of zkp response */
49-
export type AuthProofResponse = {
63+
export type AuthProof = {
5064
authMethod: AuthMethod;
51-
proof: string;
65+
} & (AuthProofEthIdentity | AuthProofZKP);
66+
67+
export type CrossChainProof = {
68+
globalStateProofs: GlobalStateUpdate[];
69+
identityStateProofs: IdentityStateUpdate[];
5270
};
5371

5472
export enum AuthMethod {

src/iden3comm/utils/contract-request.utils.ts

Lines changed: 0 additions & 85 deletions
This file was deleted.

src/iden3comm/utils/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from './envelope';
22
export * from './message';
33
export * from './did';
4-
export * from './contract-request.utils';
54
export * from './accept-profile';

0 commit comments

Comments
 (0)