@@ -4,7 +4,7 @@ import { logger } from '@libp2p/logger'
4
4
import sanitize from 'sanitize-filename'
5
5
import mergeOptions from 'merge-options'
6
6
import { Key } from 'interface-datastore/key'
7
- import errCode from 'err-code '
7
+ import { CodeError } from '@libp2p/interfaces/errors '
8
8
import { codes } from './errors.js'
9
9
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
10
10
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
@@ -169,26 +169,26 @@ export class DefaultKeyChain implements KeyChain {
169
169
async createKey ( name : string , type : KeyType , size = 2048 ) : Promise < KeyInfo > {
170
170
if ( ! validateKeyName ( name ) || name === 'self' ) {
171
171
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 )
173
173
}
174
174
175
175
if ( typeof type !== 'string' ) {
176
176
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 )
178
178
}
179
179
180
180
const dsname = DsName ( name )
181
181
const exists = await this . components . datastore . has ( dsname )
182
182
if ( exists ) {
183
183
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 )
185
185
}
186
186
187
187
switch ( type . toLowerCase ( ) ) {
188
188
case 'rsa' :
189
189
if ( ! Number . isSafeInteger ( size ) || size < 2048 ) {
190
190
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 )
192
192
}
193
193
break
194
194
default :
@@ -202,7 +202,7 @@ export class DefaultKeyChain implements KeyChain {
202
202
const cached = privates . get ( this )
203
203
204
204
if ( cached == null ) {
205
- throw errCode ( new Error ( 'dek missing' ) , codes . ERR_INVALID_PARAMETERS )
205
+ throw new CodeError ( 'dek missing' , codes . ERR_INVALID_PARAMETERS )
206
206
}
207
207
208
208
const dek = cached . dek
@@ -251,7 +251,7 @@ export class DefaultKeyChain implements KeyChain {
251
251
const key = keys . find ( ( k ) => k . id === id )
252
252
253
253
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 )
255
255
}
256
256
257
257
return key
@@ -270,7 +270,7 @@ export class DefaultKeyChain implements KeyChain {
270
270
async findKeyByName ( name : string ) : Promise < KeyInfo > {
271
271
if ( ! validateKeyName ( name ) ) {
272
272
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 )
274
274
}
275
275
276
276
const dsname = DsInfoName ( name )
@@ -280,7 +280,7 @@ export class DefaultKeyChain implements KeyChain {
280
280
} catch ( err : any ) {
281
281
await randomDelay ( )
282
282
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 )
284
284
}
285
285
}
286
286
@@ -293,7 +293,7 @@ export class DefaultKeyChain implements KeyChain {
293
293
async removeKey ( name : string ) : Promise < KeyInfo > {
294
294
if ( ! validateKeyName ( name ) || name === 'self' ) {
295
295
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 )
297
297
}
298
298
const dsname = DsName ( name )
299
299
const keyInfo = await this . findKeyByName ( name )
@@ -314,11 +314,11 @@ export class DefaultKeyChain implements KeyChain {
314
314
async renameKey ( oldName : string , newName : string ) : Promise < KeyInfo > {
315
315
if ( ! validateKeyName ( oldName ) || oldName === 'self' ) {
316
316
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 )
318
318
}
319
319
if ( ! validateKeyName ( newName ) || newName === 'self' ) {
320
320
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 )
322
322
}
323
323
const oldDsname = DsName ( oldName )
324
324
const newDsname = DsName ( newName )
@@ -328,7 +328,7 @@ export class DefaultKeyChain implements KeyChain {
328
328
const exists = await this . components . datastore . has ( newDsname )
329
329
if ( exists ) {
330
330
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 )
332
332
}
333
333
334
334
try {
@@ -356,11 +356,11 @@ export class DefaultKeyChain implements KeyChain {
356
356
async exportKey ( name : string , password : string ) : Promise < string > {
357
357
if ( ! validateKeyName ( name ) ) {
358
358
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 )
360
360
}
361
361
if ( password == null ) {
362
362
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 )
364
364
}
365
365
366
366
const dsname = DsName ( name )
@@ -370,7 +370,7 @@ export class DefaultKeyChain implements KeyChain {
370
370
const cached = privates . get ( this )
371
371
372
372
if ( cached == null ) {
373
- throw errCode ( new Error ( 'dek missing' ) , codes . ERR_INVALID_PARAMETERS )
373
+ throw new CodeError ( 'dek missing' , codes . ERR_INVALID_PARAMETERS )
374
374
}
375
375
376
376
const dek = cached . dek
@@ -404,25 +404,25 @@ export class DefaultKeyChain implements KeyChain {
404
404
async importKey ( name : string , pem : string , password : string ) : Promise < KeyInfo > {
405
405
if ( ! validateKeyName ( name ) || name === 'self' ) {
406
406
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 )
408
408
}
409
409
if ( pem == null ) {
410
410
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 )
412
412
}
413
413
const dsname = DsName ( name )
414
414
const exists = await this . components . datastore . has ( dsname )
415
415
if ( exists ) {
416
416
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 )
418
418
}
419
419
420
420
let privateKey
421
421
try {
422
422
privateKey = await importKey ( pem , password )
423
423
} catch ( err : any ) {
424
424
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 )
426
426
}
427
427
428
428
let kid
@@ -431,7 +431,7 @@ export class DefaultKeyChain implements KeyChain {
431
431
const cached = privates . get ( this )
432
432
433
433
if ( cached == null ) {
434
- throw errCode ( new Error ( 'dek missing' ) , codes . ERR_INVALID_PARAMETERS )
434
+ throw new CodeError ( 'dek missing' , codes . ERR_INVALID_PARAMETERS )
435
435
}
436
436
437
437
const dek = cached . dek
@@ -459,13 +459,13 @@ export class DefaultKeyChain implements KeyChain {
459
459
async importPeer ( name : string , peer : PeerId ) : Promise < KeyInfo > {
460
460
try {
461
461
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 )
463
463
}
464
464
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 )
466
466
}
467
467
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 )
469
469
}
470
470
471
471
const privateKey = await unmarshalPrivateKey ( peer . privateKey )
@@ -474,13 +474,13 @@ export class DefaultKeyChain implements KeyChain {
474
474
const exists = await this . components . datastore . has ( dsname )
475
475
if ( exists ) {
476
476
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 )
478
478
}
479
479
480
480
const cached = privates . get ( this )
481
481
482
482
if ( cached == null ) {
483
- throw errCode ( new Error ( 'dek missing' ) , codes . ERR_INVALID_PARAMETERS )
483
+ throw new CodeError ( 'dek missing' , codes . ERR_INVALID_PARAMETERS )
484
484
}
485
485
486
486
const dek = cached . dek
@@ -506,7 +506,7 @@ export class DefaultKeyChain implements KeyChain {
506
506
async getPrivateKey ( name : string ) : Promise < string > {
507
507
if ( ! validateKeyName ( name ) ) {
508
508
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 )
510
510
}
511
511
512
512
try {
@@ -516,7 +516,7 @@ export class DefaultKeyChain implements KeyChain {
516
516
} catch ( err : any ) {
517
517
await randomDelay ( )
518
518
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 )
520
520
}
521
521
}
522
522
@@ -526,21 +526,21 @@ export class DefaultKeyChain implements KeyChain {
526
526
async rotateKeychainPass ( oldPass : string , newPass : string ) : Promise < void > {
527
527
if ( typeof oldPass !== 'string' ) {
528
528
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 )
530
530
}
531
531
if ( typeof newPass !== 'string' ) {
532
532
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 )
534
534
}
535
535
if ( newPass . length < 20 ) {
536
536
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 )
538
538
}
539
539
log ( 'recreating keychain' )
540
540
const cached = privates . get ( this )
541
541
542
542
if ( cached == null ) {
543
- throw errCode ( new Error ( 'dek missing' ) , codes . ERR_INVALID_PARAMETERS )
543
+ throw new CodeError ( 'dek missing' , codes . ERR_INVALID_PARAMETERS )
544
544
}
545
545
546
546
const oldDek = cached . dek
0 commit comments