3
3
4
4
const { nanoid } = require ( 'nanoid' )
5
5
const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
6
+ const { keys : { supportedKeys, import : importKey } } = require ( 'libp2p-crypto' )
6
7
7
8
/**
8
9
* @typedef {import('ipfsd-ctl').Factory } Factory
@@ -18,7 +19,18 @@ module.exports = (factory, options) => {
18
19
19
20
describe ( '.key.gen' , ( ) => {
20
21
const keyTypes = [
21
- { type : 'rsa' , size : 2048 }
22
+ {
23
+ opts : { type : 'rsa' , size : 2048 } ,
24
+ expectedType : supportedKeys . rsa . RsaPrivateKey
25
+ } ,
26
+ {
27
+ opts : { type : 'ed25519' } ,
28
+ expectedType : supportedKeys . ed25519 . Ed25519PrivateKey
29
+ } ,
30
+ {
31
+ opts : { } ,
32
+ expectedType : supportedKeys . ed25519 . Ed25519PrivateKey
33
+ }
22
34
]
23
35
24
36
/** @type {import('ipfs-core-types').IPFS } */
@@ -31,14 +43,30 @@ module.exports = (factory, options) => {
31
43
after ( ( ) => factory . clean ( ) )
32
44
33
45
keyTypes . forEach ( ( kt ) => {
34
- it ( `should generate a new ${ kt . type } key` , async function ( ) {
46
+ it ( `should generate a new ${ kt . opts . type || 'default' } key` , async function ( ) {
35
47
// @ts -ignore this is mocha
36
48
this . timeout ( 20 * 1000 )
37
49
const name = nanoid ( )
38
- const key = await ipfs . key . gen ( name , kt )
50
+ const key = await ipfs . key . gen ( name , kt . opts )
39
51
expect ( key ) . to . exist ( )
40
52
expect ( key ) . to . have . property ( 'name' , name )
41
53
expect ( key ) . to . have . property ( 'id' )
54
+
55
+ try {
56
+ const password = nanoid ( ) + '-' + nanoid ( )
57
+ const exported = await ipfs . key . export ( name , password )
58
+ const imported = await importKey ( exported , password )
59
+
60
+ expect ( imported ) . to . be . an . instanceOf ( kt . expectedType )
61
+ } catch ( err ) {
62
+ if ( err . code === 'ERR_NOT_IMPLEMENTED' ) {
63
+ // key export is not exposed over the HTTP API
64
+ // @ts -ignore this is mocha
65
+ this . skip ( 'Cannot verify key type' )
66
+ }
67
+
68
+ throw err
69
+ }
42
70
} )
43
71
} )
44
72
} )
0 commit comments