-
Notifications
You must be signed in to change notification settings - Fork 640
/
governance.go
89 lines (74 loc) · 2.67 KB
/
governance.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
package vaa
import (
"encoding/binary"
"fmt"
"time"
"golang.org/x/crypto/sha3"
)
var GovernanceEmitter = Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
var GovernanceChain = ChainIDSolana
func CreateGovernanceVAA(timestamp time.Time, nonce uint32, sequence uint64, guardianSetIndex uint32, payload []byte) *VAA {
vaa := &VAA{
Version: SupportedVAAVersion,
GuardianSetIndex: guardianSetIndex,
Signatures: nil,
Timestamp: timestamp,
Nonce: nonce,
Sequence: sequence,
ConsistencyLevel: 32,
EmitterChain: GovernanceChain,
EmitterAddress: GovernanceEmitter,
Payload: payload,
}
return vaa
}
// Compute the hash for cosmwasm contract instatiation params.
// The hash is keccak256 hash(hash(hash(BigEndian(CodeID)), Label), Msg).
// We compute the nested hash so there is no chance of bytes leaking between CodeID, Label, and Msg.
func CreateInstatiateCosmwasmContractHash(codeId uint64, label string, msg []byte) [32]byte {
var expected_hash [32]byte
// hash(BigEndian(CodeID))
var codeId_hash [32]byte
keccak := sha3.NewLegacyKeccak256()
if err := binary.Write(keccak, binary.BigEndian, codeId); err != nil {
panic(fmt.Sprintf("failed to write binary data (%d): %v", codeId, err))
}
keccak.Sum(codeId_hash[:0])
keccak.Reset()
// hash(hash(BigEndian(CodeID)), Label)
var codeIdLabel_hash [32]byte
keccak.Write(codeId_hash[:])
keccak.Write([]byte(label))
keccak.Sum(codeIdLabel_hash[:0])
keccak.Reset()
// hash(hash(hash(BigEndian(CodeID)), Label), Msg)
keccak.Write(codeIdLabel_hash[:])
keccak.Write(msg)
keccak.Sum(expected_hash[:0])
return expected_hash
}
// Compute the hash for cosmwasm contract migration params.
// The hash is keccak256 hash(hash(hash(BigEndian(CodeID)), Contract), Msg).
// We compute the nested hash so there is no chance of bytes leaking between CodeID, Contract, and Msg.
func CreateMigrateCosmwasmContractHash(codeId uint64, contract string, msg []byte) [32]byte {
var expected_hash [32]byte
// hash(BigEndian(CodeID))
var codeId_hash [32]byte
keccak := sha3.NewLegacyKeccak256()
if err := binary.Write(keccak, binary.BigEndian, codeId); err != nil {
panic(fmt.Sprintf("failed to write binary data (%d): %v", codeId, err))
}
keccak.Sum(codeId_hash[:0])
keccak.Reset()
// hash(hash(BigEndian(CodeID)), Label)
var codeIdContract_hash [32]byte
keccak.Write(codeId_hash[:])
keccak.Write([]byte(contract))
keccak.Sum(codeIdContract_hash[:0])
keccak.Reset()
// hash(hash(hash(BigEndian(CodeID)), Label), Msg)
keccak.Write(codeIdContract_hash[:])
keccak.Write(msg)
keccak.Sum(expected_hash[:0])
return expected_hash
}