Skip to content

Commit a3c7cfe

Browse files
committed
chore: convert from callbacks to async
1 parent cef5feb commit a3c7cfe

File tree

3 files changed

+205
-333
lines changed

3 files changed

+205
-333
lines changed

README.md

+22-31
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,36 @@ 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)
42+
// your code goes here
4443
```
4544

4645
#### Validate record
4746

4847
```js
4948
const ipns = require('ipns')
5049

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

5755
#### Embed public key to record
5856

5957
```js
6058
const ipns = require('ipns')
6159

62-
ipns.embedPublicKey(publicKey, ipnsEntry, (err, ipnsEntryWithEmbedPublicKey) => {
63-
// your code goes here
64-
})
60+
const ipnsEntryWithEmbedPublicKey = await ipns.embedPublicKey(publicKey, ipnsEntry)
61+
// your code goes here
6562
```
6663

6764
#### Extract public key from record
6865

6966
```js
7067
const ipns = require('ipns')
7168

72-
ipns.extractPublicKey(peerId, ipnsEntry, (err, publicKey) => {
73-
// your code goes here
74-
})
69+
const publicKey = ipns.extractPublicKey(peerId, ipnsEntry)
70+
// your code goes here
7571
```
7672

7773
#### Datastore key
@@ -93,11 +89,10 @@ Returns a key to be used for storing the ipns entry locally, that is:
9389
```js
9490
const ipns = require('ipns')
9591

96-
ipns.create(privateKey, value, sequenceNumber, lifetime, (err, entryData) => {
97-
// ...
98-
const marshalledData = ipns.marshal(entryData)
99-
// ...
100-
})
92+
const entryData = ipns.create(privateKey, value, sequenceNumber, lifetime
93+
// ...
94+
const marshalledData = ipns.marshal(entryData)
95+
// ...
10196
```
10297
10398
Returns the entry data serialized.
@@ -120,19 +115,19 @@ const ipns = require('ipns')
120115
const validator = ipns.validator
121116
```
122117
123-
Contains an object with `validate (marshalledData, key, callback)` and `select (dataA, dataB, [callback])` functions.
118+
Contains an object with `validate (marshalledData, key)` and `select (dataA, dataB)` functions.
124119
125120
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).
126121
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.
122+
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`.
128123
129124
## API
130125
131126
#### Create record
132127
133128
```js
134129

135-
ipns.create(privateKey, value, sequenceNumber, lifetime, [callback])
130+
ipns.create(privateKey, value, sequenceNumber, lifetime)
136131
```
137132
138133
Create an IPNS record for being stored in a protocol buffer.
@@ -141,9 +136,8 @@ Create an IPNS record for being stored in a protocol buffer.
141136
- `value` (string): ipfs path of the object to be published.
142137
- `sequenceNumber` (Number): number representing the current version of the record.
143138
- `lifetime` (string): lifetime of the record (in milliseconds).
144-
- `callback` (function): operation result.
145139
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:
140+
Returns a `Promise` that resolves to an object with the entry's properties eg:
147141
148142
```js
149143
{
@@ -159,16 +153,15 @@ Create an IPNS record for being stored in a protocol buffer.
159153
160154
```js
161155

162-
ipns.validate(publicKey, ipnsEntry, [callback])
156+
ipns.validate(publicKey, ipnsEntry)
163157
```
164158
165159
Validate an IPNS record previously stored in a protocol buffer.
166160
167161
- `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.
168162
- `ipnsEntry` (Object): ipns entry record (obtained using the create function).
169-
- `callback` (function): operation result.
170163
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.
164+
Throws an error if the validation was not successful.
172165
173166
#### Datastore key
174167
@@ -203,16 +196,15 @@ Returns the entry data structure after being serialized.
203196
#### Embed public key to record
204197
205198
```js
206-
ipns.embedPublicKey(publicKey, ipnsEntry, [callback])
199+
ipns.embedPublicKey(publicKey, ipnsEntry)
207200
```
208201
209202
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.
210203
211204
- `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.
212205
- `ipnsEntry` (Object): ipns entry record (obtained using the create function).
213-
- `callback` (function): operation result.
214206
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.
207+
Returns a `Promise`. If the promise resolves to null it means the public key can be extracted directly from the `peer-id`.
216208
217209
#### Extract public key from record
218210
@@ -224,9 +216,8 @@ Extract a public key from an IPNS entry.
224216
225217
- `peerId` (`PeerId` [Instance](https://github.com./libp2p/js-peer-id)): peer identifier object.
226218
- `ipnsEntry` (Object): ipns entry record (obtained using the create function).
227-
- `callback` (function): operation result.
228219
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.
220+
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.
230221
231222
#### Namespace
232223

0 commit comments

Comments
 (0)