1
1
import { Duplex as DuplexStream } from 'node:stream'
2
- import { Ed25519PublicKey , Secp256k1PublicKey , marshalPublicKey , supportedKeys , unmarshalPrivateKey , unmarshalPublicKey } from '@libp2p/crypto/keys'
3
- import { InvalidCryptoExchangeError , InvalidParametersError , UnexpectedPeerError } from '@libp2p/interface'
2
+ import { Ed25519PublicKey , Secp256k1PublicKey , marshalPublicKey , supportedKeys } from '@libp2p/crypto/keys'
3
+ import { InvalidCryptoExchangeError , UnexpectedPeerError } from '@libp2p/interface'
4
4
import { peerIdFromKeys } from '@libp2p/peer-id'
5
5
import { AsnConvert } from '@peculiar/asn1-schema'
6
6
import * as asn1X509 from '@peculiar/asn1-x509'
@@ -13,7 +13,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
13
13
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
14
14
import { InvalidCertificateError } from './errors.js'
15
15
import { KeyType , PublicKey } from './pb/index.js'
16
- import type { PeerId , PublicKey as Libp2pPublicKey , Logger } from '@libp2p/interface'
16
+ import type { PeerId , PublicKey as Libp2pPublicKey , Logger , PrivateKey } from '@libp2p/interface'
17
17
import type { Duplex } from 'it-stream-types'
18
18
import type { Uint8ArrayList } from 'uint8arraylist'
19
19
@@ -74,7 +74,7 @@ export async function verifyPeerCertificate (rawCertificate: Uint8Array, expecte
74
74
75
75
if ( remotePublicKey . type === KeyType . Ed25519 ) {
76
76
remoteLibp2pPublicKey = new Ed25519PublicKey ( remotePublicKeyData )
77
- } else if ( remotePublicKey . type === KeyType . Secp256k1 ) {
77
+ } else if ( remotePublicKey . type === KeyType . secp256k1 ) {
78
78
remoteLibp2pPublicKey = new Secp256k1PublicKey ( remotePublicKeyData )
79
79
} else if ( remotePublicKey . type === KeyType . RSA ) {
80
80
remoteLibp2pPublicKey = supportedKeys . rsa . unmarshalRsaPublicKey ( remotePublicKeyData )
@@ -104,35 +104,7 @@ export async function verifyPeerCertificate (rawCertificate: Uint8Array, expecte
104
104
return remotePeerId
105
105
}
106
106
107
- export async function generateCertificate ( peerId : PeerId ) : Promise < { cert : string , key : string } > {
108
- if ( peerId . privateKey == null ) {
109
- throw new InvalidParametersError ( 'Private key was missing from PeerId' )
110
- }
111
-
112
- if ( peerId . publicKey == null ) {
113
- throw new InvalidParametersError ( 'Public key missing from PeerId' )
114
- }
115
-
116
- const publicKey = unmarshalPublicKey ( peerId . publicKey )
117
- let keyType : KeyType
118
- let keyData : Uint8Array
119
-
120
- if ( peerId . type === 'Ed25519' ) {
121
- // Ed25519: Only the 32 bytes of the public key
122
- keyType = KeyType . Ed25519
123
- keyData = publicKey . marshal ( )
124
- } else if ( peerId . type === 'secp256k1' ) {
125
- // Secp256k1: Only the compressed form of the public key. 33 bytes.
126
- keyType = KeyType . Secp256k1
127
- keyData = publicKey . marshal ( )
128
- } else if ( peerId . type === 'RSA' ) {
129
- // The rest of the keys are encoded as a SubjectPublicKeyInfo structure in PKIX, ASN.1 DER form.
130
- keyType = KeyType . RSA
131
- keyData = publicKey . marshal ( )
132
- } else {
133
- throw new InvalidParametersError ( 'PeerId had unknown or unsupported type' )
134
- }
135
-
107
+ export async function generateCertificate ( privateKey : PrivateKey ) : Promise < { cert : string , key : string } > {
136
108
const now = Date . now ( )
137
109
138
110
const alg = {
@@ -144,7 +116,6 @@ export async function generateCertificate (peerId: PeerId): Promise<{ cert: stri
144
116
const keys = await crypto . subtle . generateKey ( alg , true , [ 'sign' ] )
145
117
const certPublicKeySpki = await crypto . subtle . exportKey ( 'spki' , keys . publicKey )
146
118
const dataToSign = encodeSignatureData ( certPublicKeySpki )
147
- const privateKey = await unmarshalPrivateKey ( peerId . privateKey )
148
119
const sig = await privateKey . sign ( dataToSign )
149
120
const notAfter = new Date ( now + CERT_VALIDITY_PERIOD_TO )
150
121
// workaround for https://github.com/PeculiarVentures/x509/issues/73
@@ -163,8 +134,8 @@ export async function generateCertificate (peerId: PeerId): Promise<{ cert: stri
163
134
// publicKey
164
135
new asn1js . OctetString ( {
165
136
valueHex : PublicKey . encode ( {
166
- type : keyType ,
167
- data : keyData
137
+ type : KeyType [ privateKey . type ] ,
138
+ data : privateKey . public . marshal ( )
168
139
} )
169
140
} ) ,
170
141
// signature
0 commit comments