0

I wanto to send a simple "getinfo" request to lnd using golang. I currently have

package main

import (
    "fmt"
    "log"
    "net/rpc"
)

func main() {
    client, err := rpc.Dial("tcp", "localhost:10009")
    if err != nil {
        log.Fatal("dialing:", err)
    }

    type Rep struct {
        identity_pubkey       string
        alias                 string
        num_pending_channels  uint32
        num_active_channels   uint32
        num_peers             uint32
        block_height          uint32
        block_hash            string
        synced_to_chain       bool
        testnet               bool
        chains                string
        uris                  string
        best_header_timestamp int64
    }

    var reply Rep

    args := ""

    err = client.Call("getinfo", args, &reply)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(reply)
}

but i get 2018/04/12 23:41:09 unexpected EOF

1 Answer 1

2

Lnd uses

Here is an example code for connecting to lnd and doing getinfo request. It assumes that you use macaroons. You may need to insert correct paths for tls cert and macaroons file.

package main

import (
    "context"
    "fmt"
    "github.com/davecgh/go-spew/spew"
    "github.com/lightningnetwork/lnd/lnrpc"
    "github.com/lightningnetwork/lnd/macaroons"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials"
    "gopkg.in/macaroon.v2"
    "io/ioutil"
    "os/user"
    "path"
)

func main() {
    usr, err := user.Current()
    if err != nil {
        fmt.Println("Cannot get current user:", err)
        return
    }
    fmt.Println("The user home directory: " + usr.HomeDir)
    tlsCertPath := path.Join(usr.HomeDir, ".lnd/tls.cert")
    macaroonPath := path.Join(usr.HomeDir, ".lnd/admin.macaroon")

    tlsCreds, err := credentials.NewClientTLSFromFile(tlsCertPath, "")
    if err != nil {
        fmt.Println("Cannot get node tls credentials", err)
        return
    }

    macaroonBytes, err := ioutil.ReadFile(macaroonPath)
    if err != nil {
        fmt.Println("Cannot read macaroon file", err)
        return
    }

    mac := &macaroon.Macaroon{}
    if err = mac.UnmarshalBinary(macaroonBytes); err != nil {
        fmt.Println("Cannot unmarshal macaroon", err)
        return
    }

    opts := []grpc.DialOption{
        grpc.WithTransportCredentials(tlsCreds),
        grpc.WithBlock(),
        grpc.WithPerRPCCredentials(macaroons.NewMacaroonCredential(mac)),
    }

    conn, err := grpc.Dial("localhost:10009", opts...)
    if err != nil {
        fmt.Println("cannot dial to lnd", err)
        return
    }
    client := lnrpc.NewLightningClient(conn)

    ctx := context.Background()
    getInfoResp, err := client.GetInfo(ctx, &lnrpc.GetInfoRequest{})
    if err != nil {
        fmt.Println("Cannot get info from node:", err)
        return
    }
    spew.Dump(getInfoResp)
}

There is a great resource about lnd https://dev.lightning.community/

Hope this helps.

3
  • Thanks a lot, but I get a ./client.go:49:61: cannot use mac (type *"gopkg.in/macaroon.v2".Macaroon) as type *"github.com/lightningnetwork/lnd/vendor/gopkg.in/macaroon.v2".Macaroon in argument to macaroons.NewMacaroonCredential ./client.go:57:36: cannot use conn (type *"google.golang.org/grpc".ClientConn) as type *"github.com/lightningnetwork/lnd/vendor/google.golang.org/grpc".ClientConn in argument to lnrpc.NewLightningClient error. I am going to mark it as answered for the moment :) Commented Apr 17, 2018 at 4:05
  • Do you use dep? It seems like you didn't add google.golang.org/grpc to your dependencies. So some libraries are taken from vendor, some not.
    – mkl
    Commented Apr 17, 2018 at 11:24
  • Using dep it worked, I don't quite understand why but it works, thanks. Commented Apr 17, 2018 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.