Skip to content

Commit

Permalink
LSPS1: LNC
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Apr 18, 2024
1 parent 947e8e1 commit ccef618
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 58 deletions.
12 changes: 4 additions & 8 deletions backends/LightningNodeConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,13 @@ export default class LightningNodeConnect {
sendCustomMessage = async (data: any) =>
await this.lnc.lnd.lightning
.sendCustomMessage({
peer: data.peer,
peer: Base64Utils.hexToBase64(data.peer),
type: data.type,
data: data.data
data: Base64Utils.hexToBase64(data.data)
})
.then((data: lnrpc.SendCustomMessageResponse) => snakeize(data));
subscribeCustomMessages = async () =>
await this.lnc.lnd.lightning
.subscribeCustomMessages({})
.then((data: lnrpc.custommessage) => {
snakeize(data);
});
subscribeCustomMessages = () =>
this.lnc.lnd.lightning.subscribeCustomMessages({});
getMyNodeInfo = async () =>
await this.lnc.lnd.lightning
.getInfo({})
Expand Down
49 changes: 19 additions & 30 deletions stores/LSPStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,23 @@ export default class LSPStore {
});
};

@action
public handleCustomMessages = (decoded: any) => {
const peer = Base64Utils.base64ToHex(decoded.peer);
const data = JSON.parse(Base64Utils.base64ToUtf8(decoded.data));

console.log('peer', peer);
console.log('data', data);

if (data.id === '42') {
this.getInfoData = data;
} else if (data.id === '52') {
this.getOrderResponse = data;
} else {
this.createOrderResponse = data;
}
};

@action
public subscribeCustomMessages = async () => {
if (this.customMessagesSubscriber) return;
Expand All @@ -318,21 +335,7 @@ export default class LSPStore {
async (event: any) => {
try {
const decoded = index.decodeCustomMessage(event.data);
const peer = Base64Utils.base64ToHex(decoded.peer);
const data = JSON.parse(
Base64Utils.base64ToUtf8(decoded.data)
);

console.log('peer', peer);
console.log('data', data);

if (data.id === '42') {
this.getInfoData = data;
} else if (data.id === '52') {
this.getOrderResponse = data;
} else {
this.createOrderResponse = data;
}
this.handleCustomMessages(decoded);
} catch (error: any) {
console.error(
'sub custom messages error: ' + error.message
Expand All @@ -346,21 +349,7 @@ export default class LSPStore {
BackendUtils.subscribeCustomMessages(
(response: any) => {
const decoded = response.result;

const peer = Base64Utils.base64ToHex(decoded.peer);
const data = JSON.parse(
Base64Utils.base64ToUtf8(decoded.data)
);

console.log('peer', peer);

if (data.id === '42') {
this.getInfoData = data;
} else if (data.id === '52') {
this.getOrderResponse = data;
} else {
this.createOrderResponse = data;
}
this.handleCustomMessages(decoded);
},
(error: any) => {
console.error(
Expand Down
64 changes: 44 additions & 20 deletions views/Settings/LSPS1/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as React from 'react';
import { inject, observer } from 'mobx-react';
import {
NativeEventEmitter,
NativeModules,
View,
Text,
StyleSheet,
Expand All @@ -20,18 +22,20 @@ import Button from '../../../components/Button';
import KeyValue from '../../../components/KeyValue';
import { Row } from '../../../components/layout/Row';

import BackendUtils from '../../../utils/BackendUtils';
import { themeColor } from '../../../utils/ThemeUtils';
import { sleep } from '../../../utils/SleepUtils';
import { localeString } from '../../../utils/LocaleUtils';

import LSPStore from '../../../stores/LSPStore';
import InvoicesStore from '../../../stores/InvoicesStore';
import ChannelsStore from '../../../stores/ChannelsStore';
import SettingsStore from '../../../stores/SettingsStore';

interface LSPS1Props {
LSPStore: LSPStore;
InvoicesStore: InvoicesStore;
ChannelsStore: ChannelsStore;
SettingsStore: SettingsStore;
navigation: any;
}

Expand All @@ -48,9 +52,10 @@ interface LSPS1State {
createOrderResponse: any | object;
}

@inject('LSPStore', 'ChannelsStore', 'InvoicesStore')
@inject('LSPStore', 'ChannelsStore', 'InvoicesStore', 'SettingsStore')
@observer
export default class LSPS1 extends React.Component<LSPS1Props, LSPS1State> {
listener: any;
constructor(props: LSPS1Props) {
super(props);
this.state = {
Expand All @@ -71,27 +76,46 @@ export default class LSPS1 extends React.Component<LSPS1Props, LSPS1State> {

componentDidMount() {
this.connectPeer();
this.subscribeToCustomMessages().then(async () => {
await sleep(1000);
this.sendCustomMessage_lsps1();
});
this.subscribeToCustomMessages();
this.sendCustomMessage_lsps1();
}

subscribeToCustomMessages() {
return new Promise((resolve, reject) => {
this.props.LSPStore.subscribeCustomMessages()
.then((response) => {
console.log('Subscribed to custom messages:', response);
resolve({});
})
.catch((error) => {
console.error(
'Error subscribing to custom messages:',
error
);
reject();
});
});
if (
this.props.SettingsStore.implementation === 'lightning-node-connect'
) {
const { LncModule } = NativeModules;
const eventName = BackendUtils.subscribeCustomMessages();
const eventEmitter = new NativeEventEmitter(LncModule);
this.listener = eventEmitter.addListener(
eventName,
(event: any) => {
if (event.result) {
try {
const result = JSON.parse(event.result);
this.props.LSPStore.handleCustomMessages(result);
} catch (error) {
console.error(error);
}
}
}
);
} else {
return new Promise((resolve, reject) => {
this.props.LSPStore.subscribeCustomMessages()
.then((response) => {
console.log('Subscribed to custom messages:', response);
resolve({});
})
.catch((error) => {
console.error(
'Error subscribing to custom messages:',
error
);
reject();
});
});
}
}

sendCustomMessage_lsps1() {
Expand Down

0 comments on commit ccef618

Please sign in to comment.