payloads.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package vaa
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "github.com/ethereum/go-ethereum/common"
  6. )
  7. // CoreModule is the identifier of the Core module (which is used for governance messages)
  8. 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}
  9. type (
  10. // BodyContractUpgrade is a governance message to perform a contract upgrade of the core module
  11. BodyContractUpgrade struct {
  12. ChainID ChainID
  13. NewContract Address
  14. }
  15. // BodyGuardianSetUpdate is a governance message to set a new guardian set
  16. BodyGuardianSetUpdate struct {
  17. Keys []common.Address
  18. NewIndex uint32
  19. }
  20. // BodyTokenBridgeRegisterChain is a governance message to register a chain on the token bridge
  21. BodyTokenBridgeRegisterChain struct {
  22. Module string
  23. ChainID ChainID
  24. EmitterAddress Address
  25. }
  26. // BodyTokenBridgeUpgradeContract is a governance message to upgrade the token bridge.
  27. BodyTokenBridgeUpgradeContract struct {
  28. Module string
  29. TargetChainID ChainID
  30. NewContract Address
  31. }
  32. )
  33. func (b BodyContractUpgrade) Serialize() []byte {
  34. buf := new(bytes.Buffer)
  35. // Module
  36. buf.Write(CoreModule)
  37. // Action
  38. MustWrite(buf, binary.BigEndian, uint8(1))
  39. // ChainID
  40. MustWrite(buf, binary.BigEndian, uint16(b.ChainID))
  41. buf.Write(b.NewContract[:])
  42. return buf.Bytes()
  43. }
  44. func (b BodyGuardianSetUpdate) Serialize() []byte {
  45. buf := new(bytes.Buffer)
  46. // Module
  47. buf.Write(CoreModule)
  48. // Action
  49. MustWrite(buf, binary.BigEndian, uint8(2))
  50. // ChainID - 0 for universal
  51. MustWrite(buf, binary.BigEndian, uint16(0))
  52. MustWrite(buf, binary.BigEndian, b.NewIndex)
  53. MustWrite(buf, binary.BigEndian, uint8(len(b.Keys)))
  54. for _, k := range b.Keys {
  55. buf.Write(k[:])
  56. }
  57. return buf.Bytes()
  58. }
  59. func (r BodyTokenBridgeRegisterChain) Serialize() []byte {
  60. if len(r.Module) > 32 {
  61. panic("module longer than 32 byte")
  62. }
  63. buf := &bytes.Buffer{}
  64. // Write token bridge header
  65. for i := 0; i < (32 - len(r.Module)); i++ {
  66. buf.WriteByte(0x00)
  67. }
  68. buf.Write([]byte(r.Module))
  69. // Write action ID
  70. MustWrite(buf, binary.BigEndian, uint8(1))
  71. // Write target chain (0 = universal)
  72. MustWrite(buf, binary.BigEndian, uint16(0))
  73. // Write chain to be registered
  74. MustWrite(buf, binary.BigEndian, r.ChainID)
  75. // Write emitter address of chain to be registered
  76. buf.Write(r.EmitterAddress[:])
  77. return buf.Bytes()
  78. }
  79. func (r BodyTokenBridgeUpgradeContract) Serialize() []byte {
  80. if len(r.Module) > 32 {
  81. panic("module longer than 32 byte")
  82. }
  83. buf := &bytes.Buffer{}
  84. // Write token bridge header
  85. for i := 0; i < (32 - len(r.Module)); i++ {
  86. buf.WriteByte(0x00)
  87. }
  88. buf.Write([]byte(r.Module))
  89. // Write action ID
  90. MustWrite(buf, binary.BigEndian, uint8(2))
  91. // Write target chain
  92. MustWrite(buf, binary.BigEndian, r.TargetChainID)
  93. // Write emitter address of chain to be registered
  94. buf.Write(r.NewContract[:])
  95. return buf.Bytes()
  96. }