Skip to content

Commit

Permalink
Change where specific errors are thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
zkharit committed Dec 20, 2024
1 parent 4ae9610 commit 4136fb3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/api-key-stamper/src/tink/elliptic_curves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export function pointDecode(point: Uint8Array): JsonWebKey {
point.length !== uncompressedLength
) {
throw new Error(
"Invalid API key: Ensure that you are using a valid public and private key for your API key"
"Invalid length: point is not in compressed or uncompressed format"
);
}
// Decodes point if its length and first bit match the compressed format
Expand Down
7 changes: 6 additions & 1 deletion packages/api-key-stamper/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export function convertTurnkeyApiKeyToJwk(input: {
}): JsonWebKey {
const { uncompressedPrivateKeyHex, compressedPublicKeyHex } = input;

const jwk = pointDecode(uint8ArrayFromHexString(compressedPublicKeyHex));
let jwk;
try {
jwk = pointDecode(uint8ArrayFromHexString(compressedPublicKeyHex));
} catch (e: Error) {
throw new Error(`invalid API key: Ensure that you are using a valid public and private key in compressed or uncompressed format and they are not switched`);
}

// Ensure that d is sufficiently padded
jwk.d = hexStringToBase64url(
Expand Down
9 changes: 8 additions & 1 deletion packages/sdk-browser/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ export const createEmbeddedAPIKey = async (

// 3: import the targetPublicKey (i.e. passed in from the iframe)
const targetKeyBytes = uint8ArrayFromHexString(targetPublicKey);
const jwk = pointDecode(targetKeyBytes);

let jwk;
try {
jwk = pointDecode(targetKeyBytes);
} catch (e: Error) {
// provide more context about the error that is being thrown
throw new Error(`target public key is not a valid compressed public key: ${targetPublicKey}`);
}

const targetKey = await crypto.subtle.importKey(
"jwk",
Expand Down

0 comments on commit 4136fb3

Please sign in to comment.