Skip to content

Commit

Permalink
chore: fixes for private keys
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Dec 4, 2023
1 parent f278967 commit 871aae1
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions packages/key-utils/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,15 @@ const assertProperKeyLength = (keyHex: string, expectedKeyLength: number | numbe
*/
const toSecp256k1Jwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse; isPrivateKey?: boolean }): JsonWebKey => {
const { use } = opts ?? {}
const publicKey = publicKeyHex
// const publicKey = publicKeyHex
assertProperKeyLength(publicKeyHex, [64, 66, 130])

const secp256k1 = new elliptic.ec('secp256k1')
const keyBytes = u8a.fromString(publicKeyHex, 'base16')
const keyPair = opts?.isPrivateKey ? secp256k1.keyFromPrivate(keyBytes) : secp256k1.keyFromPublic(keyBytes)
/*const publicKeyHex = keyPair.getPublic(true, 'hex')
if (opts?.isPrivateKey) {
const key = secp256k1.keyFromPrivate(publicKey, 'hex')
// const point = key.getPrivate()
Expand All @@ -190,9 +195,9 @@ const toSecp256k1Jwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse; isPrivat
y: hex2base64url(key.ec.g.y.toString('hex')),
d: hex2base64url(key.getPrivate('hex')),
}
}
const key = secp256k1.keyFromPublic(publicKey, 'hex')
const pubPoint = key.getPublic()
}*/
// const key = secp256k1.keyFromPublic(keyPair, 'hex')
const pubPoint = keyPair.getPublic()

return {
alg: 'ES256K',
Expand All @@ -201,6 +206,7 @@ const toSecp256k1Jwk = (publicKeyHex: string, opts?: { use?: JwkKeyUse; isPrivat
crv: KeyCurve.Secp256k1,
x: hex2base64url(pubPoint.getX().toString('hex')),
y: hex2base64url(pubPoint.getY().toString('hex')),
...(opts?.isPrivateKey && { d: hex2base64url(keyPair.getPrivate('hex')) }),
}
}

Expand All @@ -216,7 +222,10 @@ const toSecp256r1Jwk = (keyHex: string, opts?: { use?: JwkKeyUse; isPrivateKey?:
assertProperKeyLength(publicKey, [64, 66, 130])

const secp256r1 = new elliptic.ec('p256')
if (opts?.isPrivateKey) {
const keyBytes = u8a.fromString(keyHex, 'base16')
const keyPair = opts?.isPrivateKey ? secp256r1.keyFromPrivate(keyBytes) : secp256r1.keyFromPublic(keyBytes)

/* if (opts?.isPrivateKey) {
const key = secp256r1.keyFromPrivate(publicKey, 'hex')
// const point = key.getPrivate()
return {
Expand All @@ -228,16 +237,17 @@ const toSecp256r1Jwk = (keyHex: string, opts?: { use?: JwkKeyUse; isPrivateKey?:
y: hex2base64url(key.ec.g.y.toString('hex')),
d: hex2base64url(key.getPrivate('hex')),
}
}
const key = secp256r1.keyFromPublic(publicKey, 'hex')
const point = key.getPublic()
}*/
// const key = secp256r1.keyFromPublic(publicKey, 'hex')
const point = keyPair.getPublic()
return {
alg: 'ES256',
...(use !== undefined && { use }),
kty: KeyType.EC,
crv: KeyCurve.P_256,
x: hex2base64url(point.getX().toString('hex')),
y: hex2base64url(point.getY().toString('hex')),
...(opts?.isPrivateKey && { d: hex2base64url(keyPair.getPrivate('hex')) }),
}
}

Expand Down

0 comments on commit 871aae1

Please sign in to comment.