From 418395a59188ebedf419cb5fa212e0b5f6282f0e Mon Sep 17 00:00:00 2001 From: Matt Schoch Date: Mon, 1 Apr 2024 14:45:47 -0400 Subject: [PATCH] fixing tests from jwk function refactor --- apps/vault/src/vault/__test__/e2e/import.spec.ts | 7 ++----- apps/vault/src/vault/__test__/e2e/sign.spec.ts | 5 ++--- packages/signature/src/lib/utils.ts | 13 +++++++++---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/apps/vault/src/vault/__test__/e2e/import.spec.ts b/apps/vault/src/vault/__test__/e2e/import.spec.ts index ee62769d1..8acef817a 100644 --- a/apps/vault/src/vault/__test__/e2e/import.spec.ts +++ b/apps/vault/src/vault/__test__/e2e/import.spec.ts @@ -1,5 +1,5 @@ import { EncryptionModuleOptionProvider } from '@narval/encryption-module' -import { RsaPublicKey, rsaEncrypt, rsaPublicKeySchema, secp256k1PrivateKeyToJwk } from '@narval/signature' +import { RsaPublicKey, rsaEncrypt, rsaPublicKeySchema, secp256k1PrivateKeyToPublicJwk } from '@narval/signature' import { HttpStatus, INestApplication } from '@nestjs/common' import { ConfigModule } from '@nestjs/config' import { Test, TestingModule } from '@nestjs/testing' @@ -25,10 +25,7 @@ describe('Import', () => { const PRIVATE_KEY = '0x7cfef3303797cbc7515d9ce22ffe849c701b0f2812f999b0847229c47951fca5' // Engine key used to sign the approval request - const enginePrivateJwk = secp256k1PrivateKeyToJwk(PRIVATE_KEY) - // Engine public key registered w/ the Vault Tenant - // eslint-disable-next-line - const { d, ...tenantPublicJWK } = enginePrivateJwk + const tenantPublicJWK = secp256k1PrivateKeyToPublicJwk(PRIVATE_KEY) const tenant: Tenant = { clientId, diff --git a/apps/vault/src/vault/__test__/e2e/sign.spec.ts b/apps/vault/src/vault/__test__/e2e/sign.spec.ts index 7b1c5d136..597321702 100644 --- a/apps/vault/src/vault/__test__/e2e/sign.spec.ts +++ b/apps/vault/src/vault/__test__/e2e/sign.spec.ts @@ -8,6 +8,7 @@ import { hash, hexToBase64Url, secp256k1PrivateKeyToJwk, + secp256k1PrivateKeyToPublicJwk, secp256k1PublicKeyToJwk, signJwsd, signJwt @@ -39,9 +40,7 @@ describe('Sign', () => { const PRIVATE_KEY = '0x7cfef3303797cbc7515d9ce22ffe849c701b0f2812f999b0847229c47951fca5' // Engine key used to sign the approval request const enginePrivateJwk = secp256k1PrivateKeyToJwk(PRIVATE_KEY) - // Engine public key registered w/ the Vault Tenant - // eslint-disable-next-line - const { d, ...tenantPublicJWK } = enginePrivateJwk + const tenantPublicJWK = secp256k1PrivateKeyToPublicJwk(PRIVATE_KEY) const tenant: Tenant = { clientId, diff --git a/packages/signature/src/lib/utils.ts b/packages/signature/src/lib/utils.ts index e9c555f71..58ec431b3 100644 --- a/packages/signature/src/lib/utils.ts +++ b/packages/signature/src/lib/utils.ts @@ -99,6 +99,11 @@ export const secp256k1PrivateKeyToJwk = (privateKey: Hex, keyId?: string): Secp2 } } +export const secp256k1PrivateKeyToPublicJwk = (privateKey: Hex, keyId?: string): Secp256k1PublicKey => { + const publicKey = toHex(secp256k1.getPublicKey(privateKey.slice(2), false)) + return secp256k1PublicKeyToJwk(publicKey, keyId) +} + export const p256PrivateKeyToJwk = (privateKey: Hex, keyId?: string): P256PrivateKey => { const publicKey = toHex(p256.getPublicKey(privateKey.slice(2), false)) const publicJwk = p256PublicKeyToJwk(publicKey, keyId) @@ -134,16 +139,16 @@ export const ellipticPublicKeyToHex = (jwk: Jwk): Hex => { return `0x04${x.slice(2)}${y.slice(2)}` } -export const publicKeyToJwk = (jwk: Hex, alg: Alg): Jwk => { +export const publicKeyToJwk = (key: Hex, alg: Alg): Jwk => { switch (alg) { case Alg.ES256K: - return secp256k1PublicKeyToJwk(jwk) + return secp256k1PublicKeyToJwk(key) case Alg.ES256: - return p256PublicKeyToJwk(jwk) + return p256PublicKeyToJwk(key) case Alg.RS256: throw new JwtError({ message: 'Conversion from Hex to JWK not supported for RSA keys', - context: { jwk } + context: { key } }) } }