Skip to content

Commit

Permalink
Add an exportable option to EcdsaKeyPair
Browse files Browse the repository at this point in the history
Addresses issue ucan-wg#108
  • Loading branch information
kshinn committed Mar 4, 2024
1 parent 4b1be87 commit 56cfaf8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
18 changes: 10 additions & 8 deletions packages/default-plugins/src/p256/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AvailableCryptoKeyPair> => {
export const generateKeypair = async (
exportable = false
): Promise<AvailableCryptoKeyPair> => {
return await webcrypto.subtle.generateKey(
{
name: ALG,
namedCurve: DEFAULT_CURVE,
},
false,
[ "sign", "verify" ]
exportable,
["sign", "verify"]
)
}

Expand All @@ -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,
Expand All @@ -44,7 +46,7 @@ export const importKeypairJwk = async (
namedCurve: DEFAULT_CURVE,
},
true,
[ "verify" ]
["verify"]
)
return { privateKey, publicKey }
}
Expand All @@ -62,7 +64,7 @@ export const importKey = async (
key,
{ name: ALG, namedCurve: DEFAULT_CURVE },
true,
[ "verify" ]
["verify"]
)
}

Expand Down
12 changes: 6 additions & 6 deletions packages/default-plugins/src/p256/keypair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class EcdsaKeypair implements DidableKey, ExportableKey {
exportable?: boolean
}): Promise<EcdsaKeypair> {
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`)
Expand All @@ -47,12 +47,12 @@ export class EcdsaKeypair implements DidableKey, ExportableKey {
params?: {
exportable?: boolean
}): Promise<EcdsaKeypair> {
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)
Expand Down

0 comments on commit 56cfaf8

Please sign in to comment.