holepunchto/hyperswarm-dht-relay

Hypercore replication in browser fails

Nuhvi opened this issue · 16 comments

Nuhvi commented

I tested the dht-relay extensively, and while announce, lookup, server.listen and connect works just fine, trying to replicate a Hypercore throws an error (only in browser environment):

Error: Noise handshake failed
    at NoiseSecretStream._onhandshakert (bundle.js:1993)
    at NoiseSecretStream._open (bundle.js:2041)
    at WritableState.updateNonPrimary (bundle.js:16661)
    at WritableState.update (bundle.js:16639)
    at WritableState.updateWriteNT (bundle.js:16947)

I tracked this issue and figured that this.protocol.connect(message) eventually returns undefined even though the content of the message is valid, and even though the server node seems to perform the handshake correctly.

I got the same result whether I am testing this in an actual application, or in more controlled test suite https://github.com/synonymdev/dht-universal/blob/5fc36d4e21ab0a04dd4bf0e64a9e9816492d941a/test/api.spec.js#L439

Nuhvi commented

Maybe this issue is more relevant in Hypercore instead, but I confirmed that the replicating using the dht-relay only fails in the browser, not nodejs.

What are you using to compile the code for the browser? I've had issues getting Webpack to work because the node globals that it injects are out of date.

I've found that browserify is the only surefire path to get it working. 😅

Nuhvi commented

@RangerMauve
I tested both with aegir (esbuild) and Parcel. I will test with Browserify similar to how hyper-sdk does it and report back.

Nuhvi commented

Tested again with browserify, same result.

Nuhvi commented

Fwiw I tested again using the latest change in the master branch

Thanks for the report and reproduction! I'll investigate this today and am already seeing some possibly related bugs after my work in #5 and #8.

With d2ff078 in place, I have Hypercore replication working in a browser between two relayed peers, i.e. both running in the browser and connected through the relay.

Nuhvi commented

@kasperisager looks great, but I tried the new API here and the replication between the browser (initiator) and the Nodejs side as (responder) is still throwing the same error.

I will try to test the other way too in case it offers any insight!

@nazeh you should change that to use the core.replicate(swarmStream) api, otherwise you’ll be double encrypting

that’s prob why it fails, there is a method missing in sodium for encryption

… in the browser

Nuhvi commented

@mafintosh Indeed, it works with Hyperswarm in the browser, ... after adding b4a to Hyperswarm

Perfect, that's great to hear! I'll put it on my list to get Hyperswarm "unbuffered".

Let me know if holepunchto/hyperswarm#98 does the trick.

Nuhvi commented

@kasperisager Unfortunately no, that PR introduced a dependency that needs Buffer ( turbo-hash-map)

Let's continue the discussion in that PR as the issue relates to Hyperswarm ☝️