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

Commit f4eb606

Browse files
richardschneiderdaviddias
authored andcommitted
test: key exchange (#187)
* test: key exchange * chore
1 parent a90ca29 commit f4eb606

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

SPEC/KEY.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,58 @@ ipfs.key.rename(
122122
}
123123
```
124124

125+
#### `export`
126+
127+
> Export a key in a PEM encoded password protected PKCS #8
128+
129+
##### `Go` **NYI**
130+
131+
##### `JavaScript` - ipfs.key.export(name, password, [callback])
132+
133+
Where:
134+
- `name` is the local name for the key
135+
- `password` is the password to protect the key
136+
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
138+
139+
If no `callback` is passed, a promise is returned.
140+
141+
**Example:**
142+
143+
```JavaScript
144+
ipfs.key.export('self', 'password', (err, pem) => console.log(pem))
145+
146+
-----BEGIN ENCRYPTED PRIVATE KEY-----
147+
MIIFDTA/BgkqhkiG9w0BBQ0wMjAaBgkqhkiG9w0BBQwwDQQIpdO40RVyBwACAWQw
148+
...
149+
YA==
150+
-----END ENCRYPTED PRIVATE KEY-----
151+
152+
```
153+
154+
#### `import`
155+
156+
> Import a PEM encoded password protected PKCS #8 key
157+
158+
##### `Go` **NYI**
159+
160+
##### `JavaScript` - ipfs.key.import(name, pem, password, [callback])
161+
162+
Where:
163+
- `name` is a local name for the key
164+
- `pem` is the PEM encoded key
165+
- `password` is the password that protects the PEM key
166+
167+
`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 new key.
168+
169+
If no `callback` is passed, a promise is returned.
170+
171+
**Example:**
172+
173+
```JavaScript
174+
ipfs.key.import('clone', 'password', (err, key) => console.log(key))
175+
176+
{ Name: 'clone',
177+
Id: 'QmQRiays958UM7norGRQUG3tmrLq8pJdmJarwYSk2eLthQ'
178+
}
179+
```

src/key.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = (common) => {
1616
]
1717
const keys = []
1818
let ipfs
19+
let withGo
1920

2021
before(function (done) {
2122
// CI takes longer to instantiate the daemon, so we need to increase the
@@ -27,7 +28,11 @@ module.exports = (common) => {
2728
factory.spawnNode((err, node) => {
2829
expect(err).to.not.exist()
2930
ipfs = node
30-
done()
31+
ipfs.id((err, id) => {
32+
expect(err).to.not.exist()
33+
withGo = id.agentVersion.startsWith('go-ipfs')
34+
done()
35+
})
3136
})
3237
})
3338
})
@@ -139,5 +144,48 @@ module.exports = (common) => {
139144
})
140145
})
141146
})
147+
148+
describe('exchange', () => {
149+
let selfPem
150+
let passwordPem = hat()
151+
152+
it('exports', (done) => {
153+
if (withGo) {
154+
console.log('Not supported by go-ipfs yet')
155+
return done()
156+
}
157+
ipfs.key.export('self', passwordPem, (err, pem) => {
158+
expect(err).to.not.exist()
159+
expect(pem).to.exist()
160+
selfPem = pem
161+
done()
162+
})
163+
})
164+
165+
it('imports', (done) => {
166+
if (withGo) {
167+
console.log('Not supported by go-ipfs yet')
168+
return done()
169+
}
170+
ipfs.key.import('clone', selfPem, passwordPem, (err, key) => {
171+
expect(err).to.not.exist()
172+
expect(key).to.exist()
173+
expect(key).to.have.property('Name', 'clone')
174+
expect(key).to.have.property('Id')
175+
done()
176+
})
177+
})
178+
179+
it('removes', (done) => {
180+
if (withGo) {
181+
console.log('Not supported by go-ipfs yet')
182+
return done()
183+
}
184+
ipfs.key.rm('clone', (err) => {
185+
expect(err).to.not.exist()
186+
done()
187+
})
188+
})
189+
})
142190
})
143191
}

0 commit comments

Comments
 (0)