Skip to content

Commit

Permalink
feat: added support for v3 config file (#196)
Browse files Browse the repository at this point in the history
* feat: added support for v3 config file

* fix: validate function

* feat: refactor update form to use the one in OA
  • Loading branch information
isaackps authored Mar 10, 2022
1 parent 7e9998b commit 52d72ec
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 48 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@govtechsg/document-store": "^2.2.2",
"@govtechsg/oa-encryption": "^1.3.3",
"@govtechsg/oa-verify": "^7.5.2",
"@govtechsg/open-attestation": "^6.2.0",
"@govtechsg/open-attestation": "^6.4.1",
"@govtechsg/token-registry": "^2.5.3",
"ajv": "^8.4.0",
"ajv-formats": "^2.1.0",
Expand Down
19 changes: 16 additions & 3 deletions src/implementations/config/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getTokenRegistryAddress,
getDocumentStoreAddress,
} from "./helpers";
import { utils, v2, v3 } from "@govtechsg/open-attestation";

const SANDBOX_ENDPOINT_URL = "https://sandbox.fyntech.io";

Expand All @@ -35,9 +36,21 @@ export const create = async ({

const hasTransferableRecord = forms.some((form) => form.type === "TRANSFERABLE_RECORD");
const hasDocumentStore = forms.some((form) => form.type === "VERIFIABLE_DOCUMENT");
const hasDid = forms.some(
(form) => form.defaults.issuers.some((issuer) => issuer.identityProof?.type.includes("DID")) // TODO: v3 does not have `issuers` but `issuer` instead
);
const hasDid = forms.some((form) => {
//check form for v2/v3
const didCheckList = ["DID", "DNS-DID"];
if (utils.isRawV3Document(form.defaults)) {
const v3Defaults = form.defaults as v3.OpenAttestationDocument;
return didCheckList.includes(v3Defaults.openAttestationMetadata.proof.method);
} else {
const v2Defaults = form.defaults as v2.OpenAttestationDocument;
return v2Defaults.issuers.some((issuer) => {
const identityProof = issuer.identityProof;
if (!identityProof) return false;
return didCheckList.includes(identityProof.type);
});
}
});

let tokenRegistryAddress = "";
let documentStoreAddress = "";
Expand Down
79 changes: 43 additions & 36 deletions src/implementations/config/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v2 } from "@govtechsg/open-attestation";
import { utils, v2, v3 } from "@govtechsg/open-attestation";
import fetch from "node-fetch";
import { info, success } from "signale";
import { highlight } from "../../utils";
Expand Down Expand Up @@ -37,37 +37,28 @@ export const getConfigWithUpdatedForms = ({
dnsTransferableRecord,
}: UpdatedForms): ConfigFile => {
const { wallet, forms } = configFile;
const { encryptedJson } = wallet;
const { address } = JSON.parse(encryptedJson);

const updatedForms = forms.map((form: Form) => {
if (form.type === "VERIFIABLE_DOCUMENT") {
const updatedIssuers = form.defaults.issuers.map((issuer) => {
if (issuer.identityProof) {
if (issuer.identityProof.type === "DNS-TXT") {
issuer.documentStore = documentStoreAddress;
issuer.identityProof.location = dnsVerifiable;
} else if (issuer.identityProof.type === "DID" || issuer.identityProof.type === "DNS-DID") {
issuer.id = `did:ethr:0x${address}`;
issuer.identityProof.location = dnsDid;
issuer.identityProof.key = `did:ethr:0x${address}#controller`;
if (issuer.revocation) {
issuer.revocation.type = "NONE" as v2.RevocationType;
}
}
}
return issuer;
if (utils.isRawV3Document(form.defaults)) {
utils.updateFormV3({
wallet,
form,
documentStoreAddress,
tokenRegistryAddress,
dnsVerifiable: dnsVerifiable || "",
dnsDid: dnsDid || "",
dnsTransferableRecord: dnsTransferableRecord || "",
});
form.defaults.issuers = updatedIssuers;
}

if (form.type === "TRANSFERABLE_RECORD") {
const updatedIssuers = form.defaults.issuers.map((issuer) => {
issuer.tokenRegistry = tokenRegistryAddress;
if (issuer.identityProof?.location) issuer.identityProof.location = dnsTransferableRecord;
return issuer;
} else {
utils.updateFormV2({
wallet,
form,
documentStoreAddress,
tokenRegistryAddress,
dnsVerifiable: dnsVerifiable || "",
dnsDid: dnsDid || "",
dnsTransferableRecord: dnsTransferableRecord || "",
});
form.defaults.issuers = updatedIssuers;
}

return form;
Expand Down Expand Up @@ -124,14 +115,30 @@ export const getDocumentStoreAddress = async (encryptedWalletPath: string): Prom
};

export const validate = (forms: Form[]): boolean => {
const isValidForm = forms.some((form: Form) => {
const isValidFormType = form.type === "TRANSFERABLE_RECORD" && "VERIFIABLE_DOCUMENT";
const isValidIdentityProofType = form.defaults.issuers.some(
(issuer) => issuer.identityProof?.type === "DNS-TXT" && "DNS-DID" && "DID"
);

const isValidForm = forms.map((form: Form) => {
const formTypeCheckList = ["TRANSFERABLE_RECORD", "VERIFIABLE_DOCUMENT"];
const isValidFormType = formTypeCheckList.includes(form.type);
let isValidIdentityProofType: boolean;

const identityProofTypeCheckList = ["DNS-TXT", "DNS-DID", "DID"];
// test for v2/v3 form defaults
if (utils.isRawV3Document(form.defaults)) {
const v3Defaults = form.defaults as v3.OpenAttestationDocument;
isValidIdentityProofType = identityProofTypeCheckList.includes(
v3Defaults.openAttestationMetadata.identityProof.type
);
} else {
const v2Defaults = form.defaults as v2.OpenAttestationDocument;
isValidIdentityProofType = v2Defaults.issuers.some((issuer) => {
const identityProofType = issuer.identityProof?.type;
if (identityProofType) {
return identityProofTypeCheckList.includes(identityProofType);
}
return false;
});
}
return isValidFormType && isValidIdentityProofType;
});

return isValidForm;
const anyInvalidForm = !isValidForm.some((validForm: boolean) => validForm === false);
return anyInvalidForm;
};
4 changes: 2 additions & 2 deletions src/implementations/config/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v2 } from "@govtechsg/open-attestation";
import { OpenAttestationDocument } from "@govtechsg/open-attestation";

type WalletEncryptedJson = {
type: "ENCRYPTED_JSON";
Expand All @@ -10,7 +10,7 @@ export type Dns = string | undefined;
export type Form = {
name: string;
type: "VERIFIABLE_DOCUMENT" | "TRANSFERABLE_RECORD";
defaults: v2.OpenAttestationDocument;
defaults: OpenAttestationDocument;
schema: any;
uiSchema?: any;
attachments?: {
Expand Down

0 comments on commit 52d72ec

Please sign in to comment.