mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-07-08 18:11:47 +00:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
a60ae091e5 | |||
4902542e78 | |||
4d5f56d06f | |||
13aa6cbfa0 | |||
a8ba13da4b | |||
75f6777d89 | |||
71b813ad3b | |||
46589ce3d0 | |||
f2a18818f2 | |||
1cc943e1b2 |
32
CHANGELOG.md
32
CHANGELOG.md
@ -1,3 +1,35 @@
|
|||||||
|
<a name="0.4.1"></a>
|
||||||
|
## [0.4.1](https://github.com/libp2p/js-interfaces/compare/v0.4.0...v0.4.1) (2020-08-11)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<a name="0.4.0"></a>
|
||||||
|
# [0.4.0](https://github.com/libp2p/js-interfaces/compare/v0.3.2...v0.4.0) (2020-08-10)
|
||||||
|
|
||||||
|
|
||||||
|
### Chores
|
||||||
|
|
||||||
|
* update deps ([#57](https://github.com/libp2p/js-interfaces/issues/57)) ([75f6777](https://github.com/libp2p/js-interfaces/commit/75f6777))
|
||||||
|
|
||||||
|
|
||||||
|
### BREAKING CHANGES
|
||||||
|
|
||||||
|
* - The peer id dep of this module has replaced node Buffers with Uint8Arrays
|
||||||
|
|
||||||
|
* chore: update gh deps
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<a name="0.3.2"></a>
|
||||||
|
## [0.3.2](https://github.com/libp2p/js-interfaces/compare/v0.3.1...v0.3.2) (2020-07-15)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* record interface ([#52](https://github.com/libp2p/js-interfaces/issues/52)) ([1cc943e](https://github.com/libp2p/js-interfaces/commit/1cc943e))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.3.1"></a>
|
<a name="0.3.1"></a>
|
||||||
## [0.3.1](https://github.com/libp2p/js-interfaces/compare/v0.2.8...v0.3.1) (2020-07-03)
|
## [0.3.1](https://github.com/libp2p/js-interfaces/compare/v0.2.8...v0.3.1) (2020-07-03)
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
- [Crypto](./src/crypto)
|
- [Crypto](./src/crypto)
|
||||||
- [Peer Discovery](./src/peer-discovery)
|
- [Peer Discovery](./src/peer-discovery)
|
||||||
- [Peer Routing](./src/peer-routing)
|
- [Peer Routing](./src/peer-routing)
|
||||||
|
- [Record](./src/record)
|
||||||
- [Stream Muxer](./src/stream-muxer)
|
- [Stream Muxer](./src/stream-muxer)
|
||||||
- [Topology](./src/topology)
|
- [Topology](./src/topology)
|
||||||
- [Transport](./src/transport)
|
- [Transport](./src/transport)
|
||||||
|
15
package.json
15
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-interfaces",
|
"name": "libp2p-interfaces",
|
||||||
"version": "0.3.1",
|
"version": "0.4.1",
|
||||||
"description": "Interfaces for JS Libp2p",
|
"description": "Interfaces for JS Libp2p",
|
||||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
@ -45,19 +45,18 @@
|
|||||||
"err-code": "^2.0.0",
|
"err-code": "^2.0.0",
|
||||||
"it-goodbye": "^2.0.1",
|
"it-goodbye": "^2.0.1",
|
||||||
"it-pair": "^1.0.0",
|
"it-pair": "^1.0.0",
|
||||||
"it-pipe": "^1.0.1",
|
"it-pipe": "^1.1.0",
|
||||||
"libp2p-tcp": "^0.14.1",
|
"libp2p-tcp": "^0.15.0",
|
||||||
"multiaddr": "^7.4.3",
|
"multiaddr": "^8.0.0",
|
||||||
"p-defer": "^3.0.0",
|
"p-defer": "^3.0.0",
|
||||||
"p-limit": "^2.3.0",
|
"p-limit": "^2.3.0",
|
||||||
"p-wait-for": "^3.1.0",
|
"p-wait-for": "^3.1.0",
|
||||||
"peer-id": "^0.13.11",
|
"peer-id": "^0.14.0",
|
||||||
"peer-info": "^0.17.0",
|
|
||||||
"sinon": "^9.0.2",
|
"sinon": "^9.0.2",
|
||||||
"streaming-iterables": "^4.1.0"
|
"streaming-iterables": "^5.0.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^22.0.0",
|
"aegir": "^25.0.0",
|
||||||
"it-handshake": "^1.0.1"
|
"it-handshake": "^1.0.1"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
|
75
src/record/README.md
Normal file
75
src/record/README.md
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
interface-record
|
||||||
|
==================
|
||||||
|
|
||||||
|
A libp2p node needs to store data in a public location (e.g. a DHT), or rely on potentially untrustworthy intermediaries to relay information. Libp2p provides an all-purpose data container called **envelope**, which includes a signature of the data, so that it its authenticity can be verified.
|
||||||
|
|
||||||
|
The record represents the data that will be stored inside the **envelope** when distributing records across the network. The `interface-record` aims to guarantee that any type of record created is compliant with the libp2p **envelope**.
|
||||||
|
|
||||||
|
Taking into account that a record might be used in different contexts, an **envelope** signature made for a specific purpose **must not** be considered valid for a different purpose. Accordingly, each record has a short and descriptive string representing the record use case, known as **domain**. The data to be signed will be prepended with the domain string, in order to create a domain signature.
|
||||||
|
|
||||||
|
A record can also contain a Buffer codec (ideally registered as a [multicodec](https://github.com/multiformats/multicodec)). This codec will prefix the record data in the **envelope** , so that it can be deserialized deterministically.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```js
|
||||||
|
const tests = require('libp2p-interfaces/src/record/tests')
|
||||||
|
describe('your record', () => {
|
||||||
|
tests({
|
||||||
|
async setup () {
|
||||||
|
return YourRecord
|
||||||
|
},
|
||||||
|
async teardown () {
|
||||||
|
// cleanup resources created by setup()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## Create Record
|
||||||
|
|
||||||
|
```js
|
||||||
|
const multicodec = require('multicodec')
|
||||||
|
const Record = require('libp2p-interfaces/src/record')
|
||||||
|
// const Protobuf = require('./record.proto')
|
||||||
|
|
||||||
|
const ENVELOPE_DOMAIN_PEER_RECORD = 'libp2p-peer-record'
|
||||||
|
const ENVELOPE_PAYLOAD_TYPE_PEER_RECORD = Buffer.from('0301', 'hex')
|
||||||
|
|
||||||
|
class PeerRecord extends Record {
|
||||||
|
constructor (peerId, multiaddrs, seqNumber) {
|
||||||
|
super (ENVELOPE_DOMAIN_PEER_RECORD, ENVELOPE_PAYLOAD_TYPE_PEER_RECORD)
|
||||||
|
}
|
||||||
|
|
||||||
|
marshal () {
|
||||||
|
// Implement and return using Protobuf
|
||||||
|
}
|
||||||
|
|
||||||
|
equals (other) {
|
||||||
|
// Verify
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
### marshal
|
||||||
|
|
||||||
|
- `record.marshal()`
|
||||||
|
|
||||||
|
Marshal a record to be used in a libp2p envelope.
|
||||||
|
|
||||||
|
**Returns**
|
||||||
|
|
||||||
|
It returns a `Protobuf` containing the record data.
|
||||||
|
|
||||||
|
### equals
|
||||||
|
|
||||||
|
- `record.equals(other)`
|
||||||
|
|
||||||
|
Verifies if the other Record is identical to this one.
|
||||||
|
|
||||||
|
**Parameters**
|
||||||
|
- other is a `Record` to compare with the current instance.
|
||||||
|
|
||||||
|
**Returns**
|
||||||
|
- `boolean`
|
35
src/record/index.js
Normal file
35
src/record/index.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const errcode = require('err-code')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Record is the base implementation of a record that can be used as the payload of a libp2p envelope.
|
||||||
|
*/
|
||||||
|
class Record {
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @param {String} domain signature domain
|
||||||
|
* @param {Buffer} codec identifier of the type of record
|
||||||
|
*/
|
||||||
|
constructor (domain, codec) {
|
||||||
|
this.domain = domain
|
||||||
|
this.codec = codec
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marshal a record to be used in an envelope.
|
||||||
|
*/
|
||||||
|
marshal () {
|
||||||
|
throw errcode(new Error('marshal must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies if the other provided Record is identical to this one.
|
||||||
|
* @param {Record} other
|
||||||
|
*/
|
||||||
|
equals (other) {
|
||||||
|
throw errcode(new Error('equals must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Record
|
35
src/record/tests/index.js
Normal file
35
src/record/tests/index.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const chai = require('chai')
|
||||||
|
const expect = chai.expect
|
||||||
|
chai.use(require('dirty-chai'))
|
||||||
|
|
||||||
|
module.exports = (test) => {
|
||||||
|
describe('record', () => {
|
||||||
|
let record
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
record = await test.setup()
|
||||||
|
if (!record) throw new Error('missing record')
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => test.teardown())
|
||||||
|
|
||||||
|
it('has domain and codec', () => {
|
||||||
|
expect(record.domain).to.exist()
|
||||||
|
expect(record.codec).to.exist()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('is able to marshal', () => {
|
||||||
|
const rawData = record.marshal()
|
||||||
|
expect(Buffer.isBuffer(rawData)).to.eql(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('is able to compare two records', () => {
|
||||||
|
const equals = record.equals(record)
|
||||||
|
expect(equals).to.eql(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
Reference in New Issue
Block a user