Skip to content

Commit

Permalink
Merge pull request #276 from Open-Attestation/wip/oa-v4-types
Browse files Browse the repository at this point in the history
chore: Wip/oa v4 types
  • Loading branch information
phanshiyu authored Apr 26, 2024
2 parents bc48b98 + d6ff500 commit 2c45f54
Show file tree
Hide file tree
Showing 24 changed files with 760 additions and 683 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ yarn.lock
*.iml
/public
/vc-test-suite
/.vscode
5 changes: 0 additions & 5 deletions scripts/postInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ if (fs.existsSync(quicktype) && process.env.npm_config_production !== "true") {
quicktype +
" -s schema -o src/__generated__/schema.3.0.ts -t OpenAttestationDocument --just-types src/3.0/schema/schema.json --no-date-times"
);
console.log('"Creating types from src/4.0/schema/schema.json"');
execSync(
quicktype +
" -s schema -o src/__generated__/schema.4.0.ts -t OpenAttestationDocument --just-types src/4.0/schema/schema.json --no-date-times"
);
} else {
console.log("Not running quicktype");
}
4 changes: 0 additions & 4 deletions scripts/publishSchema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,3 @@ cp src/2.0/schema/schema.json public/2.0/schema.json
# Copy 3.0 schema to public folder
mkdir -p public/3.0/
cp src/3.0/schema/schema.json public/3.0/schema.json

# Copy 4.0 schema to public folder
mkdir -p public/4.0/
cp src/4.0/schema/schema.json public/4.0/schema.json
4 changes: 2 additions & 2 deletions src/4.0/__tests__/digest.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { cloneDeep } from "lodash";
import { digestCredential } from "../digest";
import { WrappedDocument } from "../../4.0/types";
import { obfuscateVerifiableCredential } from "../obfuscate";
import { decodeSalt } from "../salt";
import sample from "../../../test/fixtures/v4/did-signed-wrapped.json";
import { V4WrappedDocument } from "../types";

// TODO: remove unknown
const verifiableCredential = sample as unknown as WrappedDocument;
const verifiableCredential = sample as unknown as V4WrappedDocument;
// Digest will change whenever sample document is regenerated
const credentialRoot = "f49be3b06f7a7eb074775ad12aae43936084c86646e3640eae18e7aeca4f7468";

Expand Down
273 changes: 273 additions & 0 deletions src/4.0/__tests__/guard.test.ts

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/4.0/diagnose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Diagnose } from "src/shared/utils/@types/diagnose";
import { V4WrappedDocument, V4SignedWrappedDocument, V4Document } from "./types";

export const v4Diagnose: Diagnose = ({ document, kind, debug }) => {
let Validator: typeof V4Document | typeof V4WrappedDocument | typeof V4SignedWrappedDocument = V4Document;
if (kind === "raw") {
Validator = V4Document;
} else if (kind === "wrapped") {
Validator = V4WrappedDocument;
} else {
Validator = V4SignedWrappedDocument;
}

const results = Validator.safeParse(document);

if (results.success) {
return [];
}

return results.error.errors.map(({ code, message, path }) => {
const errorMessage = `${code}: ${message} at ${path.join(".")}`;
if (debug) {
console.debug(errorMessage);
}
return {
message: errorMessage,
};
});
};
5 changes: 2 additions & 3 deletions src/4.0/digest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { get, sortBy } from "lodash";
import { keccak256 } from "js-sha3";
import { Salt } from "./types";
import { OpenAttestationDocument } from "../__generated__/schema.4.0";
import { V4Document, Salt } from "./types";

export const digestCredential = (document: OpenAttestationDocument, salts: Salt[], obfuscatedData: string[]) => {
export const digestCredential = (document: V4Document, salts: Salt[], obfuscatedData: string[]) => {
// Prepare array of hashes from visible data
const hashedUnhashedDataArray = salts
// Explictly allow falsy values (e.g. false, 0, etc.) as they can exist in the document
Expand Down
13 changes: 6 additions & 7 deletions src/4.0/obfuscate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { OpenAttestationDocument } from "../__generated__/schema.4.0";
import { toBuffer } from "../shared/utils";
import { WrappedDocument } from "./types";
import { cloneDeep, get, unset, pick } from "lodash";
import { decodeSalt, encodeSalt } from "./salt";
import { traverseAndFlatten } from "./traverseAndFlatten";
import { V4Document, V4WrappedDocument } from "./types";

const obfuscate = (_data: WrappedDocument<OpenAttestationDocument>, fields: string[] | string) => {
const obfuscate = (_data: V4WrappedDocument, fields: string[] | string) => {
const data = cloneDeep(_data); // Prevents alteration of original data

const fieldsAsArray = ([] as string[]).concat(fields);
Expand Down Expand Up @@ -37,10 +36,10 @@ const obfuscate = (_data: WrappedDocument<OpenAttestationDocument>, fields: stri
};
};

export const obfuscateVerifiableCredential = (
document: WrappedDocument<OpenAttestationDocument>,
export const obfuscateVerifiableCredential = <T extends V4Document = V4Document>(
document: V4WrappedDocument<T>,
fields: string[] | string
): WrappedDocument<OpenAttestationDocument> => {
): V4WrappedDocument<T> => {
const { data, obfuscatedData } = obfuscate(document, fields);
const currentObfuscatedData = document.proof.privacy.obfuscated;
const newObfuscatedData = currentObfuscatedData.concat(obfuscatedData);
Expand All @@ -53,5 +52,5 @@ export const obfuscateVerifiableCredential = (
obfuscated: newObfuscatedData,
},
},
};
} satisfies V4WrappedDocument as V4WrappedDocument<T>;
};
183 changes: 0 additions & 183 deletions src/4.0/schema/schema.json

This file was deleted.

36 changes: 0 additions & 36 deletions src/4.0/schema/schema.test.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/4.0/sign.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { OpenAttestationDocument, WrappedDocument, SignedWrappedDocument, SignedWrappedProof } from "./types";
import { sign } from "../shared/signer";
import { SigningKey, SUPPORTED_SIGNING_ALGORITHM } from "../shared/@types/sign";
import { isSignedWrappedV4Document } from "../shared/utils";
import { ethers } from "ethers";
import { V4Document, V4WrappedDocument, V4SignedWrappedDocument } from "./types";

export const signDocument = async <T extends OpenAttestationDocument>(
document: SignedWrappedDocument<T> | WrappedDocument<T>,
export const signDocument = async <T extends V4Document>(
document: V4SignedWrappedDocument<T> | V4WrappedDocument<T>,
algorithm: SUPPORTED_SIGNING_ALGORITHM,
keyOrSigner: SigningKey | ethers.Signer
): Promise<SignedWrappedDocument<T>> => {
): Promise<V4SignedWrappedDocument<T>> => {
if (isSignedWrappedV4Document(document)) throw new Error("Document has been signed");
const merkleRoot = `0x${document.proof.merkleRoot}`;
const signature = await sign(algorithm, merkleRoot, keyOrSigner);
const proof: SignedWrappedProof = {
const proof: V4SignedWrappedDocument["proof"] = {
...document.proof,
key: SigningKey.guard(keyOrSigner) ? keyOrSigner.public : `did:ethr:${await keyOrSigner.getAddress()}#controller`,
signature,
Expand Down
Loading

0 comments on commit 2c45f54

Please sign in to comment.