-
Notifications
You must be signed in to change notification settings - Fork 200
/
hash.go
121 lines (99 loc) · 2.89 KB
/
hash.go
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
package types
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"gopkg.in/yaml.v2"
"github.com/tendermint/tendermint/crypto/tmhash"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var _ yaml.Marshaler = AggregateVoteHash{}
// AggregateVoteHash is hash value to hide vote exchange rates
// which is formatted as hex string in SHA256("{salt}:({pair},{exchange_rate})|...|({pair},{exchange_rate}):{voter}")
type AggregateVoteHash []byte
// GetAggregateVoteHash computes hash value of ExchangeRateVote
// to avoid redundant DecCoins stringify operation, use string argument
// TODO(mercilex): use ExchangeRateTuples
func GetAggregateVoteHash(salt string, exchangeRatesStr string, voter sdk.ValAddress) AggregateVoteHash {
hash := tmhash.NewTruncated()
sourceStr := fmt.Sprintf("%s:%s:%s", salt, exchangeRatesStr, voter.String())
_, err := hash.Write([]byte(sourceStr))
if err != nil {
panic(err)
}
bz := hash.Sum(nil)
return bz
}
// AggregateVoteHashFromHexString convert hex string to AggregateVoteHash
func AggregateVoteHashFromHexString(s string) (AggregateVoteHash, error) {
h, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
return h, nil
}
// String implements fmt.Stringer interface
func (h AggregateVoteHash) String() string {
return hex.EncodeToString(h)
}
// Equal does bytes equal check
func (h AggregateVoteHash) Equal(h2 AggregateVoteHash) bool {
return bytes.Equal(h, h2)
}
// Empty check the name hash has zero length
func (h AggregateVoteHash) Empty() bool {
return len(h) == 0
}
// Bytes returns the raw address bytes.
func (h AggregateVoteHash) Bytes() []byte {
return h
}
// Size returns the raw address bytes.
func (h AggregateVoteHash) Size() int {
return len(h)
}
// Format implements the fmt.Formatter interface.
func (h AggregateVoteHash) Format(s fmt.State, verb rune) {
switch verb {
case 's':
_, _ = s.Write([]byte(h.String()))
case 'p':
_, _ = s.Write([]byte(fmt.Sprintf("%p", h)))
default:
_, _ = s.Write([]byte(fmt.Sprintf("%X", []byte(h))))
}
}
// Marshal returns the raw address bytes. It is needed for protobuf
// compatibility.
func (h AggregateVoteHash) Marshal() ([]byte, error) {
return h, nil
}
// Unmarshal sets the address to the given data. It is needed for protobuf
// compatibility.
func (h *AggregateVoteHash) Unmarshal(data []byte) error {
*h = data
return nil
}
// MarshalJSON marshals to JSON using Bech32.
func (h AggregateVoteHash) MarshalJSON() ([]byte, error) {
return json.Marshal(h.String())
}
// MarshalYAML marshals to YAML using Bech32.
func (h AggregateVoteHash) MarshalYAML() (interface{}, error) {
return h.String(), nil
}
// UnmarshalJSON unmarshals from JSON assuming Bech32 encoding.
func (h *AggregateVoteHash) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
h2, err := AggregateVoteHashFromHexString(s)
if err != nil {
return err
}
*h = h2
return nil
}