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: v4 document name #291

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions scripts/generateV4JsonSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs";
import path from "path";
import { zodToJsonSchema } from "zod-to-json-schema";
import { V4Document, V4WrappedDocument, V4SignedWrappedDocument } from "../src/4.0/types";
import { V4OpenAttestationDocument, V4WrappedDocument, V4SignedWrappedDocument } from "../src/4.0/types";

const OUTPUT_DIR = path.resolve("./src/4.0/jsonSchemas/__generated__");

Expand All @@ -15,7 +15,7 @@ const ZOD_SCHEMAS = [
{
filename: "v4-document.schema.json",
schemaName: "v4Document",
zodSchema: V4Document,
zodSchema: V4OpenAttestationDocument,
},
{
filename: "v4-wrapped-document.schema.json",
Expand Down
17 changes: 9 additions & 8 deletions src/4.0/__tests__/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { obfuscate, validateSchema, verifySignature } from "../..";
import { cloneDeep, omit } from "lodash";
import { RAW_DOCUMENT_DID, SIGNED_WRAPPED_DOCUMENT_DID, WRAPPED_DOCUMENT_DID } from "../fixtures";
import { V4Document } from "../types";
import { V4OpenAttestationDocument } from "../types";
import { wrapDocument, wrapDocuments } from "../wrap";

const DOCUMENT_ONE = {
Expand All @@ -10,15 +10,15 @@ const DOCUMENT_ONE = {
...RAW_DOCUMENT_DID.credentialSubject,
key1: "test",
},
} satisfies V4Document;
} satisfies V4OpenAttestationDocument;
const DOCUMENT_TWO = {
...RAW_DOCUMENT_DID,
credentialSubject: {
...RAW_DOCUMENT_DID.credentialSubject,
key1: "hello",
key2: "item2",
},
} satisfies V4Document;
} satisfies V4OpenAttestationDocument;

const DOCUMENT_THREE = {
...RAW_DOCUMENT_DID,
Expand All @@ -29,7 +29,7 @@ const DOCUMENT_THREE = {
key3: 3.14159,
key4: false,
},
} satisfies V4Document;
} satisfies V4OpenAttestationDocument;

const DOCUMENT_FOUR = {
...RAW_DOCUMENT_DID,
Expand All @@ -38,15 +38,16 @@ const DOCUMENT_FOUR = {
key1: "item2",
},
};
const DATUM = [DOCUMENT_ONE, DOCUMENT_TWO, DOCUMENT_THREE, DOCUMENT_FOUR] satisfies V4Document[];
const DATUM = [DOCUMENT_ONE, DOCUMENT_TWO, DOCUMENT_THREE, DOCUMENT_FOUR] satisfies V4OpenAttestationDocument[];

describe("V4 E2E Test Scenarios", () => {
describe("Issuing a single document", () => {
test("fails for missing data", async () => {
const missingData = {
...omit(cloneDeep(DOCUMENT_ONE), "issuer"),
};
await expect(wrapDocument(missingData as unknown as V4Document)).rejects.toThrowErrorMatchingInlineSnapshot(`
await expect(wrapDocument(missingData as unknown as V4OpenAttestationDocument)).rejects
.toThrowErrorMatchingInlineSnapshot(`
"Input document does not conform to Open Attestation v4.0 Data Model:
{
"_errors": [],
Expand Down Expand Up @@ -113,7 +114,7 @@ describe("V4 E2E Test Scenarios", () => {
...DATUM,
{
laurent: "task force, assemble!!",
} as unknown as V4Document,
} as unknown as V4OpenAttestationDocument,
];
await expect(wrapDocuments(malformedDatum)).rejects.toThrow(
"Input document does not conform to Verifiable Credentials"
Expand Down Expand Up @@ -171,7 +172,7 @@ describe("V4 E2E Test Scenarios", () => {
const credential = {
...RAW_DOCUMENT_DID,
issuer: modifiedIssuer,
} satisfies V4Document;
} satisfies V4OpenAttestationDocument;
expect(validateSchema(credential)).toStrictEqual(false);
});

Expand Down
19 changes: 12 additions & 7 deletions src/4.0/__tests__/guard.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { SUPPORTED_SIGNING_ALGORITHM } from "../../shared/@types/sign";
import { RAW_DOCUMENT_DID } from "../fixtures";
import { signDocument } from "../sign";
import { W3cVerifiableCredential, V4Document, V4WrappedDocument, V4SignedWrappedDocument } from "../types";
import {
W3cVerifiableCredential,
V4OpenAttestationDocument,
V4WrappedDocument,
V4SignedWrappedDocument,
} from "../types";
import { wrapDocument } from "../wrap";

const RAW_DOCUMENT = {
Expand All @@ -16,7 +21,7 @@ const RAW_DOCUMENT = {
},
],
},
} satisfies V4Document;
} satisfies V4OpenAttestationDocument;

describe("V4.0 guard", () => {
let WRAPPED_DOCUMENT: V4WrappedDocument;
Expand All @@ -41,7 +46,7 @@ describe("V4.0 guard", () => {
});

test("should pass document validation without removal of any data", () => {
const results = V4Document.parse(RAW_DOCUMENT_DID);
const results = V4OpenAttestationDocument.parse(RAW_DOCUMENT_DID);
expect(results).toEqual(RAW_DOCUMENT_DID);
});

Expand Down Expand Up @@ -90,8 +95,8 @@ describe("V4.0 guard", () => {
});

test("should pass document validation without removal of any data", () => {
const v4Document: V4Document = WRAPPED_DOCUMENT;
const results = V4Document.parse(v4Document);
const v4Document: V4OpenAttestationDocument = WRAPPED_DOCUMENT;
const results = V4OpenAttestationDocument.parse(v4Document);
expect(results).toEqual(WRAPPED_DOCUMENT);
});

Expand Down Expand Up @@ -138,8 +143,8 @@ describe("V4.0 guard", () => {
});

test("should pass document validation without removal of any data", () => {
const v4Document: V4Document = SIGNED_WRAPPED_DOCUMENT;
const results = V4Document.parse(v4Document);
const v4Document: V4OpenAttestationDocument = SIGNED_WRAPPED_DOCUMENT;
const results = V4OpenAttestationDocument.parse(v4Document);
expect(results).toEqual(SIGNED_WRAPPED_DOCUMENT);
});

Expand Down
6 changes: 3 additions & 3 deletions src/4.0/__tests__/obfuscate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { obfuscateVerifiableCredential } from "../obfuscate";
import { get } from "lodash";
import { decodeSalt } from "../salt";
import { wrapDocument } from "../wrap";
import { Salt, V4Document, V4WrappedDocument } from "../types";
import { Salt, V4OpenAttestationDocument, V4WrappedDocument } from "../types";
import { verifySignature } from "../../";
import { RAW_DOCUMENT_DID, SIGNED_WRAPPED_DOCUMENT_DID_OBFUSCATED, WRAPPED_DOCUMENT_DID } from "../fixtures";
import { hashLeafNode } from "../digest";
import { getObfuscatedData, isObfuscated } from "../../shared/utils";

const makeV4RawDocument = <T extends Pick<V4Document, "credentialSubject">>(props: T) =>
const makeV4RawDocument = <T extends Pick<V4OpenAttestationDocument, "credentialSubject">>(props: T) =>
({
...RAW_DOCUMENT_DID,
...(props as T),
} satisfies V4Document);
} satisfies V4OpenAttestationDocument);

const findSaltByPath = (salts: string, path: string): Salt | undefined => {
return decodeSalt(salts).find((salt) => salt.path === path);
Expand Down
8 changes: 4 additions & 4 deletions src/4.0/__tests__/wrap.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { V4Document, V4WrappedDocument, W3cVerifiableCredential } from "../types";
import { V4OpenAttestationDocument, V4WrappedDocument, W3cVerifiableCredential } from "../types";
import { wrapDocument } from "../wrap";

describe("V4.0 wrap document", () => {
Expand Down Expand Up @@ -53,7 +53,7 @@ describe("V4.0 wrap document", () => {
id: "did:ethr:0xB26B4941941C51a4885E5B7D3A1B861E54405f90",
name: "Government Technology Agency of Singapore (GovTech)",
identityProof: { identityProofType: "DNS-DID", identifier: "example.openattestation.com" },
} as V4Document["issuer"],
} as V4OpenAttestationDocument["issuer"],
})
).rejects.toThrowErrorMatchingInlineSnapshot(`
"Input document does not conform to Open Attestation v4.0 Data Model:
Expand Down Expand Up @@ -94,7 +94,7 @@ describe("V4.0 wrap document", () => {
},
// this should not exist
extraField: "extra",
} as V4Document)
} as V4OpenAttestationDocument)
).rejects.toThrowErrorMatchingInlineSnapshot(`
"Input document does not conform to Open Attestation v4.0 Data Model:
{
Expand All @@ -118,7 +118,7 @@ describe("V4.0 wrap document", () => {
id: "https://example.com/issuer/123",
},
};
const wrapped = await wrapDocument(genericW3cVc as unknown as V4Document);
const wrapped = await wrapDocument(genericW3cVc as unknown as V4OpenAttestationDocument);
const parsedResults = V4WrappedDocument.pick({ "@context": true, type: true }).passthrough().safeParse(wrapped);
expect(parsedResults.success).toBe(true);
expect(wrapped.proof.merkleRoot.length).toBe(64);
Expand Down
7 changes: 4 additions & 3 deletions src/4.0/diagnose.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Diagnose } from "../shared/utils/@types/diagnose";
import { V4WrappedDocument, V4SignedWrappedDocument, V4Document } from "./types";
import { V4WrappedDocument, V4SignedWrappedDocument, V4OpenAttestationDocument } from "./types";

export const v4Diagnose: Diagnose = ({ document, kind, debug }) => {
let Validator: typeof V4Document | typeof V4WrappedDocument | typeof V4SignedWrappedDocument = V4Document;
let Validator: typeof V4OpenAttestationDocument | typeof V4WrappedDocument | typeof V4SignedWrappedDocument =
V4OpenAttestationDocument;
if (kind === "raw") {
Validator = V4Document;
Validator = V4OpenAttestationDocument;
} else if (kind === "wrapped") {
Validator = V4WrappedDocument;
} else {
Expand Down
32 changes: 16 additions & 16 deletions src/4.0/documentBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import {
SvgRenderer,
OscpResponderRevocation,
RevocationStoreRevocation,
V4Document,
V4OpenAttestationDocument,
V4SignedWrappedDocument,
V4WrappedDocument,
} from "./types";

import { ZodError, z } from "zod";

const SingleDocumentProps = z.object({
name: V4Document.shape.name.unwrap(),
credentialSubject: V4Document.shape.credentialSubject,
name: V4OpenAttestationDocument.shape.name.unwrap(),
credentialSubject: V4OpenAttestationDocument.shape.credentialSubject,
});

const DocumentProps = z.union([SingleDocumentProps, z.array(SingleDocumentProps)]);
Expand Down Expand Up @@ -46,9 +46,9 @@ const SvgRendererProps = z.discriminatedUnion("type", [
]);

const DnsTextIssuanceProps = z.object({
issuerId: V4Document.shape.issuer.shape.id,
issuerName: V4Document.shape.issuer.shape.name,
identityProofDomain: V4Document.shape.issuer.shape.identityProof.shape.identifier,
issuerId: V4OpenAttestationDocument.shape.issuer.shape.id,
issuerName: V4OpenAttestationDocument.shape.issuer.shape.name,
identityProofDomain: V4OpenAttestationDocument.shape.issuer.shape.identityProof.shape.identifier,
});

/**
Expand Down Expand Up @@ -83,14 +83,14 @@ type DocumentProps = {
*
* Maps to "credentialSubject"
*/
credentialSubject: z.infer<typeof V4Document.shape.credentialSubject>;
credentialSubject: z.infer<typeof V4OpenAttestationDocument.shape.credentialSubject>;
};

type State = {
documentMainProps: DocumentProps | DocumentProps[];
renderMethod: V4Document["renderMethod"];
issuer: V4Document["issuer"] | undefined;
credentialStatus: V4Document["credentialStatus"];
renderMethod: V4OpenAttestationDocument["renderMethod"];
issuer: V4OpenAttestationDocument["issuer"] | undefined;
credentialStatus: V4OpenAttestationDocument["credentialStatus"];
};

/**
Expand Down Expand Up @@ -141,7 +141,7 @@ export class DocumentBuilder<Props extends DocumentProps | DocumentProps[]> {
credentialSubject,
...(renderMethod && { renderMethod }),
...(credentialStatus && { credentialStatus }),
} satisfies V4Document)
} satisfies V4OpenAttestationDocument)
);

return wrapDocuments(toWrap) as unknown as WrappedReturn<Props>;
Expand Down Expand Up @@ -239,7 +239,7 @@ export class DocumentBuilder<Props extends DocumentProps | DocumentProps[]> {
identityProofType: "DNS-TXT",
identifier: identityProofDomain,
},
} satisfies V4Document["issuer"];
} satisfies V4OpenAttestationDocument["issuer"];
this.setState("issuer", issuer);

return {
Expand Down Expand Up @@ -287,7 +287,7 @@ export class DocumentBuilder<Props extends DocumentProps | DocumentProps[]> {
const credentialStatus = {
id: oscpUrl,
type: "OpenAttestationOcspResponder",
} satisfies V4Document["credentialStatus"];
} satisfies V4OpenAttestationDocument["credentialStatus"];
this.setState("credentialStatus", credentialStatus);

return this.ISSUANCE_METHODS;
Expand All @@ -312,7 +312,7 @@ export class DocumentBuilder<Props extends DocumentProps | DocumentProps[]> {
const credentialStatus = {
id: storeAddress,
type: "OpenAttestationRevocationStore",
} satisfies V4Document["credentialStatus"];
} satisfies V4OpenAttestationDocument["credentialStatus"];

this.setState("credentialStatus", credentialStatus);

Expand Down Expand Up @@ -350,7 +350,7 @@ export class DocumentBuilder<Props extends DocumentProps | DocumentProps[]> {
type: "OpenAttestationEmbeddedRenderer",
templateName,
},
] satisfies V4Document["renderMethod"];
] satisfies V4OpenAttestationDocument["renderMethod"];
this.setState("renderMethod", renderMethod);

return this.REVOCATION_METHODS;
Expand Down Expand Up @@ -408,7 +408,7 @@ export class DocumentBuilder<Props extends DocumentProps | DocumentProps[]> {
type: "SvgRenderingTemplate2023" as const,
digestMultibase: parsedResults.data.digestMultibase,
},
] satisfies V4Document["renderMethod"]);
] satisfies V4OpenAttestationDocument["renderMethod"]);
this.setState("renderMethod", renderMethod);

return this.REVOCATION_METHODS;
Expand Down
2 changes: 1 addition & 1 deletion src/4.0/exports/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type {
V4Document as Document,
V4OpenAttestationDocument as OpenAttestationDocument,
V4WrappedDocument as WrappedDocument,
V4SignedWrappedDocument as SignedWrappedDocument,
} from "../types";
2 changes: 1 addition & 1 deletion src/4.0/exports/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { v4Diagnose as diagnose } from "../diagnose";
export {
isV4Document as isDocument,
isV4OpenAttestationDocument as isOpenAttestationDocument,
isV4WrappedDocument as isWrappedDocument,
isV4SignedWrappedDocument as isSignedWrappedDocument,
} from "../types";
Expand Down
8 changes: 4 additions & 4 deletions src/4.0/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { V4Document, V4SignedWrappedDocument, V4WrappedDocument } from "./types";
import { V4OpenAttestationDocument, V4SignedWrappedDocument, V4WrappedDocument } from "./types";

const ISSUER_ID = "did:ethr:0xE712878f6E8d5d4F9e87E10DA604F9cB564C9a89" as const;
export const SAMPLE_SIGNING_KEYS = {
Expand Down Expand Up @@ -44,7 +44,7 @@ export const RAW_DOCUMENT_DID = freezeObject({
},
],
},
} satisfies V4Document);
} satisfies V4OpenAttestationDocument);

export const RAW_DOCUMENT_DID_OSCP = freezeObject({
"@context": [
Expand Down Expand Up @@ -88,7 +88,7 @@ export const RAW_DOCUMENT_DID_OSCP = freezeObject({
},
],
},
} satisfies V4Document);
} satisfies V4OpenAttestationDocument);

export const BATCHED_RAW_DOCUMENTS_DID = freezeObject([
{
Expand Down Expand Up @@ -170,7 +170,7 @@ export const BATCHED_RAW_DOCUMENTS_DID = freezeObject([
},
],
},
] satisfies V4Document[]);
] satisfies V4OpenAttestationDocument[]);

export const WRAPPED_DOCUMENT_DID = freezeObject({
"@context": [
Expand Down
4 changes: 2 additions & 2 deletions src/4.0/sign.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { sign } from "../shared/signer";
import { SigningKey } from "../shared/@types/sign";
import { ethers } from "ethers";
import { V4Document, V4WrappedDocument, V4SignedWrappedDocument } from "./types";
import { V4OpenAttestationDocument, V4WrappedDocument, V4SignedWrappedDocument } from "./types";
import type { ZodError } from "zod";

export const signDocument = async <T extends V4Document>(
export const signDocument = async <T extends V4OpenAttestationDocument>(
document: V4SignedWrappedDocument<T> | V4WrappedDocument<T>,
algorithm: "Secp256k1VerificationKey2018",
keyOrSigner: SigningKey | ethers.Signer
Expand Down
Loading
Loading