This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathgen.js
75 lines (63 loc) · 1.96 KB
/
gen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* eslint-env mocha */
import { nanoid } from 'nanoid'
import { expect } from 'aegir/utils/chai.js'
import { getDescribe, getIt } from '../utils/mocha.js'
import { keys } from 'libp2p-crypto'
const { supportedKeys, import: importKey } = keys
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {Object} options
*/
export function testGen (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.key.gen', () => {
const keyTypes = [
{
opts: { type: 'rsa', size: 2048 },
expectedType: supportedKeys.rsa.RsaPrivateKey
},
{
opts: { type: 'ed25519' },
expectedType: supportedKeys.ed25519.Ed25519PrivateKey
},
{
opts: { },
expectedType: supportedKeys.ed25519.Ed25519PrivateKey
}
]
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async () => {
ipfs = (await factory.spawn()).api
})
after(() => factory.clean())
keyTypes.forEach((kt) => {
it(`should generate a new ${kt.opts.type || 'default'} key`, async function () {
// @ts-ignore this is mocha
this.timeout(20 * 1000)
const name = nanoid()
const key = await ipfs.key.gen(name, kt.opts)
expect(key).to.exist()
expect(key).to.have.property('name', name)
expect(key).to.have.property('id')
try {
const password = nanoid() + '-' + nanoid()
const exported = await ipfs.key.export(name, password)
const imported = await importKey(exported, password)
expect(imported).to.be.an.instanceOf(kt.expectedType)
} catch (/** @type {any} */ err) {
if (err.code === 'ERR_NOT_IMPLEMENTED') {
// key export is not exposed over the HTTP API
// @ts-ignore this is mocha
this.skip('Cannot verify key type')
}
throw err
}
})
})
})
}