Skip to content

Commit b95e13c

Browse files
committed
rethrow missingPublicKeyError
1 parent 52b277b commit b95e13c

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/crypto-v3.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ export default class CryptoV3 extends CryptoBase {
122122

123123
return returnedObject
124124
} catch (err) {
125+
// Should only throw if MissingPublicKeyError.
126+
// This library should be able to be used to encrypt and decrypt content
127+
// if the content does not contain verified fields.
128+
if (err instanceof MissingPublicKeyError) {
129+
throw err
130+
}
125131
return null
126132
}
127133
}
@@ -134,25 +140,34 @@ export default class CryptoV3 extends CryptoBase {
134140
* @param decryptParams.encryptedSubmissionSecretKey The encrypted submission secret key encoded with base-64.
135141
* @param decryptParams.version The version of the payload. Used to determine the decryption process to decrypt the content with.
136142
* @returns The decrypted content if successful. Else, null will be returned.
143+
* @throws {MissingPublicKeyError} if a public key is not provided when instantiating this class and is needed for verifying signed content.
137144
*/
138145
decrypt = (
139146
formSecretKey: string,
140147
decryptParams: DecryptParamsV3
141148
): DecryptedContentV3 | null => {
142-
const { encryptedSubmissionSecretKey, ...rest } = decryptParams
149+
try {
150+
const { encryptedSubmissionSecretKey, ...rest } = decryptParams
143151

144-
const submissionSecretKey = decryptContent(
145-
formSecretKey,
146-
encryptedSubmissionSecretKey
147-
)
152+
const submissionSecretKey = decryptContent(
153+
formSecretKey,
154+
encryptedSubmissionSecretKey
155+
)
148156

149-
if (submissionSecretKey === null) return null
157+
if (submissionSecretKey === null) return null
150158

151-
return this.decryptFromSubmissionKey(
152-
encodeBase64(submissionSecretKey),
153-
rest
154-
)
155-
}
159+
return this.decryptFromSubmissionKey(
160+
encodeBase64(submissionSecretKey),
161+
rest
162+
)
163+
164+
} catch(err) {
165+
if (err instanceof MissingPublicKeyError) {
166+
// rethrow to let the caller decide how to handle missing signing key
167+
throw err
168+
}
169+
return null
170+
}
156171

157172
/**
158173
* Returns true if a pair of public & secret keys are associated with each other

0 commit comments

Comments
 (0)