Add claimVerification scripts

This commit is contained in:
Yarmo Mackenbach 2020-10-25 00:31:42 +02:00
parent 1f29accb5f
commit 6e966f4df5

54
src/claimVerification.js Normal file
View file

@ -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