aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/swarm/feeds.go
blob: 9b6f67a45bdb845bc38c46005da48e85a1060715 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2016 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

// Command feed allows the user to create and update signed Swarm feeds
package main

import (
    "fmt"
    "strings"

    "github.com/dexon-foundation/dexon/common"
    "github.com/dexon-foundation/dexon/common/hexutil"
    "github.com/dexon-foundation/dexon/crypto"

    "github.com/dexon-foundation/dexon/cmd/utils"
    swarm "github.com/dexon-foundation/dexon/swarm/api/client"
    "github.com/dexon-foundation/dexon/swarm/storage/feed"
    "gopkg.in/urfave/cli.v1"
)

var feedCommand = cli.Command{
    CustomHelpTemplate: helpTemplate,
    Name:               "feed",
    Usage:              "(Advanced) Create and update Swarm Feeds",
    ArgsUsage:          "<create|update|info>",
    Description:        "Works with Swarm Feeds",
    Subcommands: []cli.Command{
        {
            Action:             feedCreateManifest,
            CustomHelpTemplate: helpTemplate,
            Name:               "create",
            Usage:              "creates and publishes a new feed manifest",
            Description: `creates and publishes a new feed manifest pointing to a specified user's updates about a particular topic.
                    The feed topic can be built in the following ways:
                    * use --topic to set the topic to an arbitrary binary hex string.
                    * use --name to set the topic to a human-readable name.
                        For example --name could be set to "profile-picture", meaning this feed allows to get this user's current profile picture.
                    * use both --topic and --name to create named subtopics. 
                        For example, --topic could be set to an Ethereum contract address and --name could be set to "comments", meaning
                        this feed tracks a discussion about that contract.
                    The --user flag allows to have this manifest refer to a user other than yourself. If not specified,
                    it will then default to your local account (--bzzaccount)`,
            Flags: []cli.Flag{SwarmFeedNameFlag, SwarmFeedTopicFlag, SwarmFeedUserFlag},
        },
        {
            Action:             feedUpdate,
            CustomHelpTemplate: helpTemplate,
            Name:               "update",
            Usage:              "updates the content of an existing Swarm Feed",
            ArgsUsage:          "<0x Hex data>",
            Description: `publishes a new update on the specified topic
                    The feed topic can be built in the following ways:
                    * use --topic to set the topic to an arbitrary binary hex string.
                    * use --name to set the topic to a human-readable name.
                        For example --name could be set to "profile-picture", meaning this feed allows to get this user's current profile picture.
                    * use both --topic and --name to create named subtopics. 
                        For example, --topic could be set to an Ethereum contract address and --name could be set to "comments", meaning
                        this feed tracks a discussion about that contract.
                    
                    If you have a manifest, you can specify it with --manifest to refer to the feed,
                    instead of using --topic / --name
                    `,
            Flags: []cli.Flag{SwarmFeedManifestFlag, SwarmFeedNameFlag, SwarmFeedTopicFlag},
        },
        {
            Action:             feedInfo,
            CustomHelpTemplate: helpTemplate,
            Name:               "info",
            Usage:              "obtains information about an existing Swarm feed",
            Description: `obtains information about an existing Swarm feed
                    The topic can be specified directly with the --topic flag as an hex string
                    If no topic is specified, the default topic (zero) will be used
                    The --name flag can be used to specify subtopics with a specific name.
                    The --user flag allows to refer to a user other than yourself. If not specified,
                    it will then default to your local account (--bzzaccount)
                    If you have a manifest, you can specify it with --manifest instead of --topic / --name / ---user
                    to refer to the feed`,
            Flags: []cli.Flag{SwarmFeedManifestFlag, SwarmFeedNameFlag, SwarmFeedTopicFlag, SwarmFeedUserFlag},
        },
    },
}

func NewGenericSigner(ctx *cli.Context) feed.Signer {
    return feed.NewGenericSigner(getPrivKey(ctx))
}

func getTopic(ctx *cli.Context) (topic feed.Topic) {
    var name = ctx.String(SwarmFeedNameFlag.Name)
    var relatedTopic = ctx.String(SwarmFeedTopicFlag.Name)
    var relatedTopicBytes []byte
    var err error

    if relatedTopic != "" {
        relatedTopicBytes, err = hexutil.Decode(relatedTopic)
        if err != nil {
            utils.Fatalf("Error parsing topic: %s", err)
        }
    }

    topic, err = feed.NewTopic(name, relatedTopicBytes)
    if err != nil {
        utils.Fatalf("Error parsing topic: %s", err)
    }
    return topic
}

// swarm feed create <frequency> [--name <name>] [--data <0x Hexdata> [--multihash=false]]
// swarm feed update <Manifest Address or ENS domain> <0x Hexdata> [--multihash=false]
// swarm feed info <Manifest Address or ENS domain>

func feedCreateManifest(ctx *cli.Context) {
    var (
        bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
        client = swarm.NewClient(bzzapi)
    )

    newFeedUpdateRequest := feed.NewFirstRequest(getTopic(ctx))
    newFeedUpdateRequest.Feed.User = feedGetUser(ctx)

    manifestAddress, err := client.CreateFeedWithManifest(newFeedUpdateRequest)
    if err != nil {
        utils.Fatalf("Error creating feed manifest: %s", err.Error())
        return
    }
    fmt.Println(manifestAddress) // output manifest address to the user in a single line (useful for other commands to pick up)

}

func feedUpdate(ctx *cli.Context) {
    args := ctx.Args()

    var (
        bzzapi                  = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
        client                  = swarm.NewClient(bzzapi)
        manifestAddressOrDomain = ctx.String(SwarmFeedManifestFlag.Name)
    )

    if len(args) < 1 {
        fmt.Println("Incorrect number of arguments")
        cli.ShowCommandHelpAndExit(ctx, "update", 1)
        return
    }

    signer := NewGenericSigner(ctx)

    data, err := hexutil.Decode(args[0])
    if err != nil {
        utils.Fatalf("Error parsing data: %s", err.Error())
        return
    }

    var updateRequest *feed.Request
    var query *feed.Query

    if manifestAddressOrDomain == "" {
        query = new(feed.Query)
        query.User = signer.Address()
        query.Topic = getTopic(ctx)
    }

    // Retrieve a feed update request
    updateRequest, err = client.GetFeedRequest(query, manifestAddressOrDomain)
    if err != nil {
        utils.Fatalf("Error retrieving feed status: %s", err.Error())
    }

    // Check that the provided signer matches the request to sign
    if updateRequest.User != signer.Address() {
        utils.Fatalf("Signer address does not match the update request")
    }

    // set the new data
    updateRequest.SetData(data)

    // sign update
    if err = updateRequest.Sign(signer); err != nil {
        utils.Fatalf("Error signing feed update: %s", err.Error())
    }

    // post update
    err = client.UpdateFeed(updateRequest)
    if err != nil {
        utils.Fatalf("Error updating feed: %s", err.Error())
        return
    }
}

func feedInfo(ctx *cli.Context) {
    var (
        bzzapi                  = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
        client                  = swarm.NewClient(bzzapi)
        manifestAddressOrDomain = ctx.String(SwarmFeedManifestFlag.Name)
    )

    var query *feed.Query
    if manifestAddressOrDomain == "" {
        query = new(feed.Query)
        query.Topic = getTopic(ctx)
        query.User = feedGetUser(ctx)
    }

    metadata, err := client.GetFeedRequest(query, manifestAddressOrDomain)
    if err != nil {
        utils.Fatalf("Error retrieving feed metadata: %s", err.Error())
        return
    }
    encodedMetadata, err := metadata.MarshalJSON()
    if err != nil {
        utils.Fatalf("Error encoding metadata to JSON for display:%s", err)
    }
    fmt.Println(string(encodedMetadata))
}

func feedGetUser(ctx *cli.Context) common.Address {
    var user = ctx.String(SwarmFeedUserFlag.Name)
    if user != "" {
        return common.HexToAddress(user)
    }
    pk := getPrivKey(ctx)
    if pk == nil {
        utils.Fatalf("Cannot read private key. Must specify --user or --bzzaccount")
    }
    return crypto.PubkeyToAddress(pk.PublicKey)

}