33import { pbkdf2 , randomBytes } from '@libp2p/crypto'
44import { privateKeyToProtobuf } from '@libp2p/crypto/keys'
55import { InvalidParametersError , NotFoundError , serviceCapabilities } from '@libp2p/interface'
6- import { mergeOptions } from '@libp2p/utils'
76import { Key } from 'interface-datastore/key'
87import { base58btc } from 'multiformats/bases/base58'
98import { sha256 } from 'multiformats/hashes/sha2'
109import sanitize from 'sanitize-filename'
1110import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
1211import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
12+ import { DEK_INIT } from './constants.ts'
1313import { exportPrivateKey } from './utils/export.js'
1414import { importPrivateKey } from './utils/import.js'
1515import type { KeychainComponents , KeychainInit , Keychain as KeychainInterface , KeyInfo } from './index.js'
@@ -26,16 +26,6 @@ const NIST = {
2626 minIterationCount : 1000
2727}
2828
29- const defaultOptions = {
30- // See https://cryptosense.com/parametesr-choice-for-pbkdf2/
31- dek : {
32- keyLength : 512 / 8 ,
33- iterationCount : 10000 ,
34- salt : 'you should override this value with a crypto secure random number' ,
35- hash : 'sha2-512'
36- }
37- }
38-
3929function validateKeyName ( name : string ) : boolean {
4030 if ( name == null ) {
4131 return false
@@ -101,7 +91,13 @@ export class Keychain implements KeychainInterface {
10191 constructor ( components : KeychainComponents , init : KeychainInit ) {
10292 this . components = components
10393 this . log = components . logger . forComponent ( 'libp2p:keychain' )
104- this . init = mergeOptions ( defaultOptions , init )
94+ this . init = {
95+ ...init ,
96+ dek : {
97+ ...DEK_INIT ,
98+ ...init . dek
99+ }
100+ }
105101 this . self = init . selfKey ?? 'self'
106102
107103 // Enforce NIST SP 800-132
@@ -142,9 +138,13 @@ export class Keychain implements KeychainInterface {
142138 * @returns {object }
143139 */
144140 static generateOptions ( ) : KeychainInit {
145- const options = Object . assign ( { } , defaultOptions )
141+ const options = Object . assign ( { } , this . options )
146142 const saltLength = Math . ceil ( NIST . minSaltLength / 3 ) * 3 // no base64 padding
147- options . dek . salt = uint8ArrayToString ( randomBytes ( saltLength ) , 'base64' )
143+
144+ if ( options . dek != null ) {
145+ options . dek . salt = uint8ArrayToString ( randomBytes ( saltLength ) , 'base64' )
146+ }
147+
148148 return options
149149 }
150150
@@ -154,8 +154,12 @@ export class Keychain implements KeychainInterface {
154154 *
155155 * @returns {object }
156156 */
157- static get options ( ) : typeof defaultOptions {
158- return defaultOptions
157+ static get options ( ) : KeychainInit {
158+ return {
159+ dek : {
160+ ...DEK_INIT
161+ }
162+ }
159163 }
160164
161165 async findKeyByName ( name : string ) : Promise < KeyInfo > {
0 commit comments