From 56cfaf82ed26413c8218eb58cadf07efe4a8a16f Mon Sep 17 00:00:00 2001 From: Kris Shinn Date: Sun, 3 Mar 2024 16:21:14 -0800 Subject: [PATCH] Add an `exportable` option to EcdsaKeyPair Addresses issue #108 --- packages/default-plugins/src/p256/crypto.ts | 18 ++++++++++-------- packages/default-plugins/src/p256/keypair.ts | 12 ++++++------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/default-plugins/src/p256/crypto.ts b/packages/default-plugins/src/p256/crypto.ts index ef8dcff..cfec24d 100644 --- a/packages/default-plugins/src/p256/crypto.ts +++ b/packages/default-plugins/src/p256/crypto.ts @@ -9,14 +9,16 @@ export const ALG = "ECDSA" export const DEFAULT_CURVE = "P-256" export const DEFAULT_HASH_ALG = "SHA-256" -export const generateKeypair = async (): Promise => { +export const generateKeypair = async ( + exportable = false +): Promise => { return await webcrypto.subtle.generateKey( { name: ALG, namedCurve: DEFAULT_CURVE, }, - false, - [ "sign", "verify" ] + exportable, + ["sign", "verify"] ) } @@ -32,10 +34,10 @@ export const importKeypairJwk = async ( namedCurve: DEFAULT_CURVE, }, exportable, - ["sign" ] + ["sign"] ) - const { kty, crv, x, y} = privKeyJwk - const pubKeyJwk = { kty, crv, x, y} + const { kty, crv, x, y } = privKeyJwk + const pubKeyJwk = { kty, crv, x, y } const publicKey = await webcrypto.subtle.importKey( "jwk", pubKeyJwk, @@ -44,7 +46,7 @@ export const importKeypairJwk = async ( namedCurve: DEFAULT_CURVE, }, true, - [ "verify" ] + ["verify"] ) return { privateKey, publicKey } } @@ -62,7 +64,7 @@ export const importKey = async ( key, { name: ALG, namedCurve: DEFAULT_CURVE }, true, - [ "verify" ] + ["verify"] ) } diff --git a/packages/default-plugins/src/p256/keypair.ts b/packages/default-plugins/src/p256/keypair.ts index 76b8e2f..b3bbdee 100644 --- a/packages/default-plugins/src/p256/keypair.ts +++ b/packages/default-plugins/src/p256/keypair.ts @@ -32,7 +32,7 @@ export class EcdsaKeypair implements DidableKey, ExportableKey { exportable?: boolean }): Promise { const { exportable = false } = params || {} - const keypair = await crypto.generateKeypair() + const keypair = await crypto.generateKeypair(exportable) if (!isAvailableCryptoKeyPair(keypair)) { throw new Error(`Couldn't generate valid keypair`) @@ -47,12 +47,12 @@ export class EcdsaKeypair implements DidableKey, ExportableKey { params?: { exportable?: boolean }): Promise { - const { exportable = false } = params || {} - const keypair = await crypto.importKeypairJwk(jwk, exportable) + const { exportable = false } = params || {} + const keypair = await crypto.importKeypairJwk(jwk, exportable) - if (!isAvailableCryptoKeyPair(keypair)) { - throw new Error(`Couldn't generate valid keypair`) - } + if (!isAvailableCryptoKeyPair(keypair)) { + throw new Error(`Couldn't generate valid keypair`) + } const publicKey = await crypto.exportKey(keypair.publicKey) return new EcdsaKeypair(keypair, publicKey, exportable)