You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.0 KiB

  1. # SimpleSwarm
  2. A library to facilitate the usage of Swarm as a standalone tool.
  3. ## SimplePss
  4. PSS is the swarm protocol for sending messages, similar to IPFs/PubSub and Ethereum/Whisper.
  5. It shares the Whisper cryptography and adds a routing layer based on Kademlia DHT.
  6. Using Swarm/PSS as a devp2p standalone protocol is hard, so this library aims to abstract the implementation and make it easy to integrate with any GoLang project.
  7. Download the library.
  8. ```
  9. go get -u github.com/vocdoni/go-dvote/net/swarm
  10. ```
  11. Import it from your GoLang code.
  12. ```
  13. import (
  14. swarm "github.com/vocdoni/go-dvote/net/swarm"
  15. )
  16. ```
  17. #### Basic usage
  18. Create a simplePSS instance
  19. ```
  20. sn := new(swarm.SimplePss)
  21. ```
  22. Initialize and stard the swarm process. This will start the p2p protocol and automatically connect to the swarm network.
  23. ```
  24. err := sn.Init()
  25. ```
  26. Lets subscribe to a specific topic "aTopic" and add a symetric key which will be used to decrypt incoming messages (if encrypted).
  27. ```
  28. // sn.PssSub(kind, key, topic, address)
  29. sn.PssSub("sym", "aSymetricKey", "aTopic, "")
  30. ```
  31. There are three kind of messages which can be used:
  32. + raw: not encrypted
  33. + sym: encrypted with a symetric key
  34. + asym: encrypted with asymetric key
  35. Now we can address to struct `sn.PssTopics[topic]` for read received messages. The incoming messages will be sent through a go channel.
  36. ```
  37. pmsg := <-sn.PssTopics[topic].Delivery
  38. fmt.Printf("<- Pss received msg:{%s}\n", pmsg.Msg)
  39. ```
  40. The channel read is blocking so it might be executed in a separate gorutine.
  41. Sending a message is as easy as follows:
  42. ```
  43. // sn.PssPub(kind, key, topic, message, address)
  44. err := sn.PssPub("sym", "aSymetricKey", "atopic", "Hello world", "")
  45. ```
  46. Address can be used for sending more direct messages (using Kademlia routing). As more address bits specified as more direct will be the message. If address is empty the message will reach all nodes (whisper mode).
  47. #### Full example
  48. ```
  49. func main() {
  50. if len(os.Args) < 2 {
  51. fmt.Println("Use <sym|asym> <key>")
  52. return
  53. }
  54. sn := new(swarm.SimplePss)
  55. err := sn.Init()
  56. if err != nil {
  57. fmt.Printf("%v\n", err)
  58. return
  59. }
  60. err = sn.SetLog("crit")
  61. if err != nil {
  62. fmt.Printf("Cannot set loglevel %v\n", err)
  63. }
  64. kind := os.Args[1]
  65. topic := "vocdoni_test"
  66. key := ""
  67. if kind == "sym" || kind == "asym" || kind == "raw" {
  68. if kind != "raw" {
  69. key = os.Args[2]
  70. }
  71. sn.PssSub(kind, key, topic, "")
  72. defer sn.PssTopics[topic].Unregister()
  73. } else {
  74. fmt.Println("First parameter must be sym or asym")
  75. return
  76. }
  77. fmt.Printf("My PSS pubKey is %s\n", sn.PssPubKey)
  78. go func() {
  79. for {
  80. pmsg := <-sn.PssTopics[topic].Delivery
  81. fmt.Printf("<- Pss received msg:{%s}\n", pmsg.Msg)
  82. }
  83. }()
  84. hostname, _ := os.Hostname()
  85. for {
  86. fmt.Printf("-> Sending %s pss to [%s]\n", kind, key)
  87. currentTime := int64(time.Now().Unix())
  88. err := sn.PssPub(kind, key, topic, fmt.Sprintf("Hello world from %s at %d", hostname, currentTime), "")
  89. log.Info("pss sent", "err", err)
  90. time.Sleep(10 * time.Second)
  91. }
  92. }
  93. ```