/
ballot_fact_sign.go
168 lines (143 loc) · 4.22 KB
/
ballot_fact_sign.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
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
package base
import (
"time"
"github.com/spikeekips/mitum/base/key"
"github.com/spikeekips/mitum/util"
"github.com/spikeekips/mitum/util/hint"
"github.com/spikeekips/mitum/util/isvalid"
"github.com/spikeekips/mitum/util/localtime"
)
var (
BallotFactSignType = hint.Type("ballot-fact-sign")
BallotFactSignHint = hint.NewHint(BallotFactSignType, "v0.0.1")
BallotFactSignHinter = BaseBallotFactSign{BaseFactSign: BaseFactSign{
BaseHinter: hint.NewBaseHinter(BallotFactSignHint),
}}
SignedBallotFactType = hint.Type("signed-ballot-fact")
SignedBallotFactHint = hint.NewHint(SignedBallotFactType, "v0.0.1")
SignedBallotFactHinter = BaseSignedBallotFact{BaseHinter: hint.NewBaseHinter(SignedBallotFactHint)}
)
type SignedBallotFact interface {
hint.Hinter
isvalid.IsValider
util.Byter
Fact() BallotFact
FactSign() BallotFactSign
}
type BaseBallotFactSign struct {
BaseFactSign
node Address
}
func NewBaseBallotFactSign(n Address, pub key.Publickey, signedAt time.Time, sig key.Signature) BaseBallotFactSign {
return BaseBallotFactSign{
BaseFactSign: NewBaseFactSignWithHint(BallotFactSignHint, pub, sig).SetSignedAt(signedAt),
node: n,
}
}
func NewBaseBallotFactSignFromFact(
fact BallotFact,
n Address,
priv key.Privatekey,
networkID NetworkID,
) (BaseBallotFactSign, error) {
signedAt := localtime.UTCNow()
sig, err := priv.Sign(util.ConcatBytesSlice(
fact.Hash().Bytes(),
localtime.NewTime(signedAt).Bytes(),
networkID,
))
if err != nil {
return BaseBallotFactSign{}, err
}
return NewBaseBallotFactSign(n, priv.Publickey(), signedAt, sig), nil
}
func (fs BaseBallotFactSign) IsValid([]byte) error {
if err := isvalid.Check(nil, false, fs.node, fs.BaseFactSign); err != nil {
return isvalid.InvalidError.Errorf("invalid ballot fact sign: %w", err)
}
return nil
}
func (fs BaseBallotFactSign) Bytes() []byte {
return util.ConcatBytesSlice(fs.BaseFactSign.Bytes(), fs.node.Bytes())
}
func (fs BaseBallotFactSign) Node() Address {
return fs.node
}
func IsValidBallotFactSign(fact BallotFact, fs BallotFactSign, b []byte) error {
if fact == nil || fact.Hash() == nil {
return isvalid.InvalidError.Errorf("empty Fact")
}
if fs == nil {
return isvalid.InvalidError.Errorf("empty FactSign")
}
if fs.Signer() == nil {
return isvalid.InvalidError.Errorf("FactSign has empty Signer()")
}
if fs.Signature() == nil {
return isvalid.InvalidError.Errorf("FactSign has empty Signature()")
}
if fs.SignedAt().IsZero() {
return isvalid.InvalidError.Errorf("FactSign has empty SignedAt()")
}
if fact.Stage() == StageProposal {
switch i, ok := fact.(ProposalFact); {
case !ok:
return isvalid.InvalidError.Errorf("proposal fact has wrong type; %T", fact)
case !i.Proposer().Equal(fs.Node()):
return isvalid.InvalidError.Errorf("proposal fact is not signed by factsign node")
}
}
if err := fs.Signer().Verify(
util.ConcatBytesSlice(
fact.Hash().Bytes(),
localtime.NewTime(fs.SignedAt()).Bytes(),
b,
),
fs.Signature(),
); err != nil {
return isvalid.InvalidError.Errorf("invalid ballot fact sign: %w", err)
}
return nil
}
type BaseSignedBallotFact struct {
hint.BaseHinter
fact BallotFact
factSign BallotFactSign
}
func NewBaseSignedBallotFact(fact BallotFact, factSign BallotFactSign) BaseSignedBallotFact {
return BaseSignedBallotFact{
BaseHinter: hint.NewBaseHinter(SignedBallotFactHint),
fact: fact,
factSign: factSign,
}
}
func NewBaseSignedBallotFactFromFact(
fact BallotFact,
n Address,
priv key.Privatekey,
networkID NetworkID,
) (BaseSignedBallotFact, error) {
fs, err := NewBaseBallotFactSignFromFact(fact, n, priv, networkID)
if err != nil {
return BaseSignedBallotFact{}, err
}
return NewBaseSignedBallotFact(fact, fs), nil
}
func (sfs BaseSignedBallotFact) IsValid(networkID []byte) error {
if err := isvalid.Check(nil, false, sfs.BaseHinter, sfs.fact, sfs.factSign); err != nil {
return err
}
return IsValidBallotFactSign(sfs.fact, sfs.factSign, networkID)
}
func (sfs BaseSignedBallotFact) Bytes() []byte {
return util.ConcatBytesSlice(
sfs.fact.Hash().Bytes(),
sfs.factSign.Bytes(),
)
}
func (sfs BaseSignedBallotFact) Fact() BallotFact {
return sfs.fact
}
func (sfs BaseSignedBallotFact) FactSign() BallotFactSign {
return sfs.factSign
}