@cryptotaxi247 / kubo / commits / 416d454b4

core/commands: Make 'ipfs name resolve' IPNS-only

And add a generic 'ipfs resolve' to handle cross-protocol name resolution.

W. Trevor King committed May 7, 2015 at 20:47 UTC 416d454b42ed5301bf38fd93654b173b7f944025
4 files changed +133 -36
core/commands/ipns.go new
+104
@@ -0,0 +1,104 @@
1 +package commands
2 +
3 +import (
4 + "errors"
5 + "io"
6 + "strings"
7 +
8 + cmds "github.com/ipfs/go-ipfs/commands"
9 + namesys "github.com/ipfs/go-ipfs/namesys"
10 + u "github.com/ipfs/go-ipfs/util"
11 +)
12 +
13 +var ipnsCmd = &cmds.Command{
14 + Helptext: cmds.HelpText{
15 + Tagline: "Gets the value currently published at an IPNS name",
16 + ShortDescription: `
17 +IPNS is a PKI namespace, where names are the hashes of public keys, and
18 +the private key enables publishing new (signed) values. In resolve, the
19 +default value of <name> is your own identity public key.
20 +`,
21 + LongDescription: `
22 +IPNS is a PKI namespace, where names are the hashes of public keys, and
23 +the private key enables publishing new (signed) values. In resolve, the
24 +default value of <name> is your own identity public key.
25 +
26 +
27 +Examples:
28 +
29 +Resolve the value of your identity:
30 +
31 + > ipfs name resolve
32 + QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
33 +
34 +Resolve the value of another name:
35 +
36 + > ipfs name resolve QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
37 + QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
38 +
39 +`,
40 + },
41 +
42 + Arguments: []cmds.Argument{
43 + cmds.StringArg("name", false, false, "The IPNS name to resolve. Defaults to your node's peerID.").EnableStdin(),
44 + },
45 + Options: []cmds.Option{
46 + cmds.BoolOption("recursive", "r", "Resolve until the result is not an IPNS name"),
47 + },
48 + Run: func(req cmds.Request, res cmds.Response) {
49 +
50 + n, err := req.Context().GetNode()
51 + if err != nil {
52 + res.SetError(err, cmds.ErrNormal)
53 + return
54 + }
55 +
56 + if !n.OnlineMode() {
57 + err := n.SetupOfflineRouting()
58 + if err != nil {
59 + res.SetError(err, cmds.ErrNormal)
60 + return
61 + }
62 + }
63 +
64 + var name string
65 +
66 + if len(req.Arguments()) == 0 {
67 + if n.Identity == "" {
68 + res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
69 + return
70 + }
71 + name = n.Identity.Pretty()
72 +
73 + } else {
74 + name = req.Arguments()[0]
75 + }
76 +
77 + recursive, _, _ := req.Option("recursive").Bool()
78 + depth := 1
79 + if recursive {
80 + depth = namesys.DefaultDepthLimit
81 + }
82 +
83 + resolver := namesys.NewRoutingResolver(n.Routing)
84 + output, err := resolver.ResolveN(n.Context(), name, depth)
85 + if err != nil {
86 + res.SetError(err, cmds.ErrNormal)
87 + return
88 + }
89 +
90 + // TODO: better errors (in the case of not finding the name, we get "failed to find any peer in table")
91 +
92 + res.SetOutput(&ResolvedPath{output})
93 + },
94 + Marshalers: cmds.MarshalerMap{
95 + cmds.Text: func(res cmds.Response) (io.Reader, error) {
96 + output, ok := res.Output().(*ResolvedPath)
97 + if !ok {
98 + return nil, u.ErrCast()
99 + }
100 + return strings.NewReader(output.Path.String()), nil
101 + },
102 + },
103 + Type: ResolvedPath{},
104 +}
core/commands/name.go
+3 -3
@@ -40,18 +40,18 @@ Publish a <ref> to another public key:
40 Resolve the value of your identity:
41
42 > ipfs name resolve
43 - QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
43 + /ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
44
45 Resolve the value of another name:
46
47 > ipfs name resolve QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
48 - QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
48 + /ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
49
50 `,
51 },
52
53 Subcommands: map[string]*cmds.Command{
54 "publish": publishCmd,
55 - "resolve": resolveCmd,
55 + "resolve": ipnsCmd,
56 },
57 }
core/commands/resolve.go
+24 -33
@@ -1,7 +1,6 @@
1 package commands
2
3 import (
4 - "errors"
4 "io"
5 "strings"
6
@@ -15,40 +14,46 @@ type ResolvedPath struct {
14 Path path.Path
15 }
16
18 -var resolveCmd = &cmds.Command{
17 +var ResolveCmd = &cmds.Command{
18 Helptext: cmds.HelpText{
20 - Tagline: "Gets the value currently published at an IPNS name",
19 + Tagline: "Resolve the value of names to IPFS",
20 ShortDescription: `
22 -IPNS is a PKI namespace, where names are the hashes of public keys, and
23 -the private key enables publishing new (signed) values. In resolve, the
24 -default value of <name> is your own identity public key.
21 +There are a number of mutable name protocols that can link among
22 +themselves and into IPNS. This command accepts any of these
23 +identifiers and resolves them to the referenced item.
24 `,
25 LongDescription: `
27 -IPNS is a PKI namespace, where names are the hashes of public keys, and
28 -the private key enables publishing new (signed) values. In resolve, the
29 -default value of <name> is your own identity public key.
30 -
26 +There are a number of mutable name protocols that can link among
27 +themselves and into IPNS. For example IPNS references can (currently)
28 +point at IPFS object, and DNS links can point at other DNS links, IPNS
29 +entries, or IPFS objects. This command accepts any of these
30 +identifiers and resolves them to the referenced item.
31
32 Examples:
33
34 Resolve the value of your identity:
35
36 - > ipfs name resolve
37 - QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
36 + > ipfs resolve /ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
37 + /ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj
38 +
39 +Resolve the value of another name:
40 +
41 + > ipfs resolve /ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
42 + /ipns/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
43
39 -Resolve te value of another name:
44 +Resolve the value of another name recursively:
45
41 - > ipfs name resolve QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
42 - QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
46 + > ipfs resolve -r /ipns/QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n
47 + /ipfs/Qmcqtw8FfrVSBaRmbWwHxt3AuySBhJLcvmFYi3Lbc4xnwj
48
49 `,
50 },
51
52 Arguments: []cmds.Argument{
48 - cmds.StringArg("name", false, false, "The IPNS name to resolve. Defaults to your node's peerID.").EnableStdin(),
53 + cmds.StringArg("name", true, false, "The name to resolve.").EnableStdin(),
54 },
55 Options: []cmds.Option{
51 - cmds.BoolOption("recursive", "r", "Resolve until the result is not an IPNS name"),
56 + cmds.BoolOption("recursive", "r", "Resolve until the result is an IPFS name"),
57 },
58 Run: func(req cmds.Request, res cmds.Response) {
59
@@ -66,33 +71,19 @@ Resolve te value of another name:
71 }
72 }
73
69 - var name string
70 -
71 - if len(req.Arguments()) == 0 {
72 - if n.Identity == "" {
73 - res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
74 - return
75 - }
76 - name = n.Identity.Pretty()
77 -
78 - } else {
79 - name = req.Arguments()[0]
80 - }
81 -
74 + name := req.Arguments()[0]
75 recursive, _, _ := req.Option("recursive").Bool()
76 depth := 1
77 if recursive {
78 depth = namesys.DefaultDepthLimit
79 }
80
88 - output, err := n.Namesys.ResolveN(n.Context(), "/ipns/"+name, depth)
81 + output, err := n.Namesys.ResolveN(n.Context(), name, depth)
82 if err != nil {
83 res.SetError(err, cmds.ErrNormal)
84 return
85 }
86
94 - // TODO: better errors (in the case of not finding the name, we get "failed to find any peer in table")
95 -
87 res.SetOutput(&ResolvedPath{output})
88 },
89 Marshalers: cmds.MarshalerMap{
core/commands/root.go
+2
@@ -40,6 +40,7 @@ ADVANCED COMMANDS
40
41 daemon Start a long-running daemon process
42 mount Mount an ipfs read-only mountpoint
43 + resolve Resolve any type of name
44 name Publish or resolve IPNS names
45 dns Resolve DNS links
46 pin Pin objects to local storage
@@ -97,6 +98,7 @@ var rootSubcommands = map[string]*cmds.Command{
98 "ping": PingCmd,
99 "refs": RefsCmd,
100 "repo": RepoCmd,
101 + "resolve": ResolveCmd,
102 "stats": StatsCmd,
103 "swarm": SwarmCmd,
104 "update": UpdateCmd,