Bitcoin Development Course in Go Language Based on Mixin Network: Creating Robots

Posted by Killswitch on Sun, 05 May 2019 21:40:03 +0200


Mixin Network It is a free and extremely fast end-to-end encrypted digital currency trading system.
In this chapter, you can create a bot in Mixin Messenger to receive user messages according to the tutorial, and learn how to transfer a bit coin to a robot or to a robot to transfer a bit coin to you.

Mixin Network Development Resource Compilation

Course introduction

  1. Create a message-receiving robot
  2. Robot accepts Bitcoin and immediately returns it to the user
  3. Create a Bitcoin Wallet

Create a message-receiving robot

Through this tutorial, you will learn how to use Go to create a robot APP that can accept messages.

Installation of Go 1.12:

Download and install from Go Go

macOS

Download the installation package go1.12.darwin-amd64.pkg Double-click to run, then install as prompted, and finally add the bin directory of Go to $PATH.

echo 'export PATH="/usr/local/opt/go/libexec/bin:$PATH"' >> ~/.bash_profile
source  ~/.bashrc

If everything works, run go version and you'll see the following tips! ____________

go version
go version go1.11.5 darwin/amd64

Ubuntu 18.04

root@n3:/usr/local/bin# snap install go --classic

Ubuntu 16.04

mkdir /usr/local/src
wget https://dl.google.com/go/go1.12.linux-amd64.tar.gz
tar xvf go1.12.linux-amd64.tar.gz
echo 'export PATH=/usr/local/src/go/bin:$PATH' >> ~/.bashrc
root@n3:/usr/local/src# source  ~/.bashrc

If everything works, run go version and you'll see the following tips! ____________

root@n3:/usr/local/bin# go version
go version go1.12 linux/amd64

Create Go s working directory

Creating a working directory for Go is highly recommended, which saves you a lot of trouble with package references.
macOS

mkdir ~/workspace/go
echo 'export GOPATH="$HOME/workspace/go"' >> ~/.bash_profile
source ~/.bash_profile

Ubuntu

mkdir ~/workspace/go
echo 'export GOPATH="$HOME/workspace/go"' >> ~/.bashrc
source ~/.bash_profile

Install Mixin Network SDK for Go

root@n3:~# go get github.com/MooooonStar/mixin-sdk-go
package github.com/MooooonStar/mixin-sdk-go: no Go files in /root/workspace/go/src/github.com/MooooonStar/mixin-sdk-go

Don't worry about the "no Go files" prompt. ls can find the following directories and files. In fact, SDK is in messenger and network respectively.

ls $GOPATH/src/github.com/MooooonStar/mixin-sdk-go

README.md    messenger    network

Create project directory under GOPATH

cd ~/workspace/go/src
mkdir mixin_labs-go-bot
cd mixin_labs-go-bot

Hello, World!

Create the first robot APP

Press the following prompt to mixin.one Create an APP tutorial.

Generate corresponding parameters

Record these Generated parameters
They will be used in main.go.


In the project directory, create main.go and replace the generated parameters with yours!

main.go

const (
    UserId    = "21042518-85c7-4903-bb19-f311813d1f51"
    PinCode   = "911424"
    SessionId = "4267b63d-3daa-449e-bc13-970aa0357776"
    PinToken  = "gUUxpm3fPRVkKZNwA/gk10SHHDtR8LmxO+N6KbsZ/jymmwwVitUHKgLbk1NISdN8jBvsYJgF/5hbkxNnCJER5XAZ0Y35gsAxBOgcFN8otsV6F0FAm5TnWN8YYCqeFnXYJnqmI30IXJTAgMhliLj7iZsvyY/3htaHUUuN5pQ5F5s="
    //please delele the blank of PrivateKey the before each line
    PrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCDXiWJRLe9BzPtXmcVe6acaFTY9Ogb4Hc2VHFjKFsp7QRVCytx
3KC/LRojTFViwwExaANTZQ6ectwpAxIvzeYeHDZCXCh6JRFIYK/ZuREmYPcPQEWD
s92Tv/4XTAdTH8l9UJ4VQY4zwqYMak237N9xEvowT0eR8lpeJG0jAjN97QIDAQAB
AoGADvORLB1hGCeQtmxvKRfIr7aEKak+HaYfi1RzD0kRjyUFwDQkPrJQrVGRzwCq
GzJ8mUXwUvaGgmwqOJS75ir2DL8KPz7UfgQnSsHDUwKqUzULgW6nd/3OdDTYWWaN
cDjbkEpsVchOpcdkywvZhhyGXszpM20Vr8emlBcFUOTfpTUCQQDVVjkeMcpRsImV
U3tPYyiuqADhBTcgPBb+Ownk/87jyKF1CZOPvJAebNmpfJP0RMxUVvT4B9/U/yxZ
WNLhLtCXAkEAnaOEuefUxGdE8/55dUTEb7xrr22mNqykJaax3zFK+hSFBrM3gUY5
fEETtHnl4gEdX4jCPybRVc1JSFY/GWoyGwJBAKoLti95JHkErEXYavuWYEEHLNwv
mgcZnoI6cOKVfEVYEEoHvhTeCkoWHVDZOd2EURIQ1eY18JYIZ0M4Z66R8DUCQCsK
iKTR3dA6eiM8qiEQw6nWgniFscpf3PnCx/Iu3U/m5mNr743GhM+eXSj7136b209I
YfEoQiPxRz8O/W+NBV0CQQDVPNqJlFD34MC9aQN42l3NV1hDsl1+nSkWkXSyhhNR
MpobtV1a7IgJGyt5HxBzgNlBNOayICRf0rRjvCdw6aTP
-----END RSA PRIVATE KEY-----`
)

Replace the above parameters with the ones you generated in mixin.one.

The complete and concise code is as follows

package main

import (
    "context"
    "encoding/base64"
    "encoding/json"
    "log"
    "github.com/MooooonStar/mixin-sdk-go/messenger"
    mixin "github.com/MooooonStar/mixin-sdk-go/network"
)

type Listener struct {
    *messenger.Messenger
}

// interface to implement if you want to handle the message
func (l *Listener) OnMessage(ctx context.Context, msg messenger.MessageView, userId string) error {
    data, err := base64.StdEncoding.DecodeString(msg.Data)
    if err != nil {
        return err
    }
  if msg.Category == "PLAIN_TEXT" {
        log.Printf("I got a message, it said: %s", string(data))
        return l.SendPlainText(ctx, msg.ConversationId, msg.UserId, string(data))
    } else {
        log.Println("Unknown message!", msg.Category)
        return err
    }
}
const (
    UserId    = "21042518-85c7-4903-bb19-f311813d1f51"
    PinCode   = "911424"
    SessionId = "4267b63d-3daa-449e-bc13-970aa0357776"
    PinToken  = "gUUxpm3fPRVkKZNwA/gk10SHHDtR8LmxO+N6KbsZ/jymmwwVitUHKgLbk1NISdN8jBvsYJgF/5hbkxNnCJER5XAZ0Y35gsAxBOgcFN8otsV6F0FAm5TnWN8YYCqeFnXYJnqmI30IXJTAgMhliLj7iZsvyY/3htaHUUuN5pQ5F5s="
    //please delele the blank of PrivateKey the before each line
    PrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCDXiWJRLe9BzPtXmcVe6acaFTY9Ogb4Hc2VHFjKFsp7QRVCytx
3KC/LRojTFViwwExaANTZQ6ectwpAxIvzeYeHDZCXCh6JRFIYK/ZuREmYPcPQEWD
s92Tv/4XTAdTH8l9UJ4VQY4zwqYMak237N9xEvowT0eR8lpeJG0jAjN97QIDAQAB
AoGADvORLB1hGCeQtmxvKRfIr7aEKak+HaYfi1RzD0kRjyUFwDQkPrJQrVGRzwCq
GzJ8mUXwUvaGgmwqOJS75ir2DL8KPz7UfgQnSsHDUwKqUzULgW6nd/3OdDTYWWaN
cDjbkEpsVchOpcdkywvZhhyGXszpM20Vr8emlBcFUOTfpTUCQQDVVjkeMcpRsImV
U3tPYyiuqADhBTcgPBb+Ownk/87jyKF1CZOPvJAebNmpfJP0RMxUVvT4B9/U/yxZ
WNLhLtCXAkEAnaOEuefUxGdE8/55dUTEb7xrr22mNqykJaax3zFK+hSFBrM3gUY5
fEETtHnl4gEdX4jCPybRVc1JSFY/GWoyGwJBAKoLti95JHkErEXYavuWYEEHLNwv
mgcZnoI6cOKVfEVYEEoHvhTeCkoWHVDZOd2EURIQ1eY18JYIZ0M4Z66R8DUCQCsK
iKTR3dA6eiM8qiEQw6nWgniFscpf3PnCx/Iu3U/m5mNr743GhM+eXSj7136b209I
YfEoQiPxRz8O/W+NBV0CQQDVPNqJlFD34MC9aQN42l3NV1hDsl1+nSkWkXSyhhNR
MpobtV1a7IgJGyt5HxBzgNlBNOayICRf0rRjvCdw6aTP
-----END RSA PRIVATE KEY-----`
)

func main() {
    ctx := context.Background()
    m := messenger.NewMessenger(UserId, SessionId, PrivateKey)
    l := &Listener{m}
    go m.Run(ctx, l)
    select {}
}

Compilation and Running

Executing go build creates a mixin_labs-go-bot, and then executes

cd mixin_labs-go-bot
go build
./mixin_labs-go-bot

Installation on Mobile Phone Mixin Messenger Add a robot as a friend (for example, the robot is 70001639) and send a message to it. The effect is as follows!

Source Code Interpretation

WebSocket is a full-duplex communication mode based on TCP. It connects to Mixin Network and sends "LISTPENDINGMESSAGES" message. The server will forward the received message to this program later.

ctx := context.Background()
m := messenger.NewMessenger(UserId, SessionId, PrivateKey)
l := &Listener{m}
go m.Run(ctx, l)

When the server pushes the message to the robot, the robot will reply back intact.

func (l *Listener) OnMessage(ctx context.Context, msg messenger.MessageView, userId string) error {
    data, err := base64.StdEncoding.DecodeString(msg.Data)
    if err != nil {
        return err
    }
  if msg.Category == "PLAIN_TEXT" {
        log.Printf("I got a message, it said: %s", string(data))
        return l.SendPlainText(ctx, msg.ConversationId, msg.UserId, string(data))
    } else {
        log.Println("Unknown message!", msg.Category)
        return err
    }
}

Mixin Messenger supports many types of messages, including text, pictures, videos, voice and so on, which can be viewed in the following links: WebSocket message type.

complete

Now that your robot APP is running, how do you plan to transform your robot?

Complete code Here

Topics: Go SDK network github Ubuntu