Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Commit

Permalink
GET /api/history?only_status(paid|expired|pending) added (#14 vol11);…
Browse files Browse the repository at this point in the history
… Streaming status for LN added to GET /api/payment (#30 beta)
  • Loading branch information
meeDamian committed Jan 18, 2019
1 parent 844af42 commit d1e6a7d
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 116 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,24 @@ On error, returns:

## `GET /api/payment?hash=LN-hash&address=BTC-address`

Takes two parameters:
#### Takes:

* `hash` - hash of the preimage returned previously by the `POST /payment` endpoint
* `address` - Bitcoin address returned previously by the `POST /payment` endpoint

> **NOTE:** providing just one will run checks on one network only.
Returns:
#### Returns:

#### on expiry (code 408)
##### on expiry (code 408)

```json
{
"error": "expired"
}
```

#### on LN success (code 200)
##### on LN success (code 200)

```json
{
Expand All @@ -151,7 +151,7 @@ Returns:
}
```

#### on BTC success w/exact amount (code 200)
##### on BTC success w/exact amount (code 200)

```json
{
Expand All @@ -166,7 +166,7 @@ Returns:
}
```

#### on BTC success w/too big amount (code 202)
##### on BTC success w/too big amount (code 202)

```json
{
Expand All @@ -181,7 +181,7 @@ Returns:
}
```

#### on BTC success w/too small amount (code 402)
##### on BTC success w/too small amount (code 402)

```json
{
Expand All @@ -197,17 +197,20 @@ Returns:
}
```

#### On any other error
##### On any other error
```json
{
"error": "error message…"
}
```


## `GET /api/history`

Presently takes no arguments, and returns (various success cases included below):
#### Takes:

* `only_status` - filter payments to only one specific state: `paid`, `expired` or `panding`.

#### Returns <small>(various cases included below):</small>

```json
{
Expand Down
15 changes: 10 additions & 5 deletions clightning/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cLightning

import (
"context"
"github.com/lncm/invoicer/common"
"github.com/pkg/errors"
)
Expand All @@ -9,23 +10,27 @@ const ClientName = "clightning"

type CLightning struct{}

func (cLightning CLightning) Invoice(amount int64, desc string) (invoice, hash string, err error) {
func (cLightning CLightning) NewInvoice(ctx context.Context, amount int64, desc string) (invoice, hash string, err error) {
return invoice, hash, errors.New("not implemented yet")
}

func (cLightning CLightning) Status(hash string) (s common.Status, err error) {
func (cLightning CLightning) Status(ctx context.Context, hash string) (s common.Status, err error) {
return s, errors.New("not implemented yet")
}

func (cLightning CLightning) Address(bool) (address string, err error) {
func (cLightning CLightning) StatusWait(ctx context.Context, hash string) (s common.Status, err error) {
return s, errors.New("not implemented yet")
}

func (cLightning CLightning) Address(context.Context, bool) (address string, err error) {
return address, errors.New("not implemented yet")
}

func (cLightning CLightning) Info() (info common.Info, err error) {
func (cLightning CLightning) Info(ctx context.Context) (info common.Info, err error) {
return info, errors.New("not implemented yet")
}

func (cLightning CLightning) History() (invoices common.Invoices, err error) {
func (cLightning CLightning) History(ctx context.Context) (invoices common.Invoices, err error) {
return invoices, errors.New("not implemented yet")
}

Expand Down
1 change: 1 addition & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type (
AddrsStatus []AddrStatus

StatusReply struct {
Code int `json:"-"`
Error string `json:"error,omitempty"`
Ln *Status `json:"ln,omitempty"`
Bitcoin *AddrStatus `json:"bitcoin,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ require (
github.com/gin-contrib/cors v0.0.0-20181008113111-488de3ec974f
github.com/gin-contrib/gzip v0.0.0-20190101123152-0eb78e93402e
github.com/gin-gonic/gin v1.3.0
github.com/go-playground/locales v0.12.1 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.5.1 // indirect
github.com/juju/clock v0.0.0-20180808021310-bab88fc67299 // indirect
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 // indirect
Expand All @@ -14,13 +16,15 @@ require (
github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073 // indirect
github.com/juju/utils v0.0.0-20180820210520-bf9cc5bdd62d // indirect
github.com/juju/version v0.0.0-20180108022336-b64dbd566305 // indirect
github.com/leodido/go-urn v1.1.0 // indirect
github.com/lightningnetwork/lnd v0.5.1-beta
github.com/pkg/errors v0.8.0
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af // indirect
github.com/ugorji/go/codec v0.0.0-20181127175209-856da096dbdf // indirect
google.golang.org/genproto v0.0.0-20181127195345-31ac5d88444a // indirect
google.golang.org/grpc v1.16.0
gopkg.in/errgo.v1 v1.0.0 // indirect
gopkg.in/go-playground/validator.v9 v9.25.0
gopkg.in/macaroon-bakery.v2 v2.1.0 // indirect
gopkg.in/macaroon.v2 v2.0.0
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cou
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc=
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM=
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
Expand Down Expand Up @@ -70,6 +74,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
github.com/lightninglabs/gozmq v0.0.0-20180324010646-462a8a753885/go.mod h1:KUh15naRlx/TmUMFS/p4JJrCrE6F7RGF7rsnvuu45E4=
github.com/lightninglabs/neutrino v0.0.0-20181017011010-4d6069299130/go.mod h1:KJq43Fu9ceitbJsSXMILcT4mGDNI/crKmPIkDOZXFyM=
github.com/lightningnetwork/lnd v0.5.1-beta h1:ft+kzuYDz8o4iE0VgU/qTyb8uc7wB8RI1EONWCQc3oI=
Expand Down Expand Up @@ -138,6 +144,8 @@ gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXa
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
gopkg.in/go-playground/validator.v9 v9.25.0 h1:Q3c4LgUofOEtz0wCE18Q2qwDkATLHLBUOmTvqjNCWkM=
gopkg.in/go-playground/validator.v9 v9.25.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/macaroon-bakery.v2 v2.1.0 h1:9Jw/+9XHBSutkaeVpWhDx38IcSNLJwWUICkOK98DHls=
gopkg.in/macaroon-bakery.v2 v2.1.0/go.mod h1:B4/T17l+ZWGwxFSZQmlBwp25x+og7OkhETfr3S9MbIA=
gopkg.in/macaroon.v2 v2.0.0 h1:LVWycAfeJBUjCIqfR9gqlo7I8vmiXRr51YEOZ1suop8=
Expand Down
48 changes: 29 additions & 19 deletions lnd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ var (
readOnlyMacaroon = flag.String("lnd-readonly", "readonly.macaroon", "Specify path to readonly.macaroon file")
)

func (lnd Lnd) Invoice(amount int64, desc string) (invoice, hash string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

func (lnd Lnd) NewInvoice(ctx context.Context, amount int64, desc string) (invoice, hash string, err error) {
inv, err := lnd.invoiceClient.AddInvoice(ctx, &lnrpc.Invoice{
Memo: desc,
Value: int64(amount),
Expand All @@ -47,10 +44,32 @@ func (lnd Lnd) Invoice(amount int64, desc string) (invoice, hash string, err err
return inv.PaymentRequest, hex.EncodeToString(inv.RHash), nil
}

func (lnd Lnd) Status(hash string) (s common.Status, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// TODO: do not resubscribe on each request
func (lnd Lnd) StatusWait(ctx context.Context, hash string) (s common.Status, err error) {
invSub, err := lnd.invoiceClient.SubscribeInvoices(ctx, &lnrpc.InvoiceSubscription{})
if err != nil {
return
}

for {
var inv *lnrpc.Invoice
inv, err = invSub.Recv()
if err != nil {
return
}

if hash == hex.EncodeToString(inv.RHash) {
return common.Status{
Ts: inv.CreationDate,
Settled: inv.Settled,
Expiry: inv.Expiry,
Value: inv.Value,
}, nil
}
}
}

func (lnd Lnd) Status(ctx context.Context, hash string) (s common.Status, err error) {
invId, err := hex.DecodeString(hash)
if err != nil {
return
Expand All @@ -69,10 +88,7 @@ func (lnd Lnd) Status(hash string) (s common.Status, err error) {
}, nil
}

func (lnd Lnd) Address(bech32 bool) (address string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

func (lnd Lnd) Address(ctx context.Context, bech32 bool) (address string, err error) {
addrType := lnrpc.NewAddressRequest_NESTED_PUBKEY_HASH
if bech32 {
addrType = lnrpc.NewAddressRequest_WITNESS_PUBKEY_HASH
Expand All @@ -88,10 +104,7 @@ func (lnd Lnd) Address(bech32 bool) (address string, err error) {
return addrResp.Address, nil
}

func (lnd Lnd) Info() (info common.Info, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()

func (lnd Lnd) Info(ctx context.Context) (info common.Info, err error) {
i, err := lnd.readOnlyClient.GetInfo(ctx, &lnrpc.GetInfoRequest{})
if err != nil {
return
Expand All @@ -100,10 +113,7 @@ func (lnd Lnd) Info() (info common.Info, err error) {
return common.Info{Uris: i.Uris}, nil
}

func (lnd Lnd) History() (invoices common.Invoices, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

func (lnd Lnd) History(ctx context.Context) (invoices common.Invoices, err error) {
list, err := lnd.readOnlyClient.ListInvoices(ctx, &lnrpc.ListInvoiceRequest{
NumMaxInvoices: 100,
Reversed: true,
Expand Down
Loading

0 comments on commit d1e6a7d

Please sign in to comment.