-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: shift context constants into 4.0 * refactor: bring context down to root of 4.0 * refactor: add custom error class, change algorithm to non enum * refactor: only leave the bare essentials of v4 in root index, fix tests * refactor: free up 4.0 from the mess of dependencies caused by importing of utils * fix: circular dependency caused by getData * feat: v4 utility guards * feat: export v4
- Loading branch information
Showing
25 changed files
with
186 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { unsaltData } from "./salt"; | ||
import { OpenAttestationDocument, WrappedDocument } from "./types"; | ||
|
||
type Extract<P> = P extends WrappedDocument<infer T> ? T : never; | ||
export const getData = <T extends WrappedDocument<OpenAttestationDocument>>(document: T): Extract<T> => { | ||
return unsaltData(document.data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export * from "./wrap"; | ||
export * from "./sign"; | ||
export * from "./obfuscate"; | ||
export * from "./verify"; | ||
export * from "./utils"; | ||
export * from "./types"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { obfuscateVerifiableCredential as obfuscate, obfuscateErrors } from "../obfuscate"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { signDocument, signDocumentErrors } from "../sign"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type { | ||
V4Document as Document, | ||
V4WrappedDocument as WrappedDocument, | ||
V4SignedWrappedDocument as SignedWrappedDocument, | ||
} from "../types"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export { v4Diagnose as diagnose } from "../diagnose"; | ||
export { | ||
isV4Document as isDocument, | ||
isV4WrappedDocument as isWrappedDocument, | ||
isV4SignedWrappedDocument as isSignedWrappedDocument, | ||
} from "../types"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { verify } from "../verify"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { wrapDocument, wrapDocuments, wrapDocumentErrors } from "../wrap"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
import { sign } from "../shared/signer"; | ||
import { SigningKey, SUPPORTED_SIGNING_ALGORITHM } from "../shared/@types/sign"; | ||
import { SigningKey } from "../shared/@types/sign"; | ||
import { ethers } from "ethers"; | ||
import { V4Document, V4WrappedDocument, V4SignedWrappedDocument } from "./types"; | ||
import type { ZodError } from "zod"; | ||
|
||
export const signDocument = async <T extends V4Document>( | ||
document: V4SignedWrappedDocument<T> | V4WrappedDocument<T>, | ||
algorithm: SUPPORTED_SIGNING_ALGORITHM, | ||
algorithm: "Secp256k1VerificationKey2018", | ||
keyOrSigner: SigningKey | ethers.Signer | ||
): Promise<V4SignedWrappedDocument<T>> => { | ||
const parsedResults = V4WrappedDocument.pick({ proof: true }).passthrough().safeParse(document); | ||
if (!parsedResults.success) { | ||
throw new Error("Document has not been properly wrapped " + JSON.stringify(parsedResults.error)); | ||
throw new WrappedDocumentValidationError(parsedResults.error); | ||
} | ||
|
||
if (!SigningKey.guard(keyOrSigner) && keyOrSigner.signMessage === undefined) { | ||
throw new Error(`Either a keypair or ethers.js Signer must be provided`); | ||
} | ||
|
||
const { proof: validatedProof } = parsedResults.data; | ||
const merkleRoot = `0x${validatedProof.merkleRoot}`; | ||
const signature = await sign(algorithm, merkleRoot, keyOrSigner); | ||
const proof: V4SignedWrappedDocument["proof"] = { | ||
...validatedProof, | ||
key: SigningKey.guard(keyOrSigner) ? keyOrSigner.public : `did:ethr:${await keyOrSigner.getAddress()}#controller`, | ||
key: "public" in keyOrSigner ? keyOrSigner.public : `did:ethr:${await keyOrSigner.getAddress()}#controller`, | ||
signature, | ||
}; | ||
return { ...document, proof }; | ||
}; | ||
|
||
class WrappedDocumentValidationError extends Error { | ||
constructor(public error: ZodError) { | ||
super(`Document has not been properly wrapped: \n ${JSON.stringify(error.format(), null, 2)}`); | ||
Object.setPrototypeOf(this, WrappedDocumentValidationError.prototype); | ||
} | ||
} | ||
|
||
export const signDocumentErrors = { | ||
WrappedDocumentValidationError, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.