Skip to content

Commit

Permalink
Merge pull request #61 from RadarTech/cf-cc-1123-subserver-support
Browse files Browse the repository at this point in the history
Typescript Refactor & Sub-Server Support
  • Loading branch information
cavanmflynn authored Nov 8, 2019
2 parents 08785be + acbefe9 commit 850c5f1
Show file tree
Hide file tree
Showing 101 changed files with 9,470 additions and 1,783 deletions.
23 changes: 0 additions & 23 deletions .eslintrc.js

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ yarn-error.log
.esm-cache
/rpc.proto
*.macaroon
dist
.DS_Store
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
189 changes: 120 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
## LNRPC
## @radar/lnrpc

[![CircleCI](https://img.shields.io/circleci/project/github/RadarTech/lnrpc/master.svg?style=flat)](https://circleci.com/gh/RadarTech/lnrpc)
[![Known Vulnerabilities](https://snyk.io/test/github/RadarTech/lnrpc/badge.svg?targetFile=package.json)](https://snyk.io/test/github/RadarTech/lnrpc?targetFile=package.json)
[![NPM Version](https://img.shields.io/npm/v/@radar/lnrpc.svg?style=flat)](https://www.npmjs.com/package/@radar/lnrpc)
[![License](https://img.shields.io/github/license/radartech/lnrpc.svg?style=flat)](https://img.shields.io/github/license/radartech/lnrpc.svg?style=flat)

Maintained fork of [lnrpc](https://github.com/Matt-Jensen/lnrpc) adding support for generating typescript type definitions.
A Typescript gRPC client for LND with support for all LND sub-servers. Originally forked from [Matt-Jensen/lnrpc](https://github.com/Matt-Jensen/lnrpc).

### Features
- Auto-generates [lnd/lnrpc](https://github.com/lightningnetwork/lnd/tree/master/lnrpc) client and typescript definitons based on target release tag
- Auto-generates [lnd/lnrpc](https://github.com/lightningnetwork/lnd/tree/master/lnrpc) clients and Typescript type definitions using a target release tag
- Supports all LND sub-servers
- Wraps requests in promises
- Easily setup SSL and Macaroons
- Instantiates all gRPC services
- uint64/int64 types cast to string
- uint64/int64 types cast to string to prevent precision loss

### Installation
```sh
Expand All @@ -21,40 +22,96 @@ npm install @radar/lnrpc
yarn add @radar/lnrpc
```

Install [lnd](https://github.com/lightningnetwork/lnd/blob/master/docs/INSTALL.md) before using this project and ensure you have an lnd instance running with `--no-macaroons`, unless you provide macaroon authentication to your lnrpc instance when created.
**Notes:**
- Ensure you have an lnd instance running with `--no-macaroons`, unless you provide macaroon authentication to your lnrpc instance when created.
- If you want to interact with the LND sub-servers, ensure that LND was compiled with the necessary sub-server build tags.

### Change LND gRPC release version
To change the gRPC definitions used for all auto-generated types and RPC methods edit the `config.lnd_release_tag` value in `package.json` to the desired [LND release tag](https://github.com/lightningnetwork/lnd/releases) and run the following:

```sh
npm run update-proto
# OR
yarn update-proto
### Usage

# AND
This package exports a create function for the main gRPC server as well as each sub-server:

npm run generate
# OR
yarn generate
```typescript
import {
createAutopilotRpc,
createChainRpc,
createInvoicesRpc,
createLnRpc,
createRouterRpc,
createSignRpc,
createWalletRpc,
createWatchtowerRpc,
createWtClientRpc,
} from '@radar/lnrpc';
```
Newly generated type definitions will be available in `./generated`.
You can now delete the old proto file inside the lnd directory.
Use the generated type definitions to update the types in `lnrpc.d.ts`.

You can also import the create function for the main gRPC server using the default import:

### Usage
```typescript
import createLnRpc from '@radar/lnrpc';
```

Connecting to an lnd instance at `localhost:10001`.
If you want to interact with all servers, wrap the functions in a class or object for easy initialization:

```typescript
import createLnRpc, {
Invoice,
InvoiceSubscription,
WalletBalanceResponse
AutopilotRpc,
ChainRpc,
createAutopilotRpc,
createChainRpc,
createInvoicesRpc,
createRouterRpc,
createSignRpc,
createWalletRpc,
createWatchtowerRpc,
createWtClientRpc,
InvoicesRpc,
LnRpc,
RouterRpc,
RpcClientConfig,
SignRpc,
WalletRpc,
WatchtowerRpc,
WtClientRpc,
} from '@radar/lnrpc';

export class Lightning {
public static lnrpc: LnRpc;
public static autopilotrpc: AutopilotRpc;
public static chainrpc: ChainRpc;
public static invoicesrpc: InvoicesRpc;
public static routerrpc: RouterRpc;
public static signrpc: SignRpc;
public static walletrpc: WalletRpc;
public static watchtowerrpc: WatchtowerRpc;
public static wtclientrpc: WtClientRpc;

/**
* Initialize gRPC clients for the main server and all sub-servers
* @param config The RPC client connection configuration
*/
public static async init(config: RpcClientConfig): Promise<void> {
this.lnrpc = await createLnRpc(config);
this.autopilotrpc = await createAutopilotRpc(config);
this.chainrpc = await createChainRpc(config);
this.invoicesrpc = await createInvoicesRpc(config);
this.routerrpc = await createRouterRpc(config);
this.signrpc = await createSignRpc(config);
this.walletrpc = await createWalletRpc(config);
this.watchtowerrpc = await createWatchtowerRpc(config);
this.wtclientrpc = await createWtClientRpc(config);
}
}
```

### Usage Example - Main Server

Connecting to an lnd instance at `localhost:10001`.

```typescript
import createLnRpc from '@radar/lnrpc';

(async () => {
const lnRpcClient = await createLnRpc();
const lnRpcClient = await createLnRpc(config);

// All requests are promisified and typed
const { confirmedBalance } = await lnRpcClient.walletBalance();
Expand All @@ -70,47 +127,45 @@ import createLnRpc, {
})();
```

### Options
### Options Example - Main Server

```typescript
import createLnRpc, {
GetInfoResponse
} from '@radar/lnrpc';
import createLnRpc from '@radar/lnrpc';

(async () => {
const lnRpcClient = await createLnRpc({
/*
By default lnrpc connects to `localhost:10001`,
however we can point to any host.
* By default lnrpc connects to `localhost:10001`,
* however we can point to any host.
*/
server: '173.239.209.2:3001',

/*
By default lnrpc looks for your tls certificate at:
`~/.lnd/tls.cert`, unless it detects you're using macOS and
defaults to `~/Library/Application\ Support/Lnd/tls.cert`
however you can configure your own SSL certificate path like:
* By default lnrpc looks for your tls certificate at:
* `~/.lnd/tls.cert`, unless it detects you're using macOS and
* defaults to `~/Library/Application\ Support/Lnd/tls.cert`
* however you can configure your own SSL certificate path like:
*/
tls: './path/to/tls.cert',

/*
You can also provide a TLS certificate directly as a string
(Just make sure you don't commit this to git).
Overwrites: `tls`
* You can also provide a TLS certificate directly as a string
* (Just make sure you don't commit this to git).
* Overwrites: `tls`
*/
cert: process.env.MY_SSL_CERT,

/*
Optional path to configure macaroon authentication
from LND generated macaroon file.
* Optional path to configure macaroon authentication
* from LND generated macaroon file.
*/
macaroonPath: './path/to/data/admin.macaroon',

/*
Optional way to configure macaroon authentication by
passing a hex encoded string of your macaroon file.
Encoding: `xxd -ps -u -c 1000 ./path/to/data/admin.macaroon`
Details: https://github.com/lightningnetwork/lnd/blob/dc3db4b/docs/macaroons.md#using-macaroons-with-grpc-clients
* Optional way to configure macaroon authentication by
* passing a hex encoded string of your macaroon file.
* Encoding: `xxd -ps -u -c 1000 ./path/to/data/admin.macaroon`
* Details: https://github.com/lightningnetwork/lnd/blob/dc3db4b/docs/macaroons.md#using-macaroons-with-grpc-clients
*/
macaroon: process.env.MY_MACAROON_HEX,
});
Expand All @@ -126,47 +181,43 @@ import createLnRpc, {

### API Reference

[All lnrpc methods documentation can be found here](http://api.lightning.community).

### Usage With Typescript

You must enable the `esModuleInterop` Typescript compiler option if you would like to use a default import.

This can be accomplished by setting `"esModuleInterop": true` in your `tsconfig.json`.

Import Example:
```typescript
import createLnRpc from '@radar/lnrpc';
```

Otherwise, you can import `createLnRpc` using the `as *` syntax.

Import Example:
```typescript
import * as createLnRpc from '@radar/lnrpc';
```
[All main server (lnrpc) methods documentation can be found here](http://api.lightning.community).

### Usage With BTCPayServer

By default lnrpc assumes SSl certificate pinning.
In order to use lnrpc with a service (like BTCPayServer) which manages your certification,
you'll have to opt to disable certificate pinning by passing `{ tls: false }` within your lnrpc configuration.

### Contributors

To develop on the project please run:
### Contributing

#### Clone Repository & Install Dependencies
```sh
git clone [email protected]:RadarTech/lnrpc.git && cd $_


npm install
npm start
# OR
yarn
yarn start
```

#### Change LND gRPC release version
To change the gRPC definitions used for all auto-generated types and RPC methods edit the `config.lnd_release_tag` value in `package.json` to the desired [LND release tag](https://github.com/lightningnetwork/lnd/releases) and run the following:

```sh
npm run update-protos
# OR
yarn update-protos

# AND

npm run generate
# OR
yarn generate
```
Newly generated type definitions will be available in `./generated`.
You can now delete the old proto file inside the lnd directory.
Use the generated type definitions to update the types in `src/types/rpc`.

### License

This project is licensed under the MIT License.
28 changes: 17 additions & 11 deletions google/api/http.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Google LLC.
// Copyright 2019 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,6 @@ option java_outer_classname = "HttpProto";
option java_package = "com.google.api";
option objc_class_prefix = "GAPI";


// Defines the HTTP configuration for an API service. It contains a list of
// [HttpRule][google.api.HttpRule], each specifying the mapping of an RPC method
// to one or more HTTP REST API methods.
Expand All @@ -34,7 +33,7 @@ message Http {
// **NOTE:** All service configuration rules follow "last one wins" order.
repeated HttpRule rules = 1;

// When set to true, URL path parmeters will be fully URI-decoded except in
// When set to true, URL path parameters will be fully URI-decoded except in
// cases of single segment matches in reserved expansion, where "%2F" will be
// left encoded.
//
Expand Down Expand Up @@ -112,7 +111,9 @@ message Http {
//
// HTTP | gRPC
// -----|-----
// `GET /v1/messages/123456?revision=2&sub.subfield=foo` | `GetMessage(message_id: "123456" revision: 2 sub: SubMessage(subfield: "foo"))`
// `GET /v1/messages/123456?revision=2&sub.subfield=foo` |
// `GetMessage(message_id: "123456" revision: 2 sub: SubMessage(subfield:
// "foo"))`
//
// Note that fields which are mapped to URL query parameters must have a
// primitive type or a repeated primitive type or a non-repeated message type.
Expand Down Expand Up @@ -144,7 +145,8 @@ message Http {
//
// HTTP | gRPC
// -----|-----
// `PATCH /v1/messages/123456 { "text": "Hi!" }` | `UpdateMessage(message_id: "123456" message { text: "Hi!" })`
// `PATCH /v1/messages/123456 { "text": "Hi!" }` | `UpdateMessage(message_id:
// "123456" message { text: "Hi!" })`
//
// The special name `*` can be used in the body mapping to define that
// every field not bound by the path template should be mapped to the
Expand All @@ -169,7 +171,8 @@ message Http {
//
// HTTP | gRPC
// -----|-----
// `PATCH /v1/messages/123456 { "text": "Hi!" }` | `UpdateMessage(message_id: "123456" text: "Hi!")`
// `PATCH /v1/messages/123456 { "text": "Hi!" }` | `UpdateMessage(message_id:
// "123456" text: "Hi!")`
//
// Note that when using `*` in the body mapping, it is not possible to
// have HTTP parameters, as all fields not bound by the path end in
Expand Down Expand Up @@ -200,7 +203,8 @@ message Http {
// HTTP | gRPC
// -----|-----
// `GET /v1/messages/123456` | `GetMessage(message_id: "123456")`
// `GET /v1/users/me/messages/123456` | `GetMessage(user_id: "me" message_id: "123456")`
// `GET /v1/users/me/messages/123456` | `GetMessage(user_id: "me" message_id:
// "123456")`
//
// ## Rules for HTTP mapping
//
Expand Down Expand Up @@ -244,16 +248,18 @@ message Http {
// `"{var=*}"`, when such a variable is expanded into a URL path on the client
// side, all characters except `[-_.~0-9a-zA-Z]` are percent-encoded. The
// server side does the reverse decoding. Such variables show up in the
// [Discovery Document](https://developers.google.com/discovery/v1/reference/apis)
// as `{var}`.
// [Discovery
// Document](https://developers.google.com/discovery/v1/reference/apis) as
// `{var}`.
//
// If a variable contains multiple path segments, such as `"{var=foo/*}"`
// or `"{var=**}"`, when such a variable is expanded into a URL path on the
// client side, all characters except `[-_.~/0-9a-zA-Z]` are percent-encoded.
// The server side does the reverse decoding, except "%2F" and "%2f" are left
// unchanged. Such variables show up in the
// [Discovery Document](https://developers.google.com/discovery/v1/reference/apis)
// as `{+var}`.
// [Discovery
// Document](https://developers.google.com/discovery/v1/reference/apis) as
// `{+var}`.
//
// ## Using gRPC API Service Configuration
//
Expand Down
1 change: 0 additions & 1 deletion index.js

This file was deleted.

Loading

0 comments on commit 850c5f1

Please sign in to comment.