publish.go 3.32 KB
Newer Older
1 2 3 4 5
package commands

import (
	"errors"
	"fmt"
6 7
	"io"
	"strings"
8

9 10 11 12
	cmds "github.com/ipfs/go-ipfs/commands"
	core "github.com/ipfs/go-ipfs/core"
	nsys "github.com/ipfs/go-ipfs/namesys"
	crypto "github.com/ipfs/go-ipfs/p2p/crypto"
Etienne Laurin's avatar
Etienne Laurin committed
13
	path "github.com/ipfs/go-ipfs/path"
14
	u "github.com/ipfs/go-ipfs/util"
15 16 17 18 19
)

var errNotOnline = errors.New("This command must be run in online mode. Try running 'ipfs daemon' first.")

var publishCmd = &cmds.Command{
20 21 22 23 24 25 26 27 28
	Helptext: cmds.HelpText{
		Tagline: "Publish an object to IPNS",
		ShortDescription: `
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.
`,
		LongDescription: `
IPNS is a PKI namespace, where names are the hashes of public keys, and
29 30
the private key enables publishing new (signed) values. In publish, the
default value of <name> is your own identity public key.
31 32 33

Examples:

Etienne Laurin's avatar
Etienne Laurin committed
34
Publish an <ipfs-path> to your identity name:
35

Etienne Laurin's avatar
Etienne Laurin committed
36
  > ipfs name publish /ipfs/QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
37 38
  published name QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n to QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy

Etienne Laurin's avatar
Etienne Laurin committed
39
Publish an <ipfs-path> to another public key (not implemented):
40 41 42 43

  > ipfs name publish QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
  published name QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n to QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy
`,
44
	},
45

46
	Arguments: []cmds.Argument{
47
		cmds.StringArg("name", false, false, "The IPNS name to publish to. Defaults to your node's peerID"),
48
		cmds.StringArg("ipfs-path", true, false, "IPFS path of the obejct to be published at <name>").EnableStdin(),
49
	},
50
	Run: func(req cmds.Request, res cmds.Response) {
51
		log.Debug("Begin Publish")
52 53
		n, err := req.Context().GetNode()
		if err != nil {
54 55
			res.SetError(err, cmds.ErrNormal)
			return
56
		}
57

58 59 60 61 62 63
		if !n.OnlineMode() {
			err := n.SetupOfflineRouting()
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
64 65
		}

66 67
		args := req.Arguments()

68
		if n.Identity == "" {
69 70
			res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)
			return
71 72
		}

Etienne Laurin's avatar
Etienne Laurin committed
73
		var pstr string
74 75 76 77

		switch len(args) {
		case 2:
			// name = args[0]
Etienne Laurin's avatar
Etienne Laurin committed
78
			pstr = args[1]
79
			res.SetError(errors.New("keychains not yet implemented"), cmds.ErrNormal)
80 81
		case 1:
			// name = n.Identity.ID.String()
Etienne Laurin's avatar
Etienne Laurin committed
82 83 84
			pstr = args[0]
		}

85
		p, err := path.ParsePath(pstr)
Etienne Laurin's avatar
Etienne Laurin committed
86
		if err != nil {
87
			res.SetError(fmt.Errorf("failed to validate path: %v", err), cmds.ErrNormal)
Etienne Laurin's avatar
Etienne Laurin committed
88
			return
89 90 91
		}

		// TODO n.Keychain.Get(name).PrivKey
92
		output, err := publish(n, n.PrivateKey, p)
93 94 95 96 97
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
		res.SetOutput(output)
98
	},
99
	Marshalers: cmds.MarshalerMap{
100
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
101 102
			v := res.Output().(*IpnsEntry)
			s := fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value)
103
			return strings.NewReader(s), nil
104 105
		},
	},
106
	Type: IpnsEntry{},
107 108
}

109
func publish(n *core.IpfsNode, k crypto.PrivKey, ref path.Path) (*IpnsEntry, error) {
110
	pub := nsys.NewRoutingPublisher(n.Routing)
111 112

	err := pub.Publish(n.Context(), k, ref)
113 114 115 116 117 118 119 120 121 122 123
	if err != nil {
		return nil, err
	}

	hash, err := k.GetPublic().Hash()
	if err != nil {
		return nil, err
	}

	return &IpnsEntry{
		Name:  u.Key(hash).String(),
124
		Value: ref.String(),
125 126
	}, nil
}