<p>Lnd uses </p>

<ul>
<li>GRPC Protobuf for API (prefered way) or HTTP REST. Command line tool lncli uses GRPC to communicate with lnd</li>
<li>SSL for data encryption</li>
<li>macaroons for RPC authentication <a href="https://github.com/lightningnetwork/lnd/blob/master/docs/macaroons.md" rel="nofollow noreferrer">https://github.com/lightningnetwork/lnd/blob/master/docs/macaroons.md</a> It can be disabled using --no-macaroons flag</li>
</ul>

<p>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.</p>

<pre><code>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 := &amp;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, &amp;lnrpc.GetInfoRequest{})
    if err != nil {
        fmt.Println("Cannot get info from node:", err)
        return
    }
    spew.Dump(getInfoResp)
}
</code></pre>

<p>There is a great resource about lnd <a href="https://dev.lightning.community/" rel="nofollow noreferrer">https://dev.lightning.community/</a></p>

<p>Hope this helps.</p>

CC BY-SA 3.0
2018-04-17T04:05:55.570Z
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 :)
k76u4vkweek547v7
CC BY-SA 3.0
2018-04-17T11:24:51.010Z
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
CC BY-SA 3.0
2018-04-17T15:29:40.323Z
Using dep it worked, I don't quite understand why but it works, thanks.
k76u4vkweek547v7
CC BY-SA 3.0
2018-04-13T03:23:47.140Z
2018-04-13T03:23:47.140Z
mkl
<p>I wanto to send a simple "getinfo" request to lnd using golang. I currently have</p>

<pre><code>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, &amp;reply)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(reply)
}
</code></pre>

<p>but i get <code>2018/04/12 23:41:09 unexpected EOF</code></p>

<lightning-network><lightning-network-daemon>
RPC call to lnd
k76u4vkweek547v7
# RPC call to lnd

(Asked by: username_0 on 2018-04-13)

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`

**Tags:** lightning-network, lightning-network-daemon


---

## 1 answers

### Answer by username_1 on 2018-04-13. Score: 2 | Accepted answer

Lnd uses

* GRPC Protobuf for API (prefered way) or HTTP REST. Command line tool lncli uses GRPC to communicate with lnd
* SSL for data encryption
* macaroons for RPC authentication <https://github.com/lightningnetwork/lnd/blob/master/docs/macaroons.md> It can be disabled using --no-macaroons flag

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.

> **Comments:**
> **username_0**: 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 :)  
> **username_1**: 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.  
> **username_0**: Using dep it worked, I don't quite understand why but it works, thanks.  

---

