github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/core/commands2/publish.go (about) 1 package commands 2 3 import ( 4 "errors" 5 "fmt" 6 7 cmds "github.com/jbenet/go-ipfs/commands" 8 core "github.com/jbenet/go-ipfs/core" 9 crypto "github.com/jbenet/go-ipfs/crypto" 10 nsys "github.com/jbenet/go-ipfs/namesys" 11 u "github.com/jbenet/go-ipfs/util" 12 ) 13 14 var errNotOnline = errors.New("This command must be run in online mode. Try running 'ipfs daemon' first.") 15 16 var publishCmd = &cmds.Command{ 17 Description: "Publish an object to IPNS", 18 Help: `IPNS is a PKI namespace, where names are the hashes of public keys, and 19 the private key enables publishing new (signed) values. In publish, the 20 default value of <name> is your own identity public key. 21 22 Examples: 23 24 Publish a <ref> to your identity name: 25 26 > ipfs name publish QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy 27 published name QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n to QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy 28 29 Publish a <ref> to another public key: 30 31 > ipfs name publish QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy 32 published name QmbCMUZw6JFeZ7Wp9jkzbye3Fzp2GGcPgC3nmeUjfVF87n to QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy 33 34 `, 35 36 Arguments: []cmds.Argument{ 37 cmds.StringArg("name", false, false, "The IPNS name to publish to. Defaults to your node's peerID"), 38 cmds.StringArg("ipfs-path", true, false, "IPFS path of the obejct to be published at <name>"), 39 }, 40 Run: func(req cmds.Request) (interface{}, error) { 41 log.Debug("Begin Publish") 42 43 n := req.Context().Node 44 args := req.Arguments() 45 46 if n.Network == nil { 47 return nil, errNotOnline 48 } 49 50 if n.Identity == nil { 51 return nil, errors.New("Identity not loaded!") 52 } 53 54 // name := "" 55 ref := "" 56 57 switch len(args) { 58 case 2: 59 // name = args[0] 60 ref = args[1].(string) 61 return nil, errors.New("keychains not yet implemented") 62 case 1: 63 // name = n.Identity.ID.String() 64 ref = args[0].(string) 65 } 66 67 // TODO n.Keychain.Get(name).PrivKey 68 k := n.Identity.PrivKey() 69 return publish(n, k, ref) 70 }, 71 Marshallers: map[cmds.EncodingType]cmds.Marshaller{ 72 cmds.Text: func(res cmds.Response) ([]byte, error) { 73 v := res.Output().(*IpnsEntry) 74 s := fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value) 75 return []byte(s), nil 76 }, 77 }, 78 Type: &IpnsEntry{}, 79 } 80 81 func publish(n *core.IpfsNode, k crypto.PrivKey, ref string) (*IpnsEntry, error) { 82 pub := nsys.NewRoutingPublisher(n.Routing) 83 err := pub.Publish(k, ref) 84 if err != nil { 85 return nil, err 86 } 87 88 hash, err := k.GetPublic().Hash() 89 if err != nil { 90 return nil, err 91 } 92 93 return &IpnsEntry{ 94 Name: u.Key(hash).String(), 95 Value: ref, 96 }, nil 97 }