lightning

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2019 License: MIT Imports: 9 Imported by: 0

README

Useful and fast interface for lightningd. All methods return gjson.Result, which is a good thing (unless your application relies on a lot of information from many different lightningd responses) and you can learn it in 10 seconds.

godoc.org

This is a simple and resistant client. It comes with a practical invoice listener method and nice (could be nicer?) defaults for retrying and connecting to faulty lightningd nodes.

Usage

package main

import (
  "github.com/fiatjaf/lightningd-gjson-rpc"
  "github.com/tidwall/gjson"
)

var ln *lightning.Client

func main () {
    lastinvoiceindex := getFromSomewhereOrStartAtZero()

    ln = &lightning.Client{
        Path:             "/home/whatever/.lightning/lightning-rpc",
        LastInvoiceIndex: lastinvoiceindex, // only needed if you're going to listen for invoices
        PaymentHandler:   handleInvoicePaid, // only needed if you're going to listen for invoices
    }
    ln.ListenForInvoices() // optional

    nodeinfo, err := ln.Call("getinfo")
    if err != nil {
        log.Fatal("getinfo error: " + err.Error())
    }

    log.Print(nodeinfo.Get("alias").String())
}

// this is called with the result of `waitanyinvoice`
func handlePaymentReceived(inv gjson.Result) {
    index := inv.Get("pay_index").Int()
    saveSomewhere(index)

    hash := inv.Get("payment_hash").String()
    log.Print("one of our invoices was paid: " + hash)
}

Passing parameters

There are three modes of passing parameters, you can call either:

// 1. `Call` with a list of parameters, in the order defined by each command;
ln.Call("invoice", 1000000, "my-label", "my description", 3600)

// 2. `Call` with a single `map[string]interface{}` with all parameters properly named; or
ln.Call("invoice", map[string]interface{
    "msatoshi": "1000000,
    "label": "my-label",
    "description": "my description",
    "preimage": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
})

// 3. `CallNamed` with a list of keys and values passed in the proper order.
ln.CallNamed("invoice",
    "msatoshi", "1000000,
    "label", "my-label",
    "description", "my description",
    "preimage", "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
    "expiry", 3600,
)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTimeout = time.Second * 5
View Source
var InvoiceListeningTimeout = time.Minute * 150
View Source
var WaitPaymentMaxAttempts = 60
View Source
var WaitSendPayTimeout = time.Hour * 24

Functions

This section is empty.

Types

type Client

type Client struct {
	Path             string
	PaymentHandler   func(gjson.Result)
	LastInvoiceIndex int
}

func (*Client) Call

func (ln *Client) Call(method string, params ...interface{}) (gjson.Result, error)

func (*Client) CallMessage

func (ln *Client) CallMessage(timeout time.Duration, message JSONRPCMessage) (gjson.Result, error)

func (*Client) CallMessageRaw

func (ln *Client) CallMessageRaw(timeout time.Duration, message JSONRPCMessage) ([]byte, error)

func (*Client) CallNamed

func (ln *Client) CallNamed(method string, params ...interface{}) (gjson.Result, error)

func (*Client) CallNamedWithCustomTimeout

func (ln *Client) CallNamedWithCustomTimeout(
	timeout time.Duration,
	method string,
	params ...interface{},
) (res gjson.Result, err error)

func (*Client) CallWithCustomTimeout

func (ln *Client) CallWithCustomTimeout(
	timeout time.Duration,
	method string,
	params ...interface{},
) (gjson.Result, error)

func (*Client) ListenForInvoices

func (ln *Client) ListenForInvoices()

ListenForInvoices starts a goroutine that will repeatedly call waitanyinvoice. Each payment received will be fed into the client.PaymentHandler function. You can change that function in the meantime. Or you can set it to nil if you want to stop listening for invoices.

func (*Client) PayAndWaitUntilResolution

func (ln *Client) PayAndWaitUntilResolution(
	bolt11 string,
	params map[string]interface{},
) (success bool, payment gjson.Result, tries []Try, err error)

PayAndWaitUntilResolution implements its 'pay' logic, querying and retrying routes. It's like the default 'pay' plugin, but it blocks until a final success or failure is achieved. After it returns you can be sure a failed payment will not succeed anymore. Any value in params will be passed to 'getroute' or 'sendpay' or smart defaults will be used. This includes values from the default 'pay' plugin.

type ErrorCommand

type ErrorCommand struct {
	Message string      `json:"msg"`
	Code    int         `json:"code"`
	Data    interface{} `json:"data"`
}

func (ErrorCommand) Error

func (l ErrorCommand) Error() string

type ErrorConnect

type ErrorConnect struct {
	Path    string `json:"path"`
	Message string `json:"message"`
}

func (ErrorConnect) Error

func (c ErrorConnect) Error() string

type ErrorConnectionBroken

type ErrorConnectionBroken struct{}

func (ErrorConnectionBroken) Error

func (c ErrorConnectionBroken) Error() string

type ErrorJSONDecode

type ErrorJSONDecode struct {
	Message string `json:"message"`
}

func (ErrorJSONDecode) Error

func (j ErrorJSONDecode) Error() string

type ErrorTimeout

type ErrorTimeout struct {
	Seconds int `json:"seconds"`
}

func (ErrorTimeout) Error

func (t ErrorTimeout) Error() string

type JSONRPCError

type JSONRPCError struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

type JSONRPCMessage

type JSONRPCMessage struct {
	Version string      `json:"jsonrpc"`
	Id      interface{} `json:"id"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params"`
}

type JSONRPCResponse

type JSONRPCResponse struct {
	Version string          `json:"jsonrpc"`
	Id      interface{}     `json:"id"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *JSONRPCError   `json:"error,omitempty"`
}

type Try

type Try struct {
	Route   interface{}   `json:"route"`
	Error   *ErrorCommand `json:"error"`
	Success bool          `json:"success"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL