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

Commit 5a21d6c

Browse files
hacdiasdaviddias
authored andcommitted
feat: normalize KEY API (#192)
1 parent c677526 commit 5a21d6c

File tree

2 files changed

+63
-67
lines changed

2 files changed

+63
-67
lines changed

SPEC/KEY.md

+44-46
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ If no `callback` is passed, a promise is returned.
2323
**Example:**
2424

2525
```JavaScript
26-
ipfs.key.add(
27-
'my-key',
28-
{ type: 'rsa', size: 2048 },
29-
(err, key) => console.log(key))
26+
ipfs.key.gen('my-key', {
27+
type: 'rsa',
28+
size: 2048
29+
}, (err, key) => console.log(key))
3030

31-
32-
{
33-
Name: 'my-key',
34-
Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg'
35-
}
31+
// { id: 'QmYWqAFvLWb2G5A69JGXui2JJXzaHXiUEmQkQgor6kNNcJ',
32+
// name: 'my-key' }
3633
```
3734

3835
#### `list`
@@ -43,7 +40,14 @@ ipfs.key.add(
4340

4441
##### `JavaScript` - ipfs.key.list([callback])
4542

46-
`callback` must follow `function (err, keys) {}` signature, where `err` is an Error if the operation was not successful. `keys` is an object with the property `Keys` that is an array of `KeyInfo` (`name` and `id`)
43+
`callback` must follow `function (err, keys) {}` signature, where `err` is an Error if the operation was not successful. `keys` is an array of:
44+
45+
```
46+
{
47+
id: 'hash', // string - the hash of the key
48+
name: 'self' // string - the name of the key
49+
}
50+
```
4751

4852
If no `callback` is passed, a promise is returned.
4953

@@ -52,14 +56,12 @@ If no `callback` is passed, a promise is returned.
5256
```JavaScript
5357
ipfs.key.list((err, keys) => console.log(keys))
5458

55-
{
56-
Keys: [
57-
{ Name: 'self',
58-
Id: 'QmRT6i9wXVSmxKi3MxVRduZqF3Wvv8DuV5utMXPN3BxPML' },
59-
{ Name: 'my-key',
60-
Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg' }
61-
]
62-
}
59+
// [
60+
// { id: 'QmTe4tuceM2sAmuZiFsJ9tmAopA8au71NabBDdpPYDjxAb',
61+
// name: 'self' },
62+
// { id: 'QmWETF5QvzGnP7jKq5sPDiRjSM2fzwzNsna4wSBEzRzK6W',
63+
// name: 'my-key' }
64+
// ]
6365
```
6466

6567
#### `rm`
@@ -73,7 +75,14 @@ ipfs.key.list((err, keys) => console.log(keys))
7375
Where:
7476
- `name` is the local name for the key
7577

76-
`callback` must follow `function (err, key) {}` signature, where `err` is an Error if the operation was not successful. `key` is an object that describes the removed key.
78+
`callback` must follow `function (err, key) {}` signature, where `err` is an Error if the operation was not successful. `key` is an object that describes the removed key:
79+
80+
```
81+
{
82+
id: 'hash', // string - the hash of the key
83+
name: 'self' // string - the name of the key
84+
}
85+
```
7786

7887
If no `callback` is passed, a promise is returned.
7988

@@ -82,12 +91,8 @@ If no `callback` is passed, a promise is returned.
8291
```JavaScript
8392
ipfs.key.rm('my-key', (err, key) => console.log(key))
8493

85-
{
86-
Keys: [
87-
{ Name: 'my-key',
88-
Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg' }
89-
]
90-
}
94+
// { id: 'QmWETF5QvzGnP7jKq5sPDiRjSM2fzwzNsna4wSBEzRzK6W',
95+
// name: 'my-key' }
9196
```
9297

9398
#### `rename`
@@ -109,17 +114,12 @@ If no `callback` is passed, a promise is returned.
109114
**Example:**
110115

111116
```JavaScript
112-
ipfs.key.rename(
113-
'my-key',
114-
'my-new-key',
115-
(err, key) => console.log(key))
116-
117-
{
118-
Was: 'my-key',
119-
Now: 'my-new-key',
120-
Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg',
121-
Overwrite: false
122-
}
117+
ipfs.key.rename('my-key', 'my-new-key', (err, key) => console.log(key))
118+
119+
// { id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg',
120+
// was: 'my-key',
121+
// now: 'my-new-key',
122+
// overwrite: false }
123123
```
124124

125125
#### `export`
@@ -134,7 +134,7 @@ Where:
134134
- `name` is the local name for the key
135135
- `password` is the password to protect the key
136136

137-
`callback` must follow `function (err, pem) {}` signature, where `err` is an Error if the operation was not successful. `pem` is the string representation of the key
137+
`callback` must follow `function (err, pem) {}` signature, where `err` is an Error if the operation was not successful. `pem` is the string representation of the key.
138138

139139
If no `callback` is passed, a promise is returned.
140140

@@ -143,12 +143,11 @@ If no `callback` is passed, a promise is returned.
143143
```JavaScript
144144
ipfs.key.export('self', 'password', (err, pem) => console.log(pem))
145145

146-
-----BEGIN ENCRYPTED PRIVATE KEY-----
147-
MIIFDTA/BgkqhkiG9w0BBQ0wMjAaBgkqhkiG9w0BBQwwDQQIpdO40RVyBwACAWQw
148-
...
149-
YA==
150-
-----END ENCRYPTED PRIVATE KEY-----
151-
146+
// -----BEGIN ENCRYPTED PRIVATE KEY-----
147+
// MIIFDTA/BgkqhkiG9w0BBQ0wMjAaBgkqhkiG9w0BBQwwDQQIpdO40RVyBwACAWQw
148+
// ...
149+
// YA==
150+
// -----END ENCRYPTED PRIVATE KEY-----
152151
```
153152

154153
#### `import`
@@ -173,7 +172,6 @@ If no `callback` is passed, a promise is returned.
173172
```JavaScript
174173
ipfs.key.import('clone', 'password', (err, key) => console.log(key))
175174

176-
{ Name: 'clone',
177-
Id: 'QmQRiays958UM7norGRQUG3tmrLq8pJdmJarwYSk2eLthQ'
178-
}
175+
// { id: 'QmQRiays958UM7norGRQUG3tmrLq8pJdmJarwYSk2eLthQ',
176+
// name: 'clone' }
179177
```

src/key.js

+19-21
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ module.exports = (common) => {
4747
ipfs.key.gen(name, kt, (err, key) => {
4848
expect(err).to.not.exist()
4949
expect(key).to.exist()
50-
expect(key).to.have.property('Name', name)
51-
expect(key).to.have.property('Id')
50+
expect(key).to.have.property('name', name)
51+
expect(key).to.have.property('id')
5252
keys.push(key)
5353
done()
5454
})
@@ -62,16 +62,16 @@ module.exports = (common) => {
6262
ipfs.key.list((err, res) => {
6363
expect(err).to.not.exist()
6464
expect(res).to.exist()
65-
expect(res.Keys).to.exist()
66-
expect(res.Keys.length).to.be.above(keys.length - 1)
67-
listedKeys = res.Keys
65+
expect(res).to.be.an('array')
66+
expect(res.length).to.be.above(keys.length - 1)
67+
listedKeys = res
6868
done()
6969
})
7070
})
7171

7272
it('contains the created keys', () => {
7373
keys.forEach(ki => {
74-
const found = listedKeys.filter(lk => ki.Name === lk.Name && ki.Id === lk.Id)
74+
const found = listedKeys.filter(lk => ki.name === lk.name && ki.id === lk.id)
7575
expect(found).to.have.length(1)
7676
})
7777
})
@@ -82,26 +82,26 @@ module.exports = (common) => {
8282
let newName
8383

8484
before(() => {
85-
oldName = keys[0].Name
85+
oldName = keys[0].name
8686
newName = 'x' + oldName
8787
})
8888

8989
it('renames a key', (done) => {
9090
ipfs.key.rename(oldName, newName, (err, res) => {
9191
expect(err).to.not.exist()
9292
expect(res).to.exist()
93-
expect(res).to.have.property('Was', oldName)
94-
expect(res).to.have.property('Now', newName)
95-
expect(res).to.have.property('Id', keys[0].Id)
96-
keys[0].Name = newName
93+
expect(res).to.have.property('was', oldName)
94+
expect(res).to.have.property('now', newName)
95+
expect(res).to.have.property('id', keys[0].id)
96+
keys[0].name = newName
9797
done()
9898
})
9999
})
100100

101101
it('contains the new name', (done) => {
102102
ipfs.key.list((err, res) => {
103103
expect(err).to.not.exist()
104-
const found = res.Keys.filter(k => k.Name === newName)
104+
const found = res.filter(k => k.name === newName)
105105
expect(found).to.have.length(1)
106106
done()
107107
})
@@ -110,7 +110,7 @@ module.exports = (common) => {
110110
it('does not contain the old name', (done) => {
111111
ipfs.key.list((err, res) => {
112112
expect(err).to.not.exist()
113-
const found = res.Keys.filter(k => k.Name === oldName)
113+
const found = res.filter(k => k.name === oldName)
114114
expect(found).to.have.length(0)
115115
done()
116116
})
@@ -124,21 +124,19 @@ module.exports = (common) => {
124124
})
125125

126126
it('removes a key', function (done) {
127-
ipfs.key.rm(key.Name, (err, res) => {
127+
ipfs.key.rm(key.name, (err, res) => {
128128
expect(err).to.not.exist()
129129
expect(res).to.exist()
130-
expect(res).to.have.property('Keys')
131-
expect(res.Keys).to.have.length(1)
132-
expect(res.Keys[0]).to.have.property('Name', key.Name)
133-
expect(res.Keys[0]).to.have.property('Id', key.Id)
130+
expect(res).to.have.property('name', key.name)
131+
expect(res).to.have.property('id', key.id)
134132
done()
135133
})
136134
})
137135

138136
it('does not contain the removed name', (done) => {
139137
ipfs.key.list((err, res) => {
140138
expect(err).to.not.exist()
141-
const found = res.Keys.filter(k => k.Name === key.Name)
139+
const found = res.filter(k => k.name === key.name)
142140
expect(found).to.have.length(0)
143141
done()
144142
})
@@ -170,8 +168,8 @@ module.exports = (common) => {
170168
ipfs.key.import('clone', selfPem, passwordPem, (err, key) => {
171169
expect(err).to.not.exist()
172170
expect(key).to.exist()
173-
expect(key).to.have.property('Name', 'clone')
174-
expect(key).to.have.property('Id')
171+
expect(key).to.have.property('name', 'clone')
172+
expect(key).to.have.property('id')
175173
done()
176174
})
177175
})

0 commit comments

Comments
 (0)