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

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

10
	"github.com/ipfs/go-ipfs/core"
Łukasz Magiera's avatar
Łukasz Magiera committed
11
	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
12
	caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13 14
	"github.com/ipfs/go-ipfs/keystore"
	"github.com/ipfs/go-ipfs/namesys"
Steven Allen's avatar
Steven Allen committed
15
	ipath "gx/ipfs/QmQmMu1vsgsjxyB8tzrA6ZTCTCLDLVaXMb4Q57r2v886Sx/go-path"
Łukasz Magiera's avatar
Łukasz Magiera committed
16

17
	"gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
Steven Allen's avatar
Steven Allen committed
18
	"gx/ipfs/QmQqPdGQfsfQTpRUUzgD3d2RzWHJffZsSqwopnZ2BkiezW/go-ipfs-routing/offline"
Steven Allen's avatar
Steven Allen committed
19
	"gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
Łukasz Magiera's avatar
Łukasz Magiera committed
20 21
)

22
type NameAPI CoreAPI
Łukasz Magiera's avatar
Łukasz Magiera committed
23

24 25 26 27 28
type ipnsEntry struct {
	name  string
	value coreiface.Path
}

29
// Name returns the ipnsEntry name.
30 31 32 33
func (e *ipnsEntry) Name() string {
	return e.name
}

34
// Value returns the ipnsEntry value.
35 36 37 38
func (e *ipnsEntry) Value() coreiface.Path {
	return e.value
}

39
// Publish announces new IPNS name and returns the new IPNS entry.
40
func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopts.NamePublishOption) (coreiface.IpnsEntry, error) {
41 42 43 44
	options, err := caopts.NamePublishOptions(opts...)
	if err != nil {
		return nil, err
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
45 46 47
	n := api.node

	if !n.OnlineMode() {
48 49 50
		if !options.AllowOffline {
			return nil, coreiface.ErrOffline
		}
Łukasz Magiera's avatar
Łukasz Magiera committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		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")
	}

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

66
	k, err := keylookup(n, options.Key)
Łukasz Magiera's avatar
Łukasz Magiera committed
67 68 69 70
	if err != nil {
		return nil, err
	}

71 72 73 74
	if options.TTL != nil {
		ctx = context.WithValue(ctx, "ipns-publish-ttl", *options.TTL)
	}

75
	eol := time.Now().Add(options.ValidTime)
Łukasz Magiera's avatar
Łukasz Magiera committed
76 77 78 79 80 81 82 83 84 85
	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
	}

86 87 88
	return &ipnsEntry{
		name:  pid.Pretty(),
		value: p,
Łukasz Magiera's avatar
Łukasz Magiera committed
89 90 91
	}, nil
}

92 93
// Resolve attempts to resolve the newest version of the specified name and
// returns its path.
94 95 96 97 98 99
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
100 101 102 103 104 105 106 107 108 109 110
	n := api.node

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

	var resolver namesys.Resolver = n.Namesys

111
	if options.Local && !options.Cache {
Łukasz Magiera's avatar
Łukasz Magiera committed
112 113 114
		return nil, errors.New("cannot specify both local and nocache")
	}

115
	if options.Local {
116
		offroute := offline.NewOfflineRouter(n.Repo.Datastore(), n.RecordValidator)
Steven Allen's avatar
Steven Allen committed
117
		resolver = namesys.NewIpnsResolver(offroute)
Łukasz Magiera's avatar
Łukasz Magiera committed
118 119
	}

120
	if !options.Cache {
Łukasz Magiera's avatar
Łukasz Magiera committed
121 122 123 124 125 126 127
		resolver = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), 0)
	}

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

128
	output, err := resolver.Resolve(ctx, name, options.ResolveOpts...)
Łukasz Magiera's avatar
Łukasz Magiera committed
129 130 131 132
	if err != nil {
		return nil, err
	}

133
	return coreiface.ParsePath(output.String())
Łukasz Magiera's avatar
Łukasz Magiera committed
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 162 163 164 165 166 167 168 169 170
}

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")
}