/
payloads.go
196 lines (157 loc) · 4.64 KB
/
payloads.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package vaa
import (
"bytes"
"encoding/binary"
"github.com/ethereum/go-ethereum/common"
)
// CoreModule is the identifier of the Core module (which is used for governance messages)
var CoreModule = []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x43, 0x6f, 0x72, 0x65}
// 000000000000000000000000000000000000000000546f6b656e427269646765
var TokenBridgeModule = []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x42, 0x72, 0x69, 0x64, 0x67, 0x65}
type (
// BodyUpdateMessageFee is a governance message to perform update message fee of the core module
BodyUpdateMessageFee struct {
NewMessageFee []byte
}
BodyTransferFee struct {
Amount []byte
Recipient []byte
}
// BodyContractUpgrade is a governance message to perform a contract upgrade of the core module
BodyContractUpgrade struct {
Payload []byte
}
// BodyGuardianSetUpgrade is a governance message to set a new guardian set
BodyGuardianSetUpgrade struct {
Keys []common.Address
NewIndex uint32
}
// BodyTokenBridgeRegisterChain is a governance message to register a chain on the token bridge
BodyTokenBridgeRegisterChain struct {
Module string
ChainID ChainID
EmitterAddress Address
}
// BodyTokenBridgeUpgradeContract is a governance message to upgrade the token bridge.
BodyTokenBridgeUpgradeContract struct {
Module string
Payload []byte
}
BodyTokenBridgeDestroyContracts struct {
EmitterChain ChainID
Sequences []uint64
}
BodyTokenBridgeUpdateMinimalConsistencyLevel struct {
NewConsistencyLevel uint8
}
BodyTokenBridgeUpdateRefundAddress struct {
NewRefundAddress []byte
}
)
func (b BodyUpdateMessageFee) Serialize() []byte {
buf := new(bytes.Buffer)
// Module
buf.Write(CoreModule)
// Action
MustWrite(buf, binary.BigEndian, uint8(3))
buf.Write(b.NewMessageFee)
return buf.Bytes()
}
func (b BodyTransferFee) Serialize() []byte {
buf := new(bytes.Buffer)
// Module
buf.Write(CoreModule)
// Action
MustWrite(buf, binary.BigEndian, uint8(4))
buf.Write(b.Amount)
buf.Write(b.Recipient)
return buf.Bytes()
}
func (b BodyContractUpgrade) Serialize() []byte {
buf := new(bytes.Buffer)
// Module
buf.Write(CoreModule)
// Action
MustWrite(buf, binary.BigEndian, uint8(1))
buf.Write(b.Payload[:])
return buf.Bytes()
}
func (b BodyGuardianSetUpgrade) Serialize() []byte {
buf := new(bytes.Buffer)
// Module
buf.Write(CoreModule)
// Action
MustWrite(buf, binary.BigEndian, uint8(2))
MustWrite(buf, binary.BigEndian, b.NewIndex)
MustWrite(buf, binary.BigEndian, uint8(len(b.Keys)))
for _, k := range b.Keys {
buf.Write(k[:])
}
return buf.Bytes()
}
func (r BodyTokenBridgeRegisterChain) Serialize() []byte {
if len(r.Module) > 32 {
panic("module longer than 32 byte")
}
buf := &bytes.Buffer{}
// Write token bridge header
for i := 0; i < (32 - len(r.Module)); i++ {
buf.WriteByte(0x00)
}
buf.Write([]byte(r.Module))
// Write action ID
MustWrite(buf, binary.BigEndian, uint8(1))
// Write chain to be registered
MustWrite(buf, binary.BigEndian, r.ChainID)
// Write emitter address of chain to be registered
buf.Write(r.EmitterAddress[:])
return buf.Bytes()
}
func (r BodyTokenBridgeUpgradeContract) Serialize() []byte {
if len(r.Module) > 32 {
panic("module longer than 32 byte")
}
buf := &bytes.Buffer{}
// Write token bridge header
for i := 0; i < (32 - len(r.Module)); i++ {
buf.WriteByte(0x00)
}
buf.Write([]byte(r.Module))
// Write action ID
MustWrite(buf, binary.BigEndian, uint8(2))
// Write contract upgrade payload
buf.Write(r.Payload[:])
return buf.Bytes()
}
func (b BodyTokenBridgeDestroyContracts) Serialize() []byte {
buf := new(bytes.Buffer)
// Module
buf.Write(TokenBridgeModule)
// Action ID
MustWrite(buf, binary.BigEndian, uint8(0xf0))
MustWrite(buf, binary.BigEndian, uint16(b.EmitterChain))
MustWrite(buf, binary.BigEndian, uint16(len(b.Sequences)))
for _, seq := range b.Sequences {
MustWrite(buf, binary.BigEndian, seq)
}
return buf.Bytes()
}
func (b BodyTokenBridgeUpdateMinimalConsistencyLevel) Serialize() []byte {
buf := new(bytes.Buffer)
// Module
buf.Write(TokenBridgeModule)
// Action ID
MustWrite(buf, binary.BigEndian, uint8(0xf1))
MustWrite(buf, binary.BigEndian, b.NewConsistencyLevel)
return buf.Bytes()
}
func (b BodyTokenBridgeUpdateRefundAddress) Serialize() []byte {
buf := new(bytes.Buffer)
// Module
buf.Write(TokenBridgeModule)
// Action ID
MustWrite(buf, binary.BigEndian, uint8(0xf2))
MustWrite(buf, binary.BigEndian, uint16(len(b.NewRefundAddress)))
buf.Write(b.NewRefundAddress)
return buf.Bytes()
}