wormhole/sdk/vaa/payloads.go

122 lines
3.2 KiB
Go

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}
type (
// BodyContractUpgrade is a governance message to perform a contract upgrade of the core module
BodyContractUpgrade struct {
ChainID ChainID
NewContract Address
}
// BodyGuardianSetUpdate is a governance message to set a new guardian set
BodyGuardianSetUpdate 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
TargetChainID ChainID
NewContract Address
}
// BodyWormchainStoreCode is a governance message to upload a new cosmwasm contract to wormchain
BodyWormchainStoreCode struct {
WasmHash [32]byte
}
// BodyWormchainInstantiateContract is a governance message to instantiate a cosmwasm contract on wormchain
BodyWormchainInstantiateContract struct {
InstantiationParamsHash [32]byte
}
)
func (b BodyContractUpgrade) Serialize() []byte {
buf := new(bytes.Buffer)
// Module
buf.Write(CoreModule)
// Action
MustWrite(buf, binary.BigEndian, uint8(1))
// ChainID
MustWrite(buf, binary.BigEndian, uint16(b.ChainID))
buf.Write(b.NewContract[:])
return buf.Bytes()
}
func (b BodyGuardianSetUpdate) Serialize() []byte {
buf := new(bytes.Buffer)
// Module
buf.Write(CoreModule)
// Action
MustWrite(buf, binary.BigEndian, uint8(2))
// ChainID - 0 for universal
MustWrite(buf, binary.BigEndian, uint16(0))
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 {
return serializeBridgeGovernanceVaa(r.Module, 1, r.ChainID, r.EmitterAddress)
}
func (r BodyTokenBridgeUpgradeContract) Serialize() []byte {
return serializeBridgeGovernanceVaa(r.Module, 2, r.TargetChainID, r.NewContract)
}
func (r BodyWormchainStoreCode) Serialize() []byte {
return serializeBridgeGovernanceVaa("WasmdModule", 1, ChainIDWormchain, r.WasmHash)
}
func (r BodyWormchainInstantiateContract) Serialize() []byte {
return serializeBridgeGovernanceVaa("WasmdModule", 2, ChainIDWormchain, r.InstantiationParamsHash)
}
func serializeBridgeGovernanceVaa(module string, actionId uint8, chainId ChainID, payload [32]byte) []byte {
if len(module) > 32 {
panic("module longer than 32 byte")
}
buf := &bytes.Buffer{}
// Write token bridge header
for i := 0; i < (32 - len(module)); i++ {
buf.WriteByte(0x00)
}
buf.Write([]byte(module))
// Write action ID
MustWrite(buf, binary.BigEndian, actionId)
// Write target chain
MustWrite(buf, binary.BigEndian, chainId)
// Write emitter address of chain to be registered
buf.Write(payload[:])
return buf.Bytes()
}