Skip to content

Commit

Permalink
Add some COSE value docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterKale committed Dec 8, 2024
1 parent c862acd commit e2beae1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/server/src/helpers/cose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
* keys", but it works.
* @module
*/

/**
* COSE public key common values
*/
export type COSEPublicKey = {
// Getters
get(key: COSEKEYS.kty): COSEKTY | undefined;
Expand All @@ -18,6 +22,9 @@ export type COSEPublicKey = {
set(key: COSEKEYS.alg, value: COSEALG): void;
};

/**
* Values specific to Octet Key Pair public keys
*/
export type COSEPublicKeyOKP = COSEPublicKey & {
// Getters
get(key: COSEKEYS.crv): number | undefined;
Expand All @@ -27,6 +34,9 @@ export type COSEPublicKeyOKP = COSEPublicKey & {
set(key: COSEKEYS.x, value: Uint8Array): void;
};

/**
* Values specific to Elliptic Curve Cryptography public keys
*/
export type COSEPublicKeyEC2 = COSEPublicKey & {
// Getters
get(key: COSEKEYS.crv): number | undefined;
Expand All @@ -38,6 +48,9 @@ export type COSEPublicKeyEC2 = COSEPublicKey & {
set(key: COSEKEYS.y, value: Uint8Array): void;
};

/**
* Values specific to RSA public keys
*/
export type COSEPublicKeyRSA = COSEPublicKey & {
// Getters
get(key: COSEKEYS.n): Uint8Array | undefined;
Expand All @@ -47,20 +60,29 @@ export type COSEPublicKeyRSA = COSEPublicKey & {
set(key: COSEKEYS.e, value: Uint8Array): void;
};

/**
* A type guard for determining if a COSE public key is an OKP key pair
*/
export function isCOSEPublicKeyOKP(
cosePublicKey: COSEPublicKey,
): cosePublicKey is COSEPublicKeyOKP {
const kty = cosePublicKey.get(COSEKEYS.kty);
return isCOSEKty(kty) && kty === COSEKTY.OKP;
}

/**
* A type guard for determining if a COSE public key is an EC2 key pair
*/
export function isCOSEPublicKeyEC2(
cosePublicKey: COSEPublicKey,
): cosePublicKey is COSEPublicKeyEC2 {
const kty = cosePublicKey.get(COSEKEYS.kty);
return isCOSEKty(kty) && kty === COSEKTY.EC2;
}

/**
* A type guard for determining if a COSE public key is an RSA key pair
*/
export function isCOSEPublicKeyRSA(
cosePublicKey: COSEPublicKey,
): cosePublicKey is COSEPublicKeyRSA {
Expand Down

0 comments on commit e2beae1

Please sign in to comment.