116 lines
2.7 KiB
JavaScript
Raw Normal View History

'use strict'
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
2020-07-15 11:40:57 +02:00
const arrayEquals = require('libp2p-utils/src/array-equals')
const Protobuf = require('./peer-record.proto')
const {
ENVELOPE_DOMAIN_PEER_RECORD,
ENVELOPE_PAYLOAD_TYPE_PEER_RECORD
} = require('./consts')
/**
2020-12-10 14:48:14 +01:00
* @typedef {import('peer-id')} PeerId
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('libp2p-interfaces/src/record/types').Record} Record
*/
2020-12-10 14:48:14 +01:00
/**
* @implements {Record}
*/
class PeerRecord {
/**
2020-12-10 14:48:14 +01:00
* The PeerRecord is used for distributing peer routing records across the network.
* It contains the peer's reachable listen addresses.
*
* @class
2020-12-10 14:48:14 +01:00
* @param {Object} params
* @param {PeerId} params.peerId
2020-12-10 14:48:14 +01:00
* @param {Multiaddr[]} params.multiaddrs - addresses of the associated peer.
* @param {number} [params.seqNumber] - monotonically-increasing sequence counter that's used to order PeerRecords in time.
*/
constructor ({ peerId, multiaddrs = [], seqNumber = Date.now() }) {
2020-12-10 14:48:14 +01:00
this.domain = ENVELOPE_DOMAIN_PEER_RECORD
this.codec = ENVELOPE_PAYLOAD_TYPE_PEER_RECORD
this.peerId = peerId
this.multiaddrs = multiaddrs
this.seqNumber = seqNumber
// Cache
this._marshal = undefined
}
/**
* Marshal a record to be used in an envelope.
*
* @returns {Uint8Array}
*/
marshal () {
if (this._marshal) {
return this._marshal
}
2020-12-10 14:48:14 +01:00
this._marshal = Protobuf.PeerRecord.encode({
peer_id: this.peerId.toBytes(),
seq: this.seqNumber,
addresses: this.multiaddrs.map((m) => ({
multiaddr: m.bytes
}))
})
return this._marshal
}
/**
2020-07-15 11:40:57 +02:00
* Returns true if `this` record equals the `other`.
*
2020-12-10 14:48:14 +01:00
* @param {unknown} other
* @returns {boolean}
*/
2020-07-15 11:40:57 +02:00
equals (other) {
2020-12-10 14:48:14 +01:00
if (!(other instanceof PeerRecord)) {
return false
}
// Validate PeerId
if (!this.peerId.equals(other.peerId)) {
return false
}
// Validate seqNumber
if (this.seqNumber !== other.seqNumber) {
return false
}
// Validate multiaddrs
2020-07-15 11:40:57 +02:00
if (!arrayEquals(this.multiaddrs, other.multiaddrs)) {
return false
}
return true
}
}
/**
* Unmarshal Peer Record Protobuf.
*
* @param {Uint8Array} buf - marshaled peer record.
* @returns {PeerRecord}
*/
2020-06-24 15:10:08 +02:00
PeerRecord.createFromProtobuf = (buf) => {
// Decode
2020-12-10 14:48:14 +01:00
const peerRecord = Protobuf.PeerRecord.decode(buf)
const peerId = PeerId.createFromBytes(peerRecord.peer_id)
const multiaddrs = (peerRecord.addresses || []).map((a) => multiaddr(a.multiaddr))
const seqNumber = peerRecord.seq
return new PeerRecord({ peerId, multiaddrs, seqNumber })
}
2020-06-24 15:10:08 +02:00
PeerRecord.DOMAIN = ENVELOPE_DOMAIN_PEER_RECORD
2020-06-24 15:10:08 +02:00
module.exports = PeerRecord