coreapi name: switch publish/resolve to coreapi
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Oct 4, 2018 at 22:11 UTC
58c9de9e638e470e387ab35815154efa18c53c68
2 files changed
+42
-150
core/commands/name/ipns.go
+16
-37
@@ -9,11 +9,10 @@ import (
9
10
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
11
e "github.com/ipfs/go-ipfs/core/commands/e"
12
- namesys "github.com/ipfs/go-ipfs/namesys"
12
+ options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13
nsopts "github.com/ipfs/go-ipfs/namesys/opts"
14
15
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
16
- offline "gx/ipfs/QmScZySgru9jaoDa12sSfvh21sWbqF5eXkieTmJzAHJXkQ/go-ipfs-routing/offline"
16
cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
17
logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log"
18
path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
@@ -80,44 +79,21 @@ Resolve the value of a dnslink:
79
cmdkit.StringOption(dhtTimeoutOptionName, "dhtt", "Max time to collect values during DHT resolution eg \"30s\". Pass 0 for no timeout."),
80
},
81
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
83
- n, err := cmdenv.GetNode(env)
82
+ api, err := cmdenv.GetApi(env)
83
if err != nil {
84
return err
85
}
86
88
- if !n.OnlineMode() {
89
- err := n.SetupOfflineRouting()
90
- if err != nil {
91
- return err
92
- }
93
- }
94
-
87
nocache, _ := req.Options["nocache"].(bool)
88
local, _ := req.Options["local"].(bool)
89
98
- // default to nodes namesys resolver
99
- var resolver namesys.Resolver = n.Namesys
100
-
101
- if local && nocache {
102
- return errors.New("cannot specify both local and nocache")
103
- }
104
-
105
- if local {
106
- offroute := offline.NewOfflineRouter(n.Repo.Datastore(), n.RecordValidator)
107
- resolver = namesys.NewIpnsResolver(offroute)
108
- }
109
-
110
- if nocache {
111
- resolver = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), 0)
112
- }
113
-
90
var name string
91
if len(req.Arguments) == 0 {
116
- if n.Identity == "" {
117
- return errors.New("identity not loaded")
92
+ self, err := api.Key().Self(req.Context)
93
+ if err != nil {
94
+ return err
95
}
119
- name = n.Identity.Pretty()
120
-
96
+ name = self.ID().Pretty()
97
} else {
98
name = req.Arguments[0]
99
}
@@ -126,12 +102,16 @@ Resolve the value of a dnslink:
102
rc, rcok := req.Options[dhtRecordCountOptionName].(int)
103
dhtt, dhttok := req.Options[dhtTimeoutOptionName].(string)
104
129
- var ropts []nsopts.ResolveOpt
105
+ opts := []options.NameResolveOption{
106
+ options.Name.Local(local),
107
+ options.Name.Cache(!nocache),
108
+ }
109
+
110
if !recursive {
131
- ropts = append(ropts, nsopts.Depth(1))
111
+ opts = append(opts, options.Name.ResolveOption(nsopts.Depth(1)))
112
}
113
if rcok {
134
- ropts = append(ropts, nsopts.DhtRecordCount(uint(rc)))
114
+ opts = append(opts, options.Name.ResolveOption(nsopts.DhtRecordCount(uint(rc))))
115
}
116
if dhttok {
117
d, err := time.ParseDuration(dhtt)
@@ -141,20 +121,19 @@ Resolve the value of a dnslink:
121
if d < 0 {
122
return errors.New("DHT timeout value must be >= 0")
123
}
144
- ropts = append(ropts, nsopts.DhtTimeout(d))
124
+ opts = append(opts, options.Name.ResolveOption(nsopts.DhtTimeout(d)))
125
}
126
127
if !strings.HasPrefix(name, "/ipns/") {
128
name = "/ipns/" + name
129
}
130
151
- output, err := resolver.Resolve(req.Context, name, ropts...)
131
+ output, err := api.Name().Resolve(req.Context, name, opts...)
132
if err != nil {
133
return err
134
}
135
156
- // TODO: better errors (in the case of not finding the name, we get "failed to find any peer in table")
157
- return cmds.EmitOnce(res, &ResolvedPath{output})
136
+ return cmds.EmitOnce(res, &ResolvedPath{path.FromString(output.String())})
137
},
138
Encoders: cmds.EncoderMap{
139
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
core/commands/name/publish.go
+26
-113
@@ -1,28 +1,22 @@
1
package name
2
3
import (
4
- "context"
4
"errors"
5
"fmt"
6
"io"
7
"time"
8
10
- core "github.com/ipfs/go-ipfs/core"
9
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10
e "github.com/ipfs/go-ipfs/core/commands/e"
13
- keystore "github.com/ipfs/go-ipfs/keystore"
11
+ iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
12
+ options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13
15
- crypto "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
14
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
15
cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
18
- peer "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
19
- path "gx/ipfs/QmcjwUb36Z16NJkvDX6ccXPqsFswo6AsRXynyXcLLCphV2/go-path"
16
)
17
18
var (
19
errAllowOffline = errors.New("can't publish while offline: pass `--allow-offline` to override")
24
- errIpnsMount = errors.New("cannot manually publish while IPNS is mounted")
25
- errIdentityLoad = errors.New("identity not loaded")
20
)
21
22
const (
@@ -90,71 +84,59 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
84
cmdkit.BoolOption(quieterOptionName, "Q", "Write only final hash."),
85
},
86
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
93
- n, err := cmdenv.GetNode(env)
87
+ api, err := cmdenv.GetApi(env)
88
if err != nil {
89
return err
90
}
91
92
allowOffline, _ := req.Options[allowOfflineOptionName].(bool)
99
- if !n.OnlineMode() {
100
- if !allowOffline {
101
- return errAllowOffline
102
- }
103
- err := n.SetupOfflineRouting()
104
- if err != nil {
105
- return err
106
- }
107
- }
108
-
109
- if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
110
- return errIpnsMount
111
- }
112
-
113
- pstr := req.Arguments[0]
114
-
115
- if n.Identity == "" {
116
- return errIdentityLoad
117
- }
118
-
119
- popts := new(publishOpts)
120
-
121
- popts.verifyExists, _ = req.Options[resolveOptionName].(bool)
93
+ kname, _ := req.Options[keyOptionName].(string)
94
123
- validtime, _ := req.Options[lifeTimeOptionName].(string)
124
- d, err := time.ParseDuration(validtime)
95
+ validTimeOpt, _ := req.Options[lifeTimeOptionName].(string)
96
+ validTime, err := time.ParseDuration(validTimeOpt)
97
if err != nil {
98
return fmt.Errorf("error parsing lifetime option: %s", err)
99
}
100
129
- popts.pubValidTime = d
101
+ opts := []options.NamePublishOption{
102
+ options.Name.AllowOffline(allowOffline),
103
+ options.Name.Key(kname),
104
+ options.Name.ValidTime(validTime),
105
+ }
106
131
- ctx := req.Context
107
if ttl, found := req.Options[ttlOptionName].(string); found {
108
d, err := time.ParseDuration(ttl)
109
if err != nil {
110
return err
111
}
112
138
- ctx = context.WithValue(ctx, "ipns-publish-ttl", d)
113
+ opts = append(opts, options.Name.TTL(d))
114
}
115
141
- kname, _ := req.Options[keyOptionName].(string)
142
- k, err := keylookup(n, kname)
116
+ p, err := iface.ParsePath(req.Arguments[0])
117
if err != nil {
118
return err
119
}
120
147
- pth, err := path.ParsePath(pstr)
148
- if err != nil {
149
- return err
121
+ if verifyExists, _ := req.Options[resolveOptionName].(bool); verifyExists {
122
+ _, err := api.ResolveNode(req.Context, p)
123
+ if err != nil {
124
+ return err
125
+ }
126
}
127
152
- output, err := publish(ctx, n, k, pth, popts)
128
+ out, err := api.Name().Publish(req.Context, p, opts...)
129
if err != nil {
130
+ if err == iface.ErrOffline {
131
+ err = errAllowOffline
132
+ }
133
return err
134
}
135
157
- return cmds.EmitOnce(res, output)
136
+ return cmds.EmitOnce(res, &IpnsEntry{
137
+ Name: out.Name(),
138
+ Value: out.Value().String(),
139
+ })
140
},
141
Encoders: cmds.EncoderMap{
142
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
@@ -175,72 +157,3 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
157
},
158
Type: IpnsEntry{},
159
}
178
-
179
-type publishOpts struct {
180
- verifyExists bool
181
- pubValidTime time.Duration
182
-}
183
-
184
-func publish(ctx context.Context, n *core.IpfsNode, k crypto.PrivKey, ref path.Path, opts *publishOpts) (*IpnsEntry, error) {
185
-
186
- if opts.verifyExists {
187
- // verify the path exists
188
- _, err := core.Resolve(ctx, n.Namesys, n.Resolver, ref)
189
- if err != nil {
190
- return nil, err
191
- }
192
- }
193
-
194
- eol := time.Now().Add(opts.pubValidTime)
195
- err := n.Namesys.PublishWithEOL(ctx, k, ref, eol)
196
- if err != nil {
197
- return nil, err
198
- }
199
-
200
- pid, err := peer.IDFromPrivateKey(k)
201
- if err != nil {
202
- return nil, err
203
- }
204
-
205
- return &IpnsEntry{
206
- Name: pid.Pretty(),
207
- Value: ref.String(),
208
- }, nil
209
-}
210
-
211
-func keylookup(n *core.IpfsNode, k string) (crypto.PrivKey, error) {
212
-
213
- res, err := n.GetKey(k)
214
- if res != nil {
215
- return res, nil
216
- }
217
-
218
- if err != nil && err != keystore.ErrNoSuchKey {
219
- return nil, err
220
- }
221
-
222
- keys, err := n.Repo.Keystore().List()
223
- if err != nil {
224
- return nil, err
225
- }
226
-
227
- for _, key := range keys {
228
- privKey, err := n.Repo.Keystore().Get(key)
229
- if err != nil {
230
- return nil, err
231
- }
232
-
233
- pubKey := privKey.GetPublic()
234
-
235
- pid, err := peer.IDFromPublicKey(pubKey)
236
- if err != nil {
237
- return nil, err
238
- }
239
-
240
- if pid.Pretty() == k {
241
- return privKey, nil
242
- }
243
- }
244
-
245
- return nil, fmt.Errorf("no key by the given name or PeerID was found")
246
-}