Giter Club home page Giter Club logo

go-nym's Introduction

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. The node owner's commission is only 4%, which is a low indicator among other nodes, where the commission can reach up to 20%, or even higher, if you add the node operating cost (sometimes there are 4000 tokens - this is about 28% of the maximum profitability of the node).

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

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

go-nym's People

Contributors

craftdome avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.