Skip to content

Commit

Permalink
wip: test conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Aug 7, 2024
1 parent 0e269bc commit 2e4fa27
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
3 changes: 3 additions & 0 deletions packages/api-key-stamper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@
"@noble/curves": "^1.3.0",
"sha256-uint8array": "^0.10.7",
"@turnkey/encoding": "workspace:*"
},
"devDependencies": {
"@turnkey/crypto": "workspace:*"
}
}
33 changes: 33 additions & 0 deletions packages/api-key-stamper/src/__tests__/utils-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { test, expect } from "@jest/globals";
import { convertTurnkeyApiKeyToJwk } from "../utils";
import { readFixture } from "../__fixtures__/shared";
import { assertValidSignature } from "./shared";
import { generateP256KeyPair } from "@turnkey/crypto";
import * as crypto from "crypto";
import { uint8ArrayFromHexString } from "@turnkey/encoding";

Object.defineProperty(globalThis, "crypto", {
value: {
getRandomValues: (arr) => crypto.randomBytes(arr.length),
},
});

test("correctly converts turnkey API key to JWK", function () {
for (let i = 0; i < 100; i++) {
try {
const newApiKeyPair = generateP256KeyPair();

const result = convertTurnkeyApiKeyToJwk({
uncompressedPrivateKeyHex: newApiKeyPair.privateKey,
compressedPublicKeyHex: newApiKeyPair.publicKey,
});
} catch (err: any) {
console.log("caught an error", err);
expect(err).toBeTruthy();

return;
}
}
});

const uint8ArrayFromHex = (hexString: string) => new Uint8Array(hexString.match(/../g)!.map((h) => parseInt(h, 16)));
26 changes: 13 additions & 13 deletions packages/api-key-stamper/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ export function convertTurnkeyApiKeyToJwk(input: {
// Ensure that each of the constituent parts are sufficiently padded
jwkCopy.d = hexStringToBase64urlString(uncompressedPrivateKeyHex);

// Manipulate x and y
const decodedX = base64urlToBuffer(jwkCopy.x!);
const paddedX = hexStringToBase64urlString(
uint8ArrayToHexString(new Uint8Array(decodedX)),
DEFAULT_JWK_MEMBER_BYTE_LENGTH
);
// // Manipulate x and y
// const decodedX = base64urlToBuffer(jwkCopy.x!);
// const paddedX = hexStringToBase64urlString(
// uint8ArrayToHexString(new Uint8Array(decodedX)),
// DEFAULT_JWK_MEMBER_BYTE_LENGTH
// );

const decodedY = base64urlToBuffer(jwkCopy.y!);
const paddedY = hexStringToBase64urlString(
uint8ArrayToHexString(new Uint8Array(decodedY)),
DEFAULT_JWK_MEMBER_BYTE_LENGTH
);
// const decodedY = base64urlToBuffer(jwkCopy.y!);
// const paddedY = hexStringToBase64urlString(
// uint8ArrayToHexString(new Uint8Array(decodedY)),
// DEFAULT_JWK_MEMBER_BYTE_LENGTH
// );

jwkCopy.x = paddedX;
jwkCopy.y = paddedY;
// jwkCopy.x = paddedX;
// jwkCopy.y = paddedY;

return jwkCopy;
}
Expand Down
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2e4fa27

Please sign in to comment.