Skip to content

Converting between Ed25519 key and peerID #2379

Answered by achingbrain
Wiezzel asked this question in Q&A
Discussion options

You must be logged in to vote

A PeerId is a multihash of a protobuf struct that contains the key Type and a Data field.

In the case of RSA Data is the public key encoded as PKIX/X.509 in ASN1 DER format and the multihash is the sha256 hash of the serialized protobuf bytes.

For Ed25519 and Secp256k1 keys Data is the raw public key bytes and the multihash is the identity hash, that is the multihash digest contains the actual serialized protobuf struct.

So for Ed25519 keys we can recreate the multihash with:

import { keysPBM } from '@libp2p/crypto/keys'
import { createPeerId } from '@libp2p/peer-id'
import { identity } from 'multiformats/hashes/identity'
import * as Digest from 'multiformats/hashes/digest'

// these are …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Wiezzel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants