Compare commits

..

No commits in common. "eb57f87eb409f73a8808ee641db8b16fc33d4602" and "78f3edd71d514f9b13bce413b6925d0c7123c6cf" have entirely different histories.

4 changed files with 16 additions and 42 deletions

View File

@ -1 +1 @@
{"version":3,"file":"DHTDataBase.d.ts","sourceRoot":"","sources":["../src/DHTDataBase.ts"],"names":[],"mappings":";;AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAiBlC,MAAM,CAAC,OAAO,OAAO,WAAY,SAAQ,YAAY;IACnD,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC;IACrB,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,YAAY,CAAU;IAC9B,OAAO,CAAC,KAAK,CAAM;IACnB,OAAO,CAAC,WAAW,CAAW;gBAElB,EAAE,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,GAAE;QAAE,KAAK,CAAC,EAAE,GAAG,CAAA;KAAO;IAavD,OAAO,CAAC,KAAK,CAAK;IAElB,IAAI,IAAI,IAAI,EAAE,CAEb;IAED,IAAI,IAAI,CAAC,KAAK,EAAE,EAAE,EAcjB;IAED,OAAO,CAAC,OAAO,CAAW;IAE1B,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAED,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,MAAM;IAIjC,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM;IAI9B,YAAY,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,EAAE,MAAM;IAI/C,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM;IAI/B,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM;IAIpC,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM;IAIpC,SAAS,CAAC,SAAS,CAAC,EAAE,EAAE,MAAM;IAqC9B,SAAS,CAAC,YAAY,CAAC,EAAE,EAAE,MAAM;IAiBjC,SAAS,CAAC,cAAc,CAAC,OAAO,EAA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AE,EAAE,MAAM,GAAG,MAAM;IAIpC,SAAS,CAAC,SAAS,CAAC,EAAE,EAAE,MAAM;IAqC9B,SAAS,CAAC,YAAY,CAAC,EAAE,EAAE,MAAM;IAiBjC,SAAS,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;IAqDpD,OAAO,CAAC,cAAc;IAmBtB,OAAO,CAAC,YAAY;IAIpB,OAAO,CAAC,QAAQ;IAahB,OAAO,CAAC,WAAW;IAOnB,OAAO,CAAC,kBAAkB;IAY1B,OAAO,CAAC,qBAAqB;IAa7B,OAAO,CAAC,cAAc;IAuCtB,OAAO,CAAC,iBAAiB;IAgBzB,OAAO,CAAC,YAAY;IAwBpB,OAAO,CAAC,YAAY;CAOrB"}

22
dist/DHTDataBase.js vendored
View File

@ -113,10 +113,6 @@ class DHTDataBase extends events_1.default {
const { data: rawData, timestamp, signature } = decoded; const { data: rawData, timestamp, signature } = decoded;
if (signature && if (signature &&
hypercore_crypto_1.default.verify(b4a_1.default.from(`${timestamp}${rawData}`), signature, id)) { hypercore_crypto_1.default.verify(b4a_1.default.from(`${timestamp}${rawData}`), signature, id)) {
if ((timestamp || 0) <= this.getPeerTimestamp(id)) {
(0, debug_1.default)(`Received old data for peer ${id}`);
return;
}
const data = rawData ? ordered_json_1.default.parse(rawData) : null; const data = rawData ? ordered_json_1.default.parse(rawData) : null;
this._setPeer(id, data, timestamp, signature); this._setPeer(id, data, timestamp, signature);
this.emit("peer-data", data, id); this.emit("peer-data", data, id);
@ -205,22 +201,16 @@ class DHTDataBase extends events_1.default {
continue; continue;
} }
if (signature && if (signature &&
hypercore_crypto_1.default.verify(b4a_1.default.from(`${timestamp}${data}`), signature, b4a_1.default.from(id, "hex"))) { hypercore_crypto_1.default.verify(b4a_1.default.from(`${timestamp}${data}`), signature, b4a_1.default.from(id))) {
const parsedData = data ? ordered_json_1.default.parse(data) : null; const parsedData = data ? ordered_json_1.default.parse(data) : null;
let peerData = parsedData || {}; let peerData = parsedData || {};
// If we're already tracking them // If we're already tracking them
if (this._hasSeenPeer(id)) { if (this._hasSeenPeer(id)) {
// Ensure we don't have old data // See what data we already have for them
if ((timestamp || 0) > this.getPeerTimestamp(id)) { // Add their existing data to what we got from the bootstrap
// See what data we already have for them const existingPeerData = this.getPeerData(id);
// Add their existing data to what we got from the bootstrap peerData = { ...existingPeerData, ...peerData };
const existingPeerData = this.getPeerData(id); this._setPeer(id, peerData, timestamp, signature);
peerData = { ...existingPeerData, ...peerData };
this._setPeer(id, peerData, timestamp, signature);
}
else {
(0, debug_1.default)(`Received old data for peer ${id}`);
}
} }
} }
else { else {

View File

@ -78,7 +78,6 @@ export default class DHTDataBase extends EventEmitter {
getPeerData(id: Buffer | string) { getPeerData(id: Buffer | string) {
return this.getPeerField(id, "data"); return this.getPeerField(id, "data");
} }
getPeerTimestamp(id: Buffer | string) { getPeerTimestamp(id: Buffer | string) {
return this.getPeerField(id, "timestamp"); return this.getPeerField(id, "timestamp");
} }
@ -160,18 +159,12 @@ export default class DHTDataBase extends EventEmitter {
signature && signature &&
crypto.verify(b4a.from(`${timestamp}${rawData}`), signature, id) crypto.verify(b4a.from(`${timestamp}${rawData}`), signature, id)
) { ) {
if ((timestamp || 0) <= this.getPeerTimestamp(id)) {
debug(`Received old data for peer ${id}`);
return;
}
const data = rawData ? orderedJSON.parse(rawData) : null; const data = rawData ? orderedJSON.parse(rawData) : null;
this._setPeer(id, data, timestamp, signature); this._setPeer(id, data, timestamp, signature);
this.emit("peer-data", data, id); this.emit("peer-data", data, id);
this._recalculate(); this._recalculate();
return; return;
} }
debug(`Invalid signature received for peer ${id}`); debug(`Invalid signature received for peer ${id}`);
} else if (type === Type.CONNECTED) { } else if (type === Type.CONNECTED) {
const { id: toId } = decoded; const { id: toId } = decoded;
@ -281,27 +274,18 @@ export default class DHTDataBase extends EventEmitter {
if ( if (
signature && signature &&
crypto.verify( crypto.verify(b4a.from(`${timestamp}${data}`), signature, b4a.from(id))
b4a.from(`${timestamp}${data}`),
signature,
b4a.from(id, "hex")
)
) { ) {
const parsedData = data ? orderedJSON.parse(data) : null; const parsedData = data ? orderedJSON.parse(data) : null;
let peerData = parsedData || {}; let peerData = parsedData || {};
// If we're already tracking them // If we're already tracking them
if (this._hasSeenPeer(id)) { if (this._hasSeenPeer(id)) {
// Ensure we don't have old data // See what data we already have for them
if ((timestamp || 0) > this.getPeerTimestamp(id)) { // Add their existing data to what we got from the bootstrap
// See what data we already have for them const existingPeerData = this.getPeerData(id);
// Add their existing data to what we got from the bootstrap peerData = { ...existingPeerData, ...peerData };
const existingPeerData = this.getPeerData(id); this._setPeer(id, peerData, timestamp, signature);
peerData = { ...existingPeerData, ...peerData };
this._setPeer(id, peerData, timestamp, signature);
} else {
debug(`Received old data for peer ${id}`);
}
} }
} else { } else {
debug(`Invalid signature received for peer ${id}`); debug(`Invalid signature received for peer ${id}`);

View File

@ -3,7 +3,7 @@ const test = require("tape");
const Hyperswarm = require("hyperswarm"); const Hyperswarm = require("hyperswarm");
const sodium = require("sodium-universal"); const sodium = require("sodium-universal");
const b4a = require("b4a"); const b4a = require("b4a");
const { default: DHTData } = require("./"); const { default: DHTOnline } = require("./");
const crypto = require("crypto"); const crypto = require("crypto");
const topicName = crypto.randomBytes(10); const topicName = crypto.randomBytes(10);
const topic = b4a.allocUnsafe(32); const topic = b4a.allocUnsafe(32);
@ -19,8 +19,8 @@ test("Basic presence test / data propagation", (t) => {
const peer1 = peers.shift(); const peer1 = peers.shift();
const peer2 = peers.shift(); const peer2 = peers.shift();
const p1 = new DHTData(peer1); const p1 = new DHTOnline(peer1);
const p2 = new DHTData(peer2); const p2 = new DHTOnline(peer2);
p1.data = { message: "Hello" }; p1.data = { message: "Hello" };
p2.data = { message: "World!" }; p2.data = { message: "World!" };