The Go implementation embeds public key in the peer id when key length is <= 42 bytes. This is the case, for example, with secp256k1 keys.
This results in an error in the browser where ids generated from the same public key with Go implementation do not match ones generated by the js implementation.
Error: dialed to the wrong peer, Ids do not match
at PeerId.createFromPubKey (crypto.js:80)
at pubKey.hash (index.js:188)
at Multihashing.Multihashing.digest (index.js:33)
at index.js:15
at run (setImmediate.js:40)
at runIfPresent (setImmediate.js:69)
at onGlobalMessage (setImmediate.js:109)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:496)
Go implementation:
// IDFromPublicKey returns the Peer ID corresponding to pk
func IDFromPublicKey(pk ic.PubKey) (ID, error) {
b, err := pk.Bytes()
if err != nil {
return "", err
}
var alg uint64 = mh.SHA2_256
if len(b) <= MaxInlineKeyLength { // <<<< Hash algorithem is set to ID when key is 42 bytes or less
alg = mh.ID
}
hash, _ := mh.Sum(b, alg, -1)
return ID(hash), nil
}
I have made changes to my local version to correct this but as a prerequisite I had to make changes to multihash in order to support 'id' function code. Please see: https://github.com/multiformats/js-multihash/issues/60
Below is a patch of changes I made against master branch:
Secp256k1 test id json to be placed in test/fixtures/go-secp256k1-id.js
The Go implementation embeds public key in the peer id when key length is <= 42 bytes. This is the case, for example, with secp256k1 keys.
This results in an error in the browser where ids generated from the same public key with Go implementation do not match ones generated by the js implementation.
Go implementation:
I have made changes to my local version to correct this but as a prerequisite I had to make changes to multihash in order to support 'id' function code. Please see: https://github.com/multiformats/js-multihash/issues/60
Below is a patch of changes I made against master branch:
Secp256k1 test id json to be placed in test/fixtures/go-secp256k1-id.js
A patch of changes to support ids with inline public key