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

Commit

Permalink
feat: normalize KEY API (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias authored and daviddias committed Jan 5, 2018
1 parent c677526 commit 5a21d6c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 67 deletions.
90 changes: 44 additions & 46 deletions SPEC/KEY.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ If no `callback` is passed, a promise is returned.
**Example:**

```JavaScript
ipfs.key.add(
'my-key',
{ type: 'rsa', size: 2048 },
(err, key) => console.log(key))
ipfs.key.gen('my-key', {
type: 'rsa',
size: 2048
}, (err, key) => console.log(key))


{
Name: 'my-key',
Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg'
}
// { id: 'QmYWqAFvLWb2G5A69JGXui2JJXzaHXiUEmQkQgor6kNNcJ',
// name: 'my-key' }
```

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

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

`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`)
`callback` must follow `function (err, keys) {}` signature, where `err` is an Error if the operation was not successful. `keys` is an array of:

```
{
id: 'hash', // string - the hash of the key
name: 'self' // string - the name of the key
}
```

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

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

{
Keys: [
{ Name: 'self',
Id: 'QmRT6i9wXVSmxKi3MxVRduZqF3Wvv8DuV5utMXPN3BxPML' },
{ Name: 'my-key',
Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg' }
]
}
// [
// { id: 'QmTe4tuceM2sAmuZiFsJ9tmAopA8au71NabBDdpPYDjxAb',
// name: 'self' },
// { id: 'QmWETF5QvzGnP7jKq5sPDiRjSM2fzwzNsna4wSBEzRzK6W',
// name: 'my-key' }
// ]
```

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

`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.
`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:

```
{
id: 'hash', // string - the hash of the key
name: 'self' // string - the name of the key
}
```

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

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

{
Keys: [
{ Name: 'my-key',
Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg' }
]
}
// { id: 'QmWETF5QvzGnP7jKq5sPDiRjSM2fzwzNsna4wSBEzRzK6W',
// name: 'my-key' }
```

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

```JavaScript
ipfs.key.rename(
'my-key',
'my-new-key',
(err, key) => console.log(key))

{
Was: 'my-key',
Now: 'my-new-key',
Id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg',
Overwrite: false
}
ipfs.key.rename('my-key', 'my-new-key', (err, key) => console.log(key))

// { id: 'Qmd4xC46Um6s24MradViGLFtMitvrR4SVexKUgPgFjMNzg',
// was: 'my-key',
// now: 'my-new-key',
// overwrite: false }
```

#### `export`
Expand All @@ -134,7 +134,7 @@ Where:
- `name` is the local name for the key
- `password` is the password to protect the key

`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
`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.

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

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

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFDTA/BgkqhkiG9w0BBQ0wMjAaBgkqhkiG9w0BBQwwDQQIpdO40RVyBwACAWQw
...
YA==
-----END ENCRYPTED PRIVATE KEY-----

// -----BEGIN ENCRYPTED PRIVATE KEY-----
// MIIFDTA/BgkqhkiG9w0BBQ0wMjAaBgkqhkiG9w0BBQwwDQQIpdO40RVyBwACAWQw
// ...
// YA==
// -----END ENCRYPTED PRIVATE KEY-----
```

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

{ Name: 'clone',
Id: 'QmQRiays958UM7norGRQUG3tmrLq8pJdmJarwYSk2eLthQ'
}
// { id: 'QmQRiays958UM7norGRQUG3tmrLq8pJdmJarwYSk2eLthQ',
// name: 'clone' }
```
40 changes: 19 additions & 21 deletions src/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ module.exports = (common) => {
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')
expect(key).to.have.property('name', name)
expect(key).to.have.property('id')
keys.push(key)
done()
})
Expand All @@ -62,16 +62,16 @@ module.exports = (common) => {
ipfs.key.list((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res.Keys).to.exist()
expect(res.Keys.length).to.be.above(keys.length - 1)
listedKeys = res.Keys
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)
const found = listedKeys.filter(lk => ki.name === lk.name && ki.id === lk.id)
expect(found).to.have.length(1)
})
})
Expand All @@ -82,26 +82,26 @@ module.exports = (common) => {
let newName

before(() => {
oldName = keys[0].Name
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
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.Keys.filter(k => k.Name === newName)
const found = res.filter(k => k.name === newName)
expect(found).to.have.length(1)
done()
})
Expand All @@ -110,7 +110,7 @@ module.exports = (common) => {
it('does not contain the old name', (done) => {
ipfs.key.list((err, res) => {
expect(err).to.not.exist()
const found = res.Keys.filter(k => k.Name === oldName)
const found = res.filter(k => k.name === oldName)
expect(found).to.have.length(0)
done()
})
Expand All @@ -124,21 +124,19 @@ module.exports = (common) => {
})

it('removes a key', function (done) {
ipfs.key.rm(key.Name, (err, res) => {
ipfs.key.rm(key.name, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.property('Keys')
expect(res.Keys).to.have.length(1)
expect(res.Keys[0]).to.have.property('Name', key.Name)
expect(res.Keys[0]).to.have.property('Id', key.Id)
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.Keys.filter(k => k.Name === key.Name)
const found = res.filter(k => k.name === key.name)
expect(found).to.have.length(0)
done()
})
Expand Down Expand Up @@ -170,8 +168,8 @@ module.exports = (common) => {
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')
expect(key).to.have.property('name', 'clone')
expect(key).to.have.property('id')
done()
})
})
Expand Down

0 comments on commit 5a21d6c

Please sign in to comment.