Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This allows users to remove the last index of a given protocol code. It behaves like decapsulate, but checks protocol codes rather than string matching the whole multiaddr.
Unlike
decapsulate
,decapsulateCode
will not throw when the code isn't present, it just returns the original address, because "I did what you asked" is better than "OMG I couldn't find it!!!".Why?
It's often the case in Libp2p that we want to encapsulate/decapsulate the PeerId in a Multiaddr. This makes it easier to avoid validation problems when using
mafmt
.For example, let's say there is a peer at
/ip4/127.0.0.1/4001
, with an id ofQmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC
.Encapsulated they yield,
/ip4/127.0.0.1/4001/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC
.If i do a validation check for tcp, it will fail, because
p2p
is not part of a valid tcp address. Today, we would doaddr.decapsulate('p2p')
to get the tcp address.The problem with this is:
p2p
is the default now, but older versions useipfs
. So any protocol that has a name alias might fail depending on the version of multiaddr being used as the stringified address will be different. In a large ecosystem of modules like IPFS or Libp2p, it's common to have multiple versions of multiaddr as it's used so prevalently.An address like
/ip4/0.0.0.0/tcp/8080/p2p/QmZR5a9AAXGqQF2ADqoDdGS8zvqv8n3Pag6TDDnTNMcFW6/p2p-circuit
would result in only/p2p-circuit
being removed, instead of the actualp2p
protocol.