Skip to content

Commit

Permalink
Fix zkp unpack (#214)
Browse files Browse the repository at this point in the history
* Fix zkp unpack

* Patch version
  • Loading branch information
Kolezhniuk authored Apr 5, 2024
1 parent ac073cf commit f96d688
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 32 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/proof/proof-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,13 @@ export class ProofService implements IProofService {
return authInputs.inputsMarshal();
}

async verifyState(circuitId: string, pubSignals: Array<string>): Promise<boolean> {
async verifyState(circuitId: string, pubSignals: string[]): Promise<boolean> {
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`);
Expand Down
15 changes: 9 additions & 6 deletions src/proof/verifiers/pub-signals-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions src/storage/blockchain/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ export class EthStateStorage implements IStateStorage {
}

/** {@inheritdoc IStateStorage.getGISTRootInfo} */
async getGISTRootInfo(id: bigint): Promise<RootInfo> {
async getGISTRootInfo(root: bigint, id: bigint): Promise<RootInfo> {
const { stateContract } = this.getStateContractAndProviderForId(id);
const data = await stateContract.getGISTRootInfo(id);
const data = await stateContract.getGISTRootInfo(root);

return {
root: BigInt(data.root.toString()),
Expand Down
10 changes: 6 additions & 4 deletions src/storage/interfaces/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ export interface IStateStorage {
* @returns `Promise<StateProof>`
*/
getGISTProof(id: bigint): Promise<StateProof>;

/**
* gets root info of global identity state tree
*
*
* @param {bigint} root - returns info about global state root
* @returns `{Promise<RootInfo>}`
* @param {bigint} root - root to check
* @param {bigint} userId - user id
* @returns `Promise<RootInfo>`
*/
getGISTRootInfo(root: bigint): Promise<RootInfo>;
getGISTRootInfo(root: bigint, userId: bigint): Promise<RootInfo>;
}
7 changes: 5 additions & 2 deletions tests/handlers/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
85 changes: 75 additions & 10 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,88 @@ export const MOCK_STATE_STORAGE: IStateStorage = {
},
getGISTProof: (): Promise<StateProof> => {
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<RootInfo> => {
return Promise.resolve({
root: 0n,
root: 17849981720634212802664189929671140762557483236708097723884172641124691222298n,
replacedByRoot: 0n,
createdAtTimestamp: 0n,
createdAtTimestamp: 1712319821n,
replacedAtTimestamp: 0n,
createdAtBlock: 0n,
createdAtBlock: 5499734n,
replacedAtBlock: 0n
});
}
Expand Down

0 comments on commit f96d688

Please sign in to comment.