javascriptnode.jshashhypercore-protocol

Hypercore Protocol: Function to get hypercore discovery key from a public key


Is there a convenient function to get a hypercore's discovery key from its public key?

I know I can use myCore.key and myCore.discoveryKey in hypersdk

But if you just had a key as a hex string (e.g. "778f8d955175c92e4ced5e4f5563f69bfec0c86cc6f670352c457943666fe639"), how would you get the discovery key?

Is there a convenience function in one of the hypercore-protocol modules?

Note, the discovery key is the blake2b-256 hash of the public key.


Solution

  • I used the hypercore-crypto module:

    const crypto = require('hypercore-crypto')
    let publicKey = "778f8d955175c92e4ced5e4f5563f69bfec0c86cc6f670352c457943666fe639"
    let discoveryKey = crypto.discoveryKey(Buffer.from(publicKey, 'hex')).toString('hex');
    

    Gives a discovery key of:

    "25a78aa81615847eba00995df29dd41d7ee30f3b01f892209f79b75a57d989e1"