simple-hypercore-protocol
Version:
Hypercore protocol state machine
169 lines (96 loc) • 3.91 kB
Markdown
Hypercore protocol state machine
```
npm install simple-hypercore-protocol
```
Includes a Noise handshake, and is not backwards compatible with Hypercore <= 7
``` js
const Protocol = require('simple-hypercore-protocol')
const a = new Protocol(true, {
send (data) { // send hook should send data
b.recv(data)
}
})
const b = new Protocol(false, {
onrequest (channel, message) {
console.log('got request message', message, 'on channel', channel)
},
send (data) {
a.recv(data)
}
})
// send a request message on channel 10
a.request(10, {
index: 42
})
```
This is still a work in progress, so that messages supported might change.
See the schema.proto file for the schema for each message.
Create a new protocol state machine.
* `isInitator` is a boolean indicating if you are a client or server
* `handlers` is a series of functions handling incoming messages
Everytime a binary message should be sent to another peer,
`handlers.send(data)` is invoked.
If there is a critical error, `handlers.destroy(err)` is called.
After the initial handshake transport encryption is enabled,
to ensure your stream is private.
To disable transport encryption set `handlers.encrypted = false`.
To disable the NOISE handshake set `handlers.noise = false` (works only when `encrypted` is also set to false).
Call this with incoming data.
Create a remote capability for a key. Use this to verify
if a remote indeed had a key when you get an `open` message.
Create a local capability.
Destroy the protocol state machine.
The local public key used for authentication.
The remotes public key.
The noise handshake hash which uniquely identifies the noise session.
http://noiseprotocol.org/noise.html#channel-binding
Called when you should authenticate a remote public key.
Called when the initial protocol handshake has finished.
Send an open message on a channel.
Note that if you message.key the protocol, will turn that into a capability that is sent instead of the key.
Receiving an open message triggers `handlers.onopen(channel, message)`
Send a options message on a channel.
Receiving a handshake message triggers `handlers.onoptions(channel, message)`
Send a status message on a channel.
Receiving a info message triggers `handlers.onstatus(channel, message)`
Send a have message on a channel.
Receiving a have message triggers `handlers.onhave(channel, message)`
Send an unhave message on a channel.
Receiving an unhave message triggers `handlers.onunhave(channel, message)`
Send a want message on a channel.
Receiving a want message triggers `handlers.onwant(channel, message)`
Send an unwant message on a channel.
Receiving an unwant message triggers `handlers.onunwant(channel, message)`
Send a request message on a channel.
Receiving a request message triggers `handlers.onrequest(channel, message)`
Send a cancel message on a channel.
Receiving a cancel message triggers `handlers.oncancel(channel, message)`
Send a data message on a channel.
Receiving a data message triggers `handlers.ondata(channel, message)`
Static function to generate a Noise key pair, optionally from a seed.
MIT