diff --git a/src/4.0/__tests__/sign.test.ts b/src/4.0/__tests__/sign.test.ts index 04fb3434..30d1042c 100644 --- a/src/4.0/__tests__/sign.test.ts +++ b/src/4.0/__tests__/sign.test.ts @@ -88,8 +88,8 @@ describe("V4 sign", () => { } ) ).rejects.toThrowErrorMatchingInlineSnapshot(` - "Document has not been properly wrapped: - { + "Document has not been properly wrapped: + { "_errors": [], "proof": { "_errors": [], diff --git a/src/4.0/exports/verify.ts b/src/4.0/exports/verify.ts index 3ada29a3..28c88f93 100644 --- a/src/4.0/exports/verify.ts +++ b/src/4.0/exports/verify.ts @@ -1 +1 @@ -export { verify } from "../verify"; +export { verify as verifySignature } from "../verify"; diff --git a/src/4.0/sign.ts b/src/4.0/sign.ts index fbcb7ac5..db0f8cb1 100644 --- a/src/4.0/sign.ts +++ b/src/4.0/sign.ts @@ -20,22 +20,38 @@ export const signDocument = async ( const { proof: validatedProof } = parsedResults.data; const merkleRoot = `0x${validatedProof.merkleRoot}`; - const signature = await sign(algorithm, merkleRoot, keyOrSigner); - const proof: V4SignedWrappedDocument["proof"] = { - ...validatedProof, - key: "public" in keyOrSigner ? keyOrSigner.public : `did:ethr:${await keyOrSigner.getAddress()}#controller`, - signature, - }; - return { ...document, proof }; + + try { + const signature = await sign(algorithm, merkleRoot, keyOrSigner); + const proof: V4SignedWrappedDocument["proof"] = { + ...validatedProof, + key: "public" in keyOrSigner ? keyOrSigner.public : `did:ethr:${await keyOrSigner.getAddress()}#controller`, + signature, + }; + return { ...document, proof }; + } catch (error) { + throw new CouldNotSignDocumentError(error); + } }; class WrappedDocumentValidationError extends Error { constructor(public error: ZodError) { - super(`Document has not been properly wrapped: \n ${JSON.stringify(error.format(), null, 2)}`); + super(`Document has not been properly wrapped:\n${JSON.stringify(error.format(), null, 2)}`); Object.setPrototypeOf(this, WrappedDocumentValidationError.prototype); } } +/** + * Cases where this can be thrown includes: network error, invalid keys or signer + */ +class CouldNotSignDocumentError extends Error { + constructor(public error: unknown) { + super(`Could not sign document:\n${error instanceof Error ? error.message : JSON.stringify(error, null, 2)}`); + Object.setPrototypeOf(this, CouldNotSignDocumentError.prototype); + } +} + export const signDocumentErrors = { WrappedDocumentValidationError, + CouldNotSignDocumentError, };