Skip to content

Commit

Permalink
Add JS project scaffolding + web -> RN changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Sep 21, 2022
1 parent 373e721 commit d9e59f1
Show file tree
Hide file tree
Showing 55 changed files with 34,299 additions and 17 deletions.
62 changes: 62 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
dist/

# OSX
#
.DS_Store

# XDE
.expo/

# VSCode
.vscode/
jsconfig.json

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace

# Android/IJ
#
.idea
.gradle
local.properties
android.iml

# Cocoapods
#
example/ios/Pods

# node.js
#
node_modules/
npm-debug.log
yarn-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
android/app/libs
android/keystores/debug.keystore

# Expo
.expo/*

# generated by bob
**/**/*.swp
10 changes: 10 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.github/**
demos/**
dist/**
lib/types/**
package-lock.json
package.json
README.md
test/**
tsconfig.json
tslint.json
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"singleQuote": true,
"tabWidth": 4,
"semi": true,
"trailingComma": "none",
"bracketSpacing": true
}
28 changes: 11 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,17 @@ The constructor for the LNC object takes a parameters object with the three foll

- `pairingPhrase` (string): Your LNC pairing phrase
- `serverHost` (string): Specify a custom Lightning Node Connect proxy server. If not specified we'll default to `mailbox.terminal.lightning.today:443`.
- `password` (string): By default, this module will handle storage of your local and remote keys for you in local storage. We highly recommend encrypting that data with a password you set here.

```
import LNC from ‘@lightninglabs/lnc-rn’;
const pairingPhrase = ‘artefact morning piano photo consider light’;
const password = 'u*E0F?gU\d($N&Ckh8u)tLm';
// default connection using WASM from CDN
// WASM loaded on object creation
// default host: mailbox.terminal.lightning.today:443
// password used for encrypting credentials
const lnc = new LNC({
pairingPhrase,
password
pairingPhrase
});
// using custom Lightning Node Connect proxy server
Expand Down Expand Up @@ -73,20 +69,18 @@ const insights = await faraday.channelInsights();
#### Subscriptions

```
import { NativeEventEmitter } from 'react-native';
const { lnd } = lnc;
// handle subscriptions
lnd.lightning.subscribeTransactions(
params,
transaction => handleNewData(transaction),
error => handleError(error),
);
lnd.lightning.subscribeChannelEvents(
params,
event => handleNewChannelEventData(event),
error => handleError(error),
);
const request = {};
const eventName = lightning.subscribePeerEvents(request);
const eventEmitter = new NativeEventEmitter();
listener = eventEmitter.addListener(eventName, (event: any) => {
console.log('Got response', event.result);
});
// when ready to stop listener
listener.stop();
```

## Updating protos
Expand Down
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
};
34 changes: 34 additions & 0 deletions lib/api/createRpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import LNC from '../lnc';
import { subscriptionMethods } from '../types/proto/schema';

// capitalize the first letter in the string
const capitalize = (s: string) => s && s[0].toUpperCase() + s.slice(1);

/**
* Creates a typed Proxy object which calls the WASM request or
* subscribe methods depending on which function is called on the object
*/
export function createRpc<T extends object>(packageName: string, lnc: LNC): T {
const rpc = {};
return new Proxy(rpc, {
get(target, key, c) {
const methodName = capitalize(key.toString());
// the full name of the method (ex: lnrpc.Lightning.OpenChannel)
const method = `${packageName}.${methodName}`;

if (subscriptionMethods.includes(method)) {
// call subscribe for streaming methods
return (request: object): string => {
return lnc.subscribe(method, request);
};
} else {
// call request for unary methods
return async (request: object): Promise<any> => {
return await lnc.request(method, request);
};
}
}
}) as T;
}

export default createRpc;
17 changes: 17 additions & 0 deletions lib/api/faraday.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import LNC from '../lnc';
import { FaradayServer } from '../types/proto/faraday/faraday';
import { serviceNames as sn } from '../types/proto/schema';
import { createRpc } from './createRpc';

/**
* An API wrapper to communicate with the Faraday node via GRPC
*/
class FaradayApi {
faradayServer: FaradayServer;

constructor(lnc: LNC) {
this.faradayServer = createRpc(sn.frdrpc.FaradayServer, lnc);
}
}

export default FaradayApi;
4 changes: 4 additions & 0 deletions lib/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { default as LndApi } from './lnd';
export { default as LoopApi } from './loop';
export { default as PoolApi } from './pool';
export { default as FaradayApi } from './faraday';
44 changes: 44 additions & 0 deletions lib/api/lnd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import LNC from '../lnc';
import { Autopilot } from '../types/proto/lnd/autopilotrpc/autopilot';
import { ChainNotifier } from '../types/proto/lnd/chainrpc/chainnotifier';
import { Invoices } from '../types/proto/lnd/invoicesrpc/invoices';
import { Lightning } from '../types/proto/lnd/lightning';
import { Router } from '../types/proto/lnd/routerrpc/router';
import { Signer } from '../types/proto/lnd/signrpc/signer';
import { WalletKit } from '../types/proto/lnd/walletrpc/walletkit';
import { WalletUnlocker } from '../types/proto/lnd/walletunlocker';
import { Watchtower } from '../types/proto/lnd/watchtowerrpc/watchtower';
import { WatchtowerClient } from '../types/proto/lnd/wtclientrpc/wtclient';
import { serviceNames as sn } from '../types/proto/schema';
import { createRpc } from './createRpc';

/**
* An API wrapper to communicate with the LND node via GRPC
*/
class LndApi {
autopilot: Autopilot;
chainNotifier: ChainNotifier;
invoices: Invoices;
lightning: Lightning;
router: Router;
signer: Signer;
walletKit: WalletKit;
walletUnlocker: WalletUnlocker;
watchtower: Watchtower;
watchtowerClient: WatchtowerClient;

constructor(lnc: LNC) {
this.autopilot = createRpc(sn.autopilotrpc.Autopilot, lnc);
this.chainNotifier = createRpc(sn.chainrpc.ChainNotifier, lnc);
this.invoices = createRpc(sn.invoicesrpc.Invoices, lnc);
this.lightning = createRpc(sn.lnrpc.Lightning, lnc);
this.router = createRpc(sn.routerrpc.Router, lnc);
this.signer = createRpc(sn.signrpc.Signer, lnc);
this.walletKit = createRpc(sn.walletrpc.WalletKit, lnc);
this.walletUnlocker = createRpc(sn.lnrpc.WalletUnlocker, lnc);
this.watchtower = createRpc(sn.watchtowerrpc.Watchtower, lnc);
this.watchtowerClient = createRpc(sn.wtclientrpc.WatchtowerClient, lnc);
}
}

export default LndApi;
20 changes: 20 additions & 0 deletions lib/api/loop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import LNC from '../lnc';
import { SwapClient } from '../types/proto/loop/client';
import { Debug } from '../types/proto/loop/debug';
import { serviceNames as sn } from '../types/proto/schema';
import { createRpc } from './createRpc';

/**
* An API wrapper to communicate with the Loop node via GRPC
*/
class LoopApi {
swapClient: SwapClient;
debug: Debug;

constructor(lnc: LNC) {
this.swapClient = createRpc(sn.looprpc.SwapClient, lnc);
this.debug = createRpc(sn.looprpc.Debug, lnc);
}
}

export default LoopApi;
23 changes: 23 additions & 0 deletions lib/api/pool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import LNC from '../lnc';
import { ChannelAuctioneer } from '../types/proto/pool/auctioneerrpc/auctioneer';
import { HashMail } from '../types/proto/pool/auctioneerrpc/hashmail';
import { Trader } from '../types/proto/pool/trader';
import { serviceNames as sn } from '../types/proto/schema';
import { createRpc } from './createRpc';

/**
* An API wrapper to communicate with the Pool node via GRPC
*/
class PoolApi {
trader: Trader;
channelAuctioneer: ChannelAuctioneer;
hashmail: HashMail;

constructor(lnc: LNC) {
this.trader = createRpc(sn.poolrpc.Trader, lnc);
this.channelAuctioneer = createRpc(sn.poolrpc.ChannelAuctioneer, lnc);
this.hashmail = createRpc(sn.poolrpc.HashMail, lnc);
}
}

export default PoolApi;
7 changes: 7 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import LNC from './lnc';

export type { LncConfig, CredentialStore } from './types/lnc';
export { camelKeysToSnake, snakeKeysToCamel } from './util/objects';
export * from './types/proto';

export default LNC;
Loading

0 comments on commit d9e59f1

Please sign in to comment.