doipjs/src/claimVerification.js

68 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-10-24 16:31:42 -06:00
const utils = require('./utils')
2020-11-02 16:25:02 -07:00
const runOnJson = (res, proofData, checkPath, checkClaim, checkRelation) => {
let re
2020-10-24 16:31:42 -06:00
2020-11-02 16:25:02 -07:00
if (res.isVerified || !proofData) {
return res
2020-10-24 16:31:42 -06:00
}
if (checkPath.length == 0) {
switch (checkRelation) {
default:
case 'contains':
2020-10-26 15:50:41 -06:00
re = new RegExp(checkClaim.replace('[', '\\[').replace(']', '\\]'), "gi")
2020-11-02 16:25:02 -07:00
res.isVerified = re.test(proofData.replace(/\r?\n|\r/, ''))
2020-10-24 16:31:42 -06:00
break
case 'equals':
2020-11-02 16:25:02 -07:00
res.isVerified = proofData.replace(/\r?\n|\r/, '').toLowerCase() == checkClaim.toLowerCase()
2020-10-24 16:31:42 -06:00
break
case 'oneOf':
re = new RegExp(checkClaim, "gi")
2020-11-02 16:25:02 -07:00
res.isVerified = re.test(proofData.join("|"))
2020-10-24 16:31:42 -06:00
break
}
2020-11-02 16:25:02 -07:00
return res
}
if (!(checkPath[0] in proofData)) {
res.errors.push('err_data_structure_incorrect')
return res
2020-10-24 16:31:42 -06:00
}
if (Array.isArray(proofData)) {
proofData.forEach((item, i) => {
2020-11-02 16:25:02 -07:00
res = runOnJson(res, item, checkPath, checkClaim, checkRelation)
2020-10-26 15:50:41 -06:00
})
2020-10-24 16:31:42 -06:00
} else if (Array.isArray(proofData[checkPath[0]])) {
proofData[checkPath[0]].forEach((item, i) => {
2020-11-02 16:25:02 -07:00
res = runOnJson(res, item, checkPath.slice(1), checkClaim, checkRelation)
2020-10-24 16:31:42 -06:00
})
} else {
2020-11-02 16:25:02 -07:00
res = runOnJson(res, proofData[checkPath[0]], checkPath.slice(1), checkClaim, checkRelation)
2020-10-24 16:31:42 -06:00
}
2020-11-02 16:25:02 -07:00
return res
2020-10-24 16:31:42 -06:00
}
const run = (proofData, spData) => {
2020-11-02 16:25:02 -07:00
let res = {
isVerified: false,
errors: []
}
2020-10-24 16:31:42 -06:00
switch (spData.proof.format) {
case 'json':
2020-11-02 16:25:02 -07:00
res = runOnJson(res, proofData, spData.claim.path, utils.generateClaim(spData.claim.fingerprint, spData.claim.format), spData.claim.relation)
2020-10-24 16:31:42 -06:00
break
case 'text':
2020-10-24 16:39:43 -06:00
re = new RegExp(utils.generateClaim(spData.claim.fingerprint, spData.claim.format), "gi")
2020-11-02 16:25:02 -07:00
res = re.test(proofData.replace(/\r?\n|\r/, ''))
2020-10-24 16:31:42 -06:00
break
}
2020-11-02 16:25:02 -07:00
return res
2020-10-24 16:31:42 -06:00
}
exports.run = run