Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 92bdd98

Browse files
MichaelMuredaviddias
authored andcommitted
Add key api from go-ipfs (#548)
* Add key api from go-ipfs Signed-off-by: Michael Muré <batolettre@gmail.com> * Add test for the key API Signed-off-by: Michael Muré <batolettre@gmail.com> * Complete the README for the key API Signed-off-by: Michael Muré <batolettre@gmail.com>
1 parent 3e739f0 commit 92bdd98

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ This means:
226226
- [`ipfs.ping()`]()
227227
- [`ipfs.log()`]()
228228

229+
#### [key]()
230+
231+
- [`ipfs.key.gen(name, [options, callback])`]()
232+
- [`ipfs.key.list([options, callback])`]()
233+
229234
##### [name]()
230235

231236
- [`ipfs.name.publish()`]()

src/api/key.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
3+
const promisify = require('promisify-es6')
4+
5+
module.exports = (send) => {
6+
return {
7+
gen: promisify((args, opts, callback) => {
8+
if (typeof (opts) === 'function') {
9+
callback = opts
10+
opts = {}
11+
}
12+
send({
13+
path: 'key/gen',
14+
args: args,
15+
qs: opts
16+
}, callback)
17+
}),
18+
list: promisify((opts, callback) => {
19+
if (typeof (opts) === 'function') {
20+
callback = opts
21+
opts = {}
22+
}
23+
send({
24+
path: 'key/list',
25+
qs: opts
26+
}, callback)
27+
})
28+
}
29+
}

src/load-commands.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function requireCommands () {
1414
dht: require('./api/dht'),
1515
diag: require('./api/diag'),
1616
id: require('./api/id'),
17+
key: require('./api/key'),
1718
get: require('./api/get'),
1819
log: require('./api/log'),
1920
ls: require('./api/ls'),

test/key.spec.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
'use strict'
4+
5+
const FactoryClient = require('./ipfs-factory/client')
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const expect = chai.expect
9+
chai.use(dirtyChai)
10+
11+
describe('.key', () => {
12+
let ipfs
13+
let fc
14+
15+
before(function (done) {
16+
this.timeout(20 * 1000) // slow CI
17+
fc = new FactoryClient()
18+
fc.spawnNode((err, node) => {
19+
expect(err).to.not.exist()
20+
ipfs = node
21+
done()
22+
})
23+
})
24+
25+
after((done) => {
26+
fc.dismantle(done)
27+
})
28+
29+
describe('Callback API', () => {
30+
describe('.gen', () => {
31+
it('create a new rsa key', (done) => {
32+
ipfs.key.gen('foobarsa', { type: 'rsa', size: 2048 }, (err, res) => {
33+
expect(err).to.not.exist()
34+
expect(res).to.exist()
35+
done()
36+
})
37+
})
38+
39+
it('create a new ed25519 key', (done) => {
40+
ipfs.key.gen('bazed', { type: 'ed25519' }, (err, res) => {
41+
expect(err).to.not.exist()
42+
expect(res).to.exist()
43+
done()
44+
})
45+
})
46+
})
47+
48+
describe('.list', () => {
49+
it('both keys show up + self', (done) => {
50+
ipfs.key.list((err, res) => {
51+
expect(err).to.not.exist()
52+
expect(res).to.exist()
53+
expect(res.Keys.length).to.equal(3)
54+
done()
55+
})
56+
})
57+
})
58+
})
59+
60+
describe('Promise API', () => {
61+
describe('.gen', () => {
62+
it('create a new rsa key', () => {
63+
return ipfs.key.gen('foobarsa2', {type: 'rsa', size: 2048}).then((res) => {
64+
expect(res).to.exist()
65+
})
66+
})
67+
68+
it('create a new ed25519 key', () => {
69+
return ipfs.key.gen('bazed2', {type: 'ed25519'}).then((res) => {
70+
expect(res).to.exist()
71+
})
72+
})
73+
})
74+
75+
describe('.list', () => {
76+
it('4 keys to show up + self', () => {
77+
return ipfs.key.list().then((res) => {
78+
expect(res).to.exist()
79+
expect(res.Keys.length).to.equal(5)
80+
})
81+
})
82+
})
83+
})
84+
})

0 commit comments

Comments
 (0)