@@ -19,13 +19,13 @@ import * as keysPBM from './keys.js'
19
19
import * as RSA from './rsa-class.js'
20
20
import { importFromPem } from './rsa-utils.js'
21
21
import * 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'
23
23
24
24
export { keyStretcher }
25
25
export { generateEphemeralKeyPair }
26
26
export { keysPBM }
27
27
28
- export type KeyTypes = 'RSA' | 'Ed25519' | 'secp256k1'
28
+ export type { KeyTypes }
29
29
30
30
export { RsaPrivateKey , RsaPublicKey , MAX_RSA_KEY_SIZE } from './rsa-class.js'
31
31
export { Ed25519PrivateKey , Ed25519PublicKey } from './ed25519-class.js'
@@ -55,11 +55,8 @@ function typeToKey (type: string): typeof RSA | typeof Ed25519 | typeof Secp256k
55
55
56
56
/**
57
57
* Generates a keypair of the given type and bitsize
58
- *
59
- * @param type
60
- * @param bits - Minimum of 1024
61
58
*/
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 > > {
63
60
return typeToKey ( type ) . generateKeyPair ( bits ?? 2048 )
64
61
}
65
62
@@ -68,7 +65,7 @@ export async function generateKeyPair (type: KeyTypes, bits?: number): Promise<P
68
65
*
69
66
* Seed is a 32 byte uint8array
70
67
*/
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 > > {
72
69
if ( type . toLowerCase ( ) !== 'ed25519' ) {
73
70
throw new CodeError ( 'Seed key derivation is unimplemented for RSA or secp256k1' , 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE' )
74
71
}
@@ -79,7 +76,7 @@ export async function generateKeyPairFromSeed (type: KeyTypes, seed: Uint8Array,
79
76
/**
80
77
* Converts a protobuf serialized public key into its representative object
81
78
*/
82
- export function unmarshalPublicKey ( buf : Uint8Array ) : PublicKey {
79
+ export function unmarshalPublicKey < T extends KeyTypes > ( buf : Uint8Array ) : PublicKey < T > {
83
80
const decoded = keysPBM . PublicKey . decode ( buf )
84
81
const data = decoded . Data ?? new Uint8Array ( )
85
82
@@ -107,7 +104,7 @@ export function marshalPublicKey (key: { bytes: Uint8Array }, type?: string): Ui
107
104
/**
108
105
* Converts a protobuf serialized private key into its representative object
109
106
*/
110
- export async function unmarshalPrivateKey ( buf : Uint8Array ) : Promise < PrivateKey > {
107
+ export async function unmarshalPrivateKey < T extends KeyTypes > ( buf : Uint8Array ) : Promise < PrivateKey < T > > {
111
108
const decoded = keysPBM . PrivateKey . decode ( buf )
112
109
const data = decoded . Data ?? new Uint8Array ( )
113
110
@@ -137,7 +134,7 @@ export function marshalPrivateKey (key: { bytes: Uint8Array }, type?: string): U
137
134
*
138
135
* Supported formats are 'pem' (RSA only) and 'libp2p-key'.
139
136
*/
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 > > {
141
138
try {
142
139
const key = await importer ( encryptedKey , password )
143
140
return await unmarshalPrivateKey ( key )
0 commit comments