Skip to content

Commit 67d91e5

Browse files
fix checkStateExistenceForId (#218)
* fix checkStateExistenceForId
1 parent b3042ce commit 67d91e5

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

package-lock.json

+2-2
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.10.3",
3+
"version": "1.10.4",
44
"description": "SDK to work with Polygon ID",
55
"main": "dist/node/cjs/index.js",
66
"module": "dist/node/esm/index.js",

src/proof/verifiers/pub-signals-verifier.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,9 @@ export class PubSignalsVerifier {
602602
try {
603603
contractState = await this._stateStorage.getStateInfoByIdAndState(idBigInt, state);
604604
} catch (e) {
605-
if ((e as { errorArgs: string[] }).errorArgs[0] === 'State does not exist') {
605+
const stateNotExistErr = ((e as unknown as { errorArgs: string[] })?.errorArgs ?? [])[0];
606+
const errMsg = stateNotExistErr || (e as unknown as Error).message;
607+
if (errMsg === 'State does not exist') {
606608
if (isGenesis) {
607609
return {
608610
latest: true,
@@ -677,10 +679,7 @@ export class PubSignalsVerifier {
677679
}
678680

679681
private checkStateExistenceForId = async (userId: Id, userState: Hash): Promise<void> => {
680-
const { latest } = await this.resolve(userId, userState.bigInt());
681-
if (!latest) {
682-
throw userStateError;
683-
}
682+
await this.resolve(userId, userState.bigInt());
684683
};
685684

686685
private checkGlobalState = async (

tests/proofs/mtp.test.ts

+39-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from '../../src';
1111
import { BjjProvider, KMS, KmsKeyType } from '../../src/kms';
1212
import { InMemoryPrivateKeyStore } from '../../src/kms/store';
13-
import { IDataStorage, IStateStorage } from '../../src/storage/interfaces';
13+
import { ICircuitStorage, IDataStorage, IStateStorage } from '../../src/storage/interfaces';
1414
import { InMemoryDataSource, InMemoryMerkleTreeStorage } from '../../src/storage/memory';
1515
import { CredentialRequest, CredentialWallet } from '../../src/credentials';
1616
import { ProofService } from '../../src/proof';
@@ -20,7 +20,7 @@ import { EthStateStorage } from '../../src/storage/blockchain/state';
2020
import { RootInfo, StateProof } from '../../src/storage/entities/state';
2121
import path from 'path';
2222
import { CredentialStatusType, VerifiableConstants, W3CCredential } from '../../src/verifiable';
23-
import { ZeroKnowledgeProofRequest } from '../../src/iden3comm';
23+
import { ZeroKnowledgeProofRequest, ZeroKnowledgeProofResponse } from '../../src/iden3comm';
2424
import { expect } from 'chai';
2525
import { CredentialStatusResolverRegistry } from '../../src/credentials';
2626
import { RHSResolver } from '../../src/credentials';
@@ -32,6 +32,7 @@ describe('mtp proofs', () => {
3232

3333
let dataStorage: IDataStorage;
3434
let proofService: ProofService;
35+
let circuitStorage: ICircuitStorage;
3536

3637
const rhsUrl = process.env.RHS_URL as string;
3738
const walletKey = process.env.WALLET_KEY as string;
@@ -93,7 +94,7 @@ describe('mtp proofs', () => {
9394
states: mockStateStorage
9495
};
9596

96-
const circuitStorage = new FSCircuitStorage({
97+
circuitStorage = new FSCircuitStorage({
9798
dirname: path.join(__dirname, './testdata')
9899
});
99100

@@ -310,4 +311,39 @@ describe('mtp proofs', () => {
310311
it('mtpv3-merklized', async () => {
311312
await merklizedTest(CircuitId.AtomicQueryV3);
312313
});
314+
315+
it('should not throw when resolving not latest state', async () => {
316+
const mockStateStorage = {
317+
getStateInfoByIdAndState: async () => {
318+
return {
319+
id: 25198543381200665770805816046271594885604002445105767653616878167826895617n,
320+
state: 5224437024673068498206105743424598123651101873588696368477339341771571761791n,
321+
replacedByState: 0n,
322+
createdAtTimestamp: 1672245326n,
323+
replacedAtTimestamp: 1672246326n,
324+
createdAtBlock: 30258020n,
325+
replacedAtBlock: 0n
326+
};
327+
}
328+
} as unknown as IStateStorage;
329+
const proofService = new ProofService(idWallet, credWallet, circuitStorage, mockStateStorage);
330+
const response: ZeroKnowledgeProofResponse = JSON.parse(
331+
`{"id":1,"circuitId":"credentialAtomicQuerySigV2","proof":{"pi_a":["1692621919535462098029340422338985117387349922432058572912503289494740072544","5849832527522776520992910317111843161659287939749030678875104723725167741629","1"],"pi_b":[["9073804311318969142382194823200861430394532493054777280144376515679156840294","320345546718280141355625312977249941988595053000873620335373153762333347618"],["21818506300133624706104504788964095807930130277005378306774974876198233822873","20508916211207310005669939018224159176090237395847319407804660514445244746059"],["1","0"]],"pi_c":["75773990211509807568779994083842535776985171363939633486110559284258142402","21708643189390101987073995679050930953292947427942962697950732212904750632605","1"],"protocol":"groth16","curve":"bn128"},"pub_signals":["0","21575127216236248869702276246037557119007466180301957762196593786733007617","4487386332479489158003597844990487984925471813907462483907054425759564175341","1","25198543381200665770805816046271594885604002445105767653616878167826895617","1","4487386332479489158003597844990487984925471813907462483907054425759564175341","1712671029","198285726510688200335207273836123338699","1","0","3","1","99","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]}`
332+
);
333+
const query = {
334+
allowedIssuers: ['*'],
335+
type: 'KYCAgeCredential',
336+
context:
337+
'https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json-ld/kyc-nonmerklized.jsonld',
338+
credentialSubject: {
339+
documentType: {
340+
$eq: 99
341+
}
342+
}
343+
};
344+
const sender = 'did:iden3:polygon:amoy:x7Z95VkUuyo6mqraJw2VGwCfqTzdqhM1RVjRHzcpK';
345+
await expect(proofService.verifyZKPResponse(response, { query, sender })).to.be.rejectedWith(
346+
'issuer state is outdated'
347+
);
348+
});
313349
});

0 commit comments

Comments
 (0)