Commit 8923fdd9 authored by Whyrusleeping's avatar Whyrusleeping Committed by GitHub

Merge pull request #4340 from ipfs/feat/extract-ipfsaddr

extract go-ipfs-addr
parents c0d6224f cf6b52e4
......@@ -13,9 +13,9 @@ import (
repo "github.com/ipfs/go-ipfs/repo"
config "github.com/ipfs/go-ipfs/repo/config"
"github.com/ipfs/go-ipfs/repo/fsrepo"
iaddr "github.com/ipfs/go-ipfs/thirdparty/ipfsaddr"
pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
swarm "gx/ipfs/QmdQFrFnPrKRQtpeHKjZ3cVNwxmGKKS2TvhJTuN9C9yduh/go-libp2p-swarm"
iaddr "gx/ipfs/QmeS8cCKawUwejVrsBtmC1toTXmwVWZGiRJqzgTURVWeF9/go-ipfs-addr"
mafilter "gx/ipfs/QmSMZwvs3n4GBikZ7hKzT17c3bk65FmyZo2JqtJ16swqCv/multiaddr-filter"
ma "gx/ipfs/QmXY77cVe7rVRQXZZQRioukUM7aRW3BTcAgJe12MCtb3Ji/go-multiaddr"
......
......@@ -481,6 +481,12 @@
"hash": "QmYkCrTwivapqdB3JbwvwvxymseahVkcm46ThRMAA24zCr",
"name": "go-libp2p-interface-connmgr",
"version": "0.0.2"
},
{
"author": "why",
"hash": "QmeS8cCKawUwejVrsBtmC1toTXmwVWZGiRJqzgTURVWeF9",
"name": "go-ipfs-addr",
"version": "0.1.1"
}
],
"gxVersion": "0.10.0",
......
......@@ -4,7 +4,7 @@ import (
"errors"
"fmt"
iaddr "github.com/ipfs/go-ipfs/thirdparty/ipfsaddr"
iaddr "gx/ipfs/QmeS8cCKawUwejVrsBtmC1toTXmwVWZGiRJqzgTURVWeF9/go-ipfs-addr"
)
// DefaultBootstrapAddresses are the hardcoded bootstrap addresses
......
package ipfsaddr
import (
"errors"
ma "gx/ipfs/QmXY77cVe7rVRQXZZQRioukUM7aRW3BTcAgJe12MCtb3Ji/go-multiaddr"
path "github.com/ipfs/go-ipfs/path"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
circuit "gx/ipfs/Qmf7GSJ4omRJsvA9uzTqzbnVhq4RWLPzjzW4xJzUta4dKE/go-libp2p-circuit"
)
var log = logging.Logger("ipfsaddr")
// ErrInvalidAddr signals an address is not a valid IPFS address.
var ErrInvalidAddr = errors.New("invalid IPFS address")
type IPFSAddr interface {
ID() peer.ID
Multiaddr() ma.Multiaddr
Transport() ma.Multiaddr
String() string
Equal(b interface{}) bool
}
type ipfsAddr struct {
ma ma.Multiaddr
id peer.ID
}
func (a ipfsAddr) ID() peer.ID {
return a.id
}
func (a ipfsAddr) Multiaddr() ma.Multiaddr {
return a.ma
}
func (a ipfsAddr) Transport() ma.Multiaddr {
return Transport(a)
}
func (a ipfsAddr) String() string {
return a.ma.String()
}
func (a ipfsAddr) Equal(b interface{}) bool {
if ib, ok := b.(IPFSAddr); ok {
return a.Multiaddr().Equal(ib.Multiaddr())
}
if mb, ok := b.(ma.Multiaddr); ok {
return a.Multiaddr().Equal(mb)
}
return false
}
// ParseString parses a string representation of an address into an IPFSAddr
func ParseString(str string) (a IPFSAddr, err error) {
if str == "" {
return nil, ErrInvalidAddr
}
m, err := ma.NewMultiaddr(str)
if err != nil {
return nil, err
}
return ParseMultiaddr(m)
}
// ParseMultiaddr parses a multiaddr into an IPFSAddr
func ParseMultiaddr(m ma.Multiaddr) (a IPFSAddr, err error) {
// never panic.
defer func() {
if r := recover(); r != nil {
log.Debug("recovered from panic: ", r)
a = nil
err = ErrInvalidAddr
}
}()
if m == nil {
return nil, ErrInvalidAddr
}
// make sure it's an IPFS addr
parts := ma.Split(m)
if len(parts) < 1 {
return nil, ErrInvalidAddr
}
ipfspart := parts[len(parts)-1] // last part
if ipfspart.Protocols()[0].Code != ma.P_IPFS {
return nil, ErrInvalidAddr
}
// make sure 'ipfs id' parses as a peer.ID
peerIdParts := path.SplitList(ipfspart.String())
peerIdStr := peerIdParts[len(peerIdParts)-1]
id, err := peer.IDB58Decode(peerIdStr)
if err != nil {
return nil, err
}
return ipfsAddr{ma: m, id: id}, nil
}
func Transport(iaddr IPFSAddr) (maddr ma.Multiaddr) {
maddr = iaddr.Multiaddr()
// /ipfs/QmId is part of the transport address for p2p-circuit
// TODO clean up the special case
// we need a consistent way of composing and consumig multiaddrs
// so that we don't have to do this
_, err := maddr.ValueForProtocol(circuit.P_CIRCUIT)
if err == nil {
return maddr
}
split := ma.Split(maddr)
maddr = ma.Join(split[:len(split)-1]...)
return
}
package ipfsaddr
import (
"testing"
path "github.com/ipfs/go-ipfs/path"
ma "gx/ipfs/QmXY77cVe7rVRQXZZQRioukUM7aRW3BTcAgJe12MCtb3Ji/go-multiaddr"
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
)
var good = []string{
"/ipfs/5dru6bJPUM1B7N69528u49DJiWZnok",
"/ipfs/kTRX47RthhwNzWdi6ggwqjuX",
"/ipfs/QmUCseQWXCSrhf9edzVKTvoj8o8Ts5aXFGNPameZRPJ6uR",
"/ip4/1.2.3.4/tcp/1234/ipfs/5dru6bJPUM1B7N69528u49DJiWZnok",
"/ip4/1.2.3.4/tcp/1234/ipfs/kTRX47RthhwNzWdi6ggwqjuX",
"/ip4/1.2.3.4/tcp/1234/ipfs/QmUCseQWXCSrhf9edzVKTvoj8o8Ts5aXFGNPameZRPJ6uR",
}
var bad = []string{
"5dru6bJPUM1B7N69528u49DJiWZnok", // bad ma
"kTRX47RthhwNzWdi6ggwqjuX", // bad ma
"QmUCseQWXCSrhf9edzVKTvoj8o8Ts5aXFGNPameZRPJ6uR", // bad ma
"ipfs/5dru6bJPUM1B7N69528u49DJiWZnok", // bad ma
"ipfs/kTRX47RthhwNzWdi6ggwqjuX", // bad ma
"ipfs/QmUCseQWXCSrhf9edzVKTvoj8o8Ts5aXFGNPameZRPJ6uR", // bad ma
"/ipfs/5dru6bJPUM1B7N69528u49DJiWZno", // bad mh
"/ipfs/kTRX47RthhwNzWdi6ggwqju", // bad mh
"/ipfs/QmUCseQWXCSrhf9edzVKTvj8o8Ts5aXFGNPameZRPJ6uR", // bad mh
"/ipfs/QmUCseQWXCSrhf9edzVKTvoj8o8Ts5aXFGNPameZRPJ6uR/tcp/1234", // ipfs not last
"/ip4/1.2.3.4/tcp/ipfs/5dru6bJPUM1B7N69528u49DJiWZnok", // bad tcp part
"/ip4/tcp/1234/ipfs/kTRX47RthhwNzWdi6ggwqjuX", // bad ip part
"/ip4/1.2.3.4/tcp/1234/ipfs", // no id
"/ip4/1.2.3.4/tcp/1234/ipfs/", // no id
}
func newMultiaddr(t *testing.T, s string) ma.Multiaddr {
maddr, err := ma.NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
return maddr
}
func TestParseStringGood(t *testing.T) {
for _, g := range good {
if _, err := ParseString(g); err != nil {
t.Error("failed to parse", g, err)
}
}
}
func TestParseStringBad(t *testing.T) {
for _, b := range bad {
if _, err := ParseString(b); err == nil {
t.Error("succeeded in parsing", b)
}
}
}
func TestParseMultiaddrGood(t *testing.T) {
for _, g := range good {
if _, err := ParseMultiaddr(newMultiaddr(t, g)); err != nil {
t.Error("failed to parse", g, err)
}
}
}
func TestParseMultiaddrBad(t *testing.T) {
for _, b := range bad {
m, err := ma.NewMultiaddr(b)
if err != nil {
continue // skip these.
}
if _, err := ParseMultiaddr(m); err == nil {
t.Error("succeeded in parsing", m)
}
}
}
func TestIDMatches(t *testing.T) {
for _, g := range good {
a, err := ParseString(g)
if err != nil {
t.Error("failed to parse", g, err)
continue
}
sp := path.SplitList(g)
sid := sp[len(sp)-1]
id, err := peer.IDB58Decode(sid)
if err != nil {
t.Error("failed to parse", sid, err)
continue
}
if a.ID() != id {
t.Error("not equal", a.ID(), id)
}
}
}
func TestMultiaddrMatches(t *testing.T) {
for _, g := range good {
a, err := ParseString(g)
if err != nil {
t.Error("failed to parse", g, err)
continue
}
m := newMultiaddr(t, g)
if !a.Multiaddr().Equal(m) {
t.Error("not equal", a.Multiaddr(), m)
}
}
}
func TestTransport(t *testing.T) {
for _, g := range good {
a, err := ParseString(g)
if err != nil {
t.Error("failed to parse", g, err)
continue
}
m := newMultiaddr(t, g)
split := ma.Split(m)
m = ma.Join(split[:len(split)-1]...)
if a.Multiaddr().Equal(m) {
t.Error("should not be equal", a.Multiaddr(), m)
}
if !Transport(a).Equal(m) {
t.Error("should be equal", Transport(a), m)
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment