governance.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package vaa
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "time"
  6. "golang.org/x/crypto/sha3"
  7. )
  8. var GovernanceEmitter = Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
  9. var GovernanceChain = ChainIDSolana
  10. func CreateGovernanceVAA(timestamp time.Time, nonce uint32, sequence uint64, guardianSetIndex uint32, payload []byte) *VAA {
  11. vaa := &VAA{
  12. Version: SupportedVAAVersion,
  13. GuardianSetIndex: guardianSetIndex,
  14. Signatures: nil,
  15. Timestamp: timestamp,
  16. Nonce: nonce,
  17. Sequence: sequence,
  18. ConsistencyLevel: 32,
  19. EmitterChain: GovernanceChain,
  20. EmitterAddress: GovernanceEmitter,
  21. Payload: payload,
  22. }
  23. return vaa
  24. }
  25. // Compute the hash for cosmwasm contract instatiation params.
  26. // The hash is keccak256 hash(hash(hash(BigEndian(CodeID)), Label), Msg).
  27. // We compute the nested hash so there is no chance of bytes leaking between CodeID, Label, and Msg.
  28. func CreateInstatiateCosmwasmContractHash(codeId uint64, label string, msg []byte) [32]byte {
  29. var expected_hash [32]byte
  30. // hash(BigEndian(CodeID))
  31. var codeId_hash [32]byte
  32. keccak := sha3.NewLegacyKeccak256()
  33. if err := binary.Write(keccak, binary.BigEndian, codeId); err != nil {
  34. panic(fmt.Sprintf("failed to write binary data (%d): %v", codeId, err))
  35. }
  36. keccak.Sum(codeId_hash[:0])
  37. keccak.Reset()
  38. // hash(hash(BigEndian(CodeID)), Label)
  39. var codeIdLabel_hash [32]byte
  40. keccak.Write(codeId_hash[:])
  41. keccak.Write([]byte(label))
  42. keccak.Sum(codeIdLabel_hash[:0])
  43. keccak.Reset()
  44. // hash(hash(hash(BigEndian(CodeID)), Label), Msg)
  45. keccak.Write(codeIdLabel_hash[:])
  46. keccak.Write(msg)
  47. keccak.Sum(expected_hash[:0])
  48. return expected_hash
  49. }
  50. // Compute the hash for cosmwasm contract migration params.
  51. // The hash is keccak256 hash(hash(hash(BigEndian(CodeID)), Contract), Msg).
  52. // We compute the nested hash so there is no chance of bytes leaking between CodeID, Contract, and Msg.
  53. func CreateMigrateCosmwasmContractHash(codeId uint64, contract string, msg []byte) [32]byte {
  54. var expected_hash [32]byte
  55. // hash(BigEndian(CodeID))
  56. var codeId_hash [32]byte
  57. keccak := sha3.NewLegacyKeccak256()
  58. if err := binary.Write(keccak, binary.BigEndian, codeId); err != nil {
  59. panic(fmt.Sprintf("failed to write binary data (%d): %v", codeId, err))
  60. }
  61. keccak.Sum(codeId_hash[:0])
  62. keccak.Reset()
  63. // hash(hash(BigEndian(CodeID)), Label)
  64. var codeIdContract_hash [32]byte
  65. keccak.Write(codeId_hash[:])
  66. keccak.Write([]byte(contract))
  67. keccak.Sum(codeIdContract_hash[:0])
  68. keccak.Reset()
  69. // hash(hash(hash(BigEndian(CodeID)), Label), Msg)
  70. keccak.Write(codeIdContract_hash[:])
  71. keccak.Write(msg)
  72. keccak.Sum(expected_hash[:0])
  73. return expected_hash
  74. }