Compare commits
4 Commits
78f3edd71d
...
eb57f87eb4
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | eb57f87eb4 | |
Derrick Hammer | a7a3ac1d03 | |
Derrick Hammer | a1d826e376 | |
Derrick Hammer | 15b088f481 |
|
@ -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;IAG/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;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,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,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;IA2DpD,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;IAgDtB,OAAO,CAAC,iBAAiB;IAgBzB,OAAO,CAAC,YAAY;IAwBpB,OAAO,CAAC,YAAY;CAOrB"}
|
|
@ -113,6 +113,10 @@ class DHTDataBase extends events_1.default {
|
|||
const { data: rawData, timestamp, signature } = decoded;
|
||||
if (signature &&
|
||||
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;
|
||||
this._setPeer(id, data, timestamp, signature);
|
||||
this.emit("peer-data", data, id);
|
||||
|
@ -201,16 +205,22 @@ class DHTDataBase extends events_1.default {
|
|||
continue;
|
||||
}
|
||||
if (signature &&
|
||||
hypercore_crypto_1.default.verify(b4a_1.default.from(`${timestamp}${data}`), signature, b4a_1.default.from(id))) {
|
||||
hypercore_crypto_1.default.verify(b4a_1.default.from(`${timestamp}${data}`), signature, b4a_1.default.from(id, "hex"))) {
|
||||
const parsedData = data ? ordered_json_1.default.parse(data) : null;
|
||||
let peerData = parsedData || {};
|
||||
// If we're already tracking them
|
||||
if (this._hasSeenPeer(id)) {
|
||||
// See what data we already have for them
|
||||
// Add their existing data to what we got from the bootstrap
|
||||
const existingPeerData = this.getPeerData(id);
|
||||
peerData = { ...existingPeerData, ...peerData };
|
||||
this._setPeer(id, peerData, timestamp, signature);
|
||||
// Ensure we don't have old data
|
||||
if ((timestamp || 0) > this.getPeerTimestamp(id)) {
|
||||
// See what data we already have for them
|
||||
// Add their existing data to what we got from the bootstrap
|
||||
const existingPeerData = this.getPeerData(id);
|
||||
peerData = { ...existingPeerData, ...peerData };
|
||||
this._setPeer(id, peerData, timestamp, signature);
|
||||
}
|
||||
else {
|
||||
(0, debug_1.default)(`Received old data for peer ${id}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -78,6 +78,7 @@ export default class DHTDataBase extends EventEmitter {
|
|||
getPeerData(id: Buffer | string) {
|
||||
return this.getPeerField(id, "data");
|
||||
}
|
||||
|
||||
getPeerTimestamp(id: Buffer | string) {
|
||||
return this.getPeerField(id, "timestamp");
|
||||
}
|
||||
|
@ -159,12 +160,18 @@ export default class DHTDataBase extends EventEmitter {
|
|||
signature &&
|
||||
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;
|
||||
this._setPeer(id, data, timestamp, signature);
|
||||
this.emit("peer-data", data, id);
|
||||
this._recalculate();
|
||||
return;
|
||||
}
|
||||
|
||||
debug(`Invalid signature received for peer ${id}`);
|
||||
} else if (type === Type.CONNECTED) {
|
||||
const { id: toId } = decoded;
|
||||
|
@ -274,18 +281,27 @@ export default class DHTDataBase extends EventEmitter {
|
|||
|
||||
if (
|
||||
signature &&
|
||||
crypto.verify(b4a.from(`${timestamp}${data}`), signature, b4a.from(id))
|
||||
crypto.verify(
|
||||
b4a.from(`${timestamp}${data}`),
|
||||
signature,
|
||||
b4a.from(id, "hex")
|
||||
)
|
||||
) {
|
||||
const parsedData = data ? orderedJSON.parse(data) : null;
|
||||
let peerData = parsedData || {};
|
||||
|
||||
// If we're already tracking them
|
||||
if (this._hasSeenPeer(id)) {
|
||||
// See what data we already have for them
|
||||
// Add their existing data to what we got from the bootstrap
|
||||
const existingPeerData = this.getPeerData(id);
|
||||
peerData = { ...existingPeerData, ...peerData };
|
||||
this._setPeer(id, peerData, timestamp, signature);
|
||||
// Ensure we don't have old data
|
||||
if ((timestamp || 0) > this.getPeerTimestamp(id)) {
|
||||
// See what data we already have for them
|
||||
// Add their existing data to what we got from the bootstrap
|
||||
const existingPeerData = this.getPeerData(id);
|
||||
peerData = { ...existingPeerData, ...peerData };
|
||||
this._setPeer(id, peerData, timestamp, signature);
|
||||
} else {
|
||||
debug(`Received old data for peer ${id}`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
debug(`Invalid signature received for peer ${id}`);
|
||||
|
|
6
test.js
6
test.js
|
@ -3,7 +3,7 @@ const test = require("tape");
|
|||
const Hyperswarm = require("hyperswarm");
|
||||
const sodium = require("sodium-universal");
|
||||
const b4a = require("b4a");
|
||||
const { default: DHTOnline } = require("./");
|
||||
const { default: DHTData } = require("./");
|
||||
const crypto = require("crypto");
|
||||
const topicName = crypto.randomBytes(10);
|
||||
const topic = b4a.allocUnsafe(32);
|
||||
|
@ -19,8 +19,8 @@ test("Basic presence test / data propagation", (t) => {
|
|||
const peer1 = peers.shift();
|
||||
const peer2 = peers.shift();
|
||||
|
||||
const p1 = new DHTOnline(peer1);
|
||||
const p2 = new DHTOnline(peer2);
|
||||
const p1 = new DHTData(peer1);
|
||||
const p2 = new DHTData(peer2);
|
||||
|
||||
p1.data = { message: "Hello" };
|
||||
p2.data = { message: "World!" };
|
||||
|
|
Loading…
Reference in New Issue