Skip to content

Commit c85a1b5

Browse files
committed
chore: doc fixes, refactor
1 parent dece009 commit c85a1b5

File tree

3 files changed

+59
-53
lines changed

3 files changed

+59
-53
lines changed

README.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ This module contains all the necessary code for creating, understanding and vali
3939
const ipns = require('ipns')
4040

4141
const entryData = await ipns.create(privateKey, value, sequenceNumber, lifetime)
42-
// your code goes here
4342
```
4443

4544
#### Validate record
@@ -48,7 +47,6 @@ const entryData = await ipns.create(privateKey, value, sequenceNumber, lifetime)
4847
const ipns = require('ipns')
4948

5049
await ipns.validate(publicKey, ipnsEntry)
51-
// your code goes here
5250
// if no error thrown, the record is valid
5351
```
5452

@@ -58,7 +56,6 @@ await ipns.validate(publicKey, ipnsEntry)
5856
const ipns = require('ipns')
5957

6058
const ipnsEntryWithEmbedPublicKey = await ipns.embedPublicKey(publicKey, ipnsEntry)
61-
// your code goes here
6259
```
6360

6461
#### Extract public key from record
@@ -67,7 +64,6 @@ const ipnsEntryWithEmbedPublicKey = await ipns.embedPublicKey(publicKey, ipnsEnt
6764
const ipns = require('ipns')
6865

6966
const publicKey = ipns.extractPublicKey(peerId, ipnsEntry)
70-
// your code goes here
7167
```
7268

7369
#### Datastore key
@@ -89,7 +85,7 @@ Returns a key to be used for storing the ipns entry locally, that is:
8985
```js
9086
const ipns = require('ipns')
9187

92-
const entryData = ipns.create(privateKey, value, sequenceNumber, lifetime)
88+
const entryData = await ipns.create(privateKey, value, sequenceNumber, lifetime)
9389
// ...
9490
const marshalledData = ipns.marshal(entryData)
9591
// ...
@@ -117,7 +113,7 @@ const validator = ipns.validator
117113

118114
Contains an object with `validate (marshalledData, key)` and `select (dataA, dataB)` functions.
119115

120-
The `validate` function aims to verify if an IPNS record is valid. First the record is unmarshalled, then the public key is obtained and finally the record is validated (signature and validity are verified).
116+
The `validate` async function aims to verify if an IPNS record is valid. First the record is unmarshalled, then the public key is obtained and finally the record is validated (signature and validity are verified).
121117

122118
The `select` function is responsible for deciding which ipns record is the best (newer) between two records. Both records are unmarshalled and their sequence numbers are compared. If the first record provided is the newer, the operation result will be `0`, otherwise the operation result will be `1`.
123119

@@ -161,7 +157,7 @@ Validate an IPNS record previously stored in a protocol buffer.
161157
- `publicKey` (`PubKey` [RSA Instance](https://github.com/libp2p/js-libp2p-crypto/blob/master/src/keys/rsa-class.js)): key to be used for cryptographic operations.
162158
- `ipnsEntry` (Object): ipns entry record (obtained using the create function).
163159

164-
Throws an error if the validation was not successful.
160+
Returns a `Promise`, which may be rejected if the validation was not successful.
165161

166162
#### Datastore key
167163

@@ -196,7 +192,7 @@ Returns the entry data structure after being serialized.
196192
#### Embed public key to record
197193

198194
```js
199-
ipns.embedPublicKey(publicKey, ipnsEntry)
195+
const recordWithPublicKey = await ipns.embedPublicKey(publicKey, ipnsEntry)
200196
```
201197

202198
Embed a public key in an IPNS entry. If it is possible to extract the public key from the `peer-id`, there is no need to embed.

src/index.js

+34-23
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ const ID_MULTIHASH_CODE = multihash.names.id
2020

2121
const namespace = '/ipns/'
2222

23+
/**
24+
* IPNS entry
25+
* @typedef {Object} IpnsEntry
26+
* @property {string} value - value to be stored in the record
27+
* @property {Buffer} signature - signature of the record
28+
* @property {number} validityType - Type of validation being used
29+
* @property {string} validity - expiration datetime for the record in RFC3339 format
30+
* @property {number} sequence - number representing the version of the record
31+
*/
32+
2333
/**
2434
* Creates a new ipns entry and signs it with the given private key.
2535
* The ipns entry validity should follow the [RFC3339]{@link https://www.ietf.org/rfc/rfc3339.txt} with nanoseconds precision.
@@ -29,7 +39,7 @@ const namespace = '/ipns/'
2939
* @param {string} value value to be stored in the record.
3040
* @param {number} seq number representing the current version of the record.
3141
* @param {number|string} lifetime lifetime of the record (in milliseconds).
32-
* @returns {Object} entry
42+
* @returns {Promise<IpnsEntry>} entry
3343
*/
3444
const create = (privateKey, value, seq, lifetime) => {
3545
// Validity in ISOString with nanoseconds precision and validity type EOL
@@ -45,38 +55,34 @@ const create = (privateKey, value, seq, lifetime) => {
4555
* @param {string} value value to be stored in the record.
4656
* @param {number} seq number representing the current version of the record.
4757
* @param {string} expiration expiration datetime for record in the [RFC3339]{@link https://www.ietf.org/rfc/rfc3339.txt} with nanoseconds precision.
48-
* @returns {Object} entry
58+
* @returns {Promise<IpnsEntry>} entry
4959
*/
5060
const createWithExpiration = (privateKey, value, seq, expiration) => {
5161
const validityType = ipnsEntryProto.ValidityType.EOL
5262
return _create(privateKey, value, seq, expiration, validityType)
5363
}
5464

5565
const _create = async (privateKey, value, seq, isoValidity, validityType) => {
56-
try {
57-
const signature = await sign(privateKey, value, validityType, isoValidity)
58-
59-
const entry = {
60-
value: value,
61-
signature: signature,
62-
validityType: validityType,
63-
validity: isoValidity,
64-
sequence: seq
65-
}
66-
67-
log(`ipns entry for ${value} created`)
68-
return entry
69-
} catch (error) {
70-
log.error('record signature creation failed')
71-
throw errCode('record signature verification failed', ERRORS.ERR_SIGNATURE_CREATION)
66+
const signature = await sign(privateKey, value, validityType, isoValidity)
67+
68+
const entry = {
69+
value: value,
70+
signature: signature,
71+
validityType: validityType,
72+
validity: isoValidity,
73+
sequence: seq
7274
}
75+
76+
log(`ipns entry for ${value} created`)
77+
return entry
7378
}
7479

7580
/**
7681
* Validates the given ipns entry against the given public key.
7782
*
7883
* @param {Object} publicKey public key for validating the record.
79-
* @param {Object} entry ipns entry record.
84+
* @param {IpnsEntry} entry ipns entry record.
85+
* @returns {Promise}
8086
*/
8187
const validate = async (publicKey, entry) => {
8288
const { value, validityType, validity } = entry
@@ -128,7 +134,7 @@ const validate = async (publicKey, entry) => {
128134
*
129135
* @param {Object} publicKey public key to embed.
130136
* @param {Object} entry ipns entry record.
131-
* @return {Object} entry with public key embedded
137+
* @return {IpnsEntry} entry with public key embedded
132138
*/
133139
const embedPublicKey = async (publicKey, entry) => {
134140
if (!publicKey || !publicKey.bytes || !entry) {
@@ -172,7 +178,7 @@ const embedPublicKey = async (publicKey, entry) => {
172178
* Extracts a public key matching `pid` from the ipns record.
173179
*
174180
* @param {Object} peerId peer identifier object.
175-
* @param {Object} entry ipns entry record.
181+
* @param {IpnsEntry} entry ipns entry record.
176182
* @returns {Object} the public key
177183
*/
178184
const extractPublicKey = (peerId, entry) => {
@@ -229,8 +235,13 @@ const getIdKeys = (pid) => {
229235

230236
// Sign ipns record data
231237
const sign = (privateKey, value, validityType, validity) => {
232-
const dataForSignature = ipnsEntryDataForSig(value, validityType, validity)
233-
return privateKey.sign(dataForSignature)
238+
try {
239+
const dataForSignature = ipnsEntryDataForSig(value, validityType, validity)
240+
return privateKey.sign(dataForSignature)
241+
} catch (error) {
242+
log.error('record signature creation failed')
243+
throw errCode('record signature creation failed: ' + error.message, ERRORS.ERR_SIGNATURE_CREATION)
244+
}
234245
}
235246

236247
// Utility for getting the validity type code name of a validity

test/index.spec.js

+21-22
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,28 @@ describe('ipns', function () {
3030
let ipfsId = null
3131
let rsa = null
3232

33-
const spawnDaemon = (cb) => {
34-
df.spawn({ initOptions: { bits: 512 } }, (err, _ipfsd) => {
35-
expect(err).to.not.exist()
36-
ipfsd = _ipfsd
37-
ipfs = ipfsd.api
38-
39-
ipfs.id((err, id) => {
40-
if (err) {
41-
return cb(err)
42-
}
43-
44-
ipfsId = id
45-
cb()
33+
const spawnDaemon = () => {
34+
return new Promise((resolve, reject) => {
35+
df.spawn({ initOptions: { bits: 512 } }, (err, _ipfsd) => {
36+
expect(err).to.not.exist()
37+
ipfsd = _ipfsd
38+
ipfs = ipfsd.api
39+
40+
ipfs.id((err, id) => {
41+
if (err) {
42+
return reject(err)
43+
}
44+
45+
ipfsId = id
46+
resolve()
47+
})
4648
})
4749
})
4850
}
4951

5052
before(async () => {
5153
rsa = await crypto.keys.generateKeyPair('RSA', 2048)
52-
return new Promise((resolve, reject) => spawnDaemon(err => err ? reject(err) : resolve()))
54+
return spawnDaemon()
5355
})
5456

5557
after(function (done) {
@@ -185,17 +187,14 @@ describe('ipns', function () {
185187
// https://github.com/libp2p/go-libp2p-peer/blob/7f219a1e70011a258c5d3e502aef6896c60d03ce/peer.go#L80
186188
// IDFromEd25519PublicKey is not currently implement on js-libp2p-peer
187189
// https://github.com/libp2p/go-libp2p-peer/pull/30
188-
it.skip('should be able to extract a public key directly from the peer', (done) => {
190+
it.skip('should be able to extract a public key directly from the peer', async () => {
189191
const sequence = 0
190192
const validity = 1000000
191193

192-
crypto.keys.generateKeyPair('ed25519', 2048, async (err, ed25519) => {
193-
expect(err).to.not.exist()
194-
195-
const entry = await ipns.create(ed25519, cid, sequence, validity)
196-
const entryWithKey = ipns.embedPublicKey(ed25519.public, entry)
197-
expect(entryWithKey).to.not.exist() // Should be null
198-
})
194+
const ed25519 = await crypto.keys.generateKeyPair('ed25519', 2048)
195+
const entry = await ipns.create(ed25519, cid, sequence, validity)
196+
const entryWithKey = ipns.embedPublicKey(ed25519.public, entry)
197+
expect(entryWithKey).to.not.exist() // Should be null
199198
})
200199

201200
it('should be able to export a previously embed public key from an ipns record', async () => {

0 commit comments

Comments
 (0)