Browse Source

feat(payments): implement payInvoice for LND nodes

master
jamaljsr 5 years ago
parent
commit
70272d19f4
  1. 1
      TODO.md
  2. 18
      electron/lnd/lndProxyServer.ts
  3. 8
      src/lib/lightning/lnd/lndProxyClient.ts
  4. 20
      src/lib/lightning/lnd/lndService.ts
  5. 2
      src/shared/ipcChannels.ts

1
TODO.md

@ -2,7 +2,6 @@
Small Stuff
- add Copy & Close button to CreateInvoiceModal
- refactor: add redraw param to designer.syncChart()
- fix pasting text in terminal
- display bitcoin rpc user/pass in sidebar

18
electron/lnd/lndProxyServer.ts

@ -101,6 +101,22 @@ const createInvoice = async (args: {
return await rpc.addInvoice(args.req);
};
const payInvoice = async (args: {
node: LndNode;
req: LND.SendRequest;
}): Promise<LND.SendResponse> => {
const rpc = await getRpc(args.node);
return await rpc.sendPaymentSync(args.req);
};
const decodeInvoice = async (args: {
node: LndNode;
req: LND.PayReqString;
}): Promise<LND.PayReq> => {
const rpc = await getRpc(args.node);
return await rpc.decodePayReq(args.req);
};
/**
* A mapping of electron IPC channel names to the functions to execute when
* messages are recieved
@ -118,6 +134,8 @@ const listeners: {
[ipcChannels.listChannels]: listChannels,
[ipcChannels.pendingChannels]: pendingChannels,
[ipcChannels.createInvoice]: createInvoice,
[ipcChannels.payInvoice]: payInvoice,
[ipcChannels.decodeInvoice]: decodeInvoice,
};
/**

8
src/lib/lightning/lnd/lndProxyClient.ts

@ -55,6 +55,14 @@ class LndProxyClient {
async createInvoice(node: LndNode, req: LND.Invoice): Promise<LND.AddInvoiceResponse> {
return await this.ipc(ipcChannels.createInvoice, { node, req });
}
async payInvoice(node: LndNode, req: LND.SendRequest): Promise<LND.SendResponse> {
return await this.ipc(ipcChannels.payInvoice, { node, req });
}
async decodeInvoice(node: LndNode, req: LND.PayReqString): Promise<LND.PayReq> {
return await this.ipc(ipcChannels.decodeInvoice, { node, req });
}
}
export default new LndProxyClient();

20
src/lib/lightning/lnd/lndService.ts

@ -120,14 +120,26 @@ class LndService implements LightningService {
return res.paymentRequest;
}
payInvoice(
async payInvoice(
node: LightningNode,
invoice: string,
amount?: number,
): Promise<PLN.LightningNodePayReceipt> {
throw new Error(
`payInvoice is not implemented for ${node.implementation} nodes ${invoice} ${amount}`,
);
const req: LND.SendRequest = {
paymentRequest: invoice,
amt: amount ? amount.toString() : undefined,
};
const res = await proxy.payInvoice(this.cast(node), req);
// handle errors manually
if (res.paymentError) throw new Error(res.paymentError);
const payReq = await proxy.decodeInvoice(this.cast(node), { payReq: invoice });
return {
amount: parseInt(payReq.numSatoshis),
preimage: res.paymentPreimage.toString(),
destination: payReq.destination,
};
}
/**

2
src/shared/ipcChannels.ts

@ -13,4 +13,6 @@ export default {
listChannels: 'list-channels',
pendingChannels: 'pending-channels',
createInvoice: 'create-invoice',
payInvoice: 'pay-invoice',
decodeInvoice: 'decode-invoice',
};

Loading…
Cancel
Save