payloads.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // TokenBridgeModule is the identifier of the token bridge module ("TokenBridge")
  10. var TokenBridgeModule = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x42, 0x72, 0x69, 0x64, 0x67, 0x65}
  11. type (
  12. // BodyContractUpgrade is a governance message to perform a contract upgrade of the core module
  13. BodyContractUpgrade struct {
  14. ChainID ChainID
  15. NewContract Address
  16. }
  17. // BodyGuardianSetUpdate is a governance message to set a new guardian set
  18. BodyGuardianSetUpdate struct {
  19. Keys []common.Address
  20. NewIndex uint32
  21. }
  22. // BodyTokenBridgeRegisterChain is a governance message to register a chain on the token bridge
  23. BodyTokenBridgeRegisterChain struct {
  24. ChainID ChainID
  25. EmitterAddress Address
  26. }
  27. )
  28. func (b BodyContractUpgrade) Serialize() []byte {
  29. buf := new(bytes.Buffer)
  30. // Module
  31. buf.Write(CoreModule)
  32. // Action
  33. MustWrite(buf, binary.BigEndian, uint8(1))
  34. // ChainID
  35. MustWrite(buf, binary.BigEndian, uint16(b.ChainID))
  36. buf.Write(b.NewContract[:])
  37. return buf.Bytes()
  38. }
  39. func (b BodyGuardianSetUpdate) Serialize() []byte {
  40. buf := new(bytes.Buffer)
  41. // Module
  42. buf.Write(CoreModule)
  43. // Action
  44. MustWrite(buf, binary.BigEndian, uint8(2))
  45. // ChainID - 0 for universal
  46. MustWrite(buf, binary.BigEndian, uint16(0))
  47. MustWrite(buf, binary.BigEndian, b.NewIndex)
  48. MustWrite(buf, binary.BigEndian, uint8(len(b.Keys)))
  49. for _, k := range b.Keys {
  50. buf.Write(k[:])
  51. }
  52. return buf.Bytes()
  53. }
  54. func (r BodyTokenBridgeRegisterChain) Serialize() []byte {
  55. buf := &bytes.Buffer{}
  56. // Write token bridge header
  57. buf.Write(TokenBridgeModule)
  58. // Write action ID
  59. MustWrite(buf, binary.BigEndian, uint8(1))
  60. // Write target chain (0 = universal)
  61. MustWrite(buf, binary.BigEndian, uint16(0))
  62. // Write chain to be registered
  63. MustWrite(buf, binary.BigEndian, r.ChainID)
  64. // Write emitter address of chain to be registered
  65. buf.Write(r.EmitterAddress[:])
  66. return buf.Bytes()
  67. }