-
Notifications
You must be signed in to change notification settings - Fork 7
/
hash.go
65 lines (50 loc) · 1.51 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
package Block
import (
"github.com/brokenbydefault/Nanollet/Util"
"encoding/json"
)
type BlockHash []byte
func (s *SendBlock) Hash() BlockHash {
return Util.CreateHash(32, s.Previous, s.Destination, s.Balance.ToBytes())
}
func (s *ReceiveBlock) Hash() BlockHash {
return Util.CreateHash(32, s.Previous, s.Source)
}
func (s *OpenBlock) Hash() BlockHash {
return Util.CreateHash(32, s.Source, s.Representative, s.Account)
}
func (s *ChangeBlock) Hash() BlockHash {
return Util.CreateHash(32, s.Previous, s.Representative)
}
// It can change in the future, consider "Destination" as "Target".
func (u *UniversalBlock) Hash() BlockHash {
return Util.CreateHash(32, u.Previous, u.Destination, u.Representative, u.Balance.ToBytes(), u.Account)
}
func (u *UniversalBlock) HashAsSend() BlockHash {
return Util.CreateHash(32, u.Previous, u.Destination, u.Balance.ToBytes())
}
func (u *UniversalBlock) HashAsReceive() BlockHash {
return Util.CreateHash(32, u.Previous, u.Source)
}
func (u *UniversalBlock) HashAsOpen() BlockHash {
return Util.CreateHash(32, u.Source, u.Representative, u.Account)
}
func (u *UniversalBlock) HashAsChange() BlockHash {
return Util.CreateHash(32, u.Previous, u.Representative)
}
func (d *BlockHash) UnmarshalJSON(data []byte) (err error) {
var str string
err = json.Unmarshal(data, &str)
if err != nil {
return
}
v, err := Util.UnsafeHexDecode(str)
if err != nil {
return
}
*d = v
return
}
func (d BlockHash) MarshalJSON() ([]byte, error) {
return json.Marshal(Util.UnsafeHexEncode(d))
}