@@ -19,13 +19,13 @@ import * as keysPBM from './keys.js'
1919import * as RSA from './rsa-class.js'
2020import { importFromPem } from './rsa-utils.js'
2121import * as Secp256k1 from './secp256k1-class.js'
22- import type { PrivateKey , PublicKey } from '@libp2p/interface'
22+ import type { PrivateKey , PublicKey , KeyType as KeyTypes } from '@libp2p/interface'
2323
2424export { keyStretcher }
2525export { generateEphemeralKeyPair }
2626export { keysPBM }
2727
28- export type KeyTypes = 'RSA' | 'Ed25519' | 'secp256k1'
28+ export type { KeyTypes }
2929
3030export { RsaPrivateKey , RsaPublicKey , MAX_RSA_KEY_SIZE } from './rsa-class.js'
3131export { Ed25519PrivateKey , Ed25519PublicKey } from './ed25519-class.js'
@@ -55,11 +55,8 @@ function typeToKey (type: string): typeof RSA | typeof Ed25519 | typeof Secp256k
5555
5656/**
5757 * Generates a keypair of the given type and bitsize
58- *
59- * @param type
60- * @param bits - Minimum of 1024
6158 */
62- export async function generateKeyPair ( type : KeyTypes , bits ?: number ) : Promise < PrivateKey > {
59+ export async function generateKeyPair < T extends KeyTypes > ( type : T , bits ?: number ) : Promise < PrivateKey < T > > {
6360 return typeToKey ( type ) . generateKeyPair ( bits ?? 2048 )
6461}
6562
@@ -68,7 +65,7 @@ export async function generateKeyPair (type: KeyTypes, bits?: number): Promise<P
6865 *
6966 * Seed is a 32 byte uint8array
7067 */
71- export async function generateKeyPairFromSeed ( type : KeyTypes , seed : Uint8Array , bits ?: number ) : Promise < PrivateKey > {
68+ export async function generateKeyPairFromSeed < T extends KeyTypes > ( type : T , seed : Uint8Array , bits ?: number ) : Promise < PrivateKey < T > > {
7269 if ( type . toLowerCase ( ) !== 'ed25519' ) {
7370 throw new CodeError ( 'Seed key derivation is unimplemented for RSA or secp256k1' , 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE' )
7471 }
@@ -79,7 +76,7 @@ export async function generateKeyPairFromSeed (type: KeyTypes, seed: Uint8Array,
7976/**
8077 * Converts a protobuf serialized public key into its representative object
8178 */
82- export function unmarshalPublicKey ( buf : Uint8Array ) : PublicKey {
79+ export function unmarshalPublicKey < T extends KeyTypes > ( buf : Uint8Array ) : PublicKey < T > {
8380 const decoded = keysPBM . PublicKey . decode ( buf )
8481 const data = decoded . Data ?? new Uint8Array ( )
8582
@@ -107,7 +104,7 @@ export function marshalPublicKey (key: { bytes: Uint8Array }, type?: string): Ui
107104/**
108105 * Converts a protobuf serialized private key into its representative object
109106 */
110- export async function unmarshalPrivateKey ( buf : Uint8Array ) : Promise < PrivateKey > {
107+ export async function unmarshalPrivateKey < T extends KeyTypes > ( buf : Uint8Array ) : Promise < PrivateKey < T > > {
111108 const decoded = keysPBM . PrivateKey . decode ( buf )
112109 const data = decoded . Data ?? new Uint8Array ( )
113110
@@ -137,7 +134,7 @@ export function marshalPrivateKey (key: { bytes: Uint8Array }, type?: string): U
137134 *
138135 * Supported formats are 'pem' (RSA only) and 'libp2p-key'.
139136 */
140- export async function importKey ( encryptedKey : string , password : string ) : Promise < PrivateKey > {
137+ export async function importKey < T extends KeyTypes > ( encryptedKey : string , password : string ) : Promise < PrivateKey < T > > {
141138 try {
142139 const key = await importer ( encryptedKey , password )
143140 return await unmarshalPrivateKey ( key )
0 commit comments