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

chore: Wip/oa v4 types #276

Merged
merged 43 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
d3aafc2
wip: oa v4 types
HJunyuan Apr 24, 2024
b102ee9
refactor: prefer string union
phanshiyu Apr 24, 2024
e693b92
refactor: reference constant tuple instead
phanshiyu Apr 24, 2024
c4c91fe
refactor: finalTypes follows pattern of finalContexts
phanshiyu Apr 24, 2024
5ff08a3
refactor: document -> raw document for clarity
phanshiyu Apr 24, 2024
23af438
fix: rawDocument should not be used directly for digest and salting
phanshiyu Apr 24, 2024
5cc50cf
fix: remove asserting to string to let the literal infer properly
phanshiyu Apr 24, 2024
e7e4c72
fix: typings
phanshiyu Apr 24, 2024
27e6e8e
refactor: move assertion earlier
phanshiyu Apr 24, 2024
2bc4066
refactor: assert oa vc than try vc
phanshiyu Apr 24, 2024
5fa7561
fix: refer to new oa vc type in digest and verify
phanshiyu Apr 24, 2024
9ce1255
fix: refer to new oa vc typings
phanshiyu Apr 24, 2024
e6bff07
fix: refer to new oa vc typings
phanshiyu Apr 24, 2024
a259eac
fix: refer to new oa vc typings
phanshiyu Apr 24, 2024
8dfd6af
refactor: improve naming consistency
phanshiyu Apr 24, 2024
9fce84a
fix: typing, should be looser
phanshiyu Apr 24, 2024
d409c68
fix: a union b does not override a props with b props
phanshiyu Apr 24, 2024
e30c8e8
fix: added assertion type to prevent accident extension to base type
phanshiyu Apr 25, 2024
194f19e
refactor: wording
phanshiyu Apr 25, 2024
cd34a7d
refactor: better readability for wrapped types
phanshiyu Apr 25, 2024
d3baf69
refactor: move things within file and some renaming
phanshiyu Apr 25, 2024
3bdc13e
refactor: improve naming of variables
phanshiyu Apr 25, 2024
db12f50
fix: use new typings
phanshiyu Apr 25, 2024
22df165
fix: passthroughs needed to allow extension, added name and render me…
phanshiyu Apr 25, 2024
fc0ce61
test: added a guard test which is currently failing
phanshiyu Apr 25, 2024
8f42178
refactor: merged data model with types and renamed VC type
phanshiyu Apr 25, 2024
3187237
refactor: improve helper fn naming
phanshiyu Apr 25, 2024
0f3d311
fix: use extend instead of and since it results in dup errors, remove…
phanshiyu Apr 26, 2024
3c40ebc
test: update snapshot
phanshiyu Apr 26, 2024
946ae63
fix: was using the wrong document for the check
phanshiyu Apr 26, 2024
d8c4694
test: added tests for wrapped only document
phanshiyu Apr 26, 2024
c5a5c96
fix: diagnose to use new guards
phanshiyu Apr 26, 2024
9561fbe
fix: path
phanshiyu Apr 26, 2024
aa66692
chore: remove vscode
phanshiyu Apr 26, 2024
9b1900e
chore: ignore vscode settings
phanshiyu Apr 26, 2024
ff57b1c
chore: remove experimental version guard
phanshiyu Apr 26, 2024
6245038
fix: add back the missing wrapDocuments that was accidentally removed
phanshiyu Apr 26, 2024
4d965a4
fix: replace back original implementation of getMerkleRoot
phanshiyu Apr 26, 2024
5b5ecea
fix: make our own real types stricter
phanshiyu Apr 26, 2024
a99584a
fix: diagnose did not handle raw v4
phanshiyu Apr 26, 2024
a8219ee
refactor: remove useless renaming
phanshiyu Apr 26, 2024
26fa265
fix: object object in test case
phanshiyu Apr 26, 2024
d6ff500
refactor: improve type safety of wrap document v4
phanshiyu Apr 26, 2024
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
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
Loading