aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/storage/mru/topic.go
blob: 5f5720ae2f486536d37d50ac44e350fc0d247954 (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
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package mru

import (
    "bytes"
    "encoding/json"
    "fmt"

    "github.com/ethereum/go-ethereum/common/bitutil"
    "github.com/ethereum/go-ethereum/common/hexutil"
    "github.com/ethereum/go-ethereum/swarm/storage"
)

// TopicLength establishes the max length of a topic string
const TopicLength = storage.AddressLength

// Topic represents what a feed is about
type Topic [TopicLength]byte

// ErrTopicTooLong is returned when creating a topic with a name/related content too long
var ErrTopicTooLong = fmt.Errorf("Topic is too long. Max length is %d", TopicLength)

// NewTopic creates a new topic from a provided name and "related content" byte array,
// merging the two together.
// If relatedContent or name are longer than TopicLength, they will be truncated and an error returned
// name can be an empty string
// relatedContent can be nil
func NewTopic(name string, relatedContent []byte) (topic Topic, err error) {
    if relatedContent != nil {
        contentLength := len(relatedContent)
        if contentLength > TopicLength {
            contentLength = TopicLength
            err = ErrTopicTooLong
        }
        copy(topic[:], relatedContent[:contentLength])
    }
    nameBytes := []byte(name)
    nameLength := len(nameBytes)
    if nameLength > TopicLength {
        nameLength = TopicLength
        err = ErrTopicTooLong
    }
    bitutil.XORBytes(topic[:], topic[:], nameBytes[:nameLength])
    return topic, err
}

// Hex will return the topic encoded as an hex string
func (t *Topic) Hex() string {
    return hexutil.Encode(t[:])
}

// FromHex will parse a hex string into this Topic instance
func (t *Topic) FromHex(hex string) error {
    bytes, err := hexutil.Decode(hex)
    if err != nil || len(bytes) != len(t) {
        return NewErrorf(ErrInvalidValue, "Cannot decode topic")
    }
    copy(t[:], bytes)
    return nil
}

// Name will try to extract the resource name out of the topic
func (t *Topic) Name(relatedContent []byte) string {
    nameBytes := *t
    if relatedContent != nil {
        contentLength := len(relatedContent)
        if contentLength > TopicLength {
            contentLength = TopicLength
        }
        bitutil.XORBytes(nameBytes[:], t[:], relatedContent[:contentLength])
    }
    z := bytes.IndexByte(nameBytes[:], 0)
    if z < 0 {
        z = TopicLength
    }
    return string(nameBytes[:z])

}

// UnmarshalJSON implements the json.Unmarshaller interface
func (t *Topic) UnmarshalJSON(data []byte) error {
    var hex string
    json.Unmarshal(data, &hex)
    return t.FromHex(hex)
}

// MarshalJSON implements the json.Marshaller interface
func (t *Topic) MarshalJSON() ([]byte, error) {
    return json.Marshal(t.Hex())
}