Skip to content

Commit

Permalink
fix: use shared verify for documentService.js
Browse files Browse the repository at this point in the history
  • Loading branch information
HJunyuan committed Aug 30, 2024
1 parent 129bebe commit 11b06af
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/shared/verify.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getData } = require("@govtechsg/open-attestation");
const { getData, validateSchema } = require("@govtechsg/open-attestation");
const {
verificationBuilder,
openAttestationVerifiers
Expand All @@ -13,15 +13,21 @@ const IS_MAINNET =
/**
* A wrapper of verify to auto-switch between Ethereum and Polygon
*/
export const verify = (document) => {
export const verify = async (document) => {
if (!validateSchema(document)) {
// Following current behaviour of from "@govtechsg/opencerts-verify"
// E.g. await verify({ "bad": "document" }) // returns undefined
return undefined;
}

const _verify = verificationBuilder(
[...openAttestationVerifiers, registryVerifier],
{
network: getNetworkName(document)
}
);

return _verify(document);
return await _verify(document);
};

function getNetworkName(document) {
Expand Down
7 changes: 4 additions & 3 deletions src/storage/documentService/documentService.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { v4: uuidv4 } = require("uuid");
const { verify, isValid } = require("@govtechsg/opencerts-verify"); // FIXME: Unable to use `src/shared/verify.js` due to weird crash claiming `network` is undefined
const { isValid } = require("@govtechsg/opencerts-verify"); // FIXME: Unable to use `src/shared/verify.js` due to weird crash claiming `network` is undefined
const {
encryptString,
generateEncryptionKey
} = require("@govtechsg/oa-encryption");

const { verify } = require("../../shared/verify");
const config = require("../config");
const { put, get, remove } = require("../s3");

Expand Down Expand Up @@ -68,7 +69,7 @@ const uploadDocumentAtId = async (
throw new Error("Ttl cannot exceed 90 days");
}

const fragments = await verify({ network: config.network })(document);
const fragments = await verify(document);
if (!isValid(fragments)) {
throw new Error("Document is not valid");
}
Expand Down Expand Up @@ -100,7 +101,7 @@ const uploadDocument = async (
document,
ttlInMicroseconds = DEFAULT_TTL_IN_MICROSECONDS
) => {
const fragments = await verify({ network: config.network })(document);
const fragments = await verify(document);
if (!isValid(fragments)) {
throw new Error("Document is not valid");
}
Expand Down

0 comments on commit 11b06af

Please sign in to comment.