forked from Open-Attestation/open-attestation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
118 lines (108 loc) · 5.16 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { validateSchema as validate } from "./shared/validate";
import { verify } from "./2.0/verify";
import { verify as verifyV3 } from "./3.0/verify";
import { wrapDocument as wrapV2Document, wrapDocuments as wrapV2Documents } from "./2.0/wrap";
import { signDocument as signV2Document } from "./2.0/sign";
import { WrappedDocument as WrappedDocumentV2 } from "./2.0/types";
import { WrappedDocument as WrappedDocumentV3 } from "./3.0/types";
import { wrapDocument as wrapV3Document, wrapDocuments as wrapV3Documents } from "./3.0/wrap";
import { signDocument as signV3Document } from "./3.0/sign";
import { SchemaId, WrappedDocument, OpenAttestationDocument } from "./shared/@types/document";
import * as utils from "./shared/utils";
import * as v2 from "./2.0/types";
import { OpenAttestationDocument as OpenAttestationDocumentV2 } from "./__generated__/schema.2.0";
import * as v3 from "./3.0/types";
import { OpenAttestationDocument as OpenAttestationDocumentV3 } from "./__generated__/schema.3.0";
import { obfuscateDocument as obfuscateDocumentV2 } from "./2.0/obfuscate";
import { obfuscateVerifiableCredential } from "./3.0/obfuscate";
import { WrapDocumentOptionV2, WrapDocumentOptionV3 } from "./shared/@types/wrap";
import { SchemaValidationError } from "./shared/utils";
import { SigningKey, SUPPORTED_SIGNING_ALGORITHM } from "./shared/@types/sign";
import { ethers, Signer } from "ethers";
import { getSchema } from "./shared/ajv";
export function __unsafe__use__it__at__your__own__risks__wrapDocument<T extends OpenAttestationDocumentV3>(
data: T,
options?: WrapDocumentOptionV3
): Promise<WrappedDocumentV3<T>> {
return wrapV3Document(data, options ?? { version: SchemaId.v3 });
}
export function __unsafe__use__it__at__your__own__risks__wrapDocuments<T extends OpenAttestationDocumentV3>(
dataArray: T[],
options?: WrapDocumentOptionV3
): Promise<WrappedDocumentV3<T>[]> {
return wrapV3Documents(dataArray, options ?? { version: SchemaId.v3 });
}
export function wrapDocument<T extends OpenAttestationDocumentV2>(
data: T,
options?: WrapDocumentOptionV2
): WrappedDocumentV2<T> {
return wrapV2Document(data, { externalSchemaId: options?.externalSchemaId });
}
export function wrapDocuments<T extends OpenAttestationDocumentV2>(
dataArray: T[],
options?: WrapDocumentOptionV2
): WrappedDocumentV2<T>[] {
return wrapV2Documents(dataArray, { externalSchemaId: options?.externalSchemaId });
}
export const validateSchema = (document: WrappedDocument<any>): boolean => {
return validate(document, getSchema(`${document?.version || SchemaId.v2}`)).length === 0;
};
export function verifySignature<T extends WrappedDocument<OpenAttestationDocument>>(document: T) {
return utils.isWrappedV3Document(document) ? verifyV3(document) : verify(document);
}
export function obfuscate<T extends OpenAttestationDocumentV2>(
document: WrappedDocument<T>,
fields: string[] | string
): WrappedDocument<T>;
export function obfuscate<T extends OpenAttestationDocumentV3>(
document: WrappedDocument<T>,
fields: string[] | string
): WrappedDocument<T>;
export function obfuscate(document: any, fields: string[] | string) {
return document.version === SchemaId.v3
? obfuscateVerifiableCredential(document, fields)
: obfuscateDocumentV2(document, fields);
}
export const isSchemaValidationError = (error: any): error is SchemaValidationError => {
return !!error.validationErrors;
};
export async function signDocument<T extends v3.OpenAttestationDocument>(
document: v3.SignedWrappedDocument<T> | v3.WrappedDocument<T>,
algorithm: SUPPORTED_SIGNING_ALGORITHM,
keyOrSigner: SigningKey | ethers.Signer
): Promise<v3.SignedWrappedDocument<T>>;
export async function signDocument<T extends v2.OpenAttestationDocument>(
document: v2.SignedWrappedDocument<T> | v2.WrappedDocument<T>,
algorithm: SUPPORTED_SIGNING_ALGORITHM,
keyOrSigner: SigningKey | ethers.Signer
): Promise<v2.SignedWrappedDocument<T>>;
export async function signDocument(
document: any,
algorithm: SUPPORTED_SIGNING_ALGORITHM,
keyOrSigner: SigningKey | ethers.Signer
) {
// rj was worried it could happen deep in the code, so I moved it to the boundaries
if (!SigningKey.guard(keyOrSigner) && !Signer.isSigner(keyOrSigner)) {
throw new Error(`Either a keypair or ethers.js Signer must be provided`);
}
switch (true) {
case utils.isWrappedV2Document(document):
return signV2Document(document, algorithm, keyOrSigner);
case utils.isWrappedV3Document(document):
return signV3Document(document, algorithm, keyOrSigner);
default:
// Unreachable code atm until utils.isWrappedV2Document & utils.isWrappedV3Document becomes more strict
throw new Error("Unsupported document type: Only OpenAttestation v2 & v3 documents can be signed");
}
}
export { digestDocument } from "./2.0/digest";
export { digestCredential } from "./3.0/digest";
export { checkProof, MerkleTree } from "./shared/merkle";
export { obfuscate as obfuscateDocument };
export { utils };
export * from "./shared/@types/document";
export * from "./shared/@types/sign";
export * from "./shared/signer";
export { getData } from "./shared/utils"; // keep it to avoid breaking change, moved from privacy to utils
export { v2 };
export { v3 };