@@ -5,6 +5,7 @@ const sanitize = require('sanitize-filename')
55const mergeOptions = require ( 'merge-options' )
66const crypto = require ( 'libp2p-crypto' )
77const DS = require ( 'interface-datastore' )
8+ const promisify = require ( 'promisify-es6' )
89const CMS = require ( './cms' )
910const errcode = require ( 'err-code' )
1011
@@ -205,9 +206,16 @@ class Keychain {
205206
206207 let keyInfo
207208 try {
208- const keypair = await crypto . keys . generateKeyPair ( type , size )
209- const kid = await keypair . id ( )
210- const pem = await keypair . export ( this . _ ( ) )
209+ const keypair = await promisify ( crypto . keys . generateKeyPair , {
210+ context : crypto . keys
211+ } ) ( type , size )
212+
213+ const kid = await promisify ( keypair . id , {
214+ context : keypair
215+ } ) ( )
216+ const pem = await promisify ( keypair . export , {
217+ context : keypair
218+ } ) ( this . _ ( ) )
211219 keyInfo = {
212220 name : name ,
213221 id : kid
@@ -359,8 +367,12 @@ class Keychain {
359367 try {
360368 const res = await this . store . get ( dsname )
361369 const pem = res . toString ( )
362- const privateKey = await crypto . keys . import ( pem , this . _ ( ) )
363- return privateKey . export ( password )
370+ const privateKey = await promisify ( crypto . keys . import , {
371+ context : crypto . keys
372+ } ) ( pem , this . _ ( ) )
373+ return promisify ( privateKey . export , {
374+ context : privateKey
375+ } ) ( password )
364376 } catch ( err ) {
365377 return throwDelayed ( err )
366378 }
@@ -388,15 +400,21 @@ class Keychain {
388400
389401 let privateKey
390402 try {
391- privateKey = await crypto . keys . import ( pem , password )
403+ privateKey = await promisify ( crypto . keys . import , {
404+ context : crypto . keys
405+ } ) ( pem , password )
392406 } catch ( err ) {
393407 return throwDelayed ( errcode ( new Error ( 'Cannot read the key, most likely the password is wrong' ) , 'ERR_CANNOT_READ_KEY' ) )
394408 }
395409
396410 let kid
397411 try {
398- kid = await privateKey . id ( )
399- pem = await privateKey . export ( this . _ ( ) )
412+ kid = await promisify ( privateKey . id , {
413+ context : privateKey
414+ } ) ( )
415+ pem = await promisify ( privateKey . export , {
416+ context : privateKey
417+ } ) ( this . _ ( ) )
400418 } catch ( err ) {
401419 return throwDelayed ( err )
402420 }
@@ -428,8 +446,12 @@ class Keychain {
428446 if ( exists ) return throwDelayed ( errcode ( new Error ( `Key '${ name } ' already exists` ) , 'ERR_KEY_ALREADY_EXISTS' ) )
429447
430448 try {
431- const kid = await privateKey . id ( )
432- const pem = await privateKey . export ( this . _ ( ) )
449+ const kid = await promisify ( privateKey . id , {
450+ context : privateKey
451+ } ) ( )
452+ const pem = await promisify ( privateKey . export , {
453+ context : privateKey
454+ } ) ( this . _ ( ) )
433455 const keyInfo = {
434456 name : name ,
435457 id : kid
0 commit comments