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