webtorrent / bittorrent-dht

🕸 Simple, robust, BitTorrent DHT implementation
https://webtorrent.io
MIT License
1.21k stars 204 forks source link

dht.get() error, BEP 44 broken #284

Closed resession closed 10 months ago

resession commented 10 months ago

What version of this package are you using? 11.0.5

What operating system, Node.js, and npm version? ubuntu and windows

What happened? throw error

What did you expect to happen? return bep 44 data such as ih(infohash)

Are you willing to submit a pull request to fix this bug? if it is easier to fix, might need guidance

when putting/getting mutable BEP 44 data

the following

(async () => {
    const {default: DHT} = await import('bittorrent-dht')
    return DHT
})().then((Data) => {
    const ed = require('ed25519-supercop')
    const dht = new Data({verify: ed.verify})

    dht.get(require('crypto').createHash('sha1').update(Buffer.from('c1eee9b47220453fdd2d9f4483dd89ee64f386d1e1530fddb42b1632bf2b77de', 'hex')).digest('hex'), {}, (err, data) => {
        if(err){
            throw err
        } else if(data){
            console.log(data)
        } else if(!data){
            console.log('none')
        }
    })
})

throws the following error, getting BEP 44 data is broken

/media/plainsstuds/e58258ae-7efb-4092-939a-b444bd78a87c/Proj/Dev/test/node_modules/ed25519-supercop/index.js:28
    throw new Error('message must be a buffer or a string')
    ^

Error: message must be a buffer or a string
    at exports.verify (/media/plainsstuds/e58258ae-7efb-4092-939a-b444bd78a87c/Proj/Dev/test/node_modules/ed25519-supercop/index.js:28:11)
    at onreply (file:///media/plainsstuds/e58258ae-7efb-4092-939a-b444bd78a87c/Proj/Dev/test/node_modules/bittorrent-dht/client.js:385:14)
    at onreply (file:///media/plainsstuds/e58258ae-7efb-4092-939a-b444bd78a87c/Proj/Dev/test/node_modules/bittorrent-dht/client.js:696:14)
    at Object.afterQuery [as callback] (/media/plainsstuds/e58258ae-7efb-4092-939a-b444bd78a87c/Proj/Dev/test/node_modules/k-rpc/index.js:286:18)
    at Socket.onmessage (/media/plainsstuds/e58258ae-7efb-4092-939a-b444bd78a87c/Proj/Dev/test/node_modules/k-rpc-socket/index.js:116:11)
    at Socket.emit (node:events:513:28)
    at UDP.onMessage [as onmessage] (node:dgram:933:8)
resession commented 10 months ago

this error is stemming from updates that has been made to https://github.com/webtorrent/node-bencode

ThaUnknown commented 10 months ago

this is an issue caused by ed25519-supercop not DHT?...

resession commented 10 months ago

this is an issue caused by ed25519-supercop not DHT?...

yes but it is because ed25519-supercop expects either a string or buffer, but bittorrent-dht/bencode.encode() is returning a Uint8Array

https://github.com/webtorrent/bittorrent-dht/blob/master/client.js#L781C22-L781C22

check index.js, line 28 https://www.npmjs.com/package/ed25519-supercop?activeTab=code

  else if (!Buffer.isBuffer(message)) {
    throw new Error('message must be a buffer or a string')
  }

its throwing the error because bittorrent-dht/bencode is returning a Uint8Array, Buffer.from(Uint8Array) fixes it

ThaUnknown commented 10 months ago

once again, this should be fixed on ed25519-supercop's end, not bittorrent-dht's end, or you can simply wrap ed25519 using buffer.from like you said

uint8array is a perfectly valid way of storing uint8 data, and unlike buffer it's cross env compatible, and as we plan on implementing DHT in web one day, Buffer would end up being removed anyways