mirror of
https://codeberg.org/keyoxide/doipjs.git
synced 2024-12-23 06:59:29 -07:00
Add claimVerification scripts
This commit is contained in:
parent
1f29accb5f
commit
6e966f4df5
1 changed files with 54 additions and 0 deletions
54
src/claimVerification.js
Normal file
54
src/claimVerification.js
Normal 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
|
Loading…
Reference in a new issue