-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix storeiface, publisher multiaddrs
- Loading branch information
Showing
6 changed files
with
95 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,11 @@ | ||
package ipniculib | ||
|
||
import ( | ||
"crypto/sha256" | ||
|
||
"github.com/ipld/go-ipld-prime/codec/dagjson" | ||
"github.com/ipld/go-ipld-prime/datamodel" | ||
"golang.org/x/xerrors" | ||
cidlink "github.com/ipld/go-ipld-prime/linking/cid" | ||
) | ||
|
||
func NodeToLink(node datamodel.Node, lp datamodel.LinkPrototype) (datamodel.Link, error) { | ||
hasher := sha256.New() | ||
err := dagjson.Encode(node, hasher) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to encode: %w", err) | ||
} | ||
return lp.BuildLink(hasher.Sum(nil)), nil | ||
linkSystem := cidlink.DefaultLinkSystem() | ||
return linkSystem.ComputeLink(lp, node) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package urltomultiaddr | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"net/url" | ||
|
||
"github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
const protocol = "tcp" | ||
|
||
func UrlToMultiaddr(urlStr string) (multiaddr.Multiaddr, error) { | ||
// Parse the URL | ||
parsedURL, err := url.Parse(urlStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Extract the host and port | ||
host := parsedURL.Hostname() | ||
port := parsedURL.Port() | ||
if port == "" { | ||
// Default port based on the scheme | ||
switch parsedURL.Scheme { | ||
case "https": | ||
port = "443" | ||
case "http": | ||
port = "80" | ||
default: | ||
return nil, errors.New("invalid URL scheme") | ||
} | ||
} | ||
|
||
// Determine if the host is an IP address or a DNS name | ||
var addrStr string | ||
if ip := net.ParseIP(host); ip != nil { | ||
// It's an IP address, check if it's IPv4 or IPv6 | ||
if ip.To4() != nil { | ||
// IPv4 | ||
addrStr = fmt.Sprintf("/ip4/%s/%s/%s/%s", ip.String(), protocol, port, parsedURL.Scheme) | ||
} else { | ||
// IPv6 | ||
addrStr = fmt.Sprintf("/ip6/%s/%s/%s/%s", ip.String(), protocol, port, parsedURL.Scheme) | ||
} | ||
} else { | ||
// It's a DNS name | ||
addrStr = fmt.Sprintf("/dns/%s/%s/%s/%s", host, protocol, port, parsedURL.Scheme) | ||
} | ||
|
||
return multiaddr.NewMultiaddr(addrStr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters