Skip to content
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: add decapsulateCode method #98

Merged
merged 2 commits into from
Sep 10, 2019
Merged

feat: add decapsulateCode method #98

merged 2 commits into from
Sep 10, 2019

Conversation

jacobheun
Copy link
Contributor

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 of QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC.
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 do addr.decapsulate('p2p') to get the tcp address.

The problem with this is:

  1. p2p is the default now, but older versions use ipfs. 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.
  2. Decapsulate currently matches strings. This is useful when you want to decapsulate more complex addresses. However, this will cause problems decapsulating protocols from complex addresses.
    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 actual p2p protocol.

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
@jacobheun jacobheun merged commit 19a3940 into master Sep 10, 2019
@jacobheun jacobheun deleted the feat/decapsulateCode branch September 10, 2019 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants