Skip to content

Commit

Permalink
Merge pull request #1 from yodaplus/stability-changes
Browse files Browse the repository at this point in the history
Stability changes
  • Loading branch information
pitalerushikesh authored May 8, 2024
2 parents d66b021 + 344a78b commit b7503ec
Show file tree
Hide file tree
Showing 10 changed files with 3,843 additions and 268 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ API_KEY=testkey
NODE_ENV=test
TT_AWS_BUCKET_NAME=
TT_STORAGE_AWS_ACCESS_KEY_ID=
TT_STORAGE_AWS_SECRET_ACCESS_KEY=
TT_STORAGE_AWS_SECRET_ACCESS_KEY=
INFURA_ID=
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ app.disable("x-powered-by");

export const handler = serverless(app);

// app.listen(9999, () => {
// console.log("Server running on port 9999");
// app.listen(8888, () => {
// console.log("Server running on port 8888");
// });
30 changes: 26 additions & 4 deletions lambda/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
CHAIN_ID,
SUPPORTED_CHAINS,
chainInfo,
} from "@govtechsg/tradetrust-utils/constants/supportedChains";
} from "@tradetrust-tt/tradetrust-utils/constants/supportedChains";
import * as dotenv from "dotenv";
dotenv.config();

Expand All @@ -12,6 +12,8 @@ export const ALLOWED_ORIGINS =
? [
"http://127.0.0.1:3000",
"http://localhost:3000",
"http://127.0.0.1:3000/*",
"http://localhost:3000/*",
"https://creator.tradetrust.io",
"https://dev.tradetrust.io",
"https://tradetrust.io",
Expand Down Expand Up @@ -76,9 +78,11 @@ export const SUPPORTED_NETWORKS: supportedNetworks = {
...SUPPORTED_CHAINS[CHAIN_ID.matic],
provider: infuraProvider("matic"),
},
[CHAIN_ID.maticmum]: {
...SUPPORTED_CHAINS[CHAIN_ID.maticmum],
provider: infuraProvider("maticmum"),
[CHAIN_ID.amoy]: {
...SUPPORTED_CHAINS[CHAIN_ID.amoy],
provider: jsonRpcProvider(
`https://polygon-amoy.infura.io/v3/${process.env.INFURA_ID}`
),
},
[CHAIN_ID.sepolia]: {
...SUPPORTED_CHAINS[CHAIN_ID.sepolia],
Expand All @@ -92,4 +96,22 @@ export const SUPPORTED_NETWORKS: supportedNetworks = {
...SUPPORTED_CHAINS[CHAIN_ID.xdcapothem],
provider: jsonRpcProvider(SUPPORTED_CHAINS[CHAIN_ID.xdcapothem].rpcUrl),
},
[CHAIN_ID.stability]: {
...SUPPORTED_CHAINS[CHAIN_ID.stability],
provider: jsonRpcProvider(SUPPORTED_CHAINS[CHAIN_ID.stability].rpcUrl),
},
[CHAIN_ID.stabilitytestnet]: {
...SUPPORTED_CHAINS[CHAIN_ID.stabilitytestnet],
provider: jsonRpcProvider(
SUPPORTED_CHAINS[CHAIN_ID.stabilitytestnet].rpcUrl
),
},
[CHAIN_ID.hederatestnet]: {
...SUPPORTED_CHAINS[CHAIN_ID.hederatestnet],
provider: jsonRpcProvider(SUPPORTED_CHAINS[CHAIN_ID.hederatestnet].rpcUrl),
},
[CHAIN_ID.hederamainnet]: {
...SUPPORTED_CHAINS[CHAIN_ID.hederamainnet],
provider: jsonRpcProvider(SUPPORTED_CHAINS[CHAIN_ID.hederamainnet].rpcUrl),
},
};
141 changes: 72 additions & 69 deletions lambda/functions/storage/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,98 @@
import { CHAIN_ID } from "@govtechsg/tradetrust-utils/constants/supportedChains";
// import { CHAIN_ID } from "@govtechsg/tradetrust-utils/constants/supportedChains";

import { v4 as uuid } from "uuid";
import { generateEncryptionKey } from "@govtechsg/oa-encryption";

import {
validateNetwork,
validateDocument,
getEncryptedDocument,
validateNetwork,
validateDocument,
getEncryptedDocument,
} from "../../utils";
import { s3Put } from "../../services/s3";
import { SUPPORTED_NETWORKS } from "../../constants";
import { s3Get } from "../../services/s3";
import * as dotenv from "dotenv";
import { generateEncryptionKey } from "@govtechsg/oa-encryption";
import { CHAIN_ID } from "@tradetrust-tt/tradetrust-utils/constants/supportedChains";
dotenv.config();

export const getDocument = async (id: string) => {
const document = await s3Get({
Bucket: process.env.TT_AWS_BUCKET_NAME,
Key: id,
});
const document = await s3Get({
Bucket: process.env.TT_AWS_BUCKET_NAME,
Key: id,
});

return document;
return document;
};

export const uploadDocument = async (document) => {
console.log("start");
// @ts-ignore
const { chainId } = await validateNetwork(document);
console.log("chainId", chainId);
await validateDocument({
document,
network: SUPPORTED_NETWORKS[chainId as CHAIN_ID].name,
});
console.log("uploadDocVerified");
const { encryptedDocument, encryptedDocumentKey } =
await getEncryptedDocument({
str: JSON.stringify(document),
});
console.log("uploadDoc", encryptedDocument, encryptedDocumentKey);
const id = uuid();
await s3Put({
Bucket: process.env.TT_AWS_BUCKET_NAME,
Key: id,
Body: JSON.stringify({ document: encryptedDocument }),
});
console.log("start");
// @ts-ignore
const { chainId } = await validateNetwork(document);
console.log("chainId", chainId);
await validateDocument({
document,
network: SUPPORTED_NETWORKS[chainId as CHAIN_ID].name,
});
console.log("uploadDocVerified");
const { encryptedDocument, encryptedDocumentKey } =
await getEncryptedDocument({
str: JSON.stringify(document),
});
console.log("uploadDoc", encryptedDocument, encryptedDocumentKey);
const id = uuid();
await s3Put({
Bucket: process.env.TT_AWS_BUCKET_NAME,
Key: id,
Body: JSON.stringify({ document: encryptedDocument }),
});

return {
id,
key: encryptedDocumentKey,
type: encryptedDocument.type,
};
return {
id,
key: encryptedDocumentKey,
type: encryptedDocument.type,
};
};

export const uploadDocumentAtId = async (document, documentId: string) => {
// @ts-ignore
const { chainId } = await validateNetwork(document);
console.log("check in uploadDocAtId", chainId);
await validateDocument({
document,
network: SUPPORTED_NETWORKS[chainId as CHAIN_ID].name,
});
console.log("check in uploadDocAtId validated?");
const { key: existingKey } = await getDocument(documentId);
const { encryptedDocument, encryptedDocumentKey } =
await getEncryptedDocument({
str: JSON.stringify(document),
existingKey,
});
// @ts-ignore
const { chainId } = await validateNetwork(document);
console.log("check in uploadDocAtId", chainId);
await validateDocument({
document,
network: SUPPORTED_NETWORKS[chainId as CHAIN_ID].name,
});
console.log("check in uploadDocAtId validated?");
const { key: existingKey } = await getDocument(documentId);
const { encryptedDocument, encryptedDocumentKey } =
await getEncryptedDocument({
str: JSON.stringify(document),
existingKey,
});

await s3Put({
Bucket: process.env.TT_AWS_BUCKET_NAME,
Key: documentId,
Body: JSON.stringify({ document: encryptedDocument }),
});
await s3Put({
Bucket: process.env.TT_AWS_BUCKET_NAME,
Key: documentId,
Body: JSON.stringify({ document: encryptedDocument }),
});

return {
id: documentId,
key: encryptedDocumentKey,
type: encryptedDocument.type,
};
return {
id: documentId,
key: encryptedDocumentKey,
type: encryptedDocument.type,
};
};

export const getQueueNumber = async () => {
const id = uuid();
const encryptionKey = generateEncryptionKey();
const id = uuid();
const encryptionKey = generateEncryptionKey();

await s3Put({
Bucket: process.env.TT_AWS_BUCKET_NAME,
Key: id,
Body: JSON.stringify({
key: encryptionKey,
}),
});
await s3Put({
Bucket: process.env.TT_AWS_BUCKET_NAME,
Key: id,
Body: JSON.stringify({
key: encryptionKey,
}),
});

return { key: encryptionKey, id };
return { key: encryptionKey, id };
};
50 changes: 26 additions & 24 deletions lambda/functions/verify/router.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
import express, { Request, Response } from "express";
import { networkName } from "@govtechsg/tradetrust-utils/constants/network";
import { isValid } from "@govtechsg/oa-verify";
// import { networkName } from "@govtechsg/tradetrust-utils/constants/network";
import { networkName } from "@tradetrust-tt/tradetrust-utils/constants/network";

import { validateDocument } from "../../utils";
import { isValid } from "@tradetrust-tt/tt-verify";

const router = express.Router();

router.post("/", async (req: Request, res: Response) => {
const {
body: { document },
query: { network = "mainnet" },
} = req;
const {
body: { document },
query: { network = "mainnet" },
} = req;

try {
console.log("start /");
const fragments = await validateDocument({
document,
network: network as networkName,
});
res.status(200).json({
summary: {
all: isValid(fragments),
documentStatus: isValid(fragments, ["DOCUMENT_STATUS"]),
documentIntegrity: isValid(fragments, ["DOCUMENT_INTEGRITY"]),
issuerIdentity: isValid(fragments, ["ISSUER_IDENTITY"]),
},
fragments,
});
} catch (err) {
res.status(400).json(err);
}
try {
console.log("start /");
const fragments = await validateDocument({
document,
network: network as networkName,
});
res.status(200).json({
summary: {
all: isValid(fragments),
documentStatus: isValid(fragments, ["DOCUMENT_STATUS"]),
documentIntegrity: isValid(fragments, ["DOCUMENT_INTEGRITY"]),
issuerIdentity: isValid(fragments, ["ISSUER_IDENTITY"]),
},
fragments,
});
} catch (err) {
res.status(400).json(err);
}
});

export { router };
Loading

0 comments on commit b7503ec

Please sign in to comment.