@@ -20,6 +20,7 @@ import (
20
20
"github.com/iotexproject/iotex-core/logger"
21
21
"github.com/iotexproject/iotex-core/pkg/enc"
22
22
"github.com/iotexproject/iotex-core/pkg/hash"
23
+ "github.com/iotexproject/iotex-core/pkg/keypair"
23
24
"github.com/iotexproject/iotex-core/pkg/version"
24
25
"github.com/iotexproject/iotex-core/proto"
25
26
)
@@ -42,17 +43,27 @@ type Vote struct {
42
43
}
43
44
44
45
// NewVote returns a Vote instance
45
- func NewVote (nonce uint64 , selfPubKey [] byte , votePubKey [] byte ) * Vote {
46
+ func NewVote (nonce uint64 , selfPubKey keypair. PublicKey , votePubKey keypair. PublicKey ) * Vote {
46
47
pbVote := & iproto.VotePb {
47
48
Version : version .ProtocolVersion ,
48
49
49
50
Nonce : nonce ,
50
- SelfPubkey : selfPubKey ,
51
- VotePubkey : votePubKey ,
51
+ SelfPubkey : selfPubKey [:] ,
52
+ VotePubkey : votePubKey [:] ,
52
53
}
53
54
return & Vote {pbVote }
54
55
}
55
56
57
+ // SelfPublicKey returns the self public key of the vote
58
+ func (v * Vote ) SelfPublicKey () (keypair.PublicKey , error ) {
59
+ return keypair .BytesToPublicKey (v .SelfPubkey )
60
+ }
61
+
62
+ // VotePublicKey returns the vote public key of the vote
63
+ func (v * Vote ) VotePublicKey () (keypair.PublicKey , error ) {
64
+ return keypair .BytesToPublicKey (v .VotePubkey )
65
+ }
66
+
56
67
// TotalSize returns the total size of this Vote
57
68
func (v * Vote ) TotalSize () uint32 {
58
69
size := TimestampSizeInBytes
@@ -86,16 +97,24 @@ func (v *Vote) ConvertToVotePb() *iproto.VotePb {
86
97
}
87
98
88
99
// ToJSON converts Vote to VoteJSON
89
- func (v * Vote ) ToJSON () * explorer.Vote {
100
+ func (v * Vote ) ToJSON () ( * explorer.Vote , error ) {
90
101
// used by account-based model
102
+ voterPubKey , err := keypair .BytesToPubKeyString (v .SelfPubkey )
103
+ if err != nil {
104
+ return nil , err
105
+ }
106
+ voteePubKey , err := keypair .BytesToPubKeyString (v .VotePubkey )
107
+ if err != nil {
108
+ return nil , err
109
+ }
91
110
vote := & explorer.Vote {
92
111
Version : int64 (v .Version ),
93
112
Nonce : int64 (v .Nonce ),
94
- VoterPubKey : hex . EncodeToString ( v . SelfPubkey ) ,
95
- VoteePubKey : hex . EncodeToString ( v . VotePubkey ) ,
113
+ VoterPubKey : voterPubKey ,
114
+ VoteePubKey : voteePubKey ,
96
115
Signature : hex .EncodeToString (v .Signature ),
97
116
}
98
- return vote
117
+ return vote , nil
99
118
}
100
119
101
120
// Serialize returns a serialized byte stream for the Transfer
@@ -114,13 +133,13 @@ func NewVoteFromJSON(jsonVote *explorer.Vote) (*Vote, error) {
114
133
v .Version = uint32 (jsonVote .Version )
115
134
// used by account-based model
116
135
v .Nonce = uint64 (jsonVote .Nonce )
117
- voterPubKey , err := hex . DecodeString (jsonVote .VoterPubKey )
136
+ voterPubKey , err := keypair . StringToPubKeyBytes (jsonVote .VoterPubKey )
118
137
if err != nil {
119
138
logger .Error ().Err (err ).Msg ("Fail to create a new Vote from VoteJSON" )
120
139
return nil , err
121
140
}
122
141
v .SelfPubkey = voterPubKey
123
- voteePubKey , err := hex . DecodeString (jsonVote .VoteePubKey )
142
+ voteePubKey , err := keypair . StringToPubKeyBytes (jsonVote .VoteePubKey )
124
143
if err != nil {
125
144
logger .Error ().Err (err ).Msg ("Fail to create a new Vote from VoteJSON" )
126
145
return nil , err
@@ -155,7 +174,7 @@ func (v *Vote) Hash() hash.Hash32B {
155
174
// Sign signs the Vote using sender's private key
156
175
func (v * Vote ) Sign (sender * iotxaddress.Address ) (* Vote , error ) {
157
176
// check the sender is correct
158
- if ! bytes .Equal (v .SelfPubkey , sender .PublicKey ) {
177
+ if ! bytes .Equal (v .SelfPubkey , sender .PublicKey [:] ) {
159
178
return nil , errors .Wrapf (ErrVoteError , "signing pubKey %x does not match with Vote pubKey %x" ,
160
179
v .SelfPubkey , sender .PublicKey )
161
180
}
0 commit comments