add a helper function to go directly from a string to an AddrInfo (#184)

* add a helper function to go directly from a string to an AddrInfo

* Update peer/addrinfo.go

Co-authored-by: Will <will.scott@protocol.ai>

Co-authored-by: Will <will.scott@protocol.ai>
This commit is contained in:
Whyrusleeping 2021-02-22 09:55:28 -08:00 committed by GitHub
parent ca38e17432
commit 11f3c3ac0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ package peer
import (
"fmt"
"github.com/multiformats/go-multiaddr"
ma "github.com/multiformats/go-multiaddr"
)
@ -61,6 +62,16 @@ func SplitAddr(m ma.Multiaddr) (transport ma.Multiaddr, id ID) {
return transport, id
}
// AddrInfoFromString builds an AddrInfo from the string representation of a Multiaddr
func AddrInfoFromString(s string) (*AddrInfo, error) {
a, err := multiaddr.NewMultiaddr(s)
if err != nil {
return nil, err
}
return AddrInfoFromP2pAddr(a)
}
// AddrInfoFromP2pAddr converts a Multiaddr to an AddrInfo.
func AddrInfoFromP2pAddr(m ma.Multiaddr) (*AddrInfo, error) {
transport, id := SplitAddr(m)