Skip to content

Commit

Permalink
feat: add records validator
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Sep 6, 2018
1 parent 6c86530 commit 2314664
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 2 deletions.
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 sequece 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 newer record returning 0 if it is the first parameter', (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 newer record returning 1 if it is the second parameter', (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()
})
})
})
})
})

0 comments on commit 2314664

Please sign in to comment.