-
Notifications
You must be signed in to change notification settings - Fork 33
/
vote.go
49 lines (42 loc) · 1.18 KB
/
vote.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
package voter
import (
"github.com/omni-network/omni/halo/attest/types"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/k1util"
"github.com/omni-network/omni/lib/xchain"
"github.com/cometbft/cometbft/crypto"
)
// CreateVote creates a vote for the given block.
func CreateVote(privKey crypto.PrivKey, block xchain.Block) (*types.Vote, error) {
pubkey := privKey.PubKey().Bytes()
if len(pubkey) != 33 {
return nil, errors.New("invalid pubkey length", "length", len(pubkey))
}
tree, err := xchain.NewBlockTree(block)
if err != nil {
return nil, err
}
attRoot := tree.Root()
sig, err := k1util.Sign(privKey, attRoot)
if err != nil {
return nil, errors.Wrap(err, "sign attestation")
}
address, err := k1util.PubKeyToAddress(privKey.PubKey())
if err != nil {
return nil, err
}
return &types.Vote{
BlockHeader: &types.BlockHeader{
ChainId: block.SourceChainID,
ConfLevel: uint32(block.ConfLevel),
Offset: block.BlockOffset,
Height: block.BlockHeight,
Hash: block.BlockHash[:],
},
AttestationRoot: attRoot[:],
Signature: &types.SigTuple{
ValidatorAddress: address[:],
Signature: sig[:],
},
}, nil
}