holepunchto / hyperdht

The DHT powering Hyperswarm
https://docs.holepunch.to
MIT License
323 stars 46 forks source link

Output Public key to file and read into another file #80

Closed cpueasy closed 2 years ago

cpueasy commented 2 years ago

I am trying to output the Public key from the server into another file which would be the client

// make a ed25519 keypair to listen on
const keyPair = DHT.keyPair(Buffer.alloc(32).fill('testing-channel'))
// print the public key bytes as a base64 encoded string
console.log(keyPair['publicKey'].toString('base64'));
// output:
// 7a6HaUKi4sZ2EsP3J3kqHvkhkJETYbLjxkx3gTGbqS8=

I tried to store this base64 string as a variable in the client using the same library and still can't connect to the server! Here is the client code

import DHT from '@hyperswarm/dht'
const anotherNode = new DHT()

// publicKey here is keyPair.publicKey from above
var key = Buffer.from('7a6HaUKi4sZ2EsP3J3kqHvkhkJETYbLjxkx3gTGbqS8=', 'base64').toString()

const noiseSocket = anotherNode.connect(key)

noiseSocket.on('open', function () {
  // noiseSocket fully open with the other peer
})

// pipe it somewhere like any duplex stream
process.stdin.pipe(noiseSocket).pipe(process.stdout)

The corresponding error:

  sodium.crypto_generichash(out, data)
         ^

TypeError: in must be an instance of TypedArray
    at hash (C:\Users\User\Documents\project\node_modules\@hyperswarm\dht\lib\crypto.js:6:10)
    at connect (C:\Users\User\Documents\project\node_modules\@hyperswarm\dht\lib\connect.js:19:13)
    at HyperDHT.connect (C:\Users\User\Documents\project\node_modules\@hyperswarm\dht\index.js:48:12)
    at file:///C:/Users/User/Documents/project/client.js:10:33
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Can anybody tell me how I can store the public key bytes as a variable through like base64 or JSON.stringify() or as a hex string?

mafintosh commented 2 years ago

Remove the toString on the key above, connect expects a buffer

baylanger commented 2 years ago

FYI - OP is for the Ukrainian war - probably due to brainwashing from Russian government.

https://github.com/erkexzcx/stoppropaganda/issues/113

cpueasy commented 2 years ago

Yeah totally. Lets all support terrorism @baylanger

cpueasy commented 2 years ago

I don't support war nor Terrorism