3

I'm trying to use hyperdb in browser with swarming via webrtc and signalhub. The code is pretty strait forward, but there is some issue with hyperdb replicate where the connecting is killed because of a sameKey check in hypercore. So, I'm thinking ... I'm not properly juggling my discovery keys and id keys so the peers know they should be sync'd. Here is some sample code, it is a bit of a mess but the relevant bits are the hyperdb initialization and the webrtc/signalhub stuff (I think) ... the key at the top is the discovery key of the other peer:

 const crypto = require('crypto'),
  sha = crypto.createHash('sha1'),
  hyperdb = require('hyperdb'),
  hyperdiscovery = require('hyperdiscovery'),
  cms = require('random-access-idb')('cms'),
  webrtc = require('webrtc-swarm'),
  signalhub = require('signalhub'),
  hyperdrive = require('hyperdrive'),
  pump = require('pump');

// Discovery key of other peer/signalhub channel
var key = "cbffda913dabfe73cbd45f64466ffda845383965e66b2aef5f3b716ee6c06528";

const db = hyperdb(filename => {
  return cms(filename);
}, { valueEncoding: 'utf-8' });

var DEFAULT_SIGNALHUBS = 'https://signalhub-jccqtwhdwc.now.sh';

db.on('ready', function () {
  const swarm = webrtc(signalhub(key, DEFAULT_SIGNALHUBS));
  swarm.on('peer', function (conn) {
    console.log("PEER!!!!!!!");
    const peer = db.replicate({
      upload: true,
      download: true
    });
    pump(conn, peer, conn)
  });
});
jbg
  • 993
  • 1
  • 11
  • 19
  • Mind taking the commented code and nonrelevant parts out? A bit hard to tell exactly what to look at. I can try to setup a basic hyperdb/webrtc setup (not sure if anyone has tested it). – joehand Jul 13 '18 at 17:38
  • It may be because you aren't initializing the new db with the existing key. – joehand Jul 13 '18 at 17:51
  • @joehand Thanks for the answer! I'm just not sure what you mean by "the key". It looks like it is passed in as an argument in your example. Similarly from the actual dat documentation: "Just make sure to reference the archive you created before by using archive.key as the first argument:" ... seems there are several possible "keys": db.key, db.discoveryKey, db.local.key ... or I'm wrong entirely. – jbg Jul 14 '18 at 07:01
  • Okay, key is the db.key. I think I have something that works here. – jbg Jul 14 '18 at 12:09

1 Answers1

2

I put up a working example here: https://github.com/joehand/hyperdb-web-example/blob/master/index.js

I think you are getting that error because you are not initializing the db with the key:

var db = hyperdb(storage, key)

Once you do that, you can get the discovery key. Generally, you don't need to be copying the discovery key around because that is always generated from the public key.

If that does not work, please include only the relevant code or a minimum example, so it is easier to debug =). Thanks!

joehand
  • 391
  • 2
  • 17