Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue with non-exists #202

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 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.9.1",
"version": "1.9.2",
"description": "SDK to work with Polygon ID",
"main": "dist/node/cjs/index.js",
"module": "dist/node/esm/index.js",
Expand Down
15 changes: 12 additions & 3 deletions src/proof/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
isValidOperation,
Operators,
QueryOperators,
XSDNS

Check warning on line 9 in src/proof/common.ts

View workflow job for this annotation

GitHub Actions / build (20.11.0)

'XSDNS' is defined but never used
} from '../circuits';
import { StateProof } from '../storage/entities/state';
import {
Expand Down Expand Up @@ -207,15 +207,17 @@
}
}

if (propertyQuery.operatorValue) {
if (propertyQuery.operatorValue !== undefined) {
if (!isValidOperation(query.datatype, propertyQuery.operator)) {
throw new Error(
`operator ${propertyQuery.operator} is not supported for datatype ${query.datatype}`
);
}

const datatype = propertyQuery.operator === Operators.EXISTS ? XSDNS.Boolean : query.datatype;
query.values = await transformQueryValueToBigInts(propertyQuery.operatorValue, datatype);
query.values =
propertyQuery.operator === Operators.EXISTS
? transformExistsValue(propertyQuery.operatorValue)
: await transformQueryValueToBigInts(propertyQuery.operatorValue, query.datatype);
}
return query;
};
Expand Down Expand Up @@ -247,3 +249,10 @@
}
return values;
};

const transformExistsValue = (value: unknown): bigint[] => {
if (typeof value == 'boolean') {
return [BigInt(value)];
}
throw new Error('exists operator value must be true or false');
};
6 changes: 1 addition & 5 deletions src/proof/proof-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,7 @@ export class ProofService implements IProofService {
query.valueProof.mtp = proof;
query.valueProof.path = queryMetadata.claimPathKey;

const mtEntry = await mtValue?.mtEntry();
if (!mtEntry) {
throw new Error(`can't merklize credential: no merkle tree entry found`);
}

const mtEntry = (await mtValue?.mtEntry()) ?? 0n;
query.valueProof.value = mtEntry;
if (!queryMetadata.fieldName) {
query.values = [mtEntry];
Expand Down
2 changes: 1 addition & 1 deletion src/storage/filters/jsonQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export class FilterQuery implements IFilterQuery {
const credentialPathValue = resolvePath(credential, this.path);
if (
(credentialPathValue === null || credentialPathValue === undefined) &&
this.operatorFunc !== existsComparator
this.operatorFunc !== comparatorOptions.$exists
) {
return false;
}
Expand Down
145 changes: 145 additions & 0 deletions tests/proofs/sig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,149 @@ describe('sig proofs', () => {
}
});
});

it('sigv3-ipfs-non-exists', async () => {
const req = {
id: '0d8e91e5-5686-49b5-85e3-2b35538c6a03',
typ: 'application/iden3comm-plain-json',
type: 'https://iden3-communication.io/authorization/1.0/request',
thid: '0d8e91e5-5686-49b5-85e3-2b35538c6a03',
body: {
callbackUrl: 'https://verifier-v2.polygonid.me/api/callback?sessionId=25269',
reason: 'test flow',
scope: [
{
circuitId: 'credentialAtomicQueryV3-beta.1',
id: 1711115116,
query: {
allowedIssuers: ['*'],
context: 'ipfs://QmcvoKLc742CyVH2Cnw6X95b4c8VdABqNPvTyAHEeaK1aP',
type: 'types123',
credentialSubject: {
bol: {
$exists: false
}
}
}
}
]
},
from: 'did:polygonid:polygon:mumbai:2qLPqvayNQz9TA2r5VPxUugoF18teGU583zJ859wfy'
};

const claimReq: CredentialRequest = {
credentialSchema: 'ipfs://QmTRpn65HN3j6Y5ZC5WDU1orXnWPWMpoPr2qpep8eMCX6e',
type: 'types123',
credentialSubject: {
id: userDID.string(),
double: 1.2,
int: 1,
string: 'test'
},
expiration: 2793526400,
revocationOpts: {
type: CredentialStatusType.Iden3ReverseSparseMerkleTreeProof,
id: rhsUrl
}
};
const issuerCred = await idWallet.issueCredential(issuerDID, claimReq, {
ipfsNodeURL
});

await credWallet.save(issuerCred);

const creds = await credWallet.findByQuery(req.body.scope[0].query);

expect(creds.length).to.not.equal(0);

const { proof, vp, circuitId, pub_signals } = await proofService.generateProof(
req.body.scope[0],
userDID
);
expect(proof).not.to.be.undefined;
expect(vp).to.be.undefined;

const isValid = await proofService.verifyProof(
{
proof,
pub_signals
},
circuitId as CircuitId
);

expect(isValid).to.be.true;
});

it('sigv3-ipfs-exists', async () => {
const req = {
id: '0d8e91e5-5686-49b5-85e3-2b35538c6a03',
typ: 'application/iden3comm-plain-json',
type: 'https://iden3-communication.io/authorization/1.0/request',
thid: '0d8e91e5-5686-49b5-85e3-2b35538c6a03',
body: {
callbackUrl: 'https://verifier-v2.polygonid.me/api/callback?sessionId=25269',
reason: 'test flow',
scope: [
{
circuitId: 'credentialAtomicQueryV3-beta.1',
id: 1711115116,
query: {
allowedIssuers: ['*'],
context: 'ipfs://QmcvoKLc742CyVH2Cnw6X95b4c8VdABqNPvTyAHEeaK1aP',
type: 'types123',
credentialSubject: {
bol: {
$exists: true
}
}
}
}
]
},
from: 'did:polygonid:polygon:mumbai:2qLPqvayNQz9TA2r5VPxUugoF18teGU583zJ859wfy'
};

const claimReq: CredentialRequest = {
credentialSchema: 'ipfs://QmTRpn65HN3j6Y5ZC5WDU1orXnWPWMpoPr2qpep8eMCX6e',
type: 'types123',
credentialSubject: {
id: userDID.string(),
double: 1.2,
int: 1,
bol: true,
string: 'test'
},
expiration: 2793526400,
revocationOpts: {
type: CredentialStatusType.Iden3ReverseSparseMerkleTreeProof,
id: rhsUrl
}
};
const issuerCred = await idWallet.issueCredential(issuerDID, claimReq, {
ipfsNodeURL
});

await credWallet.save(issuerCred);

const creds = await credWallet.findByQuery(req.body.scope[0].query);

expect(creds.length).to.not.equal(0);

const { proof, vp, circuitId, pub_signals } = await proofService.generateProof(
req.body.scope[0],
userDID
);
expect(proof).not.to.be.undefined;
expect(vp).to.be.undefined;

const isValid = await proofService.verifyProof(
{
proof,
pub_signals
},
circuitId as CircuitId
);

expect(isValid).to.be.true;
});
});
Loading