diff --git a/src/claimVerification.js b/src/claimVerification.js new file mode 100644 index 0000000..0184814 --- /dev/null +++ b/src/claimVerification.js @@ -0,0 +1,54 @@ +const utils = require('./utils') + +const runOnJson = (proofData, checkPath, checkClaim, checkRelation) => { + let isVerified = false, re + + if (!proofData) { + return isVerified + } + + if (checkPath.length == 0) { + switch (checkRelation) { + default: + case 'contains': + re = new RegExp(checkClaim, "gi") + return re.test(proofData.replace(/\r?\n|\r/, '')) + break + case 'equals': + return proofData.replace(/\r?\n|\r/, '').toLowerCase() == checkClaim.toLowerCase() + break + case 'oneOf': + re = new RegExp(checkClaim, "gi") + return re.test(proofData.join("|")) + break + } + } + + if (Array.isArray(proofData)) { + proofData.forEach((item, i) => { + isVerified = isVerified || runOnJson(item, checkPath, checkClaim, checkRelation) + }); + } else if (Array.isArray(proofData[checkPath[0]])) { + proofData[checkPath[0]].forEach((item, i) => { + isVerified = isVerified || runOnJson(item, checkPath.slice(1), checkClaim, checkRelation) + }) + } else { + isVerified = isVerified || runOnJson(proofData[checkPath[0]], checkPath.slice(1), checkClaim, checkRelation) + } + + return isVerified; +} + +const run = (proofData, spData) => { + switch (spData.proof.format) { + case 'json': + return runOnJson(proofData, spData.claim.path, utils.generateClaim(spData), spData.claim.relation) + break + case 'text': + re = new RegExp(utils.generateClaim(spData), "gi") + return re.test(proofData.replace(/\r?\n|\r/, '')) + break + } +} + +exports.run = run