@@ -4,7 +4,7 @@ import { logger } from '@libp2p/logger'
44import sanitize from 'sanitize-filename'
55import mergeOptions from 'merge-options'
66import { Key } from 'interface-datastore/key'
7- import errCode from 'err-code '
7+ import { CodeError } from '@libp2p/interfaces/errors '
88import { codes } from './errors.js'
99import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
1010import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
@@ -169,26 +169,26 @@ export class DefaultKeyChain implements KeyChain {
169169 async createKey ( name : string , type : KeyType , size = 2048 ) : Promise < KeyInfo > {
170170 if ( ! validateKeyName ( name ) || name === 'self' ) {
171171 await randomDelay ( )
172- throw errCode ( new Error ( 'Invalid key name' ) , codes . ERR_INVALID_KEY_NAME )
172+ throw new CodeError ( 'Invalid key name' , codes . ERR_INVALID_KEY_NAME )
173173 }
174174
175175 if ( typeof type !== 'string' ) {
176176 await randomDelay ( )
177- throw errCode ( new Error ( 'Invalid key type' ) , codes . ERR_INVALID_KEY_TYPE )
177+ throw new CodeError ( 'Invalid key type' , codes . ERR_INVALID_KEY_TYPE )
178178 }
179179
180180 const dsname = DsName ( name )
181181 const exists = await this . components . datastore . has ( dsname )
182182 if ( exists ) {
183183 await randomDelay ( )
184- throw errCode ( new Error ( 'Key name already exists' ) , codes . ERR_KEY_ALREADY_EXISTS )
184+ throw new CodeError ( 'Key name already exists' , codes . ERR_KEY_ALREADY_EXISTS )
185185 }
186186
187187 switch ( type . toLowerCase ( ) ) {
188188 case 'rsa' :
189189 if ( ! Number . isSafeInteger ( size ) || size < 2048 ) {
190190 await randomDelay ( )
191- throw errCode ( new Error ( 'Invalid RSA key size' ) , codes . ERR_INVALID_KEY_SIZE )
191+ throw new CodeError ( 'Invalid RSA key size' , codes . ERR_INVALID_KEY_SIZE )
192192 }
193193 break
194194 default :
@@ -202,7 +202,7 @@ export class DefaultKeyChain implements KeyChain {
202202 const cached = privates . get ( this )
203203
204204 if ( cached == null ) {
205- throw errCode ( new Error ( 'dek missing' ) , codes . ERR_INVALID_PARAMETERS )
205+ throw new CodeError ( 'dek missing' , codes . ERR_INVALID_PARAMETERS )
206206 }
207207
208208 const dek = cached . dek
@@ -251,7 +251,7 @@ export class DefaultKeyChain implements KeyChain {
251251 const key = keys . find ( ( k ) => k . id === id )
252252
253253 if ( key == null ) {
254- throw errCode ( new Error ( `Key with id '${ id } ' does not exist.` ) , codes . ERR_KEY_NOT_FOUND )
254+ throw new CodeError ( `Key with id '${ id } ' does not exist.` , codes . ERR_KEY_NOT_FOUND )
255255 }
256256
257257 return key
@@ -270,7 +270,7 @@ export class DefaultKeyChain implements KeyChain {
270270 async findKeyByName ( name : string ) : Promise < KeyInfo > {
271271 if ( ! validateKeyName ( name ) ) {
272272 await randomDelay ( )
273- throw errCode ( new Error ( `Invalid key name '${ name } '` ) , codes . ERR_INVALID_KEY_NAME )
273+ throw new CodeError ( `Invalid key name '${ name } '` , codes . ERR_INVALID_KEY_NAME )
274274 }
275275
276276 const dsname = DsInfoName ( name )
@@ -280,7 +280,7 @@ export class DefaultKeyChain implements KeyChain {
280280 } catch ( err : any ) {
281281 await randomDelay ( )
282282 log . error ( err )
283- throw errCode ( new Error ( `Key '${ name } ' does not exist.` ) , codes . ERR_KEY_NOT_FOUND )
283+ throw new CodeError ( `Key '${ name } ' does not exist.` , codes . ERR_KEY_NOT_FOUND )
284284 }
285285 }
286286
@@ -293,7 +293,7 @@ export class DefaultKeyChain implements KeyChain {
293293 async removeKey ( name : string ) : Promise < KeyInfo > {
294294 if ( ! validateKeyName ( name ) || name === 'self' ) {
295295 await randomDelay ( )
296- throw errCode ( new Error ( `Invalid key name '${ name } '` ) , codes . ERR_INVALID_KEY_NAME )
296+ throw new CodeError ( `Invalid key name '${ name } '` , codes . ERR_INVALID_KEY_NAME )
297297 }
298298 const dsname = DsName ( name )
299299 const keyInfo = await this . findKeyByName ( name )
@@ -314,11 +314,11 @@ export class DefaultKeyChain implements KeyChain {
314314 async renameKey ( oldName : string , newName : string ) : Promise < KeyInfo > {
315315 if ( ! validateKeyName ( oldName ) || oldName === 'self' ) {
316316 await randomDelay ( )
317- throw errCode ( new Error ( `Invalid old key name '${ oldName } '` ) , codes . ERR_OLD_KEY_NAME_INVALID )
317+ throw new CodeError ( `Invalid old key name '${ oldName } '` , codes . ERR_OLD_KEY_NAME_INVALID )
318318 }
319319 if ( ! validateKeyName ( newName ) || newName === 'self' ) {
320320 await randomDelay ( )
321- throw errCode ( new Error ( `Invalid new key name '${ newName } '` ) , codes . ERR_NEW_KEY_NAME_INVALID )
321+ throw new CodeError ( `Invalid new key name '${ newName } '` , codes . ERR_NEW_KEY_NAME_INVALID )
322322 }
323323 const oldDsname = DsName ( oldName )
324324 const newDsname = DsName ( newName )
@@ -328,7 +328,7 @@ export class DefaultKeyChain implements KeyChain {
328328 const exists = await this . components . datastore . has ( newDsname )
329329 if ( exists ) {
330330 await randomDelay ( )
331- throw errCode ( new Error ( `Key '${ newName } ' already exists` ) , codes . ERR_KEY_ALREADY_EXISTS )
331+ throw new CodeError ( `Key '${ newName } ' already exists` , codes . ERR_KEY_ALREADY_EXISTS )
332332 }
333333
334334 try {
@@ -356,11 +356,11 @@ export class DefaultKeyChain implements KeyChain {
356356 async exportKey ( name : string , password : string ) : Promise < string > {
357357 if ( ! validateKeyName ( name ) ) {
358358 await randomDelay ( )
359- throw errCode ( new Error ( `Invalid key name '${ name } '` ) , codes . ERR_INVALID_KEY_NAME )
359+ throw new CodeError ( `Invalid key name '${ name } '` , codes . ERR_INVALID_KEY_NAME )
360360 }
361361 if ( password == null ) {
362362 await randomDelay ( )
363- throw errCode ( new Error ( 'Password is required' ) , codes . ERR_PASSWORD_REQUIRED )
363+ throw new CodeError ( 'Password is required' , codes . ERR_PASSWORD_REQUIRED )
364364 }
365365
366366 const dsname = DsName ( name )
@@ -370,7 +370,7 @@ export class DefaultKeyChain implements KeyChain {
370370 const cached = privates . get ( this )
371371
372372 if ( cached == null ) {
373- throw errCode ( new Error ( 'dek missing' ) , codes . ERR_INVALID_PARAMETERS )
373+ throw new CodeError ( 'dek missing' , codes . ERR_INVALID_PARAMETERS )
374374 }
375375
376376 const dek = cached . dek
@@ -404,25 +404,25 @@ export class DefaultKeyChain implements KeyChain {
404404 async importKey ( name : string , pem : string , password : string ) : Promise < KeyInfo > {
405405 if ( ! validateKeyName ( name ) || name === 'self' ) {
406406 await randomDelay ( )
407- throw errCode ( new Error ( `Invalid key name '${ name } '` ) , codes . ERR_INVALID_KEY_NAME )
407+ throw new CodeError ( `Invalid key name '${ name } '` , codes . ERR_INVALID_KEY_NAME )
408408 }
409409 if ( pem == null ) {
410410 await randomDelay ( )
411- throw errCode ( new Error ( 'PEM encoded key is required' ) , codes . ERR_PEM_REQUIRED )
411+ throw new CodeError ( 'PEM encoded key is required' , codes . ERR_PEM_REQUIRED )
412412 }
413413 const dsname = DsName ( name )
414414 const exists = await this . components . datastore . has ( dsname )
415415 if ( exists ) {
416416 await randomDelay ( )
417- throw errCode ( new Error ( `Key '${ name } ' already exists` ) , codes . ERR_KEY_ALREADY_EXISTS )
417+ throw new CodeError ( `Key '${ name } ' already exists` , codes . ERR_KEY_ALREADY_EXISTS )
418418 }
419419
420420 let privateKey
421421 try {
422422 privateKey = await importKey ( pem , password )
423423 } catch ( err : any ) {
424424 await randomDelay ( )
425- throw errCode ( new Error ( 'Cannot read the key, most likely the password is wrong' ) , codes . ERR_CANNOT_READ_KEY )
425+ throw new CodeError ( 'Cannot read the key, most likely the password is wrong' , codes . ERR_CANNOT_READ_KEY )
426426 }
427427
428428 let kid
@@ -431,7 +431,7 @@ export class DefaultKeyChain implements KeyChain {
431431 const cached = privates . get ( this )
432432
433433 if ( cached == null ) {
434- throw errCode ( new Error ( 'dek missing' ) , codes . ERR_INVALID_PARAMETERS )
434+ throw new CodeError ( 'dek missing' , codes . ERR_INVALID_PARAMETERS )
435435 }
436436
437437 const dek = cached . dek
@@ -459,13 +459,13 @@ export class DefaultKeyChain implements KeyChain {
459459 async importPeer ( name : string , peer : PeerId ) : Promise < KeyInfo > {
460460 try {
461461 if ( ! validateKeyName ( name ) ) {
462- throw errCode ( new Error ( `Invalid key name '${ name } '` ) , codes . ERR_INVALID_KEY_NAME )
462+ throw new CodeError ( `Invalid key name '${ name } '` , codes . ERR_INVALID_KEY_NAME )
463463 }
464464 if ( peer == null ) {
465- throw errCode ( new Error ( 'PeerId is required' ) , codes . ERR_MISSING_PRIVATE_KEY )
465+ throw new CodeError ( 'PeerId is required' , codes . ERR_MISSING_PRIVATE_KEY )
466466 }
467467 if ( peer . privateKey == null ) {
468- throw errCode ( new Error ( 'PeerId.privKey is required' ) , codes . ERR_MISSING_PRIVATE_KEY )
468+ throw new CodeError ( 'PeerId.privKey is required' , codes . ERR_MISSING_PRIVATE_KEY )
469469 }
470470
471471 const privateKey = await unmarshalPrivateKey ( peer . privateKey )
@@ -474,13 +474,13 @@ export class DefaultKeyChain implements KeyChain {
474474 const exists = await this . components . datastore . has ( dsname )
475475 if ( exists ) {
476476 await randomDelay ( )
477- throw errCode ( new Error ( `Key '${ name } ' already exists` ) , codes . ERR_KEY_ALREADY_EXISTS )
477+ throw new CodeError ( `Key '${ name } ' already exists` , codes . ERR_KEY_ALREADY_EXISTS )
478478 }
479479
480480 const cached = privates . get ( this )
481481
482482 if ( cached == null ) {
483- throw errCode ( new Error ( 'dek missing' ) , codes . ERR_INVALID_PARAMETERS )
483+ throw new CodeError ( 'dek missing' , codes . ERR_INVALID_PARAMETERS )
484484 }
485485
486486 const dek = cached . dek
@@ -506,7 +506,7 @@ export class DefaultKeyChain implements KeyChain {
506506 async getPrivateKey ( name : string ) : Promise < string > {
507507 if ( ! validateKeyName ( name ) ) {
508508 await randomDelay ( )
509- throw errCode ( new Error ( `Invalid key name '${ name } '` ) , codes . ERR_INVALID_KEY_NAME )
509+ throw new CodeError ( `Invalid key name '${ name } '` , codes . ERR_INVALID_KEY_NAME )
510510 }
511511
512512 try {
@@ -516,7 +516,7 @@ export class DefaultKeyChain implements KeyChain {
516516 } catch ( err : any ) {
517517 await randomDelay ( )
518518 log . error ( err )
519- throw errCode ( new Error ( `Key '${ name } ' does not exist.` ) , codes . ERR_KEY_NOT_FOUND )
519+ throw new CodeError ( `Key '${ name } ' does not exist.` , codes . ERR_KEY_NOT_FOUND )
520520 }
521521 }
522522
@@ -526,21 +526,21 @@ export class DefaultKeyChain implements KeyChain {
526526 async rotateKeychainPass ( oldPass : string , newPass : string ) : Promise < void > {
527527 if ( typeof oldPass !== 'string' ) {
528528 await randomDelay ( )
529- throw errCode ( new Error ( `Invalid old pass type '${ typeof oldPass } '` ) , codes . ERR_INVALID_OLD_PASS_TYPE )
529+ throw new CodeError ( `Invalid old pass type '${ typeof oldPass } '` , codes . ERR_INVALID_OLD_PASS_TYPE )
530530 }
531531 if ( typeof newPass !== 'string' ) {
532532 await randomDelay ( )
533- throw errCode ( new Error ( `Invalid new pass type '${ typeof newPass } '` ) , codes . ERR_INVALID_NEW_PASS_TYPE )
533+ throw new CodeError ( `Invalid new pass type '${ typeof newPass } '` , codes . ERR_INVALID_NEW_PASS_TYPE )
534534 }
535535 if ( newPass . length < 20 ) {
536536 await randomDelay ( )
537- throw errCode ( new Error ( `Invalid pass length ${ newPass . length } ` ) , codes . ERR_INVALID_PASS_LENGTH )
537+ throw new CodeError ( `Invalid pass length ${ newPass . length } ` , codes . ERR_INVALID_PASS_LENGTH )
538538 }
539539 log ( 'recreating keychain' )
540540 const cached = privates . get ( this )
541541
542542 if ( cached == null ) {
543- throw errCode ( new Error ( 'dek missing' ) , codes . ERR_INVALID_PARAMETERS )
543+ throw new CodeError ( 'dek missing' , codes . ERR_INVALID_PARAMETERS )
544544 }
545545
546546 const oldDek = cached . dek
0 commit comments