Skip to content

Commit d01b304

Browse files
authored
Merge pull request #44 from lightninglabs/main
[Releases] v0.0.1-alpha.rc2
2 parents a5ec787 + d05944f commit d01b304

File tree

137 files changed

+15279
-162984
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+15279
-162984
lines changed

.github/workflows/npm.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Publish Package to npmjs
2+
on:
3+
release:
4+
types: [created]
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
# Setup .npmrc file to publish to npm
11+
- uses: actions/setup-node@v3
12+
with:
13+
node-version: '16.x'
14+
registry-url: 'https://registry.npmjs.org'
15+
- run: npm ci
16+
- run: npm publish --access public
17+
env:
18+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## A npm module for Lightning Node Connect
44

5+
## Install
6+
7+
`npm i @lightninglabs/lnc-web`
8+
59
## API Design
610

711
#### Set-up and connection
@@ -62,20 +66,23 @@ lnc.disconnect();
6266

6367
#### Base functions
6468

65-
All of the services (lnd, loop, pool, faraday) will be objects under the main lnc object. Each services’ sub-services will be underneath each service object, and each sub-service function below that (except in the case of faraday which only has one service - its functions will live directly under it). All service names and function names will be camel-cased.
69+
All of the services (lnd, loop, pool, faraday) will be objects under the main lnc object. Each services’ sub-services will be underneath each service object, and each sub-service function below that (except in the case of faraday which only has one service - its functions will live directly under it). All service, function, and param names will be camel-cased.
6670

6771
```
6872
const { lnd, loop, pool, faraday } = lnc;
6973
7074
// all functions on the base object should have proper types
7175
// sub-servers exist as objects on each main service
7276
lnd.lightning.listInvoices();
73-
lnd.lightning.connectPeer(‘03aa49c1e98ff4f216d886c09da9961c516aca22812c108af1b187896ded89807e@m3keajflswtfq3bw4kzvxtbru7r4z4cp5stlreppdllhp5a7vuvjzqyd.onion:9735’);
77+
lnd.lightning.connectPeer({ addr: ‘03aa49c1e98ff4f216d886c09da9961c516aca22812c108af1b187896ded89807e@m3keajflswtfq3bw4kzvxtbru7r4z4cp5stlreppdllhp5a7vuvjzqyd.onion:9735’ });
7478
7579
const signature = lnd.signer.signMessage({...params});
7680
7781
const swaps = await loop.swapClient.listSwaps();
78-
const poolAccount = await pool.trader.initAccount(100000000, 1000);
82+
const poolAccount = await pool.trader.initAccount({
83+
accountValue: 100000000,
84+
relativeHeight: 1000
85+
});
7986
8087
const insights = await faraday.channelInsights();
8188
```
@@ -123,3 +130,7 @@ npm run update-protos
123130
# format schemas
124131
npm run generate
125132
```
133+
134+
## Further documentation
135+
136+
- https://docs.lightning.engineering/lightning-network-tools/lightning-terminal/lnc-npm

demos/connect-demo/README.md

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,104 @@ accessible that you can obtain a pairing phrase from.
2121
```sh
2222
$ npm start
2323
```
24-
Your browser should open to http://localhost:3000 and you should see the home page
25-
below
24+
Your browser should open to http://localhost:3000 and you will see the home page.
25+
26+
## LNC Relevant Code
27+
28+
The `LNC` object in this app can be access from any component via the custom React hook
29+
[useLNC](https://github.com/lightninglabs/lnc-web/blob/main/demos/connect-demo/src/hooks/useLNC.ts).
30+
This hook returns three variables.
31+
32+
- `lnc` - the global `LNC` instance with full access to all of the `litd` RPC endpoints
33+
```ts
34+
const lnc = new LNC({});
35+
```
36+
- `connect` - a helper function that sets the `pairingPhrase` on the `lnc` object, then
37+
attempts to connect to the node. If the connections is successful (`listChannels`
38+
succeeds), then it will set the `password` on the `lnc` object which encrypts and stores
39+
the keys in the browser's `localStorage`.
40+
```ts
41+
const connect = useCallback(async (pairingPhrase: string, password: string) => {
42+
lnc.credentials.pairingPhrase = pairingPhrase;
43+
await lnc.connect();
44+
// verify we can fetch data
45+
await lnc.lnd.lightning.listChannels();
46+
// set the password after confirming the connection works
47+
lnc.credentials.password = password;
48+
}, []);
49+
```
50+
- `login` - a helper function that sets the `password` on the `lnc` object, which is used
51+
to decrypt the data stored in `localStorage`. If the password is valid, the connection
52+
is made.
53+
```ts
54+
const login = useCallback(async (password: string) => {
55+
lnc.credentials.password = password;
56+
await lnc.connect();
57+
}, []);
58+
```
59+
60+
On the [Home](./src/pages/Home.tsx) page, we can detect if the user has connected to the
61+
node via the `isConnected` field.
62+
63+
```ts
64+
const { lnc } = useLNC();
65+
66+
return (
67+
<Page>
68+
<h2 className="text-center">Welcome to lnc-web</h2>
69+
<p className="text-center">
70+
{lnc.isConnected
71+
? 'You are now connected to your Lightning node.'
72+
: 'Connect or Login to view your Lightning node info.'}
73+
</p>
74+
<GetInfo />
75+
</Page>
76+
);
77+
```
78+
79+
In the [Page](./src/components/Page.tsx) component that displays the navbar, we can detect
80+
whether to display a button to Connect, Login, or Logout based on the `lnc.isConnected`
81+
and `lnc.credentials.isPaired` fields.
82+
83+
```ts
84+
<Nav className="ml-auto">
85+
{lnc.isConnected ? (
86+
<>
87+
<Navbar.Text>Connected</Navbar.Text>
88+
<a href="/">
89+
<Button variant="link">Logout</Button>
90+
</a>
91+
</>
92+
) : lnc.credentials.isPaired ? (
93+
<Link to="/login">
94+
<Button>Login</Button>
95+
</Link>
96+
) : (
97+
<Link to="/connect">
98+
<Button>Connect</Button>
99+
</Link>
100+
)}
101+
</Nav>
102+
```
103+
104+
In the [GetInfo](./src/components/GetInfo.tsx) component, we call the
105+
`lnc.lnd.lightning.GetInfo` RPC to fetch the node's information if the user has connected.
106+
Once the info is set, it is rendered in the table.
107+
108+
```ts
109+
const { lnc } = useLNC();
110+
const [info, setInfo] = useState<any>();
111+
112+
useEffect(() => {
113+
if (lnc.isConnected) {
114+
const sendRequest = async () => {
115+
const res = await lnc.lnd.lightning.getInfo();
116+
setInfo(res);
117+
};
118+
sendRequest();
119+
}
120+
}, [lnc.isConnected, lnc.lnd.lightning]);
121+
```
26122

27123
## Screenshots
28124

0 commit comments

Comments
 (0)