chore: apply suggestions from code review

This commit is contained in:
Vasco Santos
2020-07-17 14:00:59 +02:00
committed by Jacob Heun
parent 15613ccf19
commit f574e82a5d
15 changed files with 75 additions and 68 deletions

View File

@ -88,7 +88,7 @@ class AddressBook extends Book {
}
// Verify peerId
if (peerRecord.peerId.toB58String() !== envelope.peerId.toB58String()) {
if (!peerRecord.peerId.equals(envelope.peerId)) {
log('signing key does not match PeerId in the PeerRecord')
return false
}
@ -220,10 +220,10 @@ class AddressBook extends Book {
const id = peerId.toB58String()
const entry = this.data.get(id) || {}
const rec = entry.addresses
const rec = entry.addresses || []
// Add recorded uniquely to the new array (Union)
rec && rec.forEach((addr) => {
rec.forEach((addr) => {
if (!addresses.find(r => r.multiaddr.equals(addr.multiaddr))) {
addresses.push(addr)
}
@ -244,7 +244,7 @@ class AddressBook extends Book {
log(`added provided multiaddrs for ${id}`)
// Notify the existance of a new peer
if (!rec) {
if (!entry.addresses) {
this._ps.emit('peer', peerId)
}

View File

@ -38,7 +38,7 @@ class PeerStore extends EventEmitter {
/**
* @constructor
*/
constructor ({ peerId } = {}) {
constructor ({ peerId }) {
super()
this._peerId = peerId

View File

@ -189,13 +189,15 @@ class PersistentPeerStore extends PeerStore {
const encodedData = Addresses.encode({
addrs: entry.addresses.map((address) => ({
multiaddr: address.multiaddr.buffer
multiaddr: address.multiaddr.buffer,
isCertified: address.isCertified
})),
certified_record: entry.record ? {
seq: entry.record.seqNumber,
raw: entry.record.raw
} : undefined
})
batch.put(key, encodedData)
} catch (err) {
log.error(err)
@ -302,7 +304,8 @@ class PersistentPeerStore extends PeerStore {
peerId,
{
addresses: decoded.addrs.map((address) => ({
multiaddr: multiaddr(address.multiaddr)
multiaddr: multiaddr(address.multiaddr),
isCertified: Boolean(address.isCertified)
})),
record: decoded.certified_record ? {
raw: decoded.certified_record.raw,

View File

@ -7,6 +7,9 @@ message Addresses {
// Address represents a single multiaddr.
message Address {
required bytes multiaddr = 1;
// Flag to indicate if the address comes from a certified source.
optional bool isCertified = 2;
}
// CertifiedRecord contains a serialized signed PeerRecord used to

View File

@ -112,7 +112,12 @@ const formatSignaturePayload = (domain, payloadType, payload) => {
])
}
const unmarshalEnvelope = async (data) => {
/**
* Unmarshal a serialized Envelope protobuf message.
* @param {Buffer} data
* @return {Promise<Envelope>}
*/
Envelope.createFromProtobuf = async (data) => {
const envelopeData = Protobuf.decode(data)
const peerId = await PeerId.createFromPubKey(envelopeData.public_key)
@ -124,13 +129,6 @@ const unmarshalEnvelope = async (data) => {
})
}
/**
* Unmarshal a serialized Envelope protobuf message.
* @param {Buffer} data
* @return {Promise<Envelope>}
*/
Envelope.createFromProtobuf = unmarshalEnvelope
/**
* Seal marshals the given Record, places the marshaled bytes inside an Envelope
* and signs it with the given peerId's private key.
@ -163,7 +161,7 @@ Envelope.seal = async (record, peerId) => {
* @return {Envelope}
*/
Envelope.openAndCertify = async (data, domain) => {
const envelope = await unmarshalEnvelope(data)
const envelope = await Envelope.createFromProtobuf(data)
const valid = await envelope.validate(domain)
if (!valid) {