Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move LND node communication to background script #244

Merged
merged 3 commits into from
Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/components/ChannelInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Unit from 'components/Unit';
import DetailsTable, { DetailsRow } from 'components/DetailsTable';
import TransferIcons from 'components/TransferIcons';
import Copy from 'components/Copy';
import { CHANNEL_STATUS } from 'lib/lnd-http';
import { CHANNEL_STATUS } from 'lnd/message';
import { AppState } from 'store/reducers';
import { getAccountInfo } from 'modules/account/actions';
import { closeChannel } from 'modules/channels/actions';
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/ChannelList/ChannelRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Unit from 'components/Unit';
import { enumToClassName } from 'utils/formatters';
import { channelStatusText } from 'utils/constants';
import { ChannelWithNode } from 'modules/channels/types';
import { CHANNEL_STATUS } from 'lib/lnd-http';
import { CHANNEL_STATUS } from 'lnd/message';
import './ChannelRow.less';

interface Props {
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/SelectNode/ConfirmNode.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { Button } from 'antd';
import { GetInfoResponse } from 'lib/lnd-http';
import { GetInfoResponse } from 'lnd/message';
import { blockchainDisplayName, CHAIN_TYPE } from 'utils/constants';
import './ConfirmNode.less';

Expand Down
32 changes: 0 additions & 32 deletions src/app/lib/lnd-http/utils.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/modules/account/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NewAddressArguments } from 'lib/lnd-http';
import { NewAddressArguments } from 'lnd/message';
import types from './types';

export function getAccountInfo() {
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/account/reducers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import types, { Account, LightningPaymentWithToNode } from './types';
import { LightningInvoice, ChainTransaction } from 'lib/lnd-http';
import { LightningInvoice, ChainTransaction } from 'lnd/message';

export interface AccountState {
account: Account | null;
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/account/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
LightningInvoice,
LightningPayment,
ChainTransaction,
} from 'lib/lnd-http';
} from 'lnd/message';

enum AccountTypes {
GET_ACCOUNT_INFO = 'GET_ACCOUNT_INFO',
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/channels/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ForceClosingChannel,
WaitingChannel,
LightningNode,
} from 'lib/lnd-http';
} from 'lnd/message';

enum ChannelsTypes {
GET_CHANNELS = 'GET_CHANNELS',
Expand Down
8 changes: 4 additions & 4 deletions src/app/modules/node/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LndHttpClient, { Macaroon } from 'lib/lnd-http';
import LndMessageClient, { Macaroon } from 'lnd/message';
import {
selectSyncedUnencryptedNodeState,
selectSyncedEncryptedNodeState,
Expand Down Expand Up @@ -63,7 +63,7 @@ export function setNode(
url,
adminMacaroon,
readonlyMacaroon,
lib: new LndHttpClient(url, adminMacaroon),
lib: new LndMessageClient(url, adminMacaroon),
},
};
}
Expand All @@ -81,7 +81,7 @@ export function setSyncedUnencryptedNodeState(
payload: {
url,
readonlyMacaroon,
lib: url ? new LndHttpClient(url as string, readonlyMacaroon as string) : null,
lib: url ? new LndMessageClient(url as string, readonlyMacaroon as string) : null,
},
};
}
Expand All @@ -95,7 +95,7 @@ export function setSyncedEncryptedNodeState(
payload: {
url,
adminMacaroon,
lib: url ? new LndHttpClient(url as string, adminMacaroon as string) : null,
lib: url ? new LndMessageClient(url as string, adminMacaroon as string) : null,
},
};
}
4 changes: 2 additions & 2 deletions src/app/modules/node/reducers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import LndHttpClient, { Macaroon, GetInfoResponse } from 'lib/lnd-http';
import { Macaroon, GetInfoResponse, LndAPI } from 'lnd/message';
import types from './types';
import settingsTypes from 'modules/settings/types';

export interface NodeState {
lib: LndHttpClient | null;
lib: LndAPI | null;
url: string | null;
isNodeChecked: boolean;
adminMacaroon: Macaroon | null;
Expand Down
10 changes: 5 additions & 5 deletions src/app/modules/node/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {
import { requirePassword } from 'modules/crypto/sagas';
import { accountTypes } from 'modules/account';
import { channelsTypes } from 'modules/channels';
import LndHttpClient, { MacaroonAuthError, PermissionDeniedError } from 'lib/lnd-http';
import LndMessageClient, { MacaroonAuthError, PermissionDeniedError } from 'lnd/message';
import types from './types';

export function* handleCheckNode(action: ReturnType<typeof actions.checkNode>) {
const url = action.payload;
const client = new LndHttpClient(url);
const client = new LndMessageClient(url);
try {
yield call(client.getInfo);
} catch (err) {
Expand All @@ -36,7 +36,7 @@ export function* handleCheckNodes(action: ReturnType<typeof actions.checkNodes>)
const requests = urls.map(url => {
return new Promise<string | null>(async resolve => {
try {
const client = new LndHttpClient(url);
const client = new LndMessageClient(url);
await client.getInfo();
resolve(url);
} catch (err) {
Expand Down Expand Up @@ -65,7 +65,7 @@ export function* handleCheckAuth(action: ReturnType<typeof actions.checkAuth>) {
const { url, admin, readonly } = action.payload;

// Check read-only by making sure request doesn't error
let client = new LndHttpClient(url, readonly);
let client = new LndMessageClient(url, readonly);
let nodeInfo;
try {
nodeInfo = yield call(client.getInfo);
Expand All @@ -81,7 +81,7 @@ export function* handleCheckAuth(action: ReturnType<typeof actions.checkAuth>) {
// Test admin by intentionally send an invalid payment,
// but make sure we didn't error out with a macaroon auth error
// TODO: Replace with sign message once REST supports it
client = new LndHttpClient(url, admin);
client = new LndMessageClient(url, admin);
try {
yield call(client.sendPayment, { payment_request: 'testing admin' });
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/onchain/reducers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import types from './types';
import { Utxo } from 'lib/lnd-http';
import { Utxo } from 'lnd/message';

export interface OnChainState {
utxos: Utxo[] | null;
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/onchain/sagas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SagaIterator } from 'redux-saga';
import { takeLatest, select, call, put } from 'redux-saga/effects';
import { selectNodeLibOrThrow } from 'modules/node/selectors';
import { GetUtxosResponse } from 'lib/lnd-http/types';
import { GetUtxosResponse } from 'lnd/types';
import types from './types';

export function* handleGetUtxos() {
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/payment/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
SendPaymentArguments,
CreateInvoiceArguments,
SendOnChainArguments,
} from 'lib/lnd-http';
} from 'lnd/message';
import types from './types';

export function checkPaymentRequest(paymentRequest: string, amount?: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/payment/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
SendPaymentResponse,
CreateInvoiceResponse,
SendOnChainResponse,
} from 'lib/lnd-http';
} from 'lnd/message';
import types, { PaymentRequestState, OnChainFeeEstimates } from './types';

export interface PaymentState {
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/payment/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { checkPaymentRequest, sendPayment, createInvoice, sendOnChain } from './
import { apiFetchOnChainFees } from 'lib/earn';
import types from './types';
import { CHAIN_TYPE } from 'utils/constants';
import { NoRouteError } from 'lib/lnd-http/errors';
import { NoRouteError } from 'lnd/errors';

export function* handleSendPayment(action: ReturnType<typeof sendPayment>) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/payment/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DecodePaymentRequestResponse, LightningNode, Route } from 'lib/lnd-http';
import { DecodePaymentRequestResponse, LightningNode, Route } from 'lnd/message';

enum PaymentTypes {
CHECK_PAYMENT_REQUEST = 'CHECK_PAYMENT_REQUEST',
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/peers/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Peer, LightningNode } from 'lib/lnd-http';
import { Peer, LightningNode } from 'lnd/message';

enum PeersTypes {
GET_PEERS = 'GET_PEERS_INFO',
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/sign/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import types from './types';
import {
SignMessageResponse as LndSignMessageResponse,
VerifyMessageResponse as LndVerifyMessageResponse,
} from 'lib/lnd-http/types';
} from 'lnd/types';
import { safeGetNodeInfo } from 'utils/misc';

export function* handleSignMessage(action: ReturnType<typeof signMessage>) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/utils/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import BN from 'bn.js';
import moment from 'moment';
import { ChannelWithNode } from 'modules/channels/types';
import { Utxo, CHANNEL_STATUS } from 'lib/lnd-http';
import { Utxo, CHANNEL_STATUS } from 'lnd/message';

export interface BalanceStats {
total: string;
Expand Down
4 changes: 2 additions & 2 deletions src/app/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import DecredLogo from 'static/images/decred.svg';
import GroestlcoinLogo from 'static/images/groestlcoin.svg';
import * as React from 'react';
import { CustomIconComponentProps } from 'antd/lib/icon';
import { CHANNEL_STATUS } from 'lib/lnd-http';
import { AddressType } from 'lib/lnd-http/types';
import { CHANNEL_STATUS } from 'lnd/message';
import { AddressType } from 'lnd/types';

export enum NODE_TYPE {
LOCAL = 'LOCAL',
Expand Down
8 changes: 4 additions & 4 deletions src/app/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
LndHttpClient,
GetNodeInfoResponse,
AlreadyConnectedError,
LightningNode,
} from 'lib/lnd-http';
LndAPI,
} from 'lnd/message';

export function sleep(time: number) {
return new Promise(resolve => {
Expand All @@ -29,7 +29,7 @@ export const UNKNOWN_NODE: LightningNode = {

// Run getNodeInfo, but if it fails, return a spoofed node object
export async function safeGetNodeInfo(
lib: LndHttpClient,
lib: LndAPI,
pubkey: string,
): Promise<GetNodeInfoResponse> {
if (!pubkey) {
Expand All @@ -56,7 +56,7 @@ export async function safeGetNodeInfo(
}

// Run connectPeer, but if it fails due to duplicate, just ignore
export async function safeConnectPeer(lib: LndHttpClient, address: string): Promise<any> {
export async function safeConnectPeer(lib: LndAPI, address: string): Promise<any> {
try {
lib.connectPeer(address);
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/utils/typeguards.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AnyTransaction, LightningPaymentWithToNode } from 'modules/account/types';
import { LightningInvoice, ChainTransaction } from 'lib/lnd-http';
import { LightningInvoice, ChainTransaction } from 'lnd/message';

export function isInvoice(source: AnyTransaction): source is LightningInvoice {
return !!(source as LightningInvoice).expiry;
Expand Down
2 changes: 1 addition & 1 deletion src/background_script/getNodeInfo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetInfoResponse } from 'webln';
import runSelector from '../content_script/runSelector';
import { LndHttpClient } from 'lib/lnd-http';
import { LndHttpClient } from 'lnd/http';
import { selectSyncedUnencryptedNodeState } from 'modules/node/selectors';

export default async function getNodeInfo(): Promise<GetInfoResponse> {
Expand Down
44 changes: 44 additions & 0 deletions src/background_script/handleLndHttp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { browser } from 'webextension-polyfill-ts';
import {
LndHttpClient,
LndAPIRequestMessage,
LndAPIResponseMessage,
LndAPIMethod,
LndAPIResponseError,
} from '../lnd/http';

function isLndRequestMessage(req: any): req is LndAPIRequestMessage<LndAPIMethod> {
if (req && req.type === 'lnd-api-request') {
return true;
}
return false;
}

export default function handleLndHttp() {
// Background manages communication between page and its windows
browser.runtime.onMessage.addListener(async (request: unknown) => {
if (!isLndRequestMessage(request)) {
return;
}

const client = new LndHttpClient(request.url, request.macaroon);
const fn = client[request.method] as LndHttpClient[typeof request.method];
const args = request.args as Parameters<LndHttpClient[typeof request.method]>;

return (fn as any)(...args)
.then((data: ReturnType<LndHttpClient[typeof request.method]>) => {
return {
type: 'lnd-api-response',
method: request.method,
data,
} as LndAPIResponseMessage<typeof request.method>;
})
.catch((err: LndAPIResponseError) => {
return {
type: 'lnd-api-response',
method: request.method,
error: err,
} as LndAPIResponseMessage<typeof request.method>;
});
});
}
2 changes: 2 additions & 0 deletions src/background_script/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import handleLndHttp from './handleLndHttp';
import handlePrompts from './handlePrompts';
import handlePassword from './handlePassword';
import handleContextMenu from './handleContextMenu';

function initBackground() {
handleLndHttp();
handlePrompts();
handlePassword();
handleContextMenu();
Expand Down
File renamed without changes.
Loading