Skip to content

Commit

Permalink
WIP: LSP support for LND REST
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Jan 19, 2024
1 parent eaf9d57 commit 97c275b
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 23 deletions.
92 changes: 91 additions & 1 deletion backends/LND.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,96 @@ export default class LND {
subscribeInvoice = (r_hash: string) =>
this.getRequest(`/v2/invoices/subscribe/${r_hash}`);
subscribeTransactions = () => this.getRequest('/v1/transactions/subscribe');
initChanAcceptor = (data?: any) => {

Check failure on line 436 in backends/LND.ts

View workflow job for this annotation

GitHub Actions / lint

'data' is defined but never used. Allowed unused args must match /^_/u
console.log('%%initChanAcceptor - a');
const { host, lndhubUrl, port, macaroonHex, accessToken } =
stores.settingsStore;

const auth = macaroonHex || accessToken;
const headers: any = this.getHeaders(auth, true);
const url = this.getURL(
host || lndhubUrl,
port,
'/v1/channels/acceptor?method=POST',
true
);

console.log('%%initChanAcceptor - b');

return new Promise(function (resolve, reject) {
console.log('url', url);
const ws: any = new WebSocket(url, null, {
headers
});

console.log('%%initChanAcceptor - c');

// keep pulling in responses until the socket closes
let resp: any;

ws.addEventListener('open', (e: any) => {
console.log('xxOpen', e);
// const requestPubkey = Base64Utils.bytesToHex(resp.node_pubkey);
// console.log('xxRequestPubkey', requestPubkey);

// const isZeroConfAllowed =
// data.lspPubkey === requestPubkey ||
// (data.zeroConfPeers &&
// data.zeroConfPeers.includes(requestPubkey));

// console.log('isZeroConfAllowed', isZeroConfAllowed);

// const acceptData = {
// accept: !resp.wants_zero_conf || isZeroConfAllowed,
// zero_conf: isZeroConfAllowed
// };

// console.log('acceptData', acceptData);
// ws.send(JSON.stringify(acceptData)); // send a message
});

ws.addEventListener('data', (e: any) => {
console.log('xxData');
// a message was received
const data = JSON.parse(e.data);
if (data.error) {
console.log('xxREJECT');
reject(data.error);
} else {
resp = e.data;
console.log('xxResp', resp);
}
});

ws.addEventListener('message', (e: any) => {
console.log('xxMessage');
// a message was received
const data = JSON.parse(e.data);
if (data.error) {
console.log('xxREJECT');
reject(data.error);
} else {
resp = e.data;
console.log('xxResp', resp);
}
});

ws.addEventListener('error', (e: any) => {
console.log('err', e);
const certWarning = localeString('backends.LND.wsReq.warning');
// an error occurred
reject(
e.message ? `${certWarning} (${e.message})` : certWarning
);
});

ws.addEventListener('close', () => {
console.log('CLOSE');
// connection closed
resolve(JSON.parse(resp));
});
});
};

supportsMessageSigning = () => true;
supportsLnurlAuth = () => true;
Expand All @@ -454,7 +544,7 @@ export default class LND {
supportsAddressTypeSelection = () => true;
supportsTaproot = () => this.supports('v0.15.0');
supportsBumpFee = () => true;
supportsLSPs = () => false;
supportsLSPs = () => true;
supportsNetworkInfo = () => false;
supportsSimpleTaprootChannels = () => this.supports('v0.17.0');
supportsCustomPreimages = () => true;
Expand Down
2 changes: 2 additions & 0 deletions stores/InvoicesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ export default class InvoicesStore {
return;
}

console.log('->', req);

return BackendUtils.createInvoice(req)
.then(async (data: any) => {
if (data.error) {
Expand Down
57 changes: 41 additions & 16 deletions stores/LSPStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import stores from './Stores';
import lndMobile from '../lndmobile/LndMobileInjection';
const { channel } = lndMobile;

import BackendUtils from '../utils/BackendUtils';
import Base64Utils from '../utils/Base64Utils';
import { LndMobileEventEmitter } from '../utils/LndMobileUtils';
import { localeString } from '../utils/LocaleUtils';
Expand Down Expand Up @@ -126,10 +127,17 @@ export default class LSPStore {
const status = response.info().status;
const data = response.json();
if (status == 200) {
this.zeroConfFee = Number.parseInt(
(Number(data.fee_amount_msat) / 1000).toString()
);
console.log('~~~&&&data', data);
this.zeroConfFee = data.fee_amount_msat
? Number.parseInt(
(
Number(data.fee_amount_msat) / 1000
).toString()
)
: undefined;
this.feeId = data.id;
console.log('zeroConfFee', this.zeroConfFee);
console.log('feeId', this.feeId);
this.error = false;
resolve(this.zeroConfFee);
} else {
Expand Down Expand Up @@ -170,22 +178,38 @@ export default class LSPStore {

@action
public initChannelAcceptor = async () => {
console.log('^^^initChannelAcceptor');
if (this.channelAcceptor) return;
this.channelAcceptor = LndMobileEventEmitter.addListener(
'ChannelAcceptor',
async (event: any) => {
try {
const channelAcceptRequest =
channel.decodeChannelAcceptRequest(event.data);

await this.handleChannelAcceptorEvent(channelAcceptRequest);
} catch (error: any) {
console.error('channel acceptance error: ' + error.message);
if (this.settingsStore.implementation === 'embedded-lnd') {
this.channelAcceptor = LndMobileEventEmitter.addListener(
'ChannelAcceptor',
async (event: any) => {
try {
const channelAcceptRequest =
channel.decodeChannelAcceptRequest(event.data);

await this.handleChannelAcceptorEvent(
channelAcceptRequest
);
} catch (error: any) {
console.error(
'channel acceptance error: ' + error.message
);
}
}
}
);
);

await channel.channelAcceptor();
} else {
// Only allow 0-conf chans from LSP or whitelisted peers

await channel.channelAcceptor();
console.log('^^^initChannelAcceptor - b');

BackendUtils.initChanAcceptor({
zeroConfPeers: this.settingsStore?.settings?.zeroConfPeers,
lspPubkey: this.info?.pubkey
});
}
};

@action
Expand Down Expand Up @@ -217,6 +241,7 @@ export default class LSPStore {
.then(async (response: any) => {
const status = response.info().status;
const data = response.json();
console.log('~~', data);
if (status == 200 || status == 201) {
resolve(data.jit_bolt11);
} else {
Expand Down
1 change: 1 addition & 0 deletions utils/BackendUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class BackendUtils {
this.call('subscribeInvoices', args);
subscribeTransactions = (...args: any[]) =>
this.call('subscribeTransactions', args);
initChanAcceptor = (...args: any[]) => this.call('initChanAcceptor', args);

// lndhub
login = (...args: any[]) => this.call('login', args);
Expand Down
13 changes: 7 additions & 6 deletions views/Wallet/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,6 @@ export default class Wallet extends React.Component<WalletProps, WalletState> {
embeddedLndNetwork === 'Testnet'
);
}
if (BackendUtils.supportsLSPs()) {
if (SettingsStore.settings.enableLSP) {
LSPStore.getLSPInfo();
}
LSPStore.initChannelAcceptor();
}
NodeInfoStore.getNodeInfo();
if (BackendUtils.supportsAccounts()) UTXOsStore.listAccounts();
await BalanceStore.getCombinedBalance(false);
Expand Down Expand Up @@ -455,6 +449,13 @@ export default class Wallet extends React.Component<WalletProps, WalletState> {
}
}

if (BackendUtils.supportsLSPs()) {
if (SettingsStore.settings.enableLSP) {
LSPStore.getLSPInfo();
}
LSPStore.initChannelAcceptor();
}

if (connecting) {
setConnectingStatus(false);
}
Expand Down

0 comments on commit 97c275b

Please sign in to comment.