-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
158 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# go eclai | ||
|
||
## what? | ||
|
||
A simple API client for [Acinq's Eclair](https://github.com/ACINQ/eclair) [lightning node JSON API](https://acinq.github.io/eclair/#introduction) | ||
|
||
|
||
|
||
### Examples | ||
|
||
Some helpful examples are available in [the examples folder](./examples) | ||
|
||
### Create Invoice | ||
|
||
|
||
|
||
|
||
#### Watching Events | ||
|
||
It is possible to listen for a handful of events that the node emits. This is done over websocket. Events that be subscribed to: | ||
|
||
| Event Type | Description | | ||
|---------------------------|---------------------------------------------------------------| | ||
| `payment-received` | A payment has been received | | ||
| `payment-relayed` | A payment has been successfully relayed | | ||
| `payment-sent` | A payment has been successfully sent | | ||
| `payment-settling-onchain`| A payment wasn't fulfilled and its HTLC is being redeemed on-chain | | ||
| `payment-failed` | A payment failed | | ||
| `channel-created` | A channel opening flow has started | | ||
| `channel-opened` | A channel opening flow has completed | | ||
| `channel-state-changed` | A channel state changed (e.g. going from offline to connected)| | ||
| `channel-closed` | A channel has been closed | | ||
| `onion-message-received` | An onion message was received | | ||
|
||
To open a channel and watch for events | ||
|
||
```go | ||
client = client.WithBaseURL("http://localhost:8282") // if you are using polar for a local setup | ||
|
||
channel, err := client.Subscribe() | ||
if err != nil { | ||
panic(err) | ||
return | ||
} | ||
|
||
// example fo handling events | ||
for message := range channel { | ||
switch message.Type { | ||
case eclair.ChannelOpened: | ||
// handle channel opened event | ||
case eclair.ChannelClosed: | ||
// handle channel closed event | ||
case eclair.PaymentReceived | ||
// handle channel closed event | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package eclair | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"mime/multipart" | ||
) | ||
|
||
const ( | ||
createPath = "/createinvoice" | ||
) | ||
|
||
type CreateInvoiceResponse struct { | ||
Prefix string `json:"prefix"` | ||
Timestamp int64 `json:"timestamp"` | ||
NodeId string `json:"nodeId"` | ||
Serialized string `json:"serialized"` | ||
Description string `json:"description"` | ||
PaymentHash string `json:"paymentHash"` | ||
PaymentMetadata string `json:"paymentMetadata"` | ||
Expiry int `json:"expiry"` | ||
MinFinalCltvExpiry int `json:"minFinalCltvExpiry"` | ||
Amount int `json:"amount"` | ||
Features InvoiceFeatures `json:"features"` | ||
RoutingInfo []interface{} `json:"routingInfo"` | ||
} | ||
|
||
type CreateInvoiceRequest struct { | ||
Description string `json:"description"` | ||
DescriptionHash string `json:"descriptionHash"` | ||
Expiry int `json:"expireIn"` | ||
Amount int `json:"amountMsat"` | ||
FallbackAddress string `json:"fallbackAddress"` | ||
PaymentPreimage string `json:"paymentPreimage"` | ||
} | ||
|
||
func (c *CreateInvoiceRequest) ToBuffer() (io.ReadWriter, error) { | ||
var b bytes.Buffer | ||
w := multipart.NewWriter(&b) | ||
err := w.WriteField("description", c.Description) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = w.WriteField("amountMsat", fmt.Sprintf("%d", c.Amount)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
w.Close() | ||
return &b, nil | ||
} | ||
|
||
type InvoiceFeatures struct { | ||
Activated map[string]string `json:"activated"` | ||
Unknown []interface{} `json:"unknown"` | ||
} | ||
|
||
func (c *Client) CreateInvoice(settings CreateInvoiceRequest) (*CreateInvoiceResponse, error) { | ||
resp := CreateInvoiceResponse{} | ||
body, err := settings.ToBuffer() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
data, err := c.Post(createPath, &body, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = json.Unmarshal(data, &resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &resp, nil | ||
} |