lnd_clientをgoで

とりあえず動かしてみるというレベル。 下記のコードでcertとmacaroonを準備して

$ go run main.go --lnd.network=$LND_NETWORK --lnd.host=$LND_HOST --lnd.macaroondir=$MACAROON_PATH --lnd.tlspath=$TLS_CERT_PATH

すれば動く。

package main

import (  
    "context"
    "fmt"
    "log"
    "os"
    "os/signal"
    "syscall"

    flags "github.com/jessevdk/go-flags"
    "github.com/lightningnetwork/lnd/lnrpc"

    "github.com/lightninglabs/loop/lndclient"
)

type lndConfig struct {  
    // Host is the RPC address of the lnd instance that is connecting
    // to.
    Host string `long:"host" description:"lnd instance rpc address"`

    // Network is the network that lnd is running on, i.e. mainnet.
    Network string `long:"network" description:"network to run on" choice:"regtest" choice:"testnet" choice:"mainnet" choice:"simnet"`

    // MacaroonDir is the path to lnd macaroons.
    MacaroonDir string `long:"macaroondir" description:"Path to lnd macaroons"`

    // TLSPath is the path to the lnd TLS certificate.
    TLSPath string `long:"tlspath" description:"Path to lnd tls certificate"`
}

type config struct {  
    // Lnd refers to the user's lnd configuration properties that we need to
    // connect to it.
    Lnd *lndConfig `group:"lnd" namespace:"lnd"`
}

var (  
    cfg = config{}
)

// Main is the true entrypoint to candy.
func main() {  
    // TODO: Prevent from running twice.
    err := start()
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}

func start() error {  
    _, err := flags.Parse(&cfg)
    if err != nil {
        if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
        } else {
            return err
        }
    }

    // Initialize our lnd client.
    lnd, err := lndclient.NewBasicClient(
        cfg.Lnd.Host, cfg.Lnd.TLSPath, cfg.Lnd.MacaroonDir,
        cfg.Lnd.Network, lndclient.MacFilename("admin.macaroon"),
    )
    if err != nil {
        return err
    }

    resp, err := lnd.GetInfo(context.Background(), &lnrpc.GetInfoRequest{})
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(resp.GetIdentityPubkey())

    return nil
}