From 2fa43ce46c58533d4de5cae8565ba917117211c0 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet <juan@benet.ai> Date: Thu, 9 Oct 2014 03:39:45 -0700 Subject: [PATCH] ipfs name cmd improvements - cleaned up cmd help - ipfs name publish [<name>] <ref> - ipfs name resolve [<name>] - publish validates <ref> - both validate n args --- cmd/ipfs/name.go | 42 +++++++++++++++++++++++++++++++++++----- cmd/ipfs/publish.go | 26 ++++++++++++++++++------- cmd/ipfs/resolve.go | 23 +++++++++++++++++++--- core/commands/publish.go | 22 +++++++++++++++++++-- core/commands/resolve.go | 19 +++++++++++++++++- namesys/publisher.go | 13 +++++++++++-- 6 files changed, 125 insertions(+), 20 deletions(-) diff --git a/cmd/ipfs/name.go b/cmd/ipfs/name.go index 56d8821ec..a2ef78e50 100644 --- a/cmd/ipfs/name.go +++ b/cmd/ipfs/name.go @@ -8,11 +8,43 @@ import ( ) var cmdIpfsName = &commander.Command{ - UsageLine: "name", - Short: "Ipfs namespace manipulation tools.", - Long: `ipfs name [publish|resolve] <ref/hash>`, - Run: addCmd, - Flag: *flag.NewFlagSet("ipfs-name", flag.ExitOnError), + UsageLine: "name [publish | resolve]", + Short: "ipfs namespace (ipns) tool", + Long: `ipfs name - Get/Set ipfs config values. + + ipfs name publish [<name>] <ref> - Assign the <ref> to <name> + ipfs name resolve [<name>] - Resolve the <ref> value of <name> + +IPNS is a PKI namespace, where names are the hashes of public keys, and +the private key enables publishing new (signed) values. In both publish +and resolve, the default value of <name> is your own identity public key. + + +Examples: + +Publish a <ref> to your identity name: + + > ipfs name publish QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy + published name QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n to QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy + +Publish a <ref> to another public key: + + > ipfs name publish QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy + published name QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n to QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy + +Resolve the value of your identity: + + > ipfs name resolve + QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy + +Resolve te value of another name: + + > ipfs name resolve QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n + QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy + +`, + Run: addCmd, + Flag: *flag.NewFlagSet("ipfs-name", flag.ExitOnError), Subcommands: []*commander.Command{ cmdIpfsPub, cmdIpfsResolve, diff --git a/cmd/ipfs/publish.go b/cmd/ipfs/publish.go index fe4731077..041da0028 100644 --- a/cmd/ipfs/publish.go +++ b/cmd/ipfs/publish.go @@ -8,22 +8,34 @@ import ( var cmdIpfsPub = &commander.Command{ UsageLine: "publish", - Short: "Publish an object to ipns under your key.", - Long: `ipfs publish <path> - Publish object to ipns. + Short: "publish a <ref> to ipns.", + Long: `ipfs publish [<name>] <ref> - publish a <ref> to ipns. + +IPNS is a PKI namespace, where names are the hashes of public keys, and +the private key enables publishing new (signed) values. In publish, the +default value of <name> is your own identity public key. + +Examples: + +Publish a <ref> to your identity name: + + > ipfs name publish QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy + published name QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n to QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy + +Publish a <ref> to another public key: + + > ipfs name publish QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy + published name QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n to QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy `, Run: pubCmd, Flag: *flag.NewFlagSet("ipfs-publish", flag.ExitOnError), } -func init() { - cmdIpfsPub.Flag.String("k", "", "Specify key to use for publishing.") -} - var pubCmd = makeCommand(command{ name: "publish", args: 1, - flags: []string{"k"}, + flags: nil, online: true, cmdFn: commands.Publish, }) diff --git a/cmd/ipfs/resolve.go b/cmd/ipfs/resolve.go index 75b9903b5..9f5107ff8 100644 --- a/cmd/ipfs/resolve.go +++ b/cmd/ipfs/resolve.go @@ -8,8 +8,25 @@ import ( var cmdIpfsResolve = &commander.Command{ UsageLine: "resolve", - Short: "resolve an ipns link to a hash", - Long: `ipfs resolve <hash>... - Resolve hash. + Short: "resolve an ipns name to a <ref>", + Long: `ipfs resolve [<name>] - Resolve an ipns name to a <ref>. + +IPNS is a PKI namespace, where names are the hashes of public keys, and +the private key enables publishing new (signed) values. In resolve, the +default value of <name> is your own identity public key. + + +Examples: + +Resolve the value of your identity: + + > ipfs name resolve + QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy + +Resolve te value of another name: + + > ipfs name resolve QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n + QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy `, Run: resolveCmd, @@ -18,7 +35,7 @@ var cmdIpfsResolve = &commander.Command{ var resolveCmd = makeCommand(command{ name: "resolve", - args: 1, + args: 0, flags: nil, online: true, cmdFn: commands.Resolve, diff --git a/core/commands/publish.go b/core/commands/publish.go index 5794372b7..3d58ab431 100644 --- a/core/commands/publish.go +++ b/core/commands/publish.go @@ -13,14 +13,32 @@ import ( func Publish(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error { log.Debug("Begin Publish") + if n.Identity == nil { return errors.New("Identity not loaded!") } + // name := "" + ref := "" + + switch len(args) { + case 2: + // name = args[0] + ref = args[1] + return errors.New("keychains not yet implemented") + case 1: + // name = n.Identity.ID.String() + ref = args[0] + + default: + return fmt.Errorf("Publish expects 1 or 2 args; got %d.", len(args)) + } + + // later, n.Keychain.Get(name).PrivKey k := n.Identity.PrivKey pub := nsys.NewRoutingPublisher(n.Routing) - err := pub.Publish(k, args[0]) + err := pub.Publish(k, ref) if err != nil { return err } @@ -29,7 +47,7 @@ func Publish(n *core.IpfsNode, args []string, opts map[string]interface{}, out i if err != nil { return err } - fmt.Fprintf(out, "published mapping %s to %s\n", u.Key(hash), args[0]) + fmt.Fprintf(out, "published name %s to %s\n", u.Key(hash), ref) return nil } diff --git a/core/commands/resolve.go b/core/commands/resolve.go index f4bd0c546..7307dc265 100644 --- a/core/commands/resolve.go +++ b/core/commands/resolve.go @@ -1,6 +1,7 @@ package commands import ( + "errors" "fmt" "io" @@ -8,7 +9,23 @@ import ( ) func Resolve(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error { - res, err := n.Namesys.Resolve(args[0]) + + name := "" + + switch len(args) { + case 1: + name = args[0] + case 0: + if n.Identity == nil { + return errors.New("Identity not loaded!") + } + name = n.Identity.ID.String() + + default: + return fmt.Errorf("Publish expects 1 or 2 args; got %d.", len(args)) + } + + res, err := n.Namesys.Resolve(name) if err != nil { return err } diff --git a/namesys/publisher.go b/namesys/publisher.go index 0828f5e08..88533f8a0 100644 --- a/namesys/publisher.go +++ b/namesys/publisher.go @@ -1,10 +1,12 @@ package namesys import ( + "fmt" "time" - "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" - "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto" + context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" + proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto" + mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" ci "github.com/jbenet/go-ipfs/crypto" routing "github.com/jbenet/go-ipfs/routing" @@ -25,6 +27,13 @@ func NewRoutingPublisher(route routing.IpfsRouting) Publisher { // Publish implements Publisher. Accepts a keypair and a value, func (p *ipnsPublisher) Publish(k ci.PrivKey, value string) error { log.Debug("namesys: Publish %s", value) + + // validate `value` is a ref (multihash) + _, err := mh.FromB58String(value) + if err != nil { + return fmt.Errorf("publish value must be str multihash. %v", err) + } + ctx := context.TODO() data, err := createRoutingEntryData(k, value) if err != nil { -- GitLab