util.go 745 B

123456789101112131415161718192021222324252627282930313233343536
  1. package whisper
  2. import "github.com/ethereum/go-ethereum/crypto"
  3. func hashTopic(topic []byte) []byte {
  4. return crypto.Sha3(topic)[:4]
  5. }
  6. // NOTE this isn't DRY, but I don't want to iterate twice.
  7. // Returns a formatted topics byte slice.
  8. // data: unformatted data (e.g., no hashes needed)
  9. func Topics(data [][]byte) [][]byte {
  10. d := make([][]byte, len(data))
  11. for i, byts := range data {
  12. d[i] = hashTopic(byts)
  13. }
  14. return d
  15. }
  16. func TopicsFromString(data ...string) [][]byte {
  17. d := make([][]byte, len(data))
  18. for i, str := range data {
  19. d[i] = hashTopic([]byte(str))
  20. }
  21. return d
  22. }
  23. func bytesToMap(s [][]byte) map[string]struct{} {
  24. m := make(map[string]struct{})
  25. for _, topic := range s {
  26. m[string(topic)] = struct{}{}
  27. }
  28. return m
  29. }