Encapsulate and Decapsulate Multiaddr protocols
masterMultiaddr supports nesting protocols through encapsulation and stripping them through decapsulation:
- Encapsulate: Use
m.Encapsulate(subaddr)to append a sub-address to the existing address, effectively wrapping the original address in a new protocol layer. - Decapsulate: Use
m.Decapsulate(subaddr)to remove a specific protocol layer. This removes the specified sub-address and any subsequent protocols in the chain.
import ma "github.com/multiformats/go-multiaddr"
m, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
sctpMA, err := ma.NewMultiaddr("/sctp/5678")
// Encapsulate: adds /sctp/5678 to the end
m.Encapsulate(sctpMA)
// Result: <Multiaddr /ip4/127.0.0.1/udp/1234/sctp/5678>
udpMA, err := ma.NewMultiaddr("/udp/1234")
// Decapsulate: removes /udp/1234 and everything after it
m.Decapsulate(udpMA)
// Result: <Multiaddr /ip4/127.0.0.1>