-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for key type k256 (#1722)
Signed-off-by: Sai Ranjit Tummalapalli <[email protected]>
- Loading branch information
Showing
14 changed files
with
195 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ export enum KeyType { | |
P256 = 'p256', | ||
P384 = 'p384', | ||
P521 = 'p521', | ||
K256 = 'k256', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ export enum JwaCurve { | |
P521 = 'P-521', | ||
Ed25519 = 'Ed25519', | ||
X25519 = 'X25519', | ||
Secp256k1 = 'secp256k1', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import type { JwkJson } from './Jwk' | ||
import type { JwaEncryptionAlgorithm } from '../jwa/alg' | ||
|
||
import { TypedArrayEncoder, Buffer } from '../../../utils' | ||
import { KeyType } from '../../KeyType' | ||
import { JwaCurve, JwaKeyType } from '../jwa' | ||
import { JwaSignatureAlgorithm } from '../jwa/alg' | ||
|
||
import { Jwk } from './Jwk' | ||
import { compress, expand } from './ecCompression' | ||
import { hasKty, hasCrv, hasX, hasY, hasValidUse } from './validate' | ||
|
||
export class K256Jwk extends Jwk { | ||
public static readonly supportedEncryptionAlgorithms: JwaEncryptionAlgorithm[] = [] | ||
public static readonly supportedSignatureAlgorithms: JwaSignatureAlgorithm[] = [JwaSignatureAlgorithm.ES256K] | ||
public static readonly keyType = KeyType.K256 | ||
|
||
public readonly x: string | ||
public readonly y: string | ||
|
||
public constructor({ x, y }: { x: string; y: string }) { | ||
super() | ||
|
||
this.x = x | ||
this.y = y | ||
} | ||
|
||
public get kty() { | ||
return JwaKeyType.EC as const | ||
} | ||
|
||
public get crv() { | ||
return JwaCurve.Secp256k1 as const | ||
} | ||
|
||
/** | ||
* Returns the public key of the K-256 JWK. | ||
* | ||
* NOTE: this is the compressed variant. We still need to add support for the | ||
* uncompressed variant. | ||
*/ | ||
public get publicKey() { | ||
const publicKeyBuffer = Buffer.concat([TypedArrayEncoder.fromBase64(this.x), TypedArrayEncoder.fromBase64(this.y)]) | ||
const compressedPublicKey = compress(publicKeyBuffer) | ||
|
||
return Buffer.from(compressedPublicKey) | ||
} | ||
|
||
public get keyType() { | ||
return K256Jwk.keyType | ||
} | ||
|
||
public get supportedEncryptionAlgorithms() { | ||
return K256Jwk.supportedEncryptionAlgorithms | ||
} | ||
|
||
public get supportedSignatureAlgorithms() { | ||
return K256Jwk.supportedSignatureAlgorithms | ||
} | ||
|
||
public toJson() { | ||
return { | ||
...super.toJson(), | ||
crv: this.crv, | ||
x: this.x, | ||
y: this.y, | ||
} as K256JwkJson | ||
} | ||
|
||
public static fromJson(jwkJson: JwkJson) { | ||
if (!isValidP256JwkPublicKey(jwkJson)) { | ||
throw new Error("Invalid 'K-256' JWK.") | ||
} | ||
|
||
return new K256Jwk({ | ||
x: jwkJson.x, | ||
y: jwkJson.y, | ||
}) | ||
} | ||
|
||
public static fromPublicKey(publicKey: Buffer) { | ||
const expanded = expand(publicKey, JwaCurve.Secp256k1) | ||
const x = expanded.slice(0, expanded.length / 2) | ||
const y = expanded.slice(expanded.length / 2) | ||
|
||
return new K256Jwk({ | ||
x: TypedArrayEncoder.toBase64URL(x), | ||
y: TypedArrayEncoder.toBase64URL(y), | ||
}) | ||
} | ||
} | ||
|
||
export interface K256JwkJson extends JwkJson { | ||
kty: JwaKeyType.EC | ||
crv: JwaCurve.Secp256k1 | ||
x: string | ||
y: string | ||
use?: 'sig' | 'enc' | ||
} | ||
|
||
export function isValidP256JwkPublicKey(jwk: JwkJson): jwk is K256JwkJson { | ||
return ( | ||
hasKty(jwk, JwaKeyType.EC) && | ||
hasCrv(jwk, JwaCurve.Secp256k1) && | ||
hasX(jwk) && | ||
hasY(jwk) && | ||
hasValidUse(jwk, { | ||
supportsEncrypting: true, | ||
supportsSigning: true, | ||
}) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/core/src/modules/dids/__tests__/__fixtures__/didKeyK256.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"@context": ["https://w3id.org/did/v1", "https://w3id.org/security/suites/jws-2020/v1"], | ||
"id": "did:key:zQ3shjRPgHQQbTtXyofk1ygghRJ75RZpXmWBMY1BKnhyz7zKp", | ||
"verificationMethod": [ | ||
{ | ||
"id": "did:key:zQ3shjRPgHQQbTtXyofk1ygghRJ75RZpXmWBMY1BKnhyz7zKp#zQ3shjRPgHQQbTtXyofk1ygghRJ75RZpXmWBMY1BKnhyz7zKp", | ||
"type": "JsonWebKey2020", | ||
"controller": "did:key:zQ3shjRPgHQQbTtXyofk1ygghRJ75RZpXmWBMY1BKnhyz7zKp", | ||
"publicKeyJwk": { | ||
"kty": "EC", | ||
"crv": "secp256k1", | ||
"x": "RwiZITTa2Dcmq-V1j-5tgPUshOLO31FbsnhVS-7lskc", | ||
"y": "3o1-UCc3ABh757P58gDISSc4hOj9qyfSGl3SGGA7xdc" | ||
} | ||
} | ||
], | ||
"authentication": [ | ||
"did:key:zQ3shjRPgHQQbTtXyofk1ygghRJ75RZpXmWBMY1BKnhyz7zKp#zQ3shjRPgHQQbTtXyofk1ygghRJ75RZpXmWBMY1BKnhyz7zKp" | ||
], | ||
"assertionMethod": [ | ||
"did:key:zQ3shjRPgHQQbTtXyofk1ygghRJ75RZpXmWBMY1BKnhyz7zKp#zQ3shjRPgHQQbTtXyofk1ygghRJ75RZpXmWBMY1BKnhyz7zKp" | ||
], | ||
"capabilityInvocation": [ | ||
"did:key:zQ3shjRPgHQQbTtXyofk1ygghRJ75RZpXmWBMY1BKnhyz7zKp#zQ3shjRPgHQQbTtXyofk1ygghRJ75RZpXmWBMY1BKnhyz7zKp" | ||
], | ||
"capabilityDelegation": [ | ||
"did:key:zQ3shjRPgHQQbTtXyofk1ygghRJ75RZpXmWBMY1BKnhyz7zKp#zQ3shjRPgHQQbTtXyofk1ygghRJ75RZpXmWBMY1BKnhyz7zKp" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters