Skip to content

A new abstraction layer for communicating with the nym-client via websockets

Notifications You must be signed in to change notification settings

craftdome/go-nym

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub go.mod Go version (subdirectory of monorepo) GitHub release (with filter)

About

This library is designed to simplify interaction with the NYM protocol for nym-client. It implements a basic set of commands for sending and receiving messages to/from mixnet.

Features

  • Using Gorilla WebSocket
  • Text Protocol (JSON) support
  • Binary Protocol support
  • Support for the user protocol in the body of the binary message

Preparation for use

  1. The library requires an active connection to the websocket client. The simple installation and launch of the nym-client is described in the official docs -> link

  2. Tested with nym-client version 1.1.32

  3. Importing a dependency

Use the standard Go tools to install dependencies:

go get github.com/craftdome/go-nym

Importing the basic package:

import "github.com/craftdome/go-nym"

Using

Tips

  • Remember, if you plan to give access to nym-client connect from the outside (for this you should specify the ip of the external network interface of your machine), the nym-client doesn't have a connection auth function.
  • Only 1 connection is allowed at a time.
  • If you need an external connection, use a local networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12) instead of a global unicast one to increase security.

Initialization

  1. First, you need to init the connection client with the nym-client address. We copy the connection address:port from the console after running the nym-client. By default it's localhost:1977, or in my case 192.168.88.4:1977.

go-nym/example/main.go

Lines 21 to 22 in 17a1c03

// Init the client via server credentials
client := nym.NewClient("ws://192.168.88.4:1977")

  1. Connection establishing with the nym-client.

go-nym/example/main.go

Lines 24 to 27 in 17a1c03

// Dial a connection to the server
if err := client.Dial(); err != nil {
panic(err)
}

Reading messages

  1. We turn on listening to incoming messages, which we then extract through the Messages() chan.

go-nym/example/main.go

Lines 30 to 54 in 17a1c03

go func() {
if err := client.ListenAndServe(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}()
go func() {
// Incoming Message Channel
for message := range client.Messages() {
switch message.Type() {
case tags.Error:
msg := message.(*response.Error)
fmt.Printf("Error: %s\n", msg.Message)
case tags.SelfAddress:
msg := message.(*response.SelfAddress)
fmt.Printf("SelfAddress: %s\n", msg.Address)
case tags.Received:
msg := message.(*response.Received)
fmt.Printf("Received: %s, SenderTag: %s\n", msg.Message, msg.SenderTag)
}
}
fmt.Println("Closed")
done <- struct{}{}
}()

Sending messages

  1. Getting your nym-client address (SelfAddress).

go-nym/example/main.go

Lines 57 to 59 in 17a1c03

if err := client.SendRequestAsText(nym.NewGetSelfAddress()); err != nil {
fmt.Fprintln(os.Stderr, err)
}

  1. Sending a message (Send).

go-nym/example/main.go

Lines 61 to 66 in 17a1c03

// Send a message
addr := "2w2mvQzGHuzXdz1pQSvTWXiqZe26Z2BKNkFTQ5g7MuLi.DfkhfLipgtuRLAWWHx74iGkJWCpM6U5RFwaJ3FUaMicu@HWdr8jgcr32cVGbjisjmwnVF4xrUBRGvbw86F9e3rFzS"
r := nym.NewSend("Mix it up!", addr)
if err := client.SendRequestAsText(r); err != nil {
fmt.Fprintln(os.Stderr, err)
}

  1. Sending a SURB message to receive an anonymous response (SendAnonymous).

go-nym/example/main.go

Lines 68 to 74 in 17a1c03

// Send an anonymous message
addr = "2w2mvQzGHuzXdz1pQSvTWXiqZe26Z2BKNkFTQ5g7MuLi.DfkhfLipgtuRLAWWHx74iGkJWCpM6U5RFwaJ3FUaMicu@HWdr8jgcr32cVGbjisjmwnVF4xrUBRGvbw86F9e3rFzS"
replySurbs := 1
r = nym.NewSendAnonymous("Enjoy your anonymous!", addr, replySurbs)
if err := client.SendRequestAsText(r); err != nil {
fmt.Fprintln(os.Stderr, err)
}

  1. Sending a reply to the SendAnonymous message (Reply).

go-nym/example/main.go

Lines 76 to 81 in 17a1c03

// Reply to an anonymous message
senderTag := "7vv2LmF9M6EwQRrmCiCJhr"
r = nym.NewReply("Pong.", senderTag)
if err := client.SendRequestAsText(r); err != nil {
fmt.Fprintln(os.Stderr, err)
}

Closing the connection correctly

  1. We close the connection with the nym-client after the interrupt (sigint/sigkill) and wait for the reading gorutine sent done signal.

go-nym/example/main.go

Lines 83 to 91 in 17a1c03

// Waiting for the kill or interrupt signal
<-interrupt
// Closing the client
if err := client.Close(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
// Waiting for the done signal
<-done
fmt.Println("Done.")

Support the developer (Mixnodes)

Below is a list of the developer's mixnodes. If you are looking for a node for delegating tokens, you can take a closer look at my options.

Dynamic JSON Badge Dynamic JSON Badge Dynamic JSON Badge Dynamic JSON Badge

Dynamic JSON Badge Dynamic JSON Badge Dynamic JSON Badge Dynamic JSON Badge

About

A new abstraction layer for communicating with the nym-client via websockets

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages