Skip to content

Commit 89e9903

Browse files
dirkmcvasco-santos
authored andcommitted
chore: convert from callbacks to async (#19)
BREAKING CHANGE: All places in the API that used callbacks are now replaced with async/await
1 parent ccc4178 commit 89e9903

File tree

4 files changed

+262
-395
lines changed

4 files changed

+262
-395
lines changed

README.md

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,32 @@ This module contains all the necessary code for creating, understanding and vali
3838
```js
3939
const ipns = require('ipns')
4040

41-
ipns.create(privateKey, value, sequenceNumber, lifetime, (err, entryData) => {
42-
// your code goes here
43-
})
41+
const entryData = await ipns.create(privateKey, value, sequenceNumber, lifetime)
4442
```
4543

4644
#### Validate record
4745

4846
```js
4947
const ipns = require('ipns')
5048

51-
ipns.validate(publicKey, ipnsEntry, (err) => {
52-
// your code goes here
53-
// if no error, the record is valid
54-
})
49+
await ipns.validate(publicKey, ipnsEntry)
50+
// if no error thrown, the record is valid
5551
```
5652

5753
#### Embed public key to record
5854

5955
```js
6056
const ipns = require('ipns')
6157

62-
ipns.embedPublicKey(publicKey, ipnsEntry, (err, ipnsEntryWithEmbedPublicKey) => {
63-
// your code goes here
64-
})
58+
const ipnsEntryWithEmbedPublicKey = await ipns.embedPublicKey(publicKey, ipnsEntry)
6559
```
6660

6761
#### Extract public key from record
6862

6963
```js
7064
const ipns = require('ipns')
7165

72-
ipns.extractPublicKey(peerId, ipnsEntry, (err, publicKey) => {
73-
// your code goes here
74-
})
66+
const publicKey = ipns.extractPublicKey(peerId, ipnsEntry)
7567
```
7668

7769
#### Datastore key
@@ -93,11 +85,10 @@ Returns a key to be used for storing the ipns entry locally, that is:
9385
```js
9486
const ipns = require('ipns')
9587

96-
ipns.create(privateKey, value, sequenceNumber, lifetime, (err, entryData) => {
97-
// ...
98-
const marshalledData = ipns.marshal(entryData)
99-
// ...
100-
})
88+
const entryData = await ipns.create(privateKey, value, sequenceNumber, lifetime)
89+
// ...
90+
const marshalledData = ipns.marshal(entryData)
91+
// ...
10192
```
10293

10394
Returns the entry data serialized.
@@ -120,19 +111,19 @@ const ipns = require('ipns')
120111
const validator = ipns.validator
121112
```
122113

123-
Contains an object with `validate (marshalledData, key, callback)` and `select (dataA, dataB, [callback])` functions.
114+
Contains an object with `validate (marshalledData, key)` and `select (dataA, dataB)` functions.
124115

125-
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).
126117

127-
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`. If a callback is not provided, the response is returned.
118+
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`.
128119

129120
## API
130121

131122
#### Create record
132123

133124
```js
134125

135-
ipns.create(privateKey, value, sequenceNumber, lifetime, [callback])
126+
ipns.create(privateKey, value, sequenceNumber, lifetime)
136127
```
137128

138129
Create an IPNS record for being stored in a protocol buffer.
@@ -141,9 +132,8 @@ Create an IPNS record for being stored in a protocol buffer.
141132
- `value` (string): ipfs path of the object to be published.
142133
- `sequenceNumber` (Number): number representing the current version of the record.
143134
- `lifetime` (string): lifetime of the record (in milliseconds).
144-
- `callback` (function): operation result.
145135

146-
`callback` must follow `function (err, ipnsEntry) {}` signature, where `err` is an error if the operation was not successful. `ipnsEntry` is an object that contains the entry's properties, such as:
136+
Returns a `Promise` that resolves to an object with the entry's properties eg:
147137

148138
```js
149139
{
@@ -159,16 +149,15 @@ Create an IPNS record for being stored in a protocol buffer.
159149

160150
```js
161151

162-
ipns.validate(publicKey, ipnsEntry, [callback])
152+
ipns.validate(publicKey, ipnsEntry)
163153
```
164154

165155
Validate an IPNS record previously stored in a protocol buffer.
166156

167157
- `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.
168158
- `ipnsEntry` (Object): ipns entry record (obtained using the create function).
169-
- `callback` (function): operation result.
170159

171-
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful. This way, if no error, the validation was successful.
160+
Returns a `Promise`, which may be rejected if the validation was not successful.
172161

173162
#### Datastore key
174163

@@ -203,16 +192,15 @@ Returns the entry data structure after being serialized.
203192
#### Embed public key to record
204193

205194
```js
206-
ipns.embedPublicKey(publicKey, ipnsEntry, [callback])
195+
const recordWithPublicKey = await ipns.embedPublicKey(publicKey, ipnsEntry)
207196
```
208197

209198
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.
210199

211200
- `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.
212201
- `ipnsEntry` (Object): ipns entry record (obtained using the create function).
213-
- `callback` (function): operation result.
214202

215-
`callback` must follow `function (err, resultEntry) {}` signature, where `err` is an error if the operation was not successful. This way, if no error, the operation was successful. If the `resultEntry` is also null, the `peer-id` allows to extract the public key from the `peer-id` and there is no need in extracting it.
203+
Returns a `Promise`. If the promise resolves to null it means the public key can be extracted directly from the `peer-id`.
216204

217205
#### Extract public key from record
218206

@@ -224,9 +212,8 @@ Extract a public key from an IPNS entry.
224212

225213
- `peerId` (`PeerId` [Instance](https://github.com/libp2p/js-peer-id)): peer identifier object.
226214
- `ipnsEntry` (Object): ipns entry record (obtained using the create function).
227-
- `callback` (function): operation result.
228215

229-
`callback` must follow `function (err, publicKey) {}` signature, where `err` is an error if the operation was not successful. This way, if no error, the validation was successful. The public key (`PubKey` [RSA Instance](https://github.com/libp2p/js-libp2p-crypto/blob/master/src/keys/rsa-class.js)): may be used for cryptographic operations.
216+
The returned public key (`PubKey` [RSA Instance](https://github.com/libp2p/js-libp2p-crypto/blob/master/src/keys/rsa-class.js)): may be used for cryptographic operations.
230217

231218
#### Namespace
232219

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
"dependencies": {
3636
"base32-encode": "^1.1.0",
3737
"debug": "^4.1.1",
38-
"interface-datastore": "~0.6.0",
39-
"libp2p-crypto": "~0.16.0",
38+
"err-code": "^1.1.2",
39+
"interface-datastore": "~0.7.0",
40+
"left-pad": "^1.3.0",
41+
"libp2p-crypto": "~0.17.0",
4042
"multihashes": "~0.4.14",
41-
"peer-id": "~0.12.2",
43+
"peer-id": "~0.13.2",
4244
"protons": "^1.0.1",
4345
"timestamp-nano": "^1.0.0"
4446
},

0 commit comments

Comments
 (0)