From f96d68893016d02aef551ef04ddaa11be4eec9b8 Mon Sep 17 00:00:00 2001 From: Dimasik Kolezhniuk Date: Fri, 5 Apr 2024 18:30:14 +0200 Subject: [PATCH] Fix zkp unpack (#214) * Fix zkp unpack * Patch version --- package-lock.json | 10 +-- package.json | 2 +- src/proof/proof-service.ts | 5 +- src/proof/verifiers/pub-signals-verifier.ts | 15 ++-- src/storage/blockchain/state.ts | 4 +- src/storage/interfaces/state.ts | 10 ++- tests/handlers/auth.test.ts | 7 +- tests/helpers.ts | 85 ++++++++++++++++++--- 8 files changed, 106 insertions(+), 32 deletions(-) diff --git a/package-lock.json b/package-lock.json index d0583254..18637d10 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@0xpolygonid/js-sdk", - "version": "1.10.1", + "version": "1.10.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@0xpolygonid/js-sdk", - "version": "1.10.1", + "version": "1.10.2", "license": "AGPL-3.0", "dependencies": { "@noble/curves": "^1.4.0", @@ -6904,9 +6904,9 @@ "peer": true }, "node_modules/undici": { - "version": "5.28.3", - "resolved": "https://registry.npmjs.org/undici/-/undici-5.28.3.tgz", - "integrity": "sha512-3ItfzbrhDlINjaP0duwnNsKpDQk3acHI3gVJ1z4fmwMK31k5G9OVIAMLSIaP6w4FaGkaAkN6zaQO9LUvZ1t7VA==", + "version": "5.28.4", + "resolved": "https://registry.npmjs.org/undici/-/undici-5.28.4.tgz", + "integrity": "sha512-72RFADWFqKmUb2hmmvNODKL3p9hcB6Gt2DOQMis1SEBaV6a4MH8soBvzg+95CYhCKPFedut2JY9bMfrDl9D23g==", "dependencies": { "@fastify/busboy": "^2.0.0" }, diff --git a/package.json b/package.json index 59b2f281..fccb6f70 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@0xpolygonid/js-sdk", - "version": "1.10.1", + "version": "1.10.2", "description": "SDK to work with Polygon ID", "main": "dist/node/cjs/index.js", "module": "dist/node/esm/index.js", diff --git a/src/proof/proof-service.ts b/src/proof/proof-service.ts index 618feeb1..432ee52c 100644 --- a/src/proof/proof-service.ts +++ b/src/proof/proof-service.ts @@ -525,12 +525,13 @@ export class ProofService implements IProofService { return authInputs.inputsMarshal(); } - async verifyState(circuitId: string, pubSignals: Array): Promise { + async verifyState(circuitId: string, pubSignals: string[]): Promise { if (circuitId !== CircuitId.AuthV2) { throw new Error(`CircuitId is not supported ${circuitId}`); } const gistRoot = Hash.fromString(pubSignals[2]).bigInt(); - const globalStateInfo = await this._stateStorage.getGISTRootInfo(gistRoot); + const userId = BigInt(pubSignals[0]); + const globalStateInfo = await this._stateStorage.getGISTRootInfo(gistRoot, userId); if (globalStateInfo.createdAtTimestamp === 0n) { throw new Error(`gist state doesn't exists in contract`); diff --git a/src/proof/verifiers/pub-signals-verifier.ts b/src/proof/verifiers/pub-signals-verifier.ts index ad6a1787..20e63beb 100644 --- a/src/proof/verifiers/pub-signals-verifier.ts +++ b/src/proof/verifiers/pub-signals-verifier.ts @@ -393,7 +393,7 @@ export class PubSignalsVerifier { // no query verification // verify state - const gist = await this.checkGlobalState(authV2PubSignals.GISTRoot); + const gist = await this.checkGlobalState(authV2PubSignals.GISTRoot, this.userId); let acceptedStateTransitionDelay = defaultAuthVerifyOpts; if (opts?.acceptedStateTransitionDelay) { @@ -634,13 +634,16 @@ export class PubSignalsVerifier { return { latest: true, transitionTimestamp: 0 }; } - private async rootResolve(state: bigint): Promise<{ + private async rootResolve( + state: bigint, + id: bigint + ): Promise<{ latest: boolean; transitionTimestamp: number | string; }> { let globalStateInfo: RootInfo; try { - globalStateInfo = await this._stateStorage.getGISTRootInfo(state); + globalStateInfo = await this._stateStorage.getGISTRootInfo(state, id); } catch (e: unknown) { if ((e as { errorArgs: string[] }).errorArgs[0] === 'Root does not exist') { throw new Error('GIST root does not exist in the smart contract'); @@ -676,13 +679,13 @@ export class PubSignalsVerifier { }; private checkGlobalState = async ( - state: Hash + state: Hash, + id: Id ): Promise<{ latest: boolean; transitionTimestamp: number | string; }> => { - const gistStateResolved = await this.rootResolve(state.bigInt()); - return gistStateResolved; + return this.rootResolve(state.bigInt(), id.bigInt()); }; private checkRevocationStateForId = async ( diff --git a/src/storage/blockchain/state.ts b/src/storage/blockchain/state.ts index f10641c2..d983d1cd 100644 --- a/src/storage/blockchain/state.ts +++ b/src/storage/blockchain/state.ts @@ -181,9 +181,9 @@ export class EthStateStorage implements IStateStorage { } /** {@inheritdoc IStateStorage.getGISTRootInfo} */ - async getGISTRootInfo(id: bigint): Promise { + async getGISTRootInfo(root: bigint, id: bigint): Promise { const { stateContract } = this.getStateContractAndProviderForId(id); - const data = await stateContract.getGISTRootInfo(id); + const data = await stateContract.getGISTRootInfo(root); return { root: BigInt(data.root.toString()), diff --git a/src/storage/interfaces/state.ts b/src/storage/interfaces/state.ts index 4996408b..cbf970d8 100644 --- a/src/storage/interfaces/state.ts +++ b/src/storage/interfaces/state.ts @@ -40,11 +40,13 @@ export interface IStateStorage { * @returns `Promise` */ getGISTProof(id: bigint): Promise; + /** + * gets root info of global identity state tree * - * - * @param {bigint} root - returns info about global state root - * @returns `{Promise}` + * @param {bigint} root - root to check + * @param {bigint} userId - user id + * @returns `Promise` */ - getGISTRootInfo(root: bigint): Promise; + getGISTRootInfo(root: bigint, userId: bigint): Promise; } diff --git a/tests/handlers/auth.test.ts b/tests/handlers/auth.test.ts index 3b8639d7..73a96f76 100644 --- a/tests/handlers/auth.test.ts +++ b/tests/handlers/auth.test.ts @@ -155,8 +155,11 @@ describe('auth', () => { const tokenStr = authRes.token; expect(tokenStr).to.be.a('string'); - const token = await Token.parse(tokenStr); - expect(token).to.be.a('object'); + const tokenBytes = byteEncoder.encode(tokenStr); + + const result = await packageMgr.unpack(tokenBytes); + + expect(JSON.stringify(result.unpackedMessage)).to.equal(JSON.stringify(authRes.authResponse)); }); it('request-response flow profiles', async () => { diff --git a/tests/helpers.ts b/tests/helpers.ts index 21f823d3..d5d08732 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -82,23 +82,88 @@ export const MOCK_STATE_STORAGE: IStateStorage = { }, getGISTProof: (): Promise => { return Promise.resolve({ - root: 0n, + root: 17849981720634212802664189929671140762557483236708097723884172641124691222298n, existence: false, - siblings: [], - index: 0n, - value: 0n, - auxExistence: false, - auxIndex: 0n, - auxValue: 0n + siblings: [ + 326134006790980215127990638838603607927393129620216601356067855450776136958n, + 16040166254748400928224657755817560385397468295441098176890118618372305780319n, + 7420244680292574553634398111118115717735331415360971314725970615241423752349n, + 6507662880357714762049811645379966577560455837856926574999515093494263945636n, + 0n, + 11595029294867301124773898482486523127938324454211741981988612719540601500230n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n, + 0n + ], + index: 6215769198018398530415546468862662728220697074975785700954864316151489646001n, + value: 4640764522093738397338399033214568021338945005386791807140524881813261975471n, + auxExistence: true, + auxIndex: 6362438263702490742533732866233615008788456433702165759669012876424014585585n, + auxValue: 4640764522093738397338399033214568021338945005386791807140524881813261975471n }); }, getGISTRootInfo: (): Promise => { return Promise.resolve({ - root: 0n, + root: 17849981720634212802664189929671140762557483236708097723884172641124691222298n, replacedByRoot: 0n, - createdAtTimestamp: 0n, + createdAtTimestamp: 1712319821n, replacedAtTimestamp: 0n, - createdAtBlock: 0n, + createdAtBlock: 5499734n, replacedAtBlock: 0n }); }