From 4c5427a8d7cfb4d8d509a48b863d2eeb07c609a9 Mon Sep 17 00:00:00 2001 From: Zane Kharitonov Date: Thu, 19 Dec 2024 17:35:04 -0500 Subject: [PATCH 1/7] Common error message update --- packages/api-key-stamper/src/tink/elliptic_curves.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api-key-stamper/src/tink/elliptic_curves.ts b/packages/api-key-stamper/src/tink/elliptic_curves.ts index 4fd970b72..2c5a3d9d0 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 length: point is not in compressed or uncompressed format" + "Invalid API: Ensure that you are using a valid public and private key for your API key" ); } // Decodes point if its length and first bit match the compressed format From 4ae961027aa6ba97defbbb0544703c2ab6e4ffe9 Mon Sep 17 00:00:00 2001 From: Zane Kharitonov Date: Thu, 19 Dec 2024 17:36:01 -0500 Subject: [PATCH 2/7] typo --- packages/api-key-stamper/src/tink/elliptic_curves.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api-key-stamper/src/tink/elliptic_curves.ts b/packages/api-key-stamper/src/tink/elliptic_curves.ts index 2c5a3d9d0..085000ec9 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: Ensure that you are using a valid public and private key for your API key" + "Invalid API key: Ensure that you are using a valid public and private key for your API key" ); } // Decodes point if its length and first bit match the compressed format From 4136fb3d3aa570a63e4acce07cc606766331c330 Mon Sep 17 00:00:00 2001 From: Zane Kharitonov Date: Fri, 20 Dec 2024 10:23:05 -0500 Subject: [PATCH 3/7] Change where specific errors are thrown --- packages/api-key-stamper/src/tink/elliptic_curves.ts | 2 +- packages/api-key-stamper/src/utils.ts | 7 ++++++- packages/sdk-browser/src/utils.ts | 9 ++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) 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", From b4bb09caa4f575b7b3e54ddff88a4e9773240750 Mon Sep 17 00:00:00 2001 From: Zane Kharitonov Date: Fri, 20 Dec 2024 10:32:18 -0500 Subject: [PATCH 4/7] Type fix --- packages/api-key-stamper/src/utils.ts | 2 +- packages/sdk-browser/src/utils.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/api-key-stamper/src/utils.ts b/packages/api-key-stamper/src/utils.ts index bb8f9e78f..2fe465a9e 100644 --- a/packages/api-key-stamper/src/utils.ts +++ b/packages/api-key-stamper/src/utils.ts @@ -14,7 +14,7 @@ export function convertTurnkeyApiKeyToJwk(input: { let jwk; try { jwk = pointDecode(uint8ArrayFromHexString(compressedPublicKeyHex)); - } catch (e: Error) { + } catch (e) { 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`); } diff --git a/packages/sdk-browser/src/utils.ts b/packages/sdk-browser/src/utils.ts index 21d5ba904..95daacaae 100644 --- a/packages/sdk-browser/src/utils.ts +++ b/packages/sdk-browser/src/utils.ts @@ -35,7 +35,7 @@ export const createEmbeddedAPIKey = async ( let jwk; try { jwk = pointDecode(targetKeyBytes); - } catch (e: Error) { + } catch (e) { // provide more context about the error that is being thrown throw new Error(`target public key is not a valid compressed public key: ${targetPublicKey}`); } From 4cfe663b44ec0aeefb8a520b6ed63dbb4f54c869 Mon Sep 17 00:00:00 2001 From: Zane Kharitonov Date: Fri, 20 Dec 2024 10:38:45 -0500 Subject: [PATCH 5/7] run prettier --- packages/api-key-stamper/src/utils.ts | 4 +++- packages/sdk-browser/src/utils.ts | 10 ++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/api-key-stamper/src/utils.ts b/packages/api-key-stamper/src/utils.ts index 2fe465a9e..2b7feec91 100644 --- a/packages/api-key-stamper/src/utils.ts +++ b/packages/api-key-stamper/src/utils.ts @@ -15,7 +15,9 @@ export function convertTurnkeyApiKeyToJwk(input: { try { jwk = pointDecode(uint8ArrayFromHexString(compressedPublicKeyHex)); } catch (e) { - 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`); + 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 diff --git a/packages/sdk-browser/src/utils.ts b/packages/sdk-browser/src/utils.ts index 95daacaae..c7a923791 100644 --- a/packages/sdk-browser/src/utils.ts +++ b/packages/sdk-browser/src/utils.ts @@ -33,11 +33,13 @@ export const createEmbeddedAPIKey = async ( const targetKeyBytes = uint8ArrayFromHexString(targetPublicKey); let jwk; - try { - jwk = pointDecode(targetKeyBytes); - } catch (e) { + try { + jwk = pointDecode(targetKeyBytes); + } catch (e) { // provide more context about the error that is being thrown - throw new Error(`target public key is not a valid compressed public key: ${targetPublicKey}`); + throw new Error( + `target public key is not a valid compressed public key: ${targetPublicKey}` + ); } const targetKey = await crypto.subtle.importKey( From 288b40eef6740d4c373fd58ed5404a0cb088f617 Mon Sep 17 00:00:00 2001 From: Zane Kharitonov Date: Fri, 20 Dec 2024 14:23:13 -0500 Subject: [PATCH 6/7] update message --- packages/api-key-stamper/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api-key-stamper/src/utils.ts b/packages/api-key-stamper/src/utils.ts index 2b7feec91..b9775bc60 100644 --- a/packages/api-key-stamper/src/utils.ts +++ b/packages/api-key-stamper/src/utils.ts @@ -16,7 +16,7 @@ export function convertTurnkeyApiKeyToJwk(input: { jwk = pointDecode(uint8ArrayFromHexString(compressedPublicKeyHex)); } catch (e) { 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` + `unable to load API key: invalid public key. Did you switch your public and private key?` ); } From 2d5977b6cf4da9520c2e08a6e2e8ed6ce8b814c9 Mon Sep 17 00:00:00 2001 From: Zane Kharitonov Date: Fri, 20 Dec 2024 14:25:59 -0500 Subject: [PATCH 7/7] add changeset --- .changeset/red-carrots-carry.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/red-carrots-carry.md diff --git a/.changeset/red-carrots-carry.md b/.changeset/red-carrots-carry.md new file mode 100644 index 000000000..f6ee3946e --- /dev/null +++ b/.changeset/red-carrots-carry.md @@ -0,0 +1,6 @@ +--- +"@turnkey/api-key-stamper": patch +"@turnkey/sdk-browser": patch +--- + +Update error messaging around api key and target public key usage