Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/commands/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
30 changes: 19 additions & 11 deletions core/coreapi/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,55 @@ 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
}

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
}

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])
Expand Down
2 changes: 1 addition & 1 deletion core/corehttp/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/kubo-as-a-library/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down