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

feat: add records validator #5

Merged
merged 2 commits into from
Sep 6, 2018
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist/
docs/

# Logs
logs
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ const data = ipns.unmarshal(storedData)

Returns the entry data structure after being serialized.

#### Validator

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

const validator = ipns.validator
```

Contains an object with `validate (marshalledData, peerId, callback)` and `select (dataA, dataB, callback)` 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 `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
Expand Down
38 changes: 36 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,38 @@ const extractPublicKeyFromId = (peerId) => {
return crypto.keys.unmarshalPublicKey(decodedId.digest)
}

const marshal = ipnsEntryProto.encode

const unmarshal = ipnsEntryProto.decode

const validator = {
validate: (marshalledData, peerId, callback) => {
const receivedEntry = unmarshal(marshalledData)

// extract public key
extractPublicKey(peerId, receivedEntry, (err, pubKey) => {
if (err) {
return callback(err)
}

// Record validation
validate(pubKey, receivedEntry, (err) => {
if (err) {
return callback(err)
}

callback(null, true)
})
})
},
select: (dataA, dataB, callback) => {
const entryA = unmarshal(dataA)
const entryB = unmarshal(dataB)

callback(null, entryA.sequence > entryB.sequence ? 0 : 1)
}
}

module.exports = {
// create ipns entry record
create,
Expand All @@ -271,7 +303,9 @@ module.exports = {
// get keys for routing
getIdKeys,
// marshal
marshal: ipnsEntryProto.encode,
marshal,
// unmarshal
unmarshal: ipnsEntryProto.decode
unmarshal,
// validator
validator
}
87 changes: 87 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,91 @@ describe('ipns', function () {
})
})
})

it('should use validator.validate to validate a record', (done) => {
const sequence = 0
const validity = 1000000

ipns.create(rsa, cid, sequence, validity, (err, entry) => {
expect(err).to.not.exist()

ipns.embedPublicKey(rsa.public, entry, (err, entry) => {
expect(err).to.not.exist()

const marshalledData = ipns.marshal(entry)

ipns.validator.validate(marshalledData, ipfsId, (err, valid) => {
expect(err).to.not.exist()
expect(valid).to.equal(true)
done()
})
})
})
})

it('should use validator.validate to verify that a record is not valid', (done) => {
const sequence = 0
const validity = 1000000

ipns.create(rsa, cid, sequence, validity, (err, entry) => {
expect(err).to.not.exist()

ipns.embedPublicKey(rsa.public, entry, (err, entry) => {
expect(err).to.not.exist()

// corrupt the record by changing the value to random bytes
entry.value = crypto.randomBytes(46).toString()
const marshalledData = ipns.marshal(entry)

ipns.validator.validate(marshalledData, ipfsId, (err) => {
expect(err).to.exist() // failed validation
done()
})
})
})
})

it('should use validator.select to select the first record because it is newer', (done) => {
const sequence = 0
const validity = 1000000

ipns.create(rsa, cid, sequence, validity, (err, entry) => {
expect(err).to.not.exist()

ipns.create(rsa, cid, (sequence + 1), validity, (err, newEntry) => {
expect(err).to.not.exist()

const marshalledData = ipns.marshal(entry)
const marshalledNewData = ipns.marshal(newEntry)

ipns.validator.select(marshalledNewData, marshalledData, (err, valid) => {
expect(err).to.not.exist()
expect(valid).to.equal(0) // new data is the selected one
done()
})
})
})
})

it('should use validator.select to select the second record because it is newer', (done) => {
const sequence = 0
const validity = 1000000

ipns.create(rsa, cid, sequence + 1, validity, (err, entry) => {
expect(err).to.not.exist()

ipns.create(rsa, cid, (sequence), validity, (err, newEntry) => {
expect(err).to.not.exist()

const marshalledData = ipns.marshal(entry)
const marshalledNewData = ipns.marshal(newEntry)

ipns.validator.select(marshalledNewData, marshalledData, (err, valid) => {
expect(err).to.not.exist()
expect(valid).to.equal(1) // old data is the selected one
done()
})
})
})
})
})