payloads.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package vaa
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/holiman/uint256"
  7. )
  8. // CoreModule is the identifier of the Core module (which is used for governance messages)
  9. 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}
  10. // WasmdModule is the identifier of the Wormchain Wasmd module (which is used for governance messages)
  11. var WasmdModule = [32]byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x57, 0x61, 0x73, 0x6D, 0x64, 0x4D, 0x6F, 0x64, 0x75, 0x6C, 0x65}
  12. var WasmdModuleStr = string(WasmdModule[:])
  13. type GovernanceAction uint8
  14. var (
  15. // Wormhole governance actions
  16. ActionContractUpgrade GovernanceAction = 1
  17. ActionGuardianSetUpdate GovernanceAction = 2
  18. // Wormchain cosmwasm governance actions
  19. ActionStoreCode GovernanceAction = 1
  20. ActionInstantiateContract GovernanceAction = 2
  21. ActionMigrateContract GovernanceAction = 3
  22. // Wormhole tokenbridge governance actions
  23. ActionRegisterChain GovernanceAction = 1
  24. ActionUpgradeTokenBridge GovernanceAction = 2
  25. ActionModifyBalance GovernanceAction = 3
  26. )
  27. type (
  28. // BodyContractUpgrade is a governance message to perform a contract upgrade of the core module
  29. BodyContractUpgrade struct {
  30. ChainID ChainID
  31. NewContract Address
  32. }
  33. // BodyGuardianSetUpdate is a governance message to set a new guardian set
  34. BodyGuardianSetUpdate struct {
  35. Keys []common.Address
  36. NewIndex uint32
  37. }
  38. // BodyTokenBridgeRegisterChain is a governance message to register a chain on the token bridge
  39. BodyTokenBridgeRegisterChain struct {
  40. Module string
  41. ChainID ChainID
  42. EmitterAddress Address
  43. }
  44. // BodyTokenBridgeUpgradeContract is a governance message to upgrade the token bridge.
  45. BodyTokenBridgeUpgradeContract struct {
  46. Module string
  47. TargetChainID ChainID
  48. NewContract Address
  49. }
  50. // BodyTokenBridgeModifyBalance is a governance message to modify accountant balances for the tokenbridge.
  51. BodyTokenBridgeModifyBalance struct {
  52. Module string
  53. TargetChainID ChainID
  54. Sequence uint64
  55. ChainId ChainID
  56. TokenChain ChainID
  57. TokenAddress Address
  58. Kind uint8
  59. Amount *uint256.Int
  60. Reason string
  61. }
  62. // BodyWormchainStoreCode is a governance message to upload a new cosmwasm contract to wormchain
  63. BodyWormchainStoreCode struct {
  64. WasmHash [32]byte
  65. }
  66. // BodyWormchainInstantiateContract is a governance message to instantiate a cosmwasm contract on wormchain
  67. BodyWormchainInstantiateContract struct {
  68. InstantiationParamsHash [32]byte
  69. }
  70. // BodyWormchainInstantiateContract is a governance message to migrate a cosmwasm contract on wormchain
  71. BodyWormchainMigrateContract struct {
  72. MigrationParamsHash [32]byte
  73. }
  74. )
  75. func (b BodyContractUpgrade) Serialize() []byte {
  76. buf := new(bytes.Buffer)
  77. // Module
  78. buf.Write(CoreModule)
  79. // Action
  80. MustWrite(buf, binary.BigEndian, ActionContractUpgrade)
  81. // ChainID
  82. MustWrite(buf, binary.BigEndian, uint16(b.ChainID))
  83. buf.Write(b.NewContract[:])
  84. return buf.Bytes()
  85. }
  86. func (b BodyGuardianSetUpdate) Serialize() []byte {
  87. buf := new(bytes.Buffer)
  88. // Module
  89. buf.Write(CoreModule)
  90. // Action
  91. MustWrite(buf, binary.BigEndian, ActionGuardianSetUpdate)
  92. // ChainID - 0 for universal
  93. MustWrite(buf, binary.BigEndian, uint16(0))
  94. MustWrite(buf, binary.BigEndian, b.NewIndex)
  95. MustWrite(buf, binary.BigEndian, uint8(len(b.Keys)))
  96. for _, k := range b.Keys {
  97. buf.Write(k[:])
  98. }
  99. return buf.Bytes()
  100. }
  101. func (r BodyTokenBridgeRegisterChain) Serialize() []byte {
  102. payload := &bytes.Buffer{}
  103. MustWrite(payload, binary.BigEndian, r.ChainID)
  104. payload.Write(r.EmitterAddress[:])
  105. // target chain 0 = universal
  106. return serializeBridgeGovernanceVaa(r.Module, ActionRegisterChain, 0, payload.Bytes())
  107. }
  108. func (r BodyTokenBridgeUpgradeContract) Serialize() []byte {
  109. return serializeBridgeGovernanceVaa(r.Module, ActionUpgradeTokenBridge, r.TargetChainID, r.NewContract[:])
  110. }
  111. func (r BodyTokenBridgeModifyBalance) Serialize() []byte {
  112. payload := &bytes.Buffer{}
  113. MustWrite(payload, binary.BigEndian, r.Sequence)
  114. MustWrite(payload, binary.BigEndian, r.ChainId)
  115. MustWrite(payload, binary.BigEndian, r.TokenChain)
  116. payload.Write(r.TokenAddress[:])
  117. payload.WriteByte(r.Kind)
  118. amount_bytes := r.Amount.Bytes32()
  119. payload.Write(amount_bytes[:])
  120. reason := make([]byte, 32)
  121. // truncate or pad "reason"
  122. for i := 0; i < len(reason); i += 1 {
  123. if i < len(r.Reason) {
  124. reason[i] = r.Reason[i]
  125. } else {
  126. reason[i] = ' '
  127. }
  128. }
  129. payload.Write(reason)
  130. return serializeBridgeGovernanceVaa(r.Module, ActionModifyBalance, r.TargetChainID, payload.Bytes())
  131. }
  132. func (r BodyWormchainStoreCode) Serialize() []byte {
  133. return serializeBridgeGovernanceVaa(WasmdModuleStr, ActionStoreCode, ChainIDWormchain, r.WasmHash[:])
  134. }
  135. func (r BodyWormchainInstantiateContract) Serialize() []byte {
  136. return serializeBridgeGovernanceVaa(WasmdModuleStr, ActionInstantiateContract, ChainIDWormchain, r.InstantiationParamsHash[:])
  137. }
  138. func (r BodyWormchainMigrateContract) Serialize() []byte {
  139. return serializeBridgeGovernanceVaa(WasmdModuleStr, ActionMigrateContract, ChainIDWormchain, r.MigrationParamsHash[:])
  140. }
  141. func serializeBridgeGovernanceVaa(module string, actionId GovernanceAction, chainId ChainID, payload []byte) []byte {
  142. if len(module) > 32 {
  143. panic("module longer than 32 byte")
  144. }
  145. buf := &bytes.Buffer{}
  146. // Write token bridge header
  147. for i := 0; i < (32 - len(module)); i++ {
  148. buf.WriteByte(0x00)
  149. }
  150. buf.Write([]byte(module))
  151. // Write action ID
  152. MustWrite(buf, binary.BigEndian, actionId)
  153. // Write target chain
  154. MustWrite(buf, binary.BigEndian, chainId)
  155. // Write emitter address of chain to be registered
  156. buf.Write(payload[:])
  157. return buf.Bytes()
  158. }