forked from ipfs/kubo
/
name.go
181 lines (144 loc) · 3.83 KB
/
name.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package coreapi
import (
"context"
"fmt"
"strings"
"time"
"github.com/ipfs/go-ipfs/keystore"
"github.com/ipfs/go-ipfs/namesys"
ipath "gx/ipfs/QmQAgv6Gaoe2tQpcabqwKXKChp2MZ7i3UXv9DqTTaxCaTR/go-path"
"gx/ipfs/QmTW4SdgBWq9GjsBsHeUx8WuGxzhgzAf88UMH2w62PC8yK/go-libp2p-crypto"
ci "gx/ipfs/QmTW4SdgBWq9GjsBsHeUx8WuGxzhgzAf88UMH2w62PC8yK/go-libp2p-crypto"
coreiface "gx/ipfs/QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6/interface-go-ipfs-core"
caopts "gx/ipfs/QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6/interface-go-ipfs-core/options"
"gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
)
type NameAPI CoreAPI
type ipnsEntry struct {
name string
value coreiface.Path
}
// Name returns the ipnsEntry name.
func (e *ipnsEntry) Name() string {
return e.name
}
// Value returns the ipnsEntry value.
func (e *ipnsEntry) Value() coreiface.Path {
return e.value
}
// Publish announces new IPNS name and returns the new IPNS entry.
func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopts.NamePublishOption) (coreiface.IpnsEntry, error) {
if err := api.checkPublishAllowed(); err != nil {
return nil, err
}
options, err := caopts.NamePublishOptions(opts...)
if err != nil {
return nil, err
}
err = api.checkOnline(options.AllowOffline)
if err != nil {
return nil, err
}
pth, err := ipath.ParsePath(p.String())
if err != nil {
return nil, err
}
k, err := keylookup(api.privateKey, api.repo.Keystore(), options.Key)
if err != nil {
return nil, err
}
if options.TTL != nil {
ctx = context.WithValue(ctx, "ipns-publish-ttl", *options.TTL)
}
eol := time.Now().Add(options.ValidTime)
err = api.namesys.PublishWithEOL(ctx, k, pth, eol)
if err != nil {
return nil, err
}
pid, err := peer.IDFromPrivateKey(k)
if err != nil {
return nil, err
}
return &ipnsEntry{
name: pid.Pretty(),
value: p,
}, nil
}
func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.NameResolveOption) (<-chan coreiface.IpnsResult, error) {
options, err := caopts.NameResolveOptions(opts...)
if err != nil {
return nil, err
}
err = api.checkOnline(true)
if err != nil {
return nil, err
}
var resolver namesys.Resolver = api.namesys
if !options.Cache {
resolver = namesys.NewNameSystem(api.routing, api.repo.Datastore(), 0)
}
if !strings.HasPrefix(name, "/ipns/") {
name = "/ipns/" + name
}
out := make(chan coreiface.IpnsResult)
go func() {
defer close(out)
for res := range resolver.ResolveAsync(ctx, name, options.ResolveOpts...) {
p, _ := coreiface.ParsePath(res.Path.String())
select {
case out <- coreiface.IpnsResult{Path: p, Err: res.Err}:
case <-ctx.Done():
return
}
}
}()
return out, nil
}
// Resolve attempts to resolve the newest version of the specified name and
// returns its path.
func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.NameResolveOption) (coreiface.Path, error) {
results, err := api.Search(ctx, name, opts...)
if err != nil {
return nil, err
}
err = coreiface.ErrResolveFailed
var p coreiface.Path
for res := range results {
p, err = res.Path, res.Err
if err != nil {
break
}
}
return p, err
}
func keylookup(self ci.PrivKey, kstore keystore.Keystore, k string) (crypto.PrivKey, error) {
if k == "self" {
return self, nil
}
res, err := kstore.Get(k)
if res != nil {
return res, nil
}
if err != nil && err != keystore.ErrNoSuchKey {
return nil, err
}
keys, err := kstore.List()
if err != nil {
return nil, err
}
for _, key := range keys {
privKey, err := kstore.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")
}