@@ -13,7 +13,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
1313import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
1414import { InvalidCertificateError } from './errors.js'
1515import { KeyType , PublicKey } from './pb/index.js'
16- import type { PeerId , PublicKey as Libp2pPublicKey , Logger , PrivateKey } from '@libp2p/interface'
16+ import type { PeerId , PublicKey as Libp2pPublicKey , Logger , PrivateKey , AbortOptions } from '@libp2p/interface'
1717import type { Pushable } from 'it-queueless-pushable'
1818import type { Duplex , Source } from 'it-stream-types'
1919import type { Uint8ArrayList } from 'uint8arraylist'
@@ -91,7 +91,7 @@ export async function verifyPeerCertificate (rawCertificate: Uint8Array, expecte
9191 return remotePeerId
9292}
9393
94- export async function generateCertificate ( privateKey : PrivateKey ) : Promise < { cert : string , key : string } > {
94+ export async function generateCertificate ( privateKey : PrivateKey , options ?: AbortOptions ) : Promise < { cert : string , key : string } > {
9595 const now = Date . now ( )
9696
9797 const alg = {
@@ -101,9 +101,13 @@ export async function generateCertificate (privateKey: PrivateKey): Promise<{ ce
101101 }
102102
103103 const keys = await crypto . subtle . generateKey ( alg , true , [ 'sign' ] )
104+ options ?. signal ?. throwIfAborted ( )
105+
104106 const certPublicKeySpki = await crypto . subtle . exportKey ( 'spki' , keys . publicKey )
107+ options ?. signal ?. throwIfAborted ( )
108+
105109 const dataToSign = encodeSignatureData ( certPublicKeySpki )
106- const sig = await privateKey . sign ( dataToSign )
110+ const sig = await privateKey . sign ( dataToSign , options )
107111 const notAfter = new Date ( now + CERT_VALIDITY_PERIOD_TO )
108112 // workaround for https://github.com/PeculiarVentures/x509/issues/73
109113 notAfter . setMilliseconds ( 0 )
@@ -133,8 +137,10 @@ export async function generateCertificate (privateKey: PrivateKey): Promise<{ ce
133137 } ) . toBER ( ) )
134138 ]
135139 } )
140+ options ?. signal ?. throwIfAborted ( )
136141
137142 const certPrivateKeyPkcs8 = await crypto . subtle . exportKey ( 'pkcs8' , keys . privateKey )
143+ options ?. signal ?. throwIfAborted ( )
138144
139145 return {
140146 cert : selfCert . toString ( ) ,
0 commit comments