Skip to content

Commit

Permalink
fixes for exists
Browse files Browse the repository at this point in the history
  • Loading branch information
vmidyllic committed Mar 22, 2024
1 parent 7c1dd45 commit db74ade
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 14 deletions.
18 changes: 10 additions & 8 deletions src/proof/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,17 @@ export const parseQueryMetadata = async (
}
}

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}`
);
}

query.values = Operators.EXISTS ? transformExistsValue(propertyQuery.operatorValue): await transformQueryValueToBigInts(propertyQuery.operatorValue, query.datatype);
query.values =
propertyQuery.operator === Operators.EXISTS
? transformExistsValue(propertyQuery.operatorValue)
: await transformQueryValueToBigInts(propertyQuery.operatorValue, query.datatype);
}
return query;
};
Expand Down Expand Up @@ -247,10 +250,9 @@ export const transformQueryValueToBigInts = async (
return values;
};

const transformExistsValue = (value : unknown): bigint[] =>{

if (typeof value == "boolean") {
const transformExistsValue = (value: unknown): bigint[] => {
if (typeof value == 'boolean') {
return [BigInt(value)];
}
throw new Error("exists operator value must be true or false")
}
}
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;
});
});

0 comments on commit db74ade

Please sign in to comment.