-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support Peer ID represented as CID #105
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
'use strict' | ||
|
||
const mh = require('multihashes') | ||
const CID = require('cids') | ||
const cryptoKeys = require('libp2p-crypto/src/keys') | ||
const assert = require('assert') | ||
const withIs = require('class-is') | ||
|
@@ -122,6 +123,12 @@ class PeerId { | |
return this._idB58String | ||
} | ||
|
||
// return string representation from RFC 0001: https://github.com/libp2p/specs/pull/209 | ||
toCIDString () { | ||
const cid = new CID(1, 'libp2p-key', this.id, 'base32') | ||
return cid.toBaseEncodedString('base32') | ||
lidel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
isEqual (id) { | ||
if (Buffer.isBuffer(id)) { | ||
return this.id.equals(id) | ||
|
@@ -187,6 +194,18 @@ exports.createFromB58String = (str) => { | |
return new PeerIdWithIs(mh.fromB58String(str)) | ||
lidel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
exports.createFromCID = (cid) => { | ||
if (typeof cid === 'string' || Buffer.isBuffer(cid)) { | ||
lidel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cid = new CID(cid) | ||
} else if (CID.isCID(cid)) { | ||
CID.validateCID(cid) // throws on error | ||
lidel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
// provide more meaningful error than the one in CID.validateCID | ||
throw new Error('Supplied cid value is neither String|CID|Buffer') | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: validate the codec is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is tricky. See scenario below:
I think the only way is to handle it gracefully is to check for both |
||
return new PeerIdWithIs(cid.multihash) | ||
} | ||
lidel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Public Key input will be a buffer | ||
exports.createFromPubKey = async (key) => { | ||
let buf = key | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will now become rarely used. Should we generate on demand instead of in the constructor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used in
isPeerId
and changing this triggers a bigger refactor.I'd like to keep it as-is in this PR (to keep it small) and change it in
Stage 2
of libp2p/specs#216(when we "Format peer IDs as CIDs by default.")