Skip to content

Commit

Permalink
fixing tests from jwk function refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mattschoch committed Apr 1, 2024
1 parent 934938a commit 418395a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
7 changes: 2 additions & 5 deletions apps/vault/src/vault/__test__/e2e/import.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions apps/vault/src/vault/__test__/e2e/sign.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
hash,
hexToBase64Url,
secp256k1PrivateKeyToJwk,
secp256k1PrivateKeyToPublicJwk,
secp256k1PublicKeyToJwk,
signJwsd,
signJwt
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 9 additions & 4 deletions packages/signature/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 }
})
}
}
Expand Down

0 comments on commit 418395a

Please sign in to comment.