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

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

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

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

var publishCmd = &cmds.Command{
19 20 21 22 23 24 25 26 27
	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
28 29
the private key enables publishing new (signed) values. In publish, the
default value of <name> is your own identity public key.
30 31 32

Examples:

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

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

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

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

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

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

65 66
		args := req.Arguments()

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

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

		switch len(args) {
		case 2:
			// name = args[0]
Etienne Laurin's avatar
Etienne Laurin committed
77
			pstr = args[1]
78
			res.SetError(errors.New("keychains not yet implemented"), cmds.ErrNormal)
Jeromy's avatar
Jeromy committed
79
			return
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) {
Jeromy's avatar
Jeromy committed
110
	// First, verify the path exists
Jeromy's avatar
Jeromy committed
111
	_, err := core.Resolve(n, ref)
Jeromy's avatar
Jeromy committed
112 113 114 115
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
116
	err = n.Namesys.Publish(n.Context(), k, ref)
117 118 119 120 121 122 123 124 125 126 127
	if err != nil {
		return nil, err
	}

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

	return &IpnsEntry{
		Name:  u.Key(hash).String(),
128
		Value: ref.String(),
129 130
	}, nil
}