11import { Duplex as DuplexStream } from 'node:stream'
22import { Ed25519PublicKey , Secp256k1PublicKey , marshalPublicKey , supportedKeys , unmarshalPrivateKey , unmarshalPublicKey } from '@libp2p/crypto/keys'
3- import { CodeError , InvalidCryptoExchangeError , UnexpectedPeerError } from '@libp2p/interface'
3+ import { InvalidCryptoExchangeError , InvalidParametersError , UnexpectedPeerError } from '@libp2p/interface'
44import { peerIdFromKeys } from '@libp2p/peer-id'
55import { AsnConvert } from '@peculiar/asn1-schema'
66import * as asn1X509 from '@peculiar/asn1-x509'
@@ -11,7 +11,8 @@ import { pushable } from 'it-pushable'
1111import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
1212import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
1313import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
14- import { KeyType , PublicKey } from '../src/pb/index.js'
14+ import { InvalidCertificateError } from './errors.js'
15+ import { KeyType , PublicKey } from './pb/index.js'
1516import type { PeerId , PublicKey as Libp2pPublicKey , Logger } from '@libp2p/interface'
1617import type { Duplex } from 'it-stream-types'
1718import type { Uint8ArrayList } from 'uint8arraylist'
@@ -33,12 +34,12 @@ export async function verifyPeerCertificate (rawCertificate: Uint8Array, expecte
3334
3435 if ( x509Cert . notBefore . getTime ( ) > now ) {
3536 log ?. error ( 'the certificate was not valid yet' )
36- throw new CodeError ( 'The certificate is not valid yet' , 'ERR_INVALID_CERTIFICATE ')
37+ throw new InvalidCertificateError ( 'The certificate is not valid yet' )
3738 }
3839
3940 if ( x509Cert . notAfter . getTime ( ) < now ) {
4041 log ?. error ( 'the certificate has expired' )
41- throw new CodeError ( 'The certificate has expired' , 'ERR_INVALID_CERTIFICATE ')
42+ throw new InvalidCertificateError ( 'The certificate has expired' )
4243 }
4344
4445 const certSignatureValid = await x509Cert . verify ( )
@@ -59,7 +60,7 @@ export async function verifyPeerCertificate (rawCertificate: Uint8Array, expecte
5960
6061 if ( libp2pPublicKeyExtension == null || libp2pPublicKeyExtension . type !== LIBP2P_PUBLIC_KEY_EXTENSION ) {
6162 log ?. error ( 'the certificate did not include the libp2p public key extension' )
62- throw new CodeError ( 'The certificate did not include the libp2p public key extension' , 'ERR_INVALID_CERTIFICATE ')
63+ throw new InvalidCertificateError ( 'The certificate did not include the libp2p public key extension' )
6364 }
6465
6566 const { result : libp2pKeySequence } = asn1js . fromBER ( libp2pPublicKeyExtension . value )
@@ -104,34 +105,17 @@ export async function verifyPeerCertificate (rawCertificate: Uint8Array, expecte
104105}
105106
106107export async function generateCertificate ( peerId : PeerId ) : Promise < { cert : string , key : string } > {
107- const now = Date . now ( )
108-
109- const alg = {
110- name : 'ECDSA' ,
111- namedCurve : 'P-256' ,
112- hash : 'SHA-256'
113- }
114-
115- const keys = await crypto . subtle . generateKey ( alg , true , [ 'sign' ] )
116-
117- const certPublicKeySpki = await crypto . subtle . exportKey ( 'spki' , keys . publicKey )
118- const dataToSign = encodeSignatureData ( certPublicKeySpki )
119-
120108 if ( peerId . privateKey == null ) {
121- throw new InvalidCryptoExchangeError ( 'Private key was missing from PeerId' )
109+ throw new InvalidParametersError ( 'Private key was missing from PeerId' )
122110 }
123111
124- const privateKey = await unmarshalPrivateKey ( peerId . privateKey )
125- const sig = await privateKey . sign ( dataToSign )
126-
127- let keyType : KeyType
128- let keyData : Uint8Array
129-
130112 if ( peerId . publicKey == null ) {
131- throw new CodeError ( 'Public key missing from PeerId' , 'ERR_INVALID_PEER_ID ')
113+ throw new InvalidParametersError ( 'Public key missing from PeerId' )
132114 }
133115
134116 const publicKey = unmarshalPublicKey ( peerId . publicKey )
117+ let keyType : KeyType
118+ let keyData : Uint8Array
135119
136120 if ( peerId . type === 'Ed25519' ) {
137121 // Ed25519: Only the 32 bytes of the public key
@@ -146,9 +130,22 @@ export async function generateCertificate (peerId: PeerId): Promise<{ cert: stri
146130 keyType = KeyType . RSA
147131 keyData = publicKey . marshal ( )
148132 } else {
149- throw new CodeError ( 'Unknown PeerId type' , 'ERR_UNKNOWN_PEER_ID_TYPE ')
133+ throw new InvalidParametersError ( ' PeerId had unknown or unsupported type ')
150134 }
151135
136+ const now = Date . now ( )
137+
138+ const alg = {
139+ name : 'ECDSA' ,
140+ namedCurve : 'P-256' ,
141+ hash : 'SHA-256'
142+ }
143+
144+ const keys = await crypto . subtle . generateKey ( alg , true , [ 'sign' ] )
145+ const certPublicKeySpki = await crypto . subtle . exportKey ( 'spki' , keys . publicKey )
146+ const dataToSign = encodeSignatureData ( certPublicKeySpki )
147+ const privateKey = await unmarshalPrivateKey ( peerId . privateKey )
148+ const sig = await privateKey . sign ( dataToSign )
152149 const notAfter = new Date ( now + CERT_VALIDITY_PERIOD_TO )
153150 // workaround for https://github.com/PeculiarVentures/x509/issues/73
154151 notAfter . setMilliseconds ( 0 )
0 commit comments