name.go 3.26 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1 2 3 4 5 6 7 8 9 10 11
package coreapi

import (
	"context"
	"errors"
	"fmt"
	"strings"
	"time"

	core "github.com/ipfs/go-ipfs/core"
	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
12
	caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
Łukasz Magiera's avatar
Łukasz Magiera committed
13 14 15 16 17 18 19 20 21
	keystore "github.com/ipfs/go-ipfs/keystore"
	namesys "github.com/ipfs/go-ipfs/namesys"
	ipath "github.com/ipfs/go-ipfs/path"
	offline "github.com/ipfs/go-ipfs/routing/offline"

	peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
	crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
)

22 23 24 25
type NameAPI struct {
	*CoreAPI
	*caopts.NameOptions
}
Łukasz Magiera's avatar
Łukasz Magiera committed
26

27 28 29 30 31
func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopts.NamePublishOption) (*coreiface.IpnsEntry, error) {
	options, err := caopts.NamePublishOptions(opts...)
	if err != nil {
		return nil, err
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	n := api.node

	if !n.OnlineMode() {
		err := n.SetupOfflineRouting()
		if err != nil {
			return nil, err
		}
	}

	if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
		return nil, errors.New("cannot manually publish while IPNS is mounted")
	}

	if n.Identity == "" {
		return nil, errors.New("identity not loaded")
	}

	pth, err := ipath.ParsePath(p.String())
	if err != nil {
		return nil, err
	}

54
	k, err := keylookup(n, options.Key)
Łukasz Magiera's avatar
Łukasz Magiera committed
55 56 57 58
	if err != nil {
		return nil, err
	}

59
	eol := time.Now().Add(options.ValidTime)
Łukasz Magiera's avatar
Łukasz Magiera committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	err = n.Namesys.PublishWithEOL(ctx, k, pth, eol)
	if err != nil {
		return nil, err
	}

	pid, err := peer.IDFromPrivateKey(k)
	if err != nil {
		return nil, err
	}

	return &coreiface.IpnsEntry{
		Name:  pid.Pretty(),
		Value: p,
	}, nil
}

76 77 78 79 80 81
func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.NameResolveOption) (coreiface.Path, error) {
	options, err := caopts.NameResolveOptions(opts...)
	if err != nil {
		return nil, err
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
82 83 84 85 86 87 88 89 90 91 92
	n := api.node

	if !n.OnlineMode() {
		err := n.SetupOfflineRouting()
		if err != nil {
			return nil, err
		}
	}

	var resolver namesys.Resolver = n.Namesys

93
	if options.Local && options.Nocache {
Łukasz Magiera's avatar
Łukasz Magiera committed
94 95 96
		return nil, errors.New("cannot specify both local and nocache")
	}

97
	if options.Local {
Łukasz Magiera's avatar
Łukasz Magiera committed
98 99 100 101
		offroute := offline.NewOfflineRouter(n.Repo.Datastore(), n.PrivateKey)
		resolver = namesys.NewRoutingResolver(offroute, 0)
	}

102
	if options.Nocache {
Łukasz Magiera's avatar
Łukasz Magiera committed
103 104 105 106
		resolver = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), 0)
	}

	depth := 1
107
	if options.Recursive {
Łukasz Magiera's avatar
Łukasz Magiera committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
		depth = namesys.DefaultDepthLimit
	}

	if !strings.HasPrefix(name, "/ipns/") {
		name = "/ipns/" + name
	}

	output, err := resolver.ResolveN(ctx, name, depth)
	if err != nil {
		return nil, err
	}

	return &path{path: output}, nil
}

func (api *NameAPI) core() coreiface.CoreAPI {
124
	return api.CoreAPI
Łukasz Magiera's avatar
Łukasz Magiera committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
}

func keylookup(n *core.IpfsNode, k string) (crypto.PrivKey, error) {
	res, err := n.GetKey(k)
	if res != nil {
		return res, nil
	}

	if err != nil && err != keystore.ErrNoSuchKey {
		return nil, err
	}

	keys, err := n.Repo.Keystore().List()
	if err != nil {
		return nil, err
	}

	for _, key := range keys {
		privKey, err := n.Repo.Keystore().Get(key)
		if err != nil {
			return nil, err
		}

		pubKey := privKey.GetPublic()

		pid, err := peer.IDFromPublicKey(pubKey)
		if err != nil {
			return nil, err
		}

		if pid.Pretty() == k {
			return privKey, nil
		}
	}

	return nil, fmt.Errorf("no key by the given name or PeerID was found")
}