This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathkey.js
191 lines (170 loc) · 4.91 KB
/
key.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const hat = require('hat')
module.exports = (common) => {
describe('.key', () => {
const keyTypes = [
{ type: 'rsa', size: 2048 }
]
const keys = []
let ipfs
let ipfsd
let withGo
before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)
common.setup((err, df, type, exec) => {
expect(err).to.not.exist()
df.spawn({ type, exec }, (err, node) => {
expect(err).to.not.exist()
ipfsd = node
ipfs = node.api
ipfs.id((err, id) => {
expect(err).to.not.exist()
withGo = id.agentVersion.startsWith('go-ipfs')
done()
})
})
})
})
after((done) => ipfsd.stop(done))
describe('.gen', () => {
keyTypes.forEach((kt) => {
it(`creates a new ${kt.type} key`, function (done) {
this.timeout(20 * 1000)
const name = hat()
ipfs.key.gen(name, kt, (err, key) => {
expect(err).to.not.exist()
expect(key).to.exist()
expect(key).to.have.property('name', name)
expect(key).to.have.property('id')
keys.push(key)
done()
})
})
})
})
describe('.list', () => {
let listedKeys
it('lists all the keys', (done) => {
ipfs.key.list((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.be.an('array')
expect(res.length).to.be.above(keys.length - 1)
listedKeys = res
done()
})
})
it('contains the created keys', () => {
keys.forEach(ki => {
const found = listedKeys.filter(lk => ki.name === lk.name && ki.id === lk.id)
expect(found).to.have.length(1)
})
})
})
describe('.rename', () => {
let oldName
let newName
before(() => {
oldName = keys[0].name
newName = 'x' + oldName
})
it('renames a key', (done) => {
ipfs.key.rename(oldName, newName, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.property('was', oldName)
expect(res).to.have.property('now', newName)
expect(res).to.have.property('id', keys[0].id)
keys[0].name = newName
done()
})
})
it('contains the new name', (done) => {
ipfs.key.list((err, res) => {
expect(err).to.not.exist()
const found = res.filter(k => k.name === newName)
expect(found).to.have.length(1)
done()
})
})
it('does not contain the old name', (done) => {
ipfs.key.list((err, res) => {
expect(err).to.not.exist()
const found = res.filter(k => k.name === oldName)
expect(found).to.have.length(0)
done()
})
})
})
describe('.rm', () => {
let key
before(() => {
key = keys[0]
})
it('removes a key', function (done) {
ipfs.key.rm(key.name, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.property('name', key.name)
expect(res).to.have.property('id', key.id)
done()
})
})
it('does not contain the removed name', (done) => {
ipfs.key.list((err, res) => {
expect(err).to.not.exist()
const found = res.filter(k => k.name === key.name)
expect(found).to.have.length(0)
done()
})
})
})
describe('exchange', () => {
let selfPem
let passwordPem = hat()
it('exports', (done) => {
if (withGo) {
console.log('Not supported by go-ipfs yet')
return done()
}
ipfs.key.export('self', passwordPem, (err, pem) => {
expect(err).to.not.exist()
expect(pem).to.exist()
selfPem = pem
done()
})
})
it('imports', (done) => {
if (withGo) {
console.log('Not supported by go-ipfs yet')
return done()
}
ipfs.key.import('clone', selfPem, passwordPem, (err, key) => {
expect(err).to.not.exist()
expect(key).to.exist()
expect(key).to.have.property('name', 'clone')
expect(key).to.have.property('id')
done()
})
})
it('removes', (done) => {
if (withGo) {
console.log('Not supported by go-ipfs yet')
return done()
}
ipfs.key.rm('clone', (err) => {
expect(err).to.not.exist()
done()
})
})
})
})
}