Awesome
ex-multiaddr
IPFS multiaddr implementation in Elixir
Installation
Available in Hex, the package can be installed
by adding ex_multiaddr
to your list of dependencies in mix.exs
:
def deps do
[
{:ex_multiaddr, "~> 0.1.0"}
]
end
The docs can be found at https://hexdocs.pm/ex_multiaddr.
Protocols
- ip4
- tcp
- udp
- dccp
- ip6
- ip6zone
- dns
- dns4
- dns6
- dnsaddr
- sctp
- udt
- utp
- unix
- p2p
- ipfs
- onion
- onion3
- garlic64
- garlic32
- quic
- http
- https
- ws
- wss
- p2p-websocket-star
- p2p-stardust
- p2p-webrtc-star
- p2p-webrtc-direct
- p2p-circuit
- memory
Usage
Create Multiaddr
maddr_string = "/ip4/127.0.0.1/tcp/80"
{:ok, maddr} = Multiaddr.new_multiaddr_from_string(maddr_string)
# {:ok, %Multiaddr{bytes: <<4, 127, 0, 0, 1, 6, 0, 80>>}}
En/Decapsulate
{:ok, maddr_1} = Multiaddr.new_multiaddr_from_string("/ip4/127.0.0.1")
{:ok, maddr_2} = Multiaddr.new_multiaddr_from_string("/tcp/80")
{:ok, maddr_en} = Multiaddr.encapsulate(maddr_1, maddr_2)
# {:ok, %Multiaddr{bytes: <<4, 127, 0, 0, 1, 6, 0, 80>>}}
Multiaddr.string(maddr_en)
# "/ip4/127.0.0.1/tcp/80"
{:ok, maddr_de} = Multiaddr.decapsulate(maddr_en, maddr_2)
# {:ok, %Multiaddr{bytes: <<4, 127, 0, 0, 1>>}}
Multiaddr.string(maddr_de)
# "/ip4/127.0.0.1"
Inspect Multiaddr
{:ok, maddr} = Multiaddr.new_multiaddr_from_string("/ip4/127.0.0.1/tcp/80")
protocols = Multiaddr.protocols(maddr)
{:ok, prot_1} = Enum.fetch(protocols, 0)
#{:ok, %Multiaddr.Protocol{
# code: 4, name: "ip4", size: 32,
# transcoder: %Multiaddr.Transcoder{
# bytes_to_string: #Function<2.41508498/1 in Multiaddr.Transcoder.ip4_transcoder/0>,
# string_to_bytes: #Function<3.41508498/1 in Multiaddr.Transcoder.ip4_transcoder/0>
# },
# vcode: <<4>>
# }}
{:ok, ip4_value} = Multiaddr.value_for_protocol(maddr, "ip4")
# {:ok, "127.0.0.1"}
{:ok, tcp_value} = Multiaddr.value_for_protocol(maddr, "tcp")
# {:ok, "80"}