-
Notifications
You must be signed in to change notification settings - Fork 92
/
EntryRevealMatryoshkaHash.go
137 lines (115 loc) · 3.21 KB
/
EntryRevealMatryoshkaHash.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package adminBlock
import (
"fmt"
"os"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
)
type RevealMatryoshkaHash struct {
AdminIDType uint32 `json:"adminidtype"`
IdentityChainID interfaces.IHash `json:"identitychainid"`
MHash interfaces.IHash `json:"mhash"`
}
var _ interfaces.Printable = (*RevealMatryoshkaHash)(nil)
var _ interfaces.BinaryMarshallable = (*RevealMatryoshkaHash)(nil)
var _ interfaces.IABEntry = (*RevealMatryoshkaHash)(nil)
func (e *RevealMatryoshkaHash) Init() {
if e.IdentityChainID == nil {
e.IdentityChainID = primitives.NewZeroHash()
}
if e.MHash == nil {
e.MHash = primitives.NewZeroHash()
}
e.AdminIDType = uint32(e.Type())
}
func (m *RevealMatryoshkaHash) Type() byte {
return constants.TYPE_REVEAL_MATRYOSHKA
}
func NewRevealMatryoshkaHash(identityChainID interfaces.IHash, mHash interfaces.IHash) *RevealMatryoshkaHash {
e := new(RevealMatryoshkaHash)
e.IdentityChainID = identityChainID
e.MHash = mHash
return e
}
func (c *RevealMatryoshkaHash) UpdateState(state interfaces.IState) error {
c.Init()
return nil
}
func (e *RevealMatryoshkaHash) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "RevealMatryoshkaHash.MarshalBinary err:%v", *pe)
}
}(&err)
e.Init()
var buf primitives.Buffer
err = buf.PushByte(e.Type())
if err != nil {
return nil, err
}
err = buf.PushBinaryMarshallable(e.IdentityChainID)
if err != nil {
return nil, err
}
err = buf.PushBinaryMarshallable(e.MHash)
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
func (e *RevealMatryoshkaHash) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
buf := primitives.NewBuffer(data)
b, err := buf.PopByte()
if err != nil {
return nil, err
}
if b != e.Type() {
return nil, fmt.Errorf("Invalid Entry type")
}
e.IdentityChainID = new(primitives.Hash)
err = buf.PopBinaryMarshallable(e.IdentityChainID)
if err != nil {
return nil, err
}
e.MHash = new(primitives.Hash)
err = buf.PopBinaryMarshallable(e.MHash)
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
func (e *RevealMatryoshkaHash) UnmarshalBinary(data []byte) (err error) {
_, err = e.UnmarshalBinaryData(data)
return
}
func (e *RevealMatryoshkaHash) JSONByte() ([]byte, error) {
e.AdminIDType = uint32(e.Type())
return primitives.EncodeJSON(e)
}
func (e *RevealMatryoshkaHash) JSONString() (string, error) {
e.AdminIDType = uint32(e.Type())
return primitives.EncodeJSONString(e)
}
func (e *RevealMatryoshkaHash) String() string {
e.Init()
str := fmt.Sprintf(" E: %35s -- %17s %8x %12s %x",
"RevealMatryoshkaHash",
"IdentityChainID", e.IdentityChainID.Bytes()[3:6],
"Hash", e.MHash.Bytes()[:5])
return str
}
func (e *RevealMatryoshkaHash) IsInterpretable() bool {
return false
}
func (e *RevealMatryoshkaHash) Interpret() string {
return ""
}
func (e *RevealMatryoshkaHash) Hash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "RevealMatryoshkaHash.Hash") }()
bin, err := e.MarshalBinary()
if err != nil {
panic(err)
}
return primitives.Sha(bin)
}