Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert from callbacks to async #19

Merged
merged 6 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 19 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,32 @@ This module contains all the necessary code for creating, understanding and vali
```js
const ipns = require('ipns')

ipns.create(privateKey, value, sequenceNumber, lifetime, (err, entryData) => {
// your code goes here
})
const entryData = await ipns.create(privateKey, value, sequenceNumber, lifetime)
```

#### Validate record

```js
const ipns = require('ipns')

ipns.validate(publicKey, ipnsEntry, (err) => {
// your code goes here
// if no error, the record is valid
})
await ipns.validate(publicKey, ipnsEntry)
// if no error thrown, the record is valid
```

#### Embed public key to record

```js
const ipns = require('ipns')

ipns.embedPublicKey(publicKey, ipnsEntry, (err, ipnsEntryWithEmbedPublicKey) => {
// your code goes here
})
const ipnsEntryWithEmbedPublicKey = await ipns.embedPublicKey(publicKey, ipnsEntry)
```

#### Extract public key from record

```js
const ipns = require('ipns')

ipns.extractPublicKey(peerId, ipnsEntry, (err, publicKey) => {
// your code goes here
})
const publicKey = ipns.extractPublicKey(peerId, ipnsEntry)
```

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

ipns.create(privateKey, value, sequenceNumber, lifetime, (err, entryData) => {
// ...
const marshalledData = ipns.marshal(entryData)
// ...
})
const entryData = await ipns.create(privateKey, value, sequenceNumber, lifetime)
// ...
const marshalledData = ipns.marshal(entryData)
// ...
```

Returns the entry data serialized.
Expand All @@ -120,19 +111,19 @@ const ipns = require('ipns')
const validator = ipns.validator
```

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

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

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

## API

#### Create record

```js

ipns.create(privateKey, value, sequenceNumber, lifetime, [callback])
ipns.create(privateKey, value, sequenceNumber, lifetime)
```

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

`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:
Returns a `Promise` that resolves to an object with the entry's properties eg:

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

```js

ipns.validate(publicKey, ipnsEntry, [callback])
ipns.validate(publicKey, ipnsEntry)
```

Validate an IPNS record previously stored in a protocol buffer.

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

`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.
Returns a `Promise`, which may be rejected if the validation was not successful.

#### Datastore key

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

```js
ipns.embedPublicKey(publicKey, ipnsEntry, [callback])
const recordWithPublicKey = await ipns.embedPublicKey(publicKey, ipnsEntry)
```

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.

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

`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.
Returns a `Promise`. If the promise resolves to null it means the public key can be extracted directly from the `peer-id`.

#### Extract public key from record

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

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

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

#### Namespace

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
"dependencies": {
"base32-encode": "^1.1.0",
"debug": "^4.1.1",
"interface-datastore": "~0.6.0",
"libp2p-crypto": "~0.16.0",
"err-code": "^1.1.2",
"interface-datastore": "~0.7.0",
"left-pad": "^1.3.0",
"libp2p-crypto": "~0.17.0",
"multihashes": "~0.4.14",
"peer-id": "~0.12.2",
"peer-id": "~0.13.2",
"protons": "^1.0.1",
"timestamp-nano": "^1.0.0"
},
Expand Down
Loading