Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: polygon support #161

Merged
merged 7 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": ["airbnb-base", "prettier"],
"parserOptions": { "ecmaVersion": 2020 },
"env": {
"jest/globals": true
},
Expand All @@ -15,7 +16,7 @@
"func-names": ["error", "as-needed"],
"prettier/prettier": "error",
"no-unused-expressions": "off",
"import/prefer-default-export": "off"

"import/prefer-default-export": "off",
"no-console": "off"
}
}
2,694 changes: 1,626 additions & 1,068 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"license": "ISC",
"dependencies": {
"@govtechsg/oa-encryption": "^1.3.5",
"@govtechsg/open-attestation": "^6.9.0",
"@govtechsg/oa-verify": "^9.3.1",
"@govtechsg/open-attestation": "^6.9.5",
"@govtechsg/opencerts-verify": "^3.1.3",
"@middy/core": "^5.1.0",
"@middy/http-cors": "^5.1.0",
Expand All @@ -45,7 +46,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.11",
"copy-webpack-plugin": "^5.1.1",
"copy-webpack-plugin": "^12.0.2",
"dotenv": "^7.0.0",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
Expand All @@ -62,11 +63,13 @@
"raw-loader": "^4.0.1",
"serverless-domain-manager": "^7.3.8",
"serverless-dotenv-plugin": "^6.0.0",
"serverless-offline": "^13.3.2",
"serverless-offline": "^13.7.0",
"serverless-plugin-typescript": "^2.1.5",
"serverless-s3-local": "^0.8.1",
"serverless-s3-remover": "^0.6.0",
"serverless-webpack": "^5.13.0",
"serverless-webpack": "^5.14.1",
"supertest": "^4.0.2",
"typescript": "^5.5.4",
"webpack": "^5.86.0"
},
"prettier": {
Expand Down
1 change: 0 additions & 1 deletion src/email/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module.exports = {
region: process.env.SES_REGION || "us-west-2"
},
recaptchaSecret: process.env.RECAPTCHA_SECRET,
network: process.env.NETWORK || "homestead",
emailApiKeys:
(process.env.EMAIL_API_KEYS && process.env.EMAIL_API_KEYS.split(":")) || []
};
6 changes: 4 additions & 2 deletions src/email/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import middy from "@middy/core";
import cors from "@middy/http-cors";
import { isValid } from "@govtechsg/opencerts-verify";

import { verify } from "../shared/verify";

const { get } = require("lodash");
const { isValid, verify } = require("@govtechsg/opencerts-verify");

const recaptcha = require("./recaptcha");
const certificateMailer = require("./mailer/mailerWithSESTransporter");
Expand Down Expand Up @@ -31,7 +33,7 @@ const handleEmail = async (event) => {
}

// Verify Certificate
const fragments = await verify({ network: config.network })(data);
const fragments = await verify(data);
if (!isValid(fragments)) {
throw new Error("Invalid certificate");
}
Expand Down
2 changes: 1 addition & 1 deletion src/email/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
filename: "index.js"
},
plugins: [
new CopyPlugin([{ from: "static", to: "static" }]),
new CopyPlugin({ patterns: [{ from: "static", to: "static" }] }),
new webpack.ProvidePlugin({
fetch: path.resolve(path.join(__dirname, "..", "lol"))
})
Expand Down
3 changes: 3 additions & 0 deletions src/shared/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
network: process.env.NETWORK || "mainnet"
};
69 changes: 69 additions & 0 deletions src/shared/verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const { getData, validateSchema } = require("@govtechsg/open-attestation");
const {
verificationBuilder,
openAttestationVerifiers
} = require("@govtechsg/oa-verify");
const { registryVerifier } = require("@govtechsg/opencerts-verify");

const config = require("./config");

const IS_MAINNET =
config.network === "mainnet" || config.network === "homestead";

function getNetworkName(document) {
const data = getData(document);

if (IS_MAINNET && data.network) {
/* Production Network Whitelist */
switch (data.network?.chainId) {
case "137":
return "matic";
default:
break;
}
} else {
/* Non-production Network Whitelist */
switch (data.network?.chainId) {
case "80002":
// return "amoy";
// FIXME: Setting "amoy" will fail as it's an unsupported network in Ethers v5.7.2
// Create a custom provider and specify { chainId: 80002, name: "amoy" }
// https://github.com/OpenCerts/opencerts-website/blob/1de65c66795ec2f416d6c829259e9f9ab1e49e45/src/sagas/certificate.ts#L123
console.error(`"amoy" is not supported on Ethers v5.7.2 yet`);
break;
default:
break;
}
}

// A network is specified in the certificate but not in the above whitelist
if (data.network) {
console.warn(
`"${JSON.stringify(
data.network
)}" is not a whitelisted network. Reverting back to "${config.network}".`
);
}

return config.network;
}

/**
* A wrapper of verify to auto-switch between Ethereum and Polygon
*/
export const verify = (document) => {
if (!validateSchema(document)) {
// Following current behaviour of from "@govtechsg/opencerts-verify"
// E.g. await verify({ "bad": "document" }) // returns undefined
phanshiyu marked this conversation as resolved.
Show resolved Hide resolved
return undefined;
}

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

return oaVerify(document);
};
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");
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)) {
HJunyuan marked this conversation as resolved.
Show resolved Hide resolved
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
3 changes: 0 additions & 3 deletions src/verify/config.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/verify/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import middy from "@middy/core";
import cors from "@middy/http-cors";
import { isValid } from "@govtechsg/opencerts-verify";

const { verify, isValid } = require("@govtechsg/opencerts-verify");
const config = require("./config");
import { verify } from "../shared/verify";

const handleVerify = async (event) => {
const { document } = JSON.parse(event.body);
try {
const fragments = await verify({ network: config.network })(document);
const fragments = await verify(document);
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
Expand Down
13 changes: 7 additions & 6 deletions src/verify/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ functions:
handler: index.handler
timeout: 15 # long timeout as infura api can take awhile
events:
- http:
path: /
method: post
- http:
path: /
method: post
cors: true
custom:
dotenv:
basePath: ../../
serverless-offline:
lambdaPort: 4002
httpPort: 4000
isManagedDomain:
false: 'true' # To negate the environment variable DISABLE_DOMAIN
true: 'false' # if you know of a way to do conditionals please change this
isManagedDomain:
false: "true" # To negate the environment variable DISABLE_DOMAIN
true: "false" # if you know of a way to do conditionals please change this
customDomain:
domainName: "${env:DOMAIN,''}" # Change this to your domain.
basePath: "verify" # This will be prefixed to all routes
Expand All @@ -45,6 +45,7 @@ custom:
includeModules:
packagePath: "../../package.json"
plugins:
- serverless-dotenv-plugin
- serverless-offline
- serverless-domain-manager
- serverless-webpack
Loading