Monero's p2p and RPC in go. Fork of https://github.com/cirocosta/go-monero
This repository has been archived on 2024-04-07. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
Ciro S. Costa 4dd1b58438 cmd: add get-transactions
Signed-off-by: Ciro S. Costa <ciroscosta@vmware.com>
2021-05-31 14:28:05 -04:00
.github/workflows ci: setup gh actions 2021-04-17 14:56:49 -04:00
assets cmd: get-last-block-header 2021-05-22 18:32:37 -04:00
cmd cmd: add get-transactions 2021-05-31 14:28:05 -04:00
pkg cmd: add get-transactions 2021-05-31 14:28:05 -04:00
.gitignore crawl: add country lookup 2021-04-25 19:18:55 -04:00
go.mod cmd: add geoip 2021-04-25 18:09:25 -04:00
go.sum cmd: add geoip 2021-04-25 18:09:25 -04:00
LICENSE init 2021-04-16 09:54:12 -04:00
Makefile crawl: add country lookup 2021-04-25 19:18:55 -04:00
README.md readme: remove TODO 2021-04-25 17:03:20 -04:00

go-monero

A Go library (and CLI) for interacting with Monero daemons via RPC or the P2P network free of CGO, either on clearnet or not.

Quick start

Library

$ go get -u -v github.com/cirocosta/go-monero

go-monero exposes two high-level packages: levin and daemonrpc.

The first (levin) is used for interacting with the p2p network via plain TCP (optionally, Tor and I2P can also be used via socks5 proxy - see options). For instance, to reach out to a node (of a particular address addr) and grab its list of connected peers (information that comes out of the initial handshake):

import (
        "fmt"
        "context"

        "github.com/cirocosta/go-monero/pkg/levin
)

func ListNodePeers(ctx context.Context, addr string) error {
        // start a client - this will actually establish a TCP `connect()`ion 
        // with the other node.
        //
	client, err := levin.NewClient(ctx, addr)
	if err != nil {
		return fmt.Errorf("new client '%s': %w", addr, err)
	}

        // close the connection when done
        //
	defer client.Close()

        // perform the handshake
        //
	pl, err := client.Handshake(ctx)
	if err != nil {
		return fmt.Errorf("handshake: %w", err)
	}

        // list the peers reported back (250 max per monero's implementation)
        //
	for addr := range pl.Peers {
		fmt.Println(addr)
	}

        return nil
}

The second (daemonrpc), is used to communicate with monerod via its HTTP endpoints. Note that not all endpoints/fields are exposed on a given port - if it's being served in a restricted manner, you'll have access to less endpoints than you see in the documentation (https://www.getmonero.org/resources/developer-guides/daemon-rpc.html)

For instance:

import (
        "fmt"
        "context"

        "github.com/cirocosta/go-monero/pkg/daemonrpc"
)

func ShowBlockHeight (ctx context.Context, addr string) error {
	client, err := daemonrpc.NewClient(addr)
	if err != nil {
		return fmt.Errorf("new client for '%s': %w", addr, err)
	}

	resp, err := client.GetBlockCount()
	if err != nil {
		return fmt.Errorf("get block count: %w", err)
	}

        fmt.Println(resp.Count)
	return nil
}

CLI

Under cmd/monero you'll find a command line interface that exposes most of the functionality that the library provides.

$ GO111MODULE=on go get github.com/cirocosta/go-monero/cmd/monero

$ monero --help
Usage:
  monero [OPTIONS] <command>

Application Options:
  -v, --verbose  dump http requests and responses to stderr [$MONEROD_VERBOSE]
  -a, --address= RPC server address [$MONEROD_ADDRESS]

Help Options:
  -h, --help     Show this help message

Available commands:
  crawl                 Crawl over the network to find all peers

  p2p-peer-list         Find out the list of local peers known by a node

  get-block-count       Get the block count
  get-block-template    Get a block template on which mining a new block
  get-coinbase-tx-sum   Get the coinbase amount and the fees amount for n last blocks starting at particular height
  get-connections       Retrieve information about incoming and outgoing connections to your node (restricted)
  get-fee-estimate      Gives an estimation on fees per byte
  get-info              Retrieve general information about the state of your node and the network. (restricted)
  get-transaction-pool  Get all transactions in the pool
  on-get-block-hash     Look up a block's hash by its height
  sync-info             Get synchronisation information (restricted)

License

See LICENSE.

Thanks

Big thanks to the Monero community and other projects around cryptonote: