From 717731e49a40142164af6c5c5703f0cad32edbe5 Mon Sep 17 00:00:00 2001 From: Daniel Norman <1992255+2color@users.noreply.github.com> Date: Fri, 25 Oct 2024 10:05:38 +0200 Subject: [PATCH] docs: add peerIdFromString to migration guide (#2781) Add breaking change in peerIdFromString to migration guide from 1 -> 2 Related #2772 --------- Co-authored-by: Daniel N <2color@users.noreply.github.com> Co-authored-by: Alex Potsides --- doc/migrations/v1.0.0-v2.0.0.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/doc/migrations/v1.0.0-v2.0.0.md b/doc/migrations/v1.0.0-v2.0.0.md index 9f1b964650..063a3ceafb 100644 --- a/doc/migrations/v1.0.0-v2.0.0.md +++ b/doc/migrations/v1.0.0-v2.0.0.md @@ -11,6 +11,7 @@ A migration guide for refactoring your application code from libp2p `v1.0.0` to - [The `createLibp2p` function accepts a `PrivateKey` instead of a `PeerId`](#the-createlibp2p-function-accepts-a-privatekey-instead-of-a-peerid) - [The `@libp2p/keychain` module operates on `PrivateKey`s instead of `PeerId`s](#the-libp2pkeychain-module-operates-on-privatekeys-instead-of-peerids) - [The `@libp2p/peer-id-factory` module has been removed](#the-libp2ppeer-id-factory-module-has-been-removed) +- [`peerIdFromString` no longer accepts CID encoded PeerIDs](#peeridfromstring-no-longer-accepts-cid-encoded-peerids) - [The autodialer has been removed](#the-autodialer-has-been-removed) - ["Transient" connections have been renamed "limited"](#transient-connections-have-been-renamed-limited) - [The CustomEvent polyfill has been removed from `@libp2p/interface`](#the-customevent-polyfill-has-been-removed-from-libp2pinterface) @@ -251,6 +252,36 @@ const privateKey = await generateKeyPair('Ed25519') const peerId = peerIdFromPrivateKey(privateKey) ``` +## `peerIdFromString` no longer accepts CID encoded PeerIDs + +The `peerIdFromString` function used to be quite flexible in that it would attempt +to interpret multibase strings as either a multihash encoded PeerID or a CID. + +As of `@libp2p/peer-id@5.x.x` the method has been simplified to only accept +multibase strings containing a multihash encoded PeerID. + +To read a PeerId from a string containing a CID, first parse the CID from the +string, then use the `peerIdFromCID` method. + +**Before** + +```ts +import { peerIdFromString } from '@libp2p/peer-id' + +const peer = peerIdFromString('k51qzi5uqu5dkwkqm42v9j9kqcam2jiuvloi16g72i4i4amoo2m8u3ol3mqu6s') +``` + +**After** + +```ts +import { peerIdFromCID } from '@libp2p/peer-id' +import { CID } from 'multiformats/cid' + +// CID encoded PeerID in base36 +const cid = CID.parse('k51qzi5uqu5dkwkqm42v9j9kqcam2jiuvloi16g72i4i4amoo2m8u3ol3mqu6s') +const peer = peerIdFromCID(cid) +``` + ## The autodialer has been removed The libp2p autodialer was a component that attempted to ensure a minimum number