Coder Social home page Coder Social logo

go-monero's Introduction

go-monero

This package is a hub of monero related tools for Go. At this time, only the Wallet RPC Client is available.

Go Report Card Build Status

Wallet RPC Client

GoDoc

The go-monero/walletrpc package is a RPC client with all the methods of the v0.11.0.0 release. It does support digest authentication, however I don't recommend using it alone (without https). If there is a need to split the RPC client and server into separate instances, you could put a proxy on the instance that contains the RPC server and check the authenticity of the requests using https + X-API-KEY headers between the proxy and this RPC client (there is an example about this implementation below)

Installation

go get -u github.com/gabstv/go-monero/walletrpc

Usage

The simplest way to use walletrpc is if you have both the server (monero-wallet-rpc) and the client on the same machine.

Running monero-wallet-rpc:

monero-wallet-rpc --testnet --wallet-file ~/testnet/mywallet.bin --rpc-bind-port 18082 --disable-rpc-login

Go:

package main

import (
	"fmt"
	"os"

	"github.com/gabstv/go-monero/walletrpc"
)

func main() {
	// Start a wallet client instance
	client := walletrpc.New(walletrpc.Config{
		Address: "http://127.0.0.1:18082/json_rpc",
	})

	// check wallet balance
	balance, unlocked, err := client.GetBalance()

	// there are two types of error that can happen:
	//   connection errors
	//   monero wallet errors
	// connection errors are pretty much unicorns if everything is on the
	// same instance (unless your OS hit an open files limit or something)
	if err != nil {
		if iswerr, werr := walletrpc.GetWalletError(err); iswerr {
			// it is a monero wallet error
			fmt.Printf("Wallet error (id:%v) %v\n", werr.Code, werr.Message)
			os.Exit(1)
		}
		fmt.Println("Error:", err.Error())
		os.Exit(1)
	}

	fmt.Println("Balance:", walletrpc.XMRToDecimal(balance))
	fmt.Println("Unlocked balance:", walletrpc.XMRToDecimal(unlocked))

	// Make a transfer
	res, err := client.Transfer(walletrpc.TransferRequest{
		Destinations: []walletrpc.Destination{
			{
				Address: "45eoXYNHC4LcL2Hh42T9FMPTmZHyDEwDbgfBEuNj3RZUek8A4og4KiCfVL6ZmvHBfCALnggWtHH7QHF8426yRayLQq7MLf5",
				Amount:  10000000000, // 0.01 XMR
			},
		},
		Priority: walletrpc.PriorityUnimportant,
		Mixin:    1,
	})
	if err != nil {
		if iswerr, werr := walletrpc.GetWalletError(err); iswerr {
			// insufficient funds return a monero wallet error
			// walletrpc.ErrGenericTransferError
			fmt.Printf("Wallet error (id:%v) %v\n", werr.Code, werr.Message)
			os.Exit(1)
		}
		fmt.Println("Error:", err.Error())
		os.Exit(1)
	}
	fmt.Println("Transfer success! Fee:", walletrpc.XMRToDecimal(res.Fee), "Hash:", res.TxHash)
}

Using Digest Authentication

monero-wallet-rpc --testnet --rpc-bind-ip 127.0.0.1 --rpc-bind-port 29567 --rpc-login john:doe --wallet-file ~/testnet/wallet_03.bin
package main

import (
	"fmt"

	"github.com/gabstv/go-monero/walletrpc"
	"github.com/gabstv/httpdigest"
)

func main() {
	// username: john
	// password: doe
	t := httpdigest.New("john", "doe")

	client := walletrpc.New(walletrpc.Config{
		Address:   "http://127.0.0.1:29567/json_rpc",
		Transport: t,
	})

	balance, unlocked, err := client.GetBalance()

	if err != nil {
		panic(err)
	}
	fmt.Println("balance", walletrpc.XMRToDecimal(balance))
	fmt.Println("unlocked balance", walletrpc.XMRToDecimal(unlocked))
}

Using a proxy

You can use a proxy to be in between this client and the monero RPC server. This way you can use a safe encryption tunnel around the network.

Starting the RPC server

monero-wallet-rpc --testnet --wallet-file ~/testnet/mywallet.bin --rpc-bind-port 18082 --disable-rpc-login

Starting a proxy server

This example uses sandpiper (github.com/gabstv/sandpiper/sandpiper) but you could also use nginx or apache

sandpiper config.yml:

debug: true
#listen_addr:     :8084
listen_addr_tls: :23456
fallback_domain: moneroproxy
routes:
  - 
    domain:        moneroproxy
    out_conn_type: HTTP
    out_addr:      localhost:18082
    auth_mode:  apikey
    auth_key:   X-API-KEY
    auth_value: 55c12fca1b994455d3ec1795bdc82cca
    tls_cert_file: moneroproxy.cert.pem
    tls_key_file:  moneroproxy.key.pem

The Go program is similar, but it uses an API-KEY:

package main

import (
	"fmt"
    "os"
    "net/http"
    "crypto/tls"

	"github.com/gabstv/go-monero/walletrpc"
)

func main() {
	// Start a wallet client instance
	client := walletrpc.New(walletrpc.Config{
        Address: "http://127.0.0.1:23456/json_rpc",
        CustomHeaders: map[string]string{
			"X-API-KEY": "55c12fca1b994455d3ec1795bdc82cca", // we use the same key defined above
        },
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true, // WARNING: instead of this, you can
				// provide (or install) a certificate to make it
				// really secure with Certificates: []tls.Certificate{},
			},
		},
	})

	// check wallet balance
	balance, unlocked, err := client.GetBalance()

	// there are two types of error that can happen:
	//   connection errors
	//   monero wallet errors
	// connection errors are pretty much unicorns if everything is on the
	// same instance (unless your OS hit an open files limit or something)
	if err != nil {
		if iswerr, werr := walletrpc.GetWalletError(err); iswerr {
			// it is a monero wallet error
			fmt.Printf("Wallet error (id:%v) %v\n", werr.Code, werr.Message)
			os.Exit(1)
		}
		fmt.Println("Error:", err.Error())
		os.Exit(1)
	}

	fmt.Println("Balance:", walletrpc.XMRToDecimal(balance))
    fmt.Println("Unlocked balance:", walletrpc.XMRToDecimal(unlocked))
}

go-monero's People

Contributors

gabstv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-monero's Issues

type []walletrpc.Payment has no field or method

The following program doens't work

package main

import (
        "fmt"

        "github.com/gabstv/go-monero/walletrpc"
)
/*
type Payment struct {
	PaymentID   string
	TxHash      string
	Amount      uint64
	BlockHeight uint64
	UnlockTime  uint64
}
*/

func main() {
        client := walletrpc.New(walletrpc.Config{
                Address: "http://127.0.0.1:18085/json_rpc",})

        var check []walletrpc.Payment

        check, _ = client.GetPayments("0000000000000000")

        fmt.Println(check.Amount)
}

Error message:
./main.go:18:19: check.Amount undefined (type []walletrpc.Payment has no field or method Amount)

Link to the Payment type: https://pkg.go.dev/github.com/gabstv/go-monero/walletrpc#Payment
I have tried to grab the other variables in this type with the same issue.

Add daemon API

First of all I want to thank you for this package :), I'm using it for a little project I'm working on and it works wonderfully.

I want to use some daemon calls (for now mostly just GetHeight to track if the wallet is up to date). Right now I'm just doing a simple POST myself, but it would be nice if go-monero would handle this as well.

This issue is mostly just for keeping track of progress, if I have time to spare I might try creating a PR in the future.

monero-wallet-rpc server

hello,what do you mean " if you have both the server (monero-wallet-rpc server and the client on the same machine"?when I run the command ,it returned "This is the RPC monero wallet. It needs to connect to a monero",what I should do?

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.