diff --git a/core/commands/routing.go b/core/commands/routing.go index d0f13c556af..a6f274a4d9d 100644 --- a/core/commands/routing.go +++ b/core/commands/routing.go @@ -14,6 +14,7 @@ import ( ipld "github.com/ipfs/go-ipld-format" dag "github.com/ipfs/go-merkledag" path "github.com/ipfs/go-path" + ipath "github.com/ipfs/interface-go-ipfs-core/path" peer "github.com/libp2p/go-libp2p/core/peer" routing "github.com/libp2p/go-libp2p/core/routing" ) @@ -369,7 +370,7 @@ Different key types can specify other 'best' rules. return err } - r, err := api.Routing().Get(req.Context, req.Arguments[0]) + r, err := api.Routing().Get(req.Context, ipath.New(req.Arguments[0])) if err != nil { return err } @@ -429,7 +430,7 @@ identified by QmFoo. return err } - err = api.Routing().Put(req.Context, req.Arguments[0], data) + err = api.Routing().Put(req.Context, ipath.New(req.Arguments[0]), data) if err != nil { return err } diff --git a/core/coreapi/routing.go b/core/coreapi/routing.go index 78f21c42997..b70494d6217 100644 --- a/core/coreapi/routing.go +++ b/core/coreapi/routing.go @@ -2,21 +2,22 @@ package coreapi import ( "context" - "errors" + "fmt" "github.com/ipfs/go-path" coreiface "github.com/ipfs/interface-go-ipfs-core" + ipath "github.com/ipfs/interface-go-ipfs-core/path" peer "github.com/libp2p/go-libp2p/core/peer" ) type RoutingAPI CoreAPI -func (r *RoutingAPI) Get(ctx context.Context, key string) ([]byte, error) { +func (r *RoutingAPI) Get(ctx context.Context, p ipath.Path) ([]byte, error) { if !r.nd.IsOnline { return nil, coreiface.ErrOffline } - dhtKey, err := normalizeKey(key) + dhtKey, err := normalizeKey(p) if err != nil { return nil, err } @@ -24,12 +25,12 @@ func (r *RoutingAPI) Get(ctx context.Context, key string) ([]byte, error) { return r.routing.GetValue(ctx, dhtKey) } -func (r *RoutingAPI) Put(ctx context.Context, key string, value []byte) error { +func (r *RoutingAPI) Put(ctx context.Context, p ipath.Path, value []byte) error { if !r.nd.IsOnline { return coreiface.ErrOffline } - dhtKey, err := normalizeKey(key) + dhtKey, err := normalizeKey(p) if err != nil { return err } @@ -37,12 +38,19 @@ func (r *RoutingAPI) Put(ctx context.Context, key string, value []byte) error { return r.routing.PutValue(ctx, dhtKey, value) } -func normalizeKey(s string) (string, error) { - parts := path.SplitList(s) - if len(parts) != 3 || - parts[0] != "" || - !(parts[1] == "ipns" || parts[1] == "pk") { - return "", errors.New("invalid key") +func normalizeKey(p ipath.Path) (string, error) { + if err := p.IsValid(); err != nil { + return "", fmt.Errorf("invalid key: %w", err) + } + + ns := p.Namespace() + if ns != "ipns" && ns != "pk" { + return "", fmt.Errorf("key has unexpected namespace: %s", ns) + } + + parts := path.SplitList(p.String()) + if len(parts) != 3 { + return "", fmt.Errorf("key has unexpected number of parts: %d, expected 3", len(parts)) } k, err := peer.Decode(parts[2]) diff --git a/core/corehttp/gateway.go b/core/corehttp/gateway.go index c20ab6e4a4f..2dc0d4aae52 100644 --- a/core/corehttp/gateway.go +++ b/core/corehttp/gateway.go @@ -175,7 +175,7 @@ func (gw *gatewayAPI) GetBlock(ctx context.Context, cid cid.Cid) (blocks.Block, } func (gw *gatewayAPI) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte, error) { - return gw.api.Routing().Get(ctx, "/ipns/"+c.String()) + return gw.api.Routing().Get(ctx, path.New("/ipns/"+c.String())) } func (gw *gatewayAPI) GetDNSLinkRecord(ctx context.Context, hostname string) (path.Path, error) { diff --git a/docs/examples/kubo-as-a-library/go.mod b/docs/examples/kubo-as-a-library/go.mod index a37a71c3f21..7df1b8c44f9 100644 --- a/docs/examples/kubo-as-a-library/go.mod +++ b/docs/examples/kubo-as-a-library/go.mod @@ -8,7 +8,7 @@ replace github.com/ipfs/kubo => ./../../.. require ( github.com/ipfs/go-libipfs v0.4.1-0.20230208022905-378aaf777cf9 - github.com/ipfs/interface-go-ipfs-core v0.10.1-0.20230208074154-2de38ba2c272 + github.com/ipfs/interface-go-ipfs-core v0.10.1-0.20230208075953-4d6f9f04e9fe github.com/ipfs/kubo v0.0.0-00010101000000-000000000000 github.com/libp2p/go-libp2p v0.24.2 github.com/multiformats/go-multiaddr v0.8.0 diff --git a/docs/examples/kubo-as-a-library/go.sum b/docs/examples/kubo-as-a-library/go.sum index 68cfb8a647b..82f3ea99230 100644 --- a/docs/examples/kubo-as-a-library/go.sum +++ b/docs/examples/kubo-as-a-library/go.sum @@ -593,8 +593,8 @@ github.com/ipfs/go-unixfsnode v1.5.1/go.mod h1:ed79DaG9IEuZITJVQn4U6MZDftv6I3ygU github.com/ipfs/go-verifcid v0.0.1/go.mod h1:5Hrva5KBeIog4A+UpqlaIU+DEstipcJYQQZc0g37pY0= github.com/ipfs/go-verifcid v0.0.2 h1:XPnUv0XmdH+ZIhLGKg6U2vaPaRDXb9urMyNVCE7uvTs= github.com/ipfs/go-verifcid v0.0.2/go.mod h1:40cD9x1y4OWnFXbLNJYRe7MpNvWlMn3LZAG5Wb4xnPU= -github.com/ipfs/interface-go-ipfs-core v0.10.1-0.20230208074154-2de38ba2c272 h1:1+3BC3Ghy9G5PqBRFcL64/mdvjS47wOdsF2tW2PHt3g= -github.com/ipfs/interface-go-ipfs-core v0.10.1-0.20230208074154-2de38ba2c272/go.mod h1:XRUw5unjgKwLvOucCJGOUOCP+AymSgpIqeta1wYQ2IE= +github.com/ipfs/interface-go-ipfs-core v0.10.1-0.20230208075953-4d6f9f04e9fe h1:OHSXfSeqHsNfM4c8mBFgCphqb05eEkC3Z6DB1JH0+EE= +github.com/ipfs/interface-go-ipfs-core v0.10.1-0.20230208075953-4d6f9f04e9fe/go.mod h1:XRUw5unjgKwLvOucCJGOUOCP+AymSgpIqeta1wYQ2IE= github.com/ipld/edelweiss v0.2.0 h1:KfAZBP8eeJtrLxLhi7r3N0cBCo7JmwSRhOJp3WSpNjk= github.com/ipld/edelweiss v0.2.0/go.mod h1:FJAzJRCep4iI8FOFlRriN9n0b7OuX3T/S9++NpBDmA4= github.com/ipld/go-car v0.5.0 h1:kcCEa3CvYMs0iE5BzD5sV7O2EwMiCIp3uF8tA6APQT8= diff --git a/go.mod b/go.mod index cc8a644624b..6958ebd6c8f 100644 --- a/go.mod +++ b/go.mod @@ -58,7 +58,7 @@ require ( github.com/ipfs/go-unixfs v0.4.2 github.com/ipfs/go-unixfsnode v1.5.1 github.com/ipfs/go-verifcid v0.0.2 - github.com/ipfs/interface-go-ipfs-core v0.10.1-0.20230208074154-2de38ba2c272 + github.com/ipfs/interface-go-ipfs-core v0.10.1-0.20230208075953-4d6f9f04e9fe github.com/ipld/go-car v0.5.0 github.com/ipld/go-car/v2 v2.5.1 github.com/ipld/go-codec-dagpb v1.5.0 diff --git a/go.sum b/go.sum index 80513287427..611d882b4f7 100644 --- a/go.sum +++ b/go.sum @@ -619,8 +619,8 @@ github.com/ipfs/go-unixfsnode v1.5.1/go.mod h1:ed79DaG9IEuZITJVQn4U6MZDftv6I3ygU github.com/ipfs/go-verifcid v0.0.1/go.mod h1:5Hrva5KBeIog4A+UpqlaIU+DEstipcJYQQZc0g37pY0= github.com/ipfs/go-verifcid v0.0.2 h1:XPnUv0XmdH+ZIhLGKg6U2vaPaRDXb9urMyNVCE7uvTs= github.com/ipfs/go-verifcid v0.0.2/go.mod h1:40cD9x1y4OWnFXbLNJYRe7MpNvWlMn3LZAG5Wb4xnPU= -github.com/ipfs/interface-go-ipfs-core v0.10.1-0.20230208074154-2de38ba2c272 h1:1+3BC3Ghy9G5PqBRFcL64/mdvjS47wOdsF2tW2PHt3g= -github.com/ipfs/interface-go-ipfs-core v0.10.1-0.20230208074154-2de38ba2c272/go.mod h1:XRUw5unjgKwLvOucCJGOUOCP+AymSgpIqeta1wYQ2IE= +github.com/ipfs/interface-go-ipfs-core v0.10.1-0.20230208075953-4d6f9f04e9fe h1:OHSXfSeqHsNfM4c8mBFgCphqb05eEkC3Z6DB1JH0+EE= +github.com/ipfs/interface-go-ipfs-core v0.10.1-0.20230208075953-4d6f9f04e9fe/go.mod h1:XRUw5unjgKwLvOucCJGOUOCP+AymSgpIqeta1wYQ2IE= github.com/ipld/edelweiss v0.2.0 h1:KfAZBP8eeJtrLxLhi7r3N0cBCo7JmwSRhOJp3WSpNjk= github.com/ipld/edelweiss v0.2.0/go.mod h1:FJAzJRCep4iI8FOFlRriN9n0b7OuX3T/S9++NpBDmA4= github.com/ipld/go-car v0.5.0 h1:kcCEa3CvYMs0iE5BzD5sV7O2EwMiCIp3uF8tA6APQT8=