diff --git a/packages/api-key-stamper/src/tink/elliptic_curves.ts b/packages/api-key-stamper/src/tink/elliptic_curves.ts index 085000ec9..4fd970b72 100644 --- a/packages/api-key-stamper/src/tink/elliptic_curves.ts +++ b/packages/api-key-stamper/src/tink/elliptic_curves.ts @@ -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 diff --git a/packages/api-key-stamper/src/utils.ts b/packages/api-key-stamper/src/utils.ts index 07febeb8b..bb8f9e78f 100644 --- a/packages/api-key-stamper/src/utils.ts +++ b/packages/api-key-stamper/src/utils.ts @@ -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( diff --git a/packages/sdk-browser/src/utils.ts b/packages/sdk-browser/src/utils.ts index e99b3983a..21d5ba904 100644 --- a/packages/sdk-browser/src/utils.ts +++ b/packages/sdk-browser/src/utils.ts @@ -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",