Skip to content

Golang library to handle transparent remote node pairing

License

Notifications You must be signed in to change notification settings

kairos-io/go-nodepair

Repository files navigation

go-nodepair

A small library to handle transparent remote node pairing.

Usage

On one side (that is a separate binary, or either a go routine), we generate a token, display it as QR code and we wait for pairing to complete:

import (
	"context"
	"fmt"
	"time"

	nodepair "github.com/kairos-io/go-nodepair"
	qr "github.com/kairos-io/go-nodepair/qrcode"
)

func main() {

	ctx, cancel := context.WithCancel(context.Background())

    // Generate a token, and print it out on screen as QR code
	t := nodepair.GenerateToken()
	qr.Print(t)

	// bind to the token that we previously generated until we paired with the other remote
    r := map[string]string{}
	nodepair.Receive(ctx, &r, nodepair.WithToken(t))
	fmt.Println("Just got", r)

}

Pairing is a syncronous step that waits for the other party to touch base. When pairing completes on both ends, the normal execution flow restarts.

The other party can send a payload which can be any type (interface{}).

On the other end:

import (
	"context"
	"fmt"
	"time"

	nodepair "github.com/kairos-io/go-nodepair"
	qr "github.com/kairos-io/go-nodepair/qrcode"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())

	if err := nodepair.Send(
		ctx, map[string]string{"foo": "Bar"},
		nodepair.WithReader(qr.Reader),
	); err != nil {
		fmt.Println("ERROR", err)
		cancel()
	}
	fmt.Println("Finished sending pairing payload")
}

The pairing options can be used in both sender/receiver, allowing to use QR code also for receiving payload, and vice-versa.