From e054f14e34f9255fbaaa0d1fc9fd40491b4c7f4f Mon Sep 17 00:00:00 2001 From: shubham Date: Wed, 3 Apr 2024 23:22:08 +0530 Subject: [PATCH] WIP: lsps... --- Navigation.ts | 4 + ios/LndMobile/Lnd.swift | 3 +- lndmobile/LndMobileInjection.ts | 14 ++- lndmobile/index.ts | 35 ++++---- stores/ChannelsStore.ts | 3 + stores/LSPStore.ts | 45 ++++++++++ views/Settings/Lsps1Test.tsx | 146 ++++++++++++++++++++++++++++++++ views/Settings/Settings.tsx | 33 ++++++++ 8 files changed, 254 insertions(+), 29 deletions(-) create mode 100644 views/Settings/Lsps1Test.tsx diff --git a/Navigation.ts b/Navigation.ts index 153dcf3d61..1efb98d2ca 100644 --- a/Navigation.ts +++ b/Navigation.ts @@ -128,6 +128,7 @@ import RestoreChannelBackups from './views/Settings/EmbeddedNode/RestoreChannelB import RawTxHex from './views/RawTxHex'; import CustodialWalletWarning from './views/Settings/CustodialWalletWarning'; +import Lsps1Test from './views/Settings/Lsps1Test'; const AppScenes = { Wallet: { @@ -450,6 +451,9 @@ const AppScenes = { }, CustodialWalletWarning: { screen: CustodialWalletWarning + }, + Lsps1Test: { + screen: Lsps1Test } }; diff --git a/ios/LndMobile/Lnd.swift b/ios/LndMobile/Lnd.swift index 09d9beb676..b420ff4a72 100644 --- a/ios/LndMobile/Lnd.swift +++ b/ios/LndMobile/Lnd.swift @@ -76,7 +76,6 @@ open class Lnd { "InvoicesCancelInvoice": { bytes, cb in LndmobileInvoicesCancelInvoice(bytes, cb) }, "ConnectPeer": { bytes, cb in LndmobileConnectPeer(bytes, cb) }, "SendCustomMessage": { bytes, cb in LndmobileSendCustomMessage(bytes, cb) }, - "SubscribeCustomMessages": { bytes, cb in LndmobileSubscribeCustomMessages(bytes, cb) }, "DecodePayReq": { bytes, cb in LndmobileDecodePayReq(bytes, cb) }, "DescribeGraph": { bytes, cb in LndmobileDescribeGraph(bytes, cb) }, "GetInfo": { bytes, cb in LndmobileGetInfo(bytes, cb) }, @@ -140,6 +139,8 @@ open class Lnd { "RouterSendPaymentV2": { req, cb in return LndmobileRouterSendPaymentV2(req, cb) }, "SubscribeState": { req, cb in return LndmobileSubscribeState(req, cb) }, "RouterTrackPaymentV2": { req, cb in return LndmobileRouterTrackPaymentV2(req, cb) }, + "SubscribeCustomMessages": { bytes, cb in LndmobileSubscribeCustomMessages(bytes, cb) }, + // channel // "CloseChannel": { req, cb in return LndmobileCloseChannel(req, cb)}, diff --git a/lndmobile/LndMobileInjection.ts b/lndmobile/LndMobileInjection.ts index 08f0d8680f..f8f1b6927d 100644 --- a/lndmobile/LndMobileInjection.ts +++ b/lndmobile/LndMobileInjection.ts @@ -224,15 +224,11 @@ export interface ILndMobileInjections { dest: string; dest_custom_records?: any; }) => Promise; - sendCustomMessage: ({ - peer, - type, - data - }: { - peer: Uint8Array | null; - type: number | null; - data: Uint8Array | null; - }) => Promise; + sendCustomMessage: ( + peer: Uint8Array | null, + type: number | null, + data: Uint8Array | null + ) => Promise; subscribeCustomMessages: () => Promise; }; channel: { diff --git a/lndmobile/index.ts b/lndmobile/index.ts index 5686d4b39c..cd2839475d 100644 --- a/lndmobile/index.ts +++ b/lndmobile/index.ts @@ -161,9 +161,9 @@ export const connectPeer = async ( * @throws */ export const sendCustomMessage = async ( - peer: Uint8Array | null, - type: number | null, - data: Uint8Array | null + peer: string, + type: number, + data: string ): Promise => { return await sendCommand< lnrpc.ISendCustomMessageRequest, @@ -174,9 +174,9 @@ export const sendCustomMessage = async ( response: lnrpc.SendCustomMessageResponse, method: 'SendCustomMessage', options: { - peer, + peer: Base64Utils.hexToBase64(peer), type, - data + data: Base64Utils.hexToBase64(data) } }); }; @@ -184,20 +184,17 @@ export const sendCustomMessage = async ( /** * @throws */ -export const subscribeCustomMessages = - async (): Promise => { - const response = await sendCommand< - lnrpc.ISubscribeCustomMessagesRequest, - lnrpc.SubscribeCustomMessagesRequest, - lnrpc.CustomMessage - >({ - request: lnrpc.SubscribeCustomMessagesRequest, - response: lnrpc.CustomMessage, - method: 'SubscribeCustomMessages', - options: {} - }); - return response; - }; +export const subscribeCustomMessages = async (): Promise => { + const response = await sendStreamCommand< + lnrpc.ISubscribeCustomMessagesRequest, + lnrpc.SubscribeCustomMessagesRequest + >({ + request: lnrpc.SubscribeCustomMessagesRequest, + method: 'SubscribeCustomMessages', + options: {} + }); + return response; +}; /** * @throws diff --git a/stores/ChannelsStore.ts b/stores/ChannelsStore.ts index cbc3564c50..c2a5b4dcd1 100644 --- a/stores/ChannelsStore.ts +++ b/stores/ChannelsStore.ts @@ -534,6 +534,7 @@ export default class ChannelsStore { perm }) .then(() => { + console.log('here in store'); if (!silent) { this.errorPeerConnect = false; this.connectingToPeer = false; @@ -544,6 +545,8 @@ export default class ChannelsStore { resolve(true); }) .catch((error: Error) => { + console.log('here in store-- catch block', error); + if (!silent) { this.connectingToPeer = false; this.peerSuccess = false; diff --git a/stores/LSPStore.ts b/stores/LSPStore.ts index 1db3869888..e58df56509 100644 --- a/stores/LSPStore.ts +++ b/stores/LSPStore.ts @@ -269,4 +269,49 @@ export default class LSPStore { }); }); }; + + @action + public sendCustomMessage = ({ + peer, + type, + data + }: { + peer: string; + type: number | null; + data: string; + }) => { + return new Promise((resolve, reject) => { + if (!peer || !type || !data) { + reject('Invalid parameters for custom message.'); + return; + } + + BackendUtils.sendCustomMessage({ peer, type, data }) + .then((response: any) => { + console.log(response); + resolve(response); + }) + .catch((error: any) => { + this.error = true; + this.error_msg = 'send message error'; + reject(error); + }); + }); + }; + + @action + public subscribeCustomMessages = () => { + return new Promise((resolve, reject) => { + BackendUtils.subscribeCustomMessages() + .then((response: any) => { + console.log('test'); + resolve(response); + }) + .catch((error: any) => { + this.error = true; + this.error_msg = 'subsribw message error'; + reject(error); + }); + }); + }; } diff --git a/views/Settings/Lsps1Test.tsx b/views/Settings/Lsps1Test.tsx new file mode 100644 index 0000000000..fe0ceead18 --- /dev/null +++ b/views/Settings/Lsps1Test.tsx @@ -0,0 +1,146 @@ +import * as React from 'react'; + +import Header from '../../components/Header'; +import Screen from '../../components/Screen'; +import { inject, observer } from 'mobx-react'; + +import LSPStore from '../../stores/LSPStore'; +import ChannelsStore from '../../stores/ChannelsStore'; +import { View } from 'react-native'; +import Base64Utils from '../../utils/Base64Utils'; + +import Button from '../../components/Button'; + +interface Lsps1TestProps { + LSPStore: LSPStore; + ChannelsStore: ChannelsStore; + navigation: any; +} + +interface Lsps1TestState { + peer: string; +} + +@inject('LSPStore', 'ChannelsStore') +@observer +export default class Lsps1Test extends React.Component< + Lsps1TestProps, + Lsps1TestState +> { + state = { + peer: '028c589131fae8c7e2103326542d568373019b50a9eb376a139a330c8545efb79a@130.44.132.180:9735' + }; + + encodeMesage = (n: any) => Buffer.from(JSON.stringify(n)).toString('hex'); + + subscribeToCustomMessages() { + this.props.LSPStore.subscribeCustomMessages() + .then((response) => { + console.log('Subscribed to custom messages:', response); + }) + .catch((error) => { + console.error('Error subscribing to custom messages:', error); + }); + } + + sendCustomMessage_lsps1() { + const peer = this.state.peer; + const [node_pubkey_string] = peer.split('@'); + const type = 37913; + const data = this.encodeMesage({ + jsonrpc: '2.0', + method: 'lsps1.get_info', + params: {}, + id: '42' + }); + + this.props.LSPStore.sendCustomMessage({ + peer: node_pubkey_string, + type, + data + }) + .then((response) => { + console.log('Custom message sent:', response); + }) + .catch((error) => { + // Handle errors + console.error('Error sending custom message:', error); + }); + } + + sendCustomMessage_lsps0() { + const peer = this.state.peer; + const [node_pubkey_string] = peer.split('@'); + const type = 37913; + const data = this.encodeMesage({ + method: 'lsps0.list_protocols', + jsonrpc: '2.0', + id: 'example#3cad6a54d302edba4c9ade2f7ffac098', + params: {} + }); + + this.props.LSPStore.sendCustomMessage({ + peer: node_pubkey_string, + type, + data + }) + .then((response) => { + console.log('Custom message sent:', response); + }) + .catch((error) => { + // Handle errors + console.error('Error sending custom message:', error); + }); + } + + connectPeer() { + const { ChannelsStore } = this.props; + const peer = this.state.peer; + const [node_pubkey_string, host] = peer.split('@'); + try { + console.log('here'); + ChannelsStore.connectPeer( + { + node_pubkey_string, + host, + local_funding_amount: '' + }, + true, + true + ); + } catch (err) { + console.log('here'); + console.log(err); + } + } + + render() { + const { navigation } = this.props; + return ( + +
+ +